1 /*
2  * Copyright (C) by Klaas Freitag <freitag@owncloud.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12  * for more details.
13  */
14 
15 #ifndef LOGGER_H
16 #define LOGGER_H
17 
18 #include <QObject>
19 #include <QList>
20 #include <QDateTime>
21 #include <QFile>
22 #include <QTextStream>
23 #include <qmutex.h>
24 
25 #include "common/utility.h"
26 #include "owncloudlib.h"
27 
28 namespace OCC {
29 
30 /**
31  * @brief The Logger class
32  * @ingroup libsync
33  */
34 class OWNCLOUDSYNC_EXPORT Logger : public QObject
35 {
36     Q_OBJECT
37 public:
38     bool isLoggingToFile() const;
39 
40     void doLog(QtMsgType type, const QMessageLogContext &ctx, const QString &message);
41 
42     static Logger *instance();
43 
44     void postGuiLog(const QString &title, const QString &message);
45     void postOptionalGuiLog(const QString &title, const QString &message);
46     void postGuiMessage(const QString &title, const QString &message);
47 
48     QString logFile() const;
49     void setLogFile(const QString &name);
50 
51     void setLogExpire(int expire);
52 
53     QString logDir() const;
54     void setLogDir(const QString &dir);
55 
56     void setLogFlush(bool flush);
57 
logDebug()58     bool logDebug() const { return _logDebug; }
59     void setLogDebug(bool debug);
60 
61     /** Returns where the automatic logdir would be */
62     QString temporaryFolderLogDirPath() const;
63 
64     /** Sets up default dir log setup.
65      *
66      * logdir: a temporary folder
67      * logexpire: 4 hours
68      * logdebug: true
69      *
70      * Used in conjunction with ConfigFile::automaticLogDir
71      */
72     void setupTemporaryFolderLogDir();
73 
74     /** For switching off via logwindow */
75     void disableTemporaryFolderLogDir();
76 
addLogRule(const QSet<QString> & rules)77     void addLogRule(const QSet<QString> &rules) {
78         setLogRules(_logRules + rules);
79     }
removeLogRule(const QSet<QString> & rules)80     void removeLogRule(const QSet<QString> &rules) {
81         setLogRules(_logRules - rules);
82     }
83     void setLogRules(const QSet<QString> &rules);
84 
85 signals:
86     void logWindowLog(const QString &);
87 
88     void guiLog(const QString &, const QString &);
89     void guiMessage(const QString &, const QString &);
90     void optionalGuiLog(const QString &, const QString &);
91 
92 public slots:
93     void enterNextLogFile();
94 
95 private:
96     Logger(QObject *parent = nullptr);
97     ~Logger() override;
98 
99     void close();
100     void dumpCrashLog();
101 
102     QFile _logFile;
103     bool _doFileFlush = false;
104     int _logExpire = 0;
105     bool _logDebug = false;
106     QScopedPointer<QTextStream> _logstream;
107     mutable QMutex _mutex;
108     QString _logDirectory;
109     bool _temporaryFolderLogDir = false;
110     QSet<QString> _logRules;
111     QVector<QString> _crashLog;
112     int _crashLogIndex = 0;
113 };
114 
115 } // namespace OCC
116 
117 #endif // LOGGER_H
118