source: calamares/trunk/fuentes/src/libcalamares/utils/Logger.h @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 5.3 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
4 *   Copyright 2014,      Teo Mrnjavac <teo@kde.org>
5 *   Copyright 2017-2018, Adriaan de Groot <groot@kde.org>
6 *
7 *   Calamares is free software: you can redistribute it and/or modify
8 *   it under the terms of the GNU General Public License as published by
9 *   the Free Software Foundation, either version 3 of the License, or
10 *   (at your option) any later version.
11 *
12 *   Calamares is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#ifndef CALAMARES_LOGGER_H
22#define CALAMARES_LOGGER_H
23
24#include <QDebug>
25
26#include "DllMacro.h"
27
28namespace Logger
29{
30    extern const char* continuation;
31
32    enum
33    {
34        LOG_DISABLE = 0,
35        LOGERROR = 1,
36        LOGWARNING = 2,
37        LOGINFO  = 3,
38        LOGEXTRA = 5,
39        LOGDEBUG = 6,
40        LOGVERBOSE = 8
41    } ;
42
43    class DLLEXPORT CLog : public QDebug
44    {
45    public:
46        explicit CLog( unsigned int debugLevel );
47        virtual ~CLog();
48
49    private:
50        QString m_msg;
51        unsigned int m_debugLevel;
52    };
53
54    class DLLEXPORT CDebug : public CLog
55    {
56    public:
57        CDebug( unsigned int debugLevel = LOGDEBUG ) : CLog( debugLevel )
58        {
59            if ( debugLevel <= LOGERROR )
60                *this << "ERROR:";
61            else if ( debugLevel <= LOGWARNING )
62                *this << "WARNING:";
63        }
64        virtual ~CDebug();
65    };
66
67    /**
68     * @brief The full path of the log file.
69     */
70    DLLEXPORT QString logFile();
71
72    /**
73     * @brief Start logging to the log file.
74     *
75     * Call this (once) to start logging to the log file (usually
76     * ~/.cache/calamares/session.log ). An existing log file is
77     * rolled over if it is too large.
78     */
79    DLLEXPORT void setupLogfile();
80
81    /**
82     * @brief Set a log level for future logging.
83     *
84     * Pass in a value from the LOG* enum, above. Use 0 to
85     * disable logging. Values greater than LOGVERBOSE are
86     * limited to LOGVERBOSE, which will log everything.
87     *
88     * Practical values are 0, 1, 2, and 6.
89     */
90    DLLEXPORT void setupLogLevel( unsigned int level );
91
92    /**
93     * @brief Row-oriented formatted logging.
94     *
95     * Use DebugRow to produce multiple rows of 2-column output
96     * in a debugging statement. For instance,
97     *      cDebug() << DebugRow<int,int>(1,12)
98     *               << DebugRow<int,int>(2,24)
99     * will produce a single timestamped debug line with continuations.
100     * Each DebugRow produces one line of output, with the two values.
101     */
102    template<typename T, typename U>
103    struct DebugRow
104    {
105    public:
106        explicit DebugRow(const T& t, const U& u)
107            : first(t)
108            , second(u)
109        {}
110
111        const T& first;
112        const U& second;
113    } ;
114
115    /**
116     * @brief List-oriented formatted logging.
117     *
118     * Use DebugList to produce multiple rows of output in a debugging
119     * statement. For instance,
120     *      cDebug() << DebugList( QStringList() << "foo" << "bar" )
121     * will produce a single timestamped debug line with continuations.
122     * Each element of the list of strings will be logged on a separate line.
123     */
124    struct DebugList
125    {
126        explicit DebugList( const QStringList& l )
127            : list(l)
128        {}
129
130        const QStringList& list;
131    } ;
132
133    /**
134     * @brief Map-oriented formatted logging.
135     *
136     * Use DebugMap to produce multiple rows of output in a debugging
137     * statement from a map. The output is intentionally a bit-yaml-ish.
138     *      cDebug() << DebugMap( map )
139     * will produce a single timestamped debug line with continuations.
140     * The continued lines will have a key (from the map) and a value
141     * on each line.
142     */
143    struct DebugMap
144    {
145    public:
146        explicit DebugMap(const QVariantMap& m)
147            : map( m )
148        {}
149
150        const QVariantMap& map;
151    } ;
152
153    /** @brief output operator for DebugRow */
154    template<typename T, typename U>
155    inline QDebug&
156    operator <<( QDebug& s, const DebugRow<T, U>& t )
157    {
158        s << continuation << t.first << ':' << ' ' << t.second;
159        return s;
160    }
161
162    /** @brief output operator for DebugList */
163    inline QDebug&
164    operator <<( QDebug& s, const DebugList& c )
165    {
166        for( const auto& i : c.list )
167            s << continuation << i;
168        return s;
169    }
170
171    /** @brief supporting method for outputting a DebugMap */
172    QString toString( const QVariant& v );
173
174    /** @brief output operator for DebugMap */
175    inline QDebug&
176    operator <<( QDebug& s, const DebugMap& t )
177    {
178        for ( auto it = t.map.constBegin(); it != t.map.constEnd(); ++it )
179            s << continuation << it.key().toUtf8().constData() << ':' << ' ' << toString( it.value() ).toUtf8().constData();
180        return s;
181    }
182}
183
184#define cDebug Logger::CDebug
185#define cWarning() Logger::CDebug(Logger::LOGWARNING)
186#define cError() Logger::CDebug(Logger::LOGERROR)
187
188#endif // CALAMARES_LOGGER_H
Note: See TracBrowser for help on using the repository browser.