1 /*
2 Drawpile - a collaborative drawing program.
3
4 Copyright (C) 2017 Calle Laakkonen
5
6 Drawpile is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Drawpile is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Drawpile. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "logging.h"
21 #include "config.h"
22 #include "../libshared/util/paths.h"
23
24 #include <QMessageLogContext>
25 #include <QDateTime>
26 #include <QSettings>
27
28 #include <cstdio>
29
30 namespace utils {
31
32 static FILE *logfile;
33 static QtMessageHandler defaultLogger;
34
logToFile(QtMsgType type,const QMessageLogContext & ctx,const QString & msg)35 void logToFile(QtMsgType type, const QMessageLogContext &ctx, const QString &msg)
36 {
37 const QByteArray m = msg.toLocal8Bit();
38 const QByteArray ts = QDateTime::currentDateTime().toString("hh:mm:ss").toLocal8Bit();
39 switch(type) {
40 case QtDebugMsg:
41 fprintf(logfile, "[%s DEBUG] %s:%u (%s): %s\n", ts.constData(), ctx.file, ctx.line, ctx.function, m.constData());
42 break;
43 case QtInfoMsg:
44 fprintf(logfile, "[%s INFO] %s\n", ts.constData(), m.constData());
45 break;
46 case QtWarningMsg:
47 fprintf(logfile, "[%s WARNING] %s\n", ts.constData(), m.constData());
48 break;
49 case QtCriticalMsg:
50 fprintf(logfile, "[%s CRITICAL] %s\n", ts.constData(), m.constData());
51 break;
52 case QtFatalMsg:
53 fprintf(logfile, "[%s FATAL] %s\n", ts.constData(), m.constData());
54 break;
55 }
56 fflush(logfile);
57 defaultLogger(type, ctx, msg);
58 }
59
logFilePath()60 QByteArray logFilePath()
61 {
62 return utils::paths::writablePath(
63 QStandardPaths::AppLocalDataLocation,
64 "logs/",
65 ("drawpile-" DRAWPILE_VERSION "-") + QDateTime::currentDateTime().toString("yyyy-MM-dd") + ".log"
66 ).toLocal8Bit();
67 }
68
initLogging()69 void initLogging()
70 {
71 if(!QSettings().value("settings/logfile", true).toBool()) {
72 qInfo("Logfile disabled");
73 return;
74 }
75
76 const QByteArray logpath = logFilePath();
77
78 qInfo("Opening log file: %s", logpath.constData());
79 logfile = fopen(logpath.constData(), "a");
80 if(!logfile) {
81 qWarning("Unable to open logfile");
82
83 } else {
84 defaultLogger = qInstallMessageHandler(logToFile);
85 qInfo("Drawpile started.");
86 }
87 }
88
89 }
90
91