added options of logging
This commit is contained in:
parent
41bc01902b
commit
18f2a2b847
|
|
@ -4,9 +4,12 @@
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
QString Logging::fileName = "../logs/EventConfigurator.log";
|
QString Logging::fileName = "../logs/EventConfigurator.log";
|
||||||
qint64 Logging::maxFileSize = 1024 *1024 * 10;
|
qint64 Logging::maxFileSize = 1024 *1024 * 10;
|
||||||
int Logging::maxBackupFiles = 5;
|
int Logging::maxBackupFiles = 5;
|
||||||
|
bool Logging::outputToConsole = false;
|
||||||
|
bool Logging::outputToFile = true;
|
||||||
QString Logging::messagePattern = "[%{time yyyyMMdd h:mm:ss.zzz ttt} %{if-debug}D%{endif}%{if-info}I%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}] %{file}:%{line} - %{message}";
|
QString Logging::messagePattern = "[%{time yyyyMMdd h:mm:ss.zzz ttt} %{if-debug}D%{endif}%{if-info}I%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}] %{file}:%{line} - %{message}";
|
||||||
|
|
||||||
QtMessageHandler Logging::originalHandler = nullptr;
|
QtMessageHandler Logging::originalHandler = nullptr;
|
||||||
|
|
||||||
void Logging::setupLogging()
|
void Logging::setupLogging()
|
||||||
|
|
@ -15,6 +18,9 @@ void Logging::setupLogging()
|
||||||
messagePattern = Settings::instance().value("Log", "pattern").toString();
|
messagePattern = Settings::instance().value("Log", "pattern").toString();
|
||||||
maxFileSize = Settings::instance().value("Log", "maxSize").toLongLong();
|
maxFileSize = Settings::instance().value("Log", "maxSize").toLongLong();
|
||||||
maxBackupFiles = Settings::instance().value("Log", "backups").toInt();
|
maxBackupFiles = Settings::instance().value("Log", "backups").toInt();
|
||||||
|
outputToConsole = Settings::instance().value("Log", "consoleOutput").toBool();
|
||||||
|
outputToFile = Settings::instance().value("Log", "fileOutput").toBool();
|
||||||
|
|
||||||
qSetMessagePattern(messagePattern);
|
qSetMessagePattern(messagePattern);
|
||||||
originalHandler = qInstallMessageHandler(Logging::logMessageHandler);
|
originalHandler = qInstallMessageHandler(Logging::logMessageHandler);
|
||||||
}
|
}
|
||||||
|
|
@ -22,31 +28,34 @@ void Logging::setupLogging()
|
||||||
void Logging::logMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
void Logging::logMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
||||||
{
|
{
|
||||||
QString message = qFormatLogMessage(type, context, msg);
|
QString message = qFormatLogMessage(type, context, msg);
|
||||||
static FILE *f = fopen(Logging::fileName.toLocal8Bit().constData(), "a");
|
|
||||||
if (f) {
|
if (outputToFile) {
|
||||||
fprintf(f, "%s\n", qPrintable(message));
|
static FILE *f = fopen(Logging::fileName.toLocal8Bit().constData(), "a");
|
||||||
fflush(f);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
f = fopen(Logging::fileName.toLocal8Bit().constData(), "a");
|
|
||||||
if (f) {
|
if (f) {
|
||||||
fprintf(f, "%s\n", qPrintable(message));
|
fprintf(f, "%s\n", qPrintable(message));
|
||||||
fflush(f);
|
fflush(f);
|
||||||
}
|
}
|
||||||
}
|
else {
|
||||||
|
f = fopen(Logging::fileName.toLocal8Bit().constData(), "a");
|
||||||
struct stat st;
|
if (f) {
|
||||||
stat(Logging::fileName.toLocal8Bit().constData(), &st);
|
fprintf(f, "%s\n", qPrintable(message));
|
||||||
auto fileSize = st.st_size;
|
fflush(f);
|
||||||
if(fileSize > Logging::maxFileSize) {
|
}
|
||||||
if (f) {
|
}
|
||||||
fclose(f);
|
|
||||||
f = nullptr;
|
struct stat st;
|
||||||
|
stat(Logging::fileName.toLocal8Bit().constData(), &st);
|
||||||
|
auto fileSize = st.st_size;
|
||||||
|
if(fileSize > Logging::maxFileSize) {
|
||||||
|
if (f) {
|
||||||
|
fclose(f);
|
||||||
|
f = nullptr;
|
||||||
|
}
|
||||||
|
rollLogFiles();
|
||||||
}
|
}
|
||||||
rollLogFiles();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (originalHandler)
|
if (originalHandler && outputToConsole)
|
||||||
(*originalHandler)(type, context, msg);
|
(*originalHandler)(type, context, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,14 +16,16 @@ class Logging: public QObject
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static void setupLogging();
|
static void setupLogging();
|
||||||
static void logMessageHandler(QtMsgType, const QMessageLogContext &, const QString &);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
static void logMessageHandler(QtMsgType, const QMessageLogContext &, const QString &);
|
||||||
static void rollLogFiles();
|
static void rollLogFiles();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static qint64 maxFileSize;
|
static qint64 maxFileSize;
|
||||||
static int maxBackupFiles;
|
static int maxBackupFiles;
|
||||||
|
static bool outputToConsole;
|
||||||
|
static bool outputToFile;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static QString fileName;
|
static QString fileName;
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ QVariant Settings::getDefaultValue(const QString& group, const QString& name)
|
||||||
else if(group == "Log" && name == "backups")
|
else if(group == "Log" && name == "backups")
|
||||||
return 5;
|
return 5;
|
||||||
else if(group == "Log" && name == "consoleOutput")
|
else if(group == "Log" && name == "consoleOutput")
|
||||||
return "false";
|
return "true";
|
||||||
else if(group == "Log" && name == "fileOutput")
|
else if(group == "Log" && name == "fileOutput")
|
||||||
return "true";
|
return "true";
|
||||||
else
|
else
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue