added options of logging
This commit is contained in:
parent
41bc01902b
commit
18f2a2b847
|
|
@ -6,7 +6,10 @@
|
|||
QString Logging::fileName = "../logs/EventConfigurator.log";
|
||||
qint64 Logging::maxFileSize = 1024 *1024 * 10;
|
||||
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}";
|
||||
|
||||
QtMessageHandler Logging::originalHandler = nullptr;
|
||||
|
||||
void Logging::setupLogging()
|
||||
|
|
@ -15,6 +18,9 @@ void Logging::setupLogging()
|
|||
messagePattern = Settings::instance().value("Log", "pattern").toString();
|
||||
maxFileSize = Settings::instance().value("Log", "maxSize").toLongLong();
|
||||
maxBackupFiles = Settings::instance().value("Log", "backups").toInt();
|
||||
outputToConsole = Settings::instance().value("Log", "consoleOutput").toBool();
|
||||
outputToFile = Settings::instance().value("Log", "fileOutput").toBool();
|
||||
|
||||
qSetMessagePattern(messagePattern);
|
||||
originalHandler = qInstallMessageHandler(Logging::logMessageHandler);
|
||||
}
|
||||
|
|
@ -22,6 +28,8 @@ void Logging::setupLogging()
|
|||
void Logging::logMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
||||
{
|
||||
QString message = qFormatLogMessage(type, context, msg);
|
||||
|
||||
if (outputToFile) {
|
||||
static FILE *f = fopen(Logging::fileName.toLocal8Bit().constData(), "a");
|
||||
if (f) {
|
||||
fprintf(f, "%s\n", qPrintable(message));
|
||||
|
|
@ -45,8 +53,9 @@ void Logging::logMessageHandler(QtMsgType type, const QMessageLogContext &contex
|
|||
}
|
||||
rollLogFiles();
|
||||
}
|
||||
}
|
||||
|
||||
if (originalHandler)
|
||||
if (originalHandler && outputToConsole)
|
||||
(*originalHandler)(type, context, msg);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,14 +16,16 @@ class Logging: public QObject
|
|||
|
||||
public:
|
||||
static void setupLogging();
|
||||
static void logMessageHandler(QtMsgType, const QMessageLogContext &, const QString &);
|
||||
|
||||
private:
|
||||
static void logMessageHandler(QtMsgType, const QMessageLogContext &, const QString &);
|
||||
static void rollLogFiles();
|
||||
|
||||
private:
|
||||
static qint64 maxFileSize;
|
||||
static int maxBackupFiles;
|
||||
static bool outputToConsole;
|
||||
static bool outputToFile;
|
||||
|
||||
private:
|
||||
static QString fileName;
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ QVariant Settings::getDefaultValue(const QString& group, const QString& name)
|
|||
else if(group == "Log" && name == "backups")
|
||||
return 5;
|
||||
else if(group == "Log" && name == "consoleOutput")
|
||||
return "false";
|
||||
return "true";
|
||||
else if(group == "Log" && name == "fileOutput")
|
||||
return "true";
|
||||
else
|
||||
|
|
|
|||
Loading…
Reference in New Issue