File indexing completed on 2025-01-05 04:37:30

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef JORISLOG_H
0008 #define JORISLOG_H
0009 
0010 #include "constants.h"
0011 #include <QString>
0012 #include <QUrl>
0013 #include <ktorrent_export.h>
0014 
0015 // LOG MESSAGES CONSTANTS
0016 #define LOG_NONE 0x00
0017 #define LOG_IMPORTANT 0x01
0018 #define LOG_NOTICE 0x03
0019 #define LOG_DEBUG 0x07
0020 #define LOG_ALL 0x0F
0021 
0022 #define SYS_GEN 0x0010 // Genereral info messages
0023 #define SYS_CON 0x0020 // Connections
0024 #define SYS_TRK 0x0040 // Tracker
0025 #define SYS_DHT 0x0080 // DHT
0026 #define SYS_DIO 0x0100 // Disk IO related stuff, saving and loading of chunks ...
0027 #define SYS_UTP 0x0200 // UTP
0028 
0029 // plugins
0030 #define SYS_IPF 0x1000 // IPFilter
0031 #define SYS_SRC 0x2000 // Search plugin
0032 #define SYS_PNP 0x4000 // UPnP plugin
0033 #define SYS_INW 0x8000 // InfoWidget
0034 #define SYS_SNF 0x10000 // ScanFolder plugin
0035 #define SYS_MPL 0x20000 // Media player plugin
0036 #define SYS_SCD 0x40000 // Scheduler plugin
0037 #define SYS_BTF 0x80000 // BitFinder plugin
0038 #define SYS_WEB 0x100000 // WebInterface plugin
0039 #define SYS_ZCO 0x200000 // ZeroConf plugin
0040 #define SYS_SCR 0x400000 // Scripting plugin
0041 #define SYS_SYN 0x800000 // Syndication plugin
0042 
0043 namespace bt
0044 {
0045 class LogMonitorInterface;
0046 
0047 /**
0048  * @author Joris Guisson
0049  * @brief Class which writes messages to a logfile
0050  *
0051  * This class writes messages to a logfile. To use it, create an instance,
0052  * set the output file and write stuff with the << operator.
0053  *
0054  * By default all messages will also be printed on the standard output. This
0055  * can be turned down using the @a setOutputToConsole function.
0056  *
0057  * There is also the possibility to monitor what is written to the log using
0058  * the LogMonitorInterface class.
0059  */
0060 class KTORRENT_EXPORT Log
0061 {
0062     class Private;
0063 
0064     Private *priv;
0065 
0066 public:
0067     /**
0068      * Constructor.
0069      */
0070     Log();
0071 
0072     /**
0073      * Destructor, closes the file.
0074      */
0075     virtual ~Log();
0076 
0077     /**
0078      * Enable or disable the printing of log messages to the standard
0079      * output.
0080      * @param on Enable or disable
0081      */
0082     void setOutputToConsole(bool on);
0083 
0084     /**
0085      * Add a log monitor.
0086      * @param m The log monitor
0087      */
0088     void addMonitor(LogMonitorInterface *m);
0089 
0090     /**
0091      * Remove a log monitor. It will not be deleted.
0092      * @param m The log monitor
0093      */
0094     void removeMonitor(LogMonitorInterface *m);
0095 
0096     /**
0097      * Set the output logfile.
0098      * @param file The name of the file
0099      * @param rotate Whether or not to rotate the logs
0100      * @param bool handle_qt_messages Whether or not handle Qt messages
0101      * @throw Exception if the file can't be opened
0102      */
0103     void setOutputFile(const QString &file, bool rotate, bool handle_qt_messages);
0104 
0105     /**
0106      * Write a number to the log file.
0107      * Anything which can be passed to QString::number will do.
0108      * @param val The value
0109      * @return This Log
0110      */
0111     template<class T> Log &operator<<(T val)
0112     {
0113         return operator<<(QString::number(val));
0114     }
0115 
0116     /**
0117      * Apply a function to the Log.
0118      * @param func The function
0119      * @return This Log
0120      */
0121     Log &operator<<(Log &(*func)(Log &))
0122     {
0123         return func(*this);
0124     }
0125 
0126     /**
0127      * Output a QString to the log.
0128      * @param s The QString
0129      * @return This Log
0130      */
0131     Log &operator<<(const char *s);
0132 
0133     /**
0134      * Output a QString to the log.
0135      * @param s The QString
0136      * @return This Log
0137      */
0138     Log &operator<<(const QString &s);
0139 
0140     /**
0141      * Output a 64 bit integer to the log.
0142      * @param v The integer
0143      * @return This Log
0144      */
0145     Log &operator<<(Uint64 v);
0146 
0147     /**
0148      * Output a 64 bit integer to the log.
0149      * @param v The integer
0150      * @return This Log
0151      */
0152     Log &operator<<(Int64 v);
0153 
0154     /**
0155      * Prints and endline character to the Log and flushes it.
0156      * @param lg The Log
0157      * @return @a lg
0158      */
0159     KTORRENT_EXPORT friend Log &endl(Log &lg);
0160 
0161     /**
0162      * Write an URL to the file.
0163      * @param text The QUrl
0164      * @return This Log
0165      */
0166     Log &operator<<(const QUrl &url);
0167 
0168     /**
0169      * Sets a filter for log messages. Applies only to listeners via LogMonitorInterface!
0170      * @param filter SYS & LOG flags combined with bitwise OR.
0171      */
0172     void setFilter(unsigned int filter);
0173 
0174     /// Lock the mutex of the log, should be called in Out()
0175     void lock();
0176 
0177     /// Called by the auto log rotate job when it has finished
0178     void logRotateDone();
0179 };
0180 
0181 KTORRENT_EXPORT Log &endl(Log &lg);
0182 KTORRENT_EXPORT Log &Out(unsigned int arg = 0x00);
0183 
0184 /**
0185  * Initialize the global log.
0186  * @param file The log file
0187  * @param rotate_logs Set to true if the logs need to be rotated
0188  * @param handle_qt_messages Set to true if Qt messages need to be logged
0189  * @param to_stdout Set to true if output to standard output is required
0190  * */
0191 KTORRENT_EXPORT void InitLog(const QString &file, bool rotate_logs = false, bool handle_qt_messages = true, bool to_stdout = false);
0192 
0193 /// Add a monitor to the global log
0194 KTORRENT_EXPORT void AddLogMonitor(LogMonitorInterface *m);
0195 
0196 /// Remove a monitor from the global log
0197 KTORRENT_EXPORT void RemoveLogMonitor(LogMonitorInterface *m);
0198 }
0199 
0200 #endif