Esempio n. 1
0
void logWrite(const QString &v) {
	if (!mainLog.isOpen()) return;

	time_t t = time(NULL);
	struct tm tm;
    mylocaltime(&tm, &t);

	{
		QMutexLocker lock(&mainLogMutex);
		QString msg(QString("[%1.%2.%3 %4:%5:%6] %7\n").arg(tm.tm_year + 1900).arg(tm.tm_mon + 1, 2, 10, zero).arg(tm.tm_mday, 2, 10, zero).arg(tm.tm_hour, 2, 10, zero).arg(tm.tm_min, 2, 10, zero).arg(tm.tm_sec, 2, 10, zero).arg(v));
		(*mainLogStream) << msg;
		mainLogStream->flush();
	}

	if (cDebug()) debugLogWrite("logs", 0, v);
}
Esempio n. 2
0
QString filedialogDefaultName(
		const QString &prefix,
		const QString &extension,
		const QString &path,
		bool skipExistance,
		TimeId fileTime) {
	auto directoryPath = path;
	if (directoryPath.isEmpty()) {
		if (cDialogLastPath().isEmpty()) {
			Platform::FileDialog::InitLastPath();
		}
		directoryPath = cDialogLastPath();
	}

	QString base;
	if (fileTime) {
		const auto date = ParseDateTime(fileTime);
		base = prefix + date.toString("_yyyy-MM-dd_HH-mm-ss");
	} else {
		struct tm tm;
		time_t t = time(NULL);
		mylocaltime(&tm, &t);

		QChar zero('0');
		base = prefix + qsl("_%1-%2-%3_%4-%5-%6").arg(tm.tm_year + 1900).arg(tm.tm_mon + 1, 2, 10, zero).arg(tm.tm_mday, 2, 10, zero).arg(tm.tm_hour, 2, 10, zero).arg(tm.tm_min, 2, 10, zero).arg(tm.tm_sec, 2, 10, zero);
	}

	QString name;
	if (skipExistance) {
		name = base + extension;
	} else {
		QDir directory(directoryPath);
		const auto dir = directory.absolutePath();
		const auto nameBase = (dir.endsWith('/') ? dir : (dir + '/'))
			+ base;
		name = nameBase + extension;
		for (int i = 0; QFileInfo(name).exists(); ++i) {
			name = nameBase + qsl(" (%1)").arg(i + 2) + extension;
		}
	}
	return name;
}
Esempio n. 3
0
void logsInitDebug() {
	time_t t = time(NULL);
	struct tm tm;
	mylocaltime(&tm, &t);

	static const int switchEach = 15; // minutes
	int32 newPart = (tm.tm_min + tm.tm_hour * 60) / switchEach;
	if (newPart == part) return;

	part = newPart;

	int32 dayIndex = (tm.tm_year + 1900) * 10000 + (tm.tm_mon + 1) * 100 + tm.tm_mday;
	QString logPostfix = QString("_%4_%5").arg((part * switchEach) / 60, 2, 10, zero).arg((part * switchEach) % 60, 2, 10, zero);

	if (debugLogStream) {
		delete debugLogStream;
		debugLogStream = 0;
		debugLog.close();
	}
	debugLog.setFileName(cWorkingDir() + qsl("DebugLogs/log") + logPostfix + qsl(".txt"));
	QIODevice::OpenMode debugLogMode = QIODevice::WriteOnly | QIODevice::Text;
	if (debugLog.exists()) {
		if (debugLog.open(QIODevice::ReadOnly | QIODevice::Text)) {
			if (QString::fromUtf8(debugLog.readLine()).toInt() == dayIndex) {
				debugLogMode |= QIODevice::Append;
			}
			debugLog.close();
		}
	}
	if (!debugLog.open(debugLogMode)) {
		QDir dir(QDir::current());
		dir.mkdir(cWorkingDir() + qsl("DebugLogs"));
		debugLog.open(debugLogMode);
	}
	if (debugLog.isOpen()) {
		debugLogStream = new QTextStream();
		debugLogStream->setDevice(&debugLog);
		debugLogStream->setCodec("UTF-8");
		(*debugLogStream) << ((debugLogMode & QIODevice::Append) ? qsl("----------------------------------------------------------------\nNEW LOGGING INSTANCE STARTED!!!\n----------------------------------------------------------------\n") : qsl("%1\n").arg(dayIndex));
		debugLogStream->flush();
	}
	if (tcpLogStream) {
		delete tcpLogStream;
		tcpLogStream = 0;
		tcpLog.close();
	}
	tcpLog.setFileName(cWorkingDir() + qsl("DebugLogs/tcp") + logPostfix + qsl(".txt"));
	QIODevice::OpenMode tcpLogMode = QIODevice::WriteOnly | QIODevice::Text;
	if (tcpLog.exists()) {
		if (tcpLog.open(QIODevice::ReadOnly | QIODevice::Text)) {
			if (QString::fromUtf8(tcpLog.readLine()).toInt() == dayIndex) {
				tcpLogMode |= QIODevice::Append;
			}
			tcpLog.close();
		}
	}
	if (tcpLog.open(tcpLogMode)) {
		tcpLogStream = new QTextStream();
		tcpLogStream->setDevice(&tcpLog);
		tcpLogStream->setCodec("UTF-8");
		(*tcpLogStream) << ((tcpLogMode & QIODevice::Append) ? qsl("----------------------------------------------------------------\nNEW LOGGING INSTANCE STARTED!!!\n----------------------------------------------------------------\n") : qsl("%1\n").arg(dayIndex));
		tcpLogStream->flush();
	}
	if (mtpLogStream) {
		delete mtpLogStream;
		mtpLogStream = 0;
		mtpLog.close();
	}
	mtpLog.setFileName(cWorkingDir() + qsl("DebugLogs/mtp") + logPostfix + qsl(".txt"));
	QIODevice::OpenMode mtpLogMode = QIODevice::WriteOnly | QIODevice::Text;
	if (mtpLog.exists()) {
		if (mtpLog.open(QIODevice::ReadOnly | QIODevice::Text)) {
			if (QString::fromUtf8(mtpLog.readLine()).toInt() == dayIndex) {
				mtpLogMode |= QIODevice::Append;
			}
			mtpLog.close();
		}
	}
	if (mtpLog.open(mtpLogMode)) {
		mtpLogStream = new QTextStream();
		mtpLogStream->setDevice(&mtpLog);
		mtpLogStream->setCodec("UTF-8");
		(*mtpLogStream) << ((mtpLogMode & QIODevice::Append) ? qsl("----------------------------------------------------------------\nNEW LOGGING INSTANCE STARTED!!!\n----------------------------------------------------------------\n") : qsl("%1\n").arg(dayIndex));
		mtpLogStream->flush();
	}
}