added options of logging
This commit is contained in:
parent
41bc01902b
commit
18f2a2b847
|
|
@ -4,9 +4,12 @@
|
|||
#include <sys/stat.h>
|
||||
|
||||
QString Logging::fileName = "../logs/EventConfigurator.log";
|
||||
qint64 Logging::maxFileSize = 1024 *1024 * 10;
|
||||
int Logging::maxBackupFiles = 5;
|
||||
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,31 +28,34 @@ void Logging::setupLogging()
|
|||
void Logging::logMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
||||
{
|
||||
QString message = qFormatLogMessage(type, context, msg);
|
||||
static FILE *f = fopen(Logging::fileName.toLocal8Bit().constData(), "a");
|
||||
if (f) {
|
||||
fprintf(f, "%s\n", qPrintable(message));
|
||||
fflush(f);
|
||||
}
|
||||
else {
|
||||
f = fopen(Logging::fileName.toLocal8Bit().constData(), "a");
|
||||
|
||||
if (outputToFile) {
|
||||
static FILE *f = fopen(Logging::fileName.toLocal8Bit().constData(), "a");
|
||||
if (f) {
|
||||
fprintf(f, "%s\n", qPrintable(message));
|
||||
fflush(f);
|
||||
}
|
||||
}
|
||||
|
||||
struct stat st;
|
||||
stat(Logging::fileName.toLocal8Bit().constData(), &st);
|
||||
auto fileSize = st.st_size;
|
||||
if(fileSize > Logging::maxFileSize) {
|
||||
if (f) {
|
||||
fclose(f);
|
||||
f = nullptr;
|
||||
else {
|
||||
f = fopen(Logging::fileName.toLocal8Bit().constData(), "a");
|
||||
if (f) {
|
||||
fprintf(f, "%s\n", qPrintable(message));
|
||||
fflush(f);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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