File indexing completed on 2024-04-21 16:17:34

0001 /*
0002  *  SPDX-FileCopyrightText: 2016 Sebastian Kügler <sebas@kde.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 
0007 #ifndef KSCREEN_LOG_H
0008 #define KSCREEN_LOG_H
0009 
0010 #include "kscreen_export.h"
0011 
0012 #include <QLoggingCategory>
0013 #include <QObject>
0014 
0015 namespace KScreen
0016 {
0017 void log(const QString &msg);
0018 
0019 /** KScreen-internal file logging.
0020  *
0021  * The purpose of this class is to allow better debugging of kscreen. QDebug falls short here, since
0022  * we need to debug the concert of kscreen components from different processes.
0023  *
0024  * KScreen::Log manages access to kscreen's log file.
0025  *
0026  * The following environment variables are considered:
0027  * - disable logging by setting
0028  * KSCREEN_LOGGING=false
0029  * - set the log file to a custom path, the default is in ~/.local/share/kscreen/kscreen.log
0030  *
0031  * Please do not translate messages written to the logs, it's developer information and should be
0032  * english, independent from the user's locale preferences.
0033  *
0034  * @code
0035  *
0036  * Log::instance()->setContext("resume");
0037  * Log::log("Applying detected output configuration.");
0038  *
0039  * @endcode
0040  *
0041  * @since 5.8
0042  */
0043 class KSCREEN_EXPORT Log
0044 {
0045 public:
0046     virtual ~Log();
0047 
0048     static Log *instance();
0049 
0050     /** Log a message to a file
0051      *
0052      * Call this static method to add a new line to the log.
0053      *
0054      * @arg msg The log message to write.
0055      */
0056     static void log(const QString &msg, const QString &category = QString());
0057 
0058     /** Context for the logs.
0059      *
0060      * The context can be used to indicate what is going on overall, it is used to be able
0061      * to group log entries into subsequential operations. For example the context can be
0062      * "handling resume", which is then added to the log messages.
0063      *
0064      * @arg msg The log message to write to the file.
0065      *
0066      * @see ontext()
0067      */
0068     QString context() const;
0069 
0070     /** Set the context for the logs.
0071      *
0072      * @see context()
0073      */
0074     void setContext(const QString &context);
0075 
0076     /** Logging to file is enabled by environmental var, is it?
0077      *
0078      * @arg msg The log message to write to the file.
0079      * @return Whether logging is enabled.
0080      */
0081     bool enabled() const;
0082 
0083     /** Path to the log file
0084      *
0085      * This is usually ~/.local/share/kscreen/kscreen.log, but can be changed by setting
0086      * KSCREEN_LOGFILE in the environment.
0087      *
0088      * @return The path to the log file.
0089      */
0090     QString logFile() const;
0091 
0092 private:
0093     explicit Log();
0094     class Private;
0095     Private *const d;
0096 
0097     static Log *sInstance;
0098     explicit Log(Private *dd);
0099 };
0100 
0101 } // KSCreen namespace
0102 
0103 #endif // KSCREEN_LOG_H