2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-09-05 00:55:12 +00:00

moved almost all Application to AppClass (autoupdate and singleinstance left), logs rewritten

This commit is contained in:
John Preston
2016-01-11 23:43:29 +08:00
parent f3e560541b
commit 58777dbc21
55 changed files with 1904 additions and 1773 deletions

View File

@@ -22,111 +22,313 @@ Copyright (c) 2014-2015 John Preston, https://desktop.telegram.org
#include <iostream>
#include "pspecific.h"
namespace {
QFile debugLog, tcpLog, mtpLog, mainLog;
QTextStream *debugLogStream = 0, *tcpLogStream = 0, *mtpLogStream = 0, *mainLogStream = 0;
int32 part = -1;
QChar zero('0');
enum LogDataType {
LogDataMain,
LogDataDebug,
LogDataTcp,
LogDataMtp,
QMutex debugLogMutex, mainLogMutex;
LogDataCount
};
QString debugLogEntryStart() {
static uint32 logEntry = 0;
QMutex *LogsMutexes = 0;
QMutex *_logsMutex(LogDataType type) {
if (!LogsMutexes) {
LogsMutexes = new QMutex[LogDataCount];
}
return &LogsMutexes[type];
}
QString _logsFilePath(LogDataType type, const QString &postfix = QString()) {
QString path(cWorkingDir());
switch (type) {
case LogDataMain: path += qstr("log.txt"); break;
case LogDataDebug: path += qstr("DebugLogs/log") + postfix + qstr(".txt"); break;
case LogDataTcp: path += qstr("DebugLogs/tcp") + postfix + qstr(".txt"); break;
case LogDataMtp: path += qstr("DebugLogs/mtp") + postfix + qstr(".txt"); break;
}
return path;
}
class LogsDataFields {
public:
QString entryStart() {
QDateTime tm(QDateTime::currentDateTime());
QThread *thread = QThread::currentThread();
MTPThread *mtpThread = qobject_cast<MTPThread*>(thread);
uint32 threadId = mtpThread ? mtpThread->getThreadId() : 0;
uint threadId = mtpThread ? mtpThread->getThreadId() : 0;
return QString("[%1 %2-%3]").arg(tm.toString("hh:mm:ss.zzz")).arg(QString("%1").arg(threadId, 2, 10, zero)).arg(++logEntry, 7, 10, zero);
return QString("[%1 %2-%3]").arg(tm.toString("hh:mm:ss.zzz")).arg(QString("%1").arg(threadId, 2, 10, QChar('0'))).arg(++index, 7, 10, QChar('0'));
}
bool openMain() {
QMutexLocker lock(_logsMutex(LogDataMain));
return reopen(LogDataMain, 0, QString());
}
void write(LogDataType type, const QString &msg) {
QMutexLocker lock(_logsMutex(type));
if (type != LogDataMain) reopenDebug();
if (!streams[type].device()) return;
streams[type] << msg;
streams[type].flush();
}
private:
QFile files[LogDataCount];
QTextStream streams[LogDataCount];
int32 part = -1, index = 0;
bool reopen(LogDataType type, int32 dayIndex, const QString &postfix) {
if (streams[type].device()) {
if (type == LogDataMain) {
return true;
}
streams[type].setDevice(0);
files[type].close();
}
files[type].setFileName(_logsFilePath(type, postfix));
QFlags<QIODevice::OpenModeFlag> mode = QIODevice::WriteOnly | QIODevice::Text;
if (type != LogDataMain) {
if (files[type].exists()) {
if (files[type].open(QIODevice::ReadOnly | QIODevice::Text)) {
if (QString::fromUtf8(files[type].readLine()).toInt() == dayIndex) {
mode |= QIODevice::Append;
}
files[type].close();
}
} else {
QDir().mkdir(cWorkingDir() + qstr("DebugLogs"));
}
}
if (files[type].open(mode)) {
streams[type].setDevice(&files[type]);
streams[type].setCodec("UTF-8");
if (type != LogDataMain) {
streams[type] << ((mode & QIODevice::Append) ? qsl("----------------------------------------------------------------\nNEW LOGGING INSTANCE STARTED!!!\n----------------------------------------------------------------\n") : qsl("%1\n").arg(dayIndex));
streams[type].flush();
}
return true;
}
return false;
}
void reopenDebug() {
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 postfix = QString("_%4_%5").arg((part * switchEach) / 60, 2, 10, QChar('0')).arg((part * switchEach) % 60, 2, 10, QChar('0'));
reopen(LogDataDebug, dayIndex, postfix);
reopen(LogDataTcp, dayIndex, postfix);
reopen(LogDataMtp, dayIndex, postfix);
}
};
LogsDataFields *LogsData = 0;
typedef QList<QPair<LogDataType, QString> > LogsInMemoryList;
LogsInMemoryList *LogsInMemory = 0;
LogsInMemoryList *DeletedLogsInMemory = SharedMemoryLocation<LogsInMemoryList, 0>();
void _logsWrite(LogDataType type, const QString &msg) {
if (LogsData) {
if (type == LogDataMain || cDebug()) {
LogsData->write(type, msg);
}
} else if (LogsInMemory != DeletedLogsInMemory) {
if (!LogsInMemory) {
LogsInMemory = new LogsInMemoryList;
}
LogsInMemory->push_back(qMakePair(LogDataMain, msg));
}
}
void debugLogWrite(const char *file, int32 line, const QString &v) {
if (!cDebug() || !debugLogStream) return;
void _moveOldDataFiles(const QString &from);
const char *last = strstr(file, "/"), *found = 0;
while (last) {
found = last;
last = strstr(last + 1, "/");
}
last = strstr(file, "\\");
while (last) {
found = last;
last = strstr(last + 1, "\\");
}
if (found) {
file = found + 1;
}
namespace Logs {
{
QMutexLocker lock(&debugLogMutex);
if (!cDebug() || !debugLogStream) return;
Initializer::Initializer() {
t_assert(LogsData == 0);
logsInitDebug(); // maybe need to reopen new file
if (!Global::CheckBetaVersionDir()) {
return;
}
bool workingDirChosen = cBetaVersion();
QString moveOldDataFrom;
if (cBetaVersion()) {
cSetDebug(true);
#if (defined Q_OS_MAC || defined Q_OS_LINUX)
} else {
QString wasDir = QDir(cWorkingDir()).absolutePath() + '/';
#ifdef _DEBUG
cForceWorkingDir(cExeDir());
#else
if (cWorkingDir().isEmpty()) {
cForceWorkingDir(psAppDataPath());
}
#endif
workingDirChosen = true;
#if (defined Q_OS_LINUX && !defined _DEBUG) // fix first version
moveOldDataFrom = wasDir;
#endif
#endif
}
LogsData = new LogsDataFields();
if (!workingDirChosen) {
cForceWorkingDir(cWorkingDir());
if (!LogsData->openMain()) {
cForceWorkingDir(cExeDir());
if (!LogsData->openMain()) {
cForceWorkingDir(psAppDataPath());
}
}
}
cForceWorkingDir(QDir(cWorkingDir()).absolutePath() + '/');
QDir().setCurrent(cWorkingDir());
Global::WorkingDirReady();
LOG(("Launched version: %1, dev: %2, beta: %3, debug mode: %4, test dc: %5").arg(AppVersion).arg(Logs::b(cDevVersion())).arg(cBetaVersion()).arg(Logs::b(cDebug())).arg(Logs::b(cTestMode())));
LOG(("Executable dir: %1, name: %2").arg(cExeDir()).arg(cExeName()));
LOG(("Working dir: %1").arg(cWorkingDir()));
LOG(("Arguments: %1").arg(cArguments()));
if (!LogsData->openMain()) {
delete LogsData;
LogsData = 0;
LOG(("Error: could not open '%1' for writing log!").arg(_logsFilePath(LogDataMain)));
return;
}
QString msg(QString("%1 %2 (%3 : %4)\n").arg(debugLogEntryStart()).arg(v).arg(file).arg(line));
(*debugLogStream) << msg;
debugLogStream->flush();
#ifdef Q_OS_WIN
// OutputDebugString(reinterpret_cast<const wchar_t *>(msg.utf16()));
if (cWorkingDir() == psAppDataPath()) { // fix old "Telegram Win (Unofficial)" version
moveOldDataFrom = psAppDataPathOld();
}
#endif
if (!moveOldDataFrom.isEmpty()) {
_moveOldDataFiles(moveOldDataFrom);
}
if (LogsInMemory) {
t_assert(LogsInMemory != DeletedLogsInMemory);
LogsInMemoryList list = *LogsInMemory;
for (LogsInMemoryList::const_iterator i = list.cbegin(), e = list.cend(); i != e; ++i) {
_logsWrite(i->first, i->second);
}
}
if (LogsInMemory) {
t_assert(LogsInMemory != DeletedLogsInMemory);
delete LogsInMemory;
LogsInMemory = DeletedLogsInMemory;
}
LOG(("Logs started."));
DEBUG_LOG(("Debug logs started."));
}
Initializer::~Initializer() {
delete LogsData;
LogsData = 0;
delete[] LogsMutexes;
LogsMutexes = 0;
}
bool started() {
return LogsData != 0;
}
void writeMain(const QString &v) {
time_t t = time(NULL);
struct tm tm;
mylocaltime(&tm, &t);
QString msg(QString("[%1.%2.%3 %4:%5:%6] %7\n").arg(tm.tm_year + 1900).arg(tm.tm_mon + 1, 2, 10, QChar('0')).arg(tm.tm_mday, 2, 10, QChar('0')).arg(tm.tm_hour, 2, 10, QChar('0')).arg(tm.tm_min, 2, 10, QChar('0')).arg(tm.tm_sec, 2, 10, QChar('0')).arg(v));
_logsWrite(LogDataMain, msg);
QString debugmsg(QString("%1 %2\n").arg(LogsData->entryStart()).arg(v));
_logsWrite(LogDataDebug, debugmsg);
}
void writeDebug(const char *file, int32 line, const QString &v) {
const char *last = strstr(file, "/"), *found = 0;
while (last) {
found = last;
last = strstr(last + 1, "/");
}
last = strstr(file, "\\");
while (last) {
found = last;
last = strstr(last + 1, "\\");
}
if (found) {
file = found + 1;
}
QString msg(QString("%1 %2 (%3 : %4)\n").arg(LogsData->entryStart()).arg(v).arg(file).arg(line));
_logsWrite(LogDataDebug, msg);
#ifdef Q_OS_WIN
//OutputDebugString(reinterpret_cast<const wchar_t *>(msg.utf16()));
#elif defined Q_OS_MAC
objc_outputDebugString(msg);
//objc_outputDebugString(msg);
#elif defined Q_OS_LINUX && defined _DEBUG
// std::cout << msg.toUtf8().constData();
//std::cout << msg.toUtf8().constData();
#endif
}
}
void tcpLogWrite(const QString &v) {
if (!cDebug() || !tcpLogStream) return;
{
QMutexLocker lock(&debugLogMutex);
if (!cDebug() || !tcpLogStream) return;
logsInitDebug(); // maybe need to reopen new file
(*tcpLogStream) << QString("%1 %2\n").arg(debugLogEntryStart()).arg(v);
tcpLogStream->flush();
}
}
void mtpLogWrite(int32 dc, const QString &v) {
if (!cDebug() || !mtpLogStream) return;
{
QMutexLocker lock(&debugLogMutex);
if (!cDebug() || !mtpLogStream) return;
logsInitDebug(); // maybe need to reopen new file
(*mtpLogStream) << QString("%1 (dc:%2) %3\n").arg(debugLogEntryStart()).arg(dc).arg(v);
mtpLogStream->flush();
}
}
void logWrite(const QString &v) {
if (!mainLogStream) return;
time_t t = time(NULL);
struct tm tm;
mylocaltime(&tm, &t);
{
QMutexLocker lock(&mainLogMutex);
if (!mainLogStream) return;
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();
void writeTcp(const QString &v) {
QString msg(QString("%1 %2\n").arg(LogsData->entryStart()).arg(v));
_logsWrite(LogDataTcp, msg);
}
void writeMtp(int32 dc, const QString &v) {
QString msg(QString("%1 (dc:%2) %3\n").arg(LogsData->entryStart()).arg(dc).arg(v));
_logsWrite(LogDataMtp, msg);
}
QString vector(const QVector<MTPlong> &ids) {
if (!ids.size()) return "[]";
QString idsStr = QString("[%1").arg(ids.cbegin()->v);
for (QVector<MTPlong>::const_iterator i = ids.cbegin() + 1, e = ids.cend(); i != e; ++i) {
idsStr += QString(", %2").arg(i->v);
}
return idsStr + "]";
}
QString vector(const QVector<uint64> &ids) {
if (!ids.size()) return "[]";
QString idsStr = QString("[%1").arg(*ids.cbegin());
for (QVector<uint64>::const_iterator i = ids.cbegin() + 1, e = ids.cend(); i != e; ++i) {
idsStr += QString(", %2").arg(*i);
}
return idsStr + "]";
}
if (cDebug()) debugLogWrite("logs", 0, v);
}
void moveOldDataFiles(const QString &wasDir) {
void _moveOldDataFiles(const QString &wasDir) {
QFile data(wasDir + "data"), dataConfig(wasDir + "data_config"), tdataConfig(wasDir + "tdata/config");
if (data.exists() && dataConfig.exists() && !QFileInfo(cWorkingDir() + "data").exists() && !QFileInfo(cWorkingDir() + "data_config").exists()) { // move to home dir
LOG(("Copying data to home dir '%1' from '%2'").arg(cWorkingDir()).arg(wasDir));
@@ -171,239 +373,3 @@ void moveOldDataFiles(const QString &wasDir) {
}
}
}
bool logsInit() {
t_assert(mainLogStream == 0);
QFile beta(cExeDir() + qsl("TelegramBeta_data/tdata/beta"));
if (cBetaVersion()) {
cForceWorkingDir(cExeDir() + qsl("TelegramBeta_data/"));
if (*BetaPrivateKey) {
cSetBetaPrivateKey(QByteArray(BetaPrivateKey));
}
if (beta.open(QIODevice::WriteOnly)) {
QDataStream dataStream(&beta);
dataStream.setVersion(QDataStream::Qt_5_3);
dataStream << quint64(cRealBetaVersion()) << cBetaPrivateKey();
} else {
LOG(("Error: could not open \"beta\" file for writing private key!"));
}
} else if (beta.exists()) {
if (beta.open(QIODevice::ReadOnly)) {
QDataStream dataStream(&beta);
dataStream.setVersion(QDataStream::Qt_5_3);
quint64 v;
QByteArray k;
dataStream >> v >> k;
if (dataStream.status() == QDataStream::Ok) {
cSetBetaVersion(qMax(v, AppVersion * 1000ULL));
cSetBetaPrivateKey(k);
cSetRealBetaVersion(v);
cForceWorkingDir(cExeDir() + qsl("TelegramBeta_data/"));
}
}
}
if (cBetaVersion()) {
cSetDebug(true);
} else {
QString wasDir = cWorkingDir();
#if (defined Q_OS_MAC || defined Q_OS_LINUX)
#ifdef _DEBUG
cForceWorkingDir(cExeDir());
#else
if(cWorkingDir().isEmpty()){
cForceWorkingDir(psAppDataPath());
}
#endif
#if (defined Q_OS_LINUX && !defined _DEBUG) // fix first version
moveOldDataFiles(wasDir);
#endif
#endif
}
QString rightDir = cWorkingDir();
cForceWorkingDir(rightDir);
mainLog.setFileName(cWorkingDir() + "log.txt");
mainLog.open(QIODevice::WriteOnly | QIODevice::Text);
if (!cBetaVersion() && !mainLog.isOpen()) {
cForceWorkingDir(cExeDir());
mainLog.setFileName(cWorkingDir() + "log.txt");
mainLog.open(QIODevice::WriteOnly | QIODevice::Text);
if (!mainLog.isOpen()) {
cForceWorkingDir(psAppDataPath());
mainLog.setFileName(cWorkingDir() + "log.txt");
mainLog.open(QIODevice::WriteOnly | QIODevice::Text);
}
}
if (mainLog.isOpen()) {
mainLogStream = new QTextStream();
mainLogStream->setDevice(&mainLog);
mainLogStream->setCodec("UTF-8");
} else {
cForceWorkingDir(rightDir);
}
cForceWorkingDir(QDir(cWorkingDir()).absolutePath() + '/');
if (QFile(cWorkingDir() + qsl("tdata/withtestmode")).exists()) {
cSetTestMode(true);
LOG(("Switched to test mode!"));
}
#ifdef Q_OS_WIN
if (cWorkingDir() == psAppDataPath()) { // fix old "Telegram Win (Unofficial)" version
moveOldDataFiles(psAppDataPathOld());
}
#endif
if (cDebug()) {
logsInitDebug();
} else if (QFile(cWorkingDir() + qsl("tdata/withdebug")).exists()) {
logsInitDebug();
cSetDebug(true);
}
if (cBetaVersion()) {
cSetDevVersion(false);
} else if (!cDevVersion() && QFile(cWorkingDir() + qsl("tdata/devversion")).exists()) {
cSetDevVersion(true);
}
QDir().setCurrent(cWorkingDir());
return true;
}
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();
}
}
void logsClose() {
if (cDebug()) {
if (debugLogStream) {
delete debugLogStream;
debugLogStream = 0;
debugLog.close();
}
if (tcpLogStream) {
delete tcpLogStream;
tcpLogStream = 0;
tcpLog.close();
}
if (mtpLogStream) {
delete mtpLogStream;
mtpLogStream = 0;
mtpLog.close();
}
}
if (mainLogStream) {
delete mainLogStream;
mainLogStream = 0;
mainLog.close();
}
}
QString logVectorLong(const QVector<MTPlong> &ids) {
if (!ids.size()) return "[]";
QString idsStr = QString("[%1").arg(ids.cbegin()->v);
for (QVector<MTPlong>::const_iterator i = ids.cbegin() + 1, e = ids.cend(); i != e; ++i) {
idsStr += QString(", %2").arg(i->v);
}
return idsStr + "]";
}
QString logVectorLong(const QVector<uint64> &ids) {
if (!ids.size()) return "[]";
QString idsStr = QString("[%1").arg(*ids.cbegin());
for (QVector<uint64>::const_iterator i = ids.cbegin() + 1, e = ids.cend(); i != e; ++i) {
idsStr += QString(", %2").arg(*i);
}
return idsStr + "]";
}