1 /*
2     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #ifndef JORISLOG_H
8 #define JORISLOG_H
9 
10 #include "constants.h"
11 #include <QString>
12 #include <QUrl>
13 #include <ktorrent_export.h>
14 
15 // LOG MESSAGES CONSTANTS
16 #define LOG_NONE 0x00
17 #define LOG_IMPORTANT 0x01
18 #define LOG_NOTICE 0x03
19 #define LOG_DEBUG 0x07
20 #define LOG_ALL 0x0F
21 
22 #define SYS_GEN 0x0010 // Genereral info messages
23 #define SYS_CON 0x0020 // Connections
24 #define SYS_TRK 0x0040 // Tracker
25 #define SYS_DHT 0x0080 // DHT
26 #define SYS_DIO 0x0100 // Disk IO related stuff, saving and loading of chunks ...
27 #define SYS_UTP 0x0200 // UTP
28 
29 // plugins
30 #define SYS_IPF 0x1000 // IPFilter
31 #define SYS_SRC 0x2000 // Search plugin
32 #define SYS_PNP 0x4000 // UPnP plugin
33 #define SYS_INW 0x8000 // InfoWidget
34 #define SYS_SNF 0x10000 // ScanFolder plugin
35 #define SYS_MPL 0x20000 // Media player plugin
36 #define SYS_SCD 0x40000 // Scheduler plugin
37 #define SYS_BTF 0x80000 // BitFinder plugin
38 #define SYS_WEB 0x100000 // WebInterface plugin
39 #define SYS_ZCO 0x200000 // ZeroConf plugin
40 #define SYS_SCR 0x400000 // Scripting plugin
41 #define SYS_SYN 0x800000 // Syndication plugin
42 
43 namespace bt
44 {
45 class LogMonitorInterface;
46 
47 /**
48  * @author Joris Guisson
49  * @brief Class which writes messages to a logfile
50  *
51  * This class writes messages to a logfile. To use it, create an instance,
52  * set the output file and write stuff with the << operator.
53  *
54  * By default all messages will also be printed on the standard output. This
55  * can be turned down using the @a setOutputToConsole function.
56  *
57  * There is also the possibility to monitor what is written to the log using
58  * the LogMonitorInterface class.
59  */
60 class KTORRENT_EXPORT Log
61 {
62     class Private;
63 
64     Private *priv;
65 
66 public:
67     /**
68      * Constructor.
69      */
70     Log();
71 
72     /**
73      * Destructor, closes the file.
74      */
75     virtual ~Log();
76 
77     /**
78      * Enable or disable the printing of log messages to the standard
79      * output.
80      * @param on Enable or disable
81      */
82     void setOutputToConsole(bool on);
83 
84     /**
85      * Add a log monitor.
86      * @param m The log monitor
87      */
88     void addMonitor(LogMonitorInterface *m);
89 
90     /**
91      * Remove a log monitor. It will not be deleted.
92      * @param m The log monitor
93      */
94     void removeMonitor(LogMonitorInterface *m);
95 
96     /**
97      * Set the output logfile.
98      * @param file The name of the file
99      * @param rotate Whether or not to rotate the logs
100      * @param bool handle_qt_messages Whether or not handle Qt messages
101      * @throw Exception if the file can't be opened
102      */
103     void setOutputFile(const QString &file, bool rotate, bool handle_qt_messages);
104 
105     /**
106      * Write a number to the log file.
107      * Anything which can be passed to QString::number will do.
108      * @param val The value
109      * @return This Log
110      */
111     template<class T> Log &operator<<(T val)
112     {
113         return operator<<(QString::number(val));
114     }
115 
116     /**
117      * Apply a function to the Log.
118      * @param func The function
119      * @return This Log
120      */
121     Log &operator<<(Log &(*func)(Log &))
122     {
123         return func(*this);
124     }
125 
126     /**
127      * Output a QString to the log.
128      * @param s The QString
129      * @return This Log
130      */
131     Log &operator<<(const char *s);
132 
133     /**
134      * Output a QString to the log.
135      * @param s The QString
136      * @return This Log
137      */
138     Log &operator<<(const QString &s);
139 
140     /**
141      * Output a 64 bit integer to the log.
142      * @param v The integer
143      * @return This Log
144      */
145     Log &operator<<(Uint64 v);
146 
147     /**
148      * Output a 64 bit integer to the log.
149      * @param v The integer
150      * @return This Log
151      */
152     Log &operator<<(Int64 v);
153 
154     /**
155      * Prints and endline character to the Log and flushes it.
156      * @param lg The Log
157      * @return @a lg
158      */
159     KTORRENT_EXPORT friend Log &endl(Log &lg);
160 
161     /**
162      * Write an URL to the file.
163      * @param text The QUrl
164      * @return This Log
165      */
166     Log &operator<<(const QUrl &url);
167 
168     /**
169      * Sets a filter for log messages. Applies only to listeners via LogMonitorInterface!
170      * @param filter SYS & LOG flags combined with bitwise OR.
171      */
172     void setFilter(unsigned int filter);
173 
174     /// Lock the mutex of the log, should be called in Out()
175     void lock();
176 
177     /// Called by the auto log rotate job when it has finished
178     void logRotateDone();
179 };
180 
181 KTORRENT_EXPORT Log &endl(Log &lg);
182 KTORRENT_EXPORT Log &Out(unsigned int arg = 0x00);
183 
184 /**
185  * Initialize the global log.
186  * @param file The log file
187  * @param rotate_logs Set to true if the logs need to be rotated
188  * @param handle_qt_messages Set to true if Qt messages need to be logged
189  * @param to_stdout Set to true if output to standard output is required
190  * */
191 KTORRENT_EXPORT void InitLog(const QString &file, bool rotate_logs = false, bool handle_qt_messages = true, bool to_stdout = false);
192 
193 /// Add a monitor to the global log
194 KTORRENT_EXPORT void AddLogMonitor(LogMonitorInterface *m);
195 
196 /// Remove a monitor from the global log
197 KTORRENT_EXPORT void RemoveLogMonitor(LogMonitorInterface *m);
198 }
199 
200 #endif
201