File: clModuleLogger.hpp

package info (click to toggle)
codelite 17.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 136,204 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (244 lines) | stat: -rw-r--r-- 5,797 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#ifndef CLMODULELOGGER_HPP
#define CLMODULELOGGER_HPP

#include "codelite_exports.h"
#include "file_logger.h"

#include <wx/colour.h>
#include <wx/gdicmn.h>

/// A small logger designed to be used by small subsystem of CodeLite
class clModuleLogger;
typedef clModuleLogger& (*clModuleLoggerFunction)(clModuleLogger&);

class WXDLLIMPEXP_CL clModuleLogger
{
    int m_current_log_level = FileLogger::Error;
    wxString m_buffer;
    wxFileName m_logfile;

public:
    clModuleLogger();
    ~clModuleLogger();

    /// used to check if an entry added using the current log level
    /// will eventually be logged (we check this against the global
    /// logger debug level)
    bool CanLog() const;

    clModuleLogger& SetCurrentLogLevel(int level);

    /**
     * @brief return prefix string suitable for the current time and log level
     */
    wxString Prefix();

    /**
     * @brief open log file
     */
    void Open(const wxFileName& filepath);

    inline clModuleLogger& operator<<(clModuleLoggerFunction f)
    {
        Flush();
        return *this;
    }

    // special types printing
    inline clModuleLogger& operator<<(const std::vector<wxString>& arr)
    {
        if(!CanLog()) {
            return *this;
        }

        if(!m_buffer.empty()) {
            m_buffer << " ";
        }

        m_buffer << "[";
        if(!arr.empty()) {
            for(size_t i = 0; i < arr.size(); ++i) {
                m_buffer << arr[i] << ", ";
            }
            m_buffer.RemoveLast(2);
        }
        m_buffer << "]";
        return *this;
    }

    inline clModuleLogger& operator<<(const wxStringSet_t& S)
    {
        if(!CanLog()) {
            return *this;
        }
        if(!m_buffer.empty()) {
            m_buffer << " ";
        }
        m_buffer << "{";
        if(!S.empty()) {
            for(const wxString& s : S) {
                m_buffer << s << ", ";
            }
            m_buffer.RemoveLast(2);
        }
        m_buffer << "}";
        return *this;
    }

    inline clModuleLogger& operator<<(const wxStringMap_t& M)
    {
        if(!CanLog()) {
            return *this;
        }
        if(!m_buffer.empty()) {
            m_buffer << " ";
        }
        m_buffer << "{";
        if(!M.empty()) {
            for(const auto& vt : M) {
                m_buffer << "{" << vt.first << ", " << vt.second << "}, ";
            }
            m_buffer.RemoveLast(2);
        }
        m_buffer << "}";
        return *this;
    }

    inline clModuleLogger& operator<<(const wxArrayString& arr)
    {
        if(!CanLog()) {
            return *this;
        }
        std::vector<wxString> v{ arr.begin(), arr.end() };
        *this << v;
        return *this;
    }

    inline clModuleLogger& operator<<(const wxColour& colour)
    {
        if(!CanLog()) {
            return *this;
        }

        *this << colour.GetAsString(wxC2S_HTML_SYNTAX);
        return *this;
    }

    inline clModuleLogger& operator<<(const wxPoint& point)
    {
        if(!CanLog()) {
            return *this;
        }

        wxString str;
        str << "{x:" << point.x << ", y:" << point.y << "}";
        *this << str;
        return *this;
    }

    inline clModuleLogger& operator<<(const wxSize& size)
    {
        if(!CanLog()) {
            return *this;
        }

        wxString str;
        str << "{w:" << size.GetWidth() << ", h:" << size.GetHeight() << "}";
        *this << str;
        return *this;
    }

    inline clModuleLogger& operator<<(const wxRect& rect)
    {
        if(!CanLog()) {
            return *this;
        }

        *this << "{" << rect.GetTopLeft() << "," << rect.GetSize() << "}";
        return *this;
    }

    /**
     * @brief special wxString printing
     * Without this overload operator, on some compilers, the "clDEBUG()<< wxString" might be "going" to the one
     * that handles wxFileName...
     */
    inline clModuleLogger& operator<<(const wxString& str)
    {
        if(!CanLog()) {
            return *this;
        }
        if(!m_buffer.empty()) {
            m_buffer << " ";
        }
        m_buffer << str;
        return *this;
    }
    /**
     * @brief handle char*
     */
    inline clModuleLogger& operator<<(const char* str)
    {
        if(!CanLog()) {
            return *this;
        }
        wxString s(str);
        return *this << s;
    }

    /**
     * @brief special wxFileName printing
     */
    inline clModuleLogger& operator<<(const wxFileName& fn)
    {
        if(!CanLog()) {
            return *this;
        }
        if(!m_buffer.empty()) {
            m_buffer << " ";
        }
        m_buffer << fn.GetFullPath();
        return *this;
    }

    /**
     * @brief append any type to the buffer, take log level into consideration
     */
    template <typename T> clModuleLogger& Append(const T& elem)
    {
        if(!m_buffer.empty()) {
            m_buffer << " ";
        }
        m_buffer << elem;
        return *this;
    }

    /**
     * @brief flush the logger content
     */
    void Flush();
};

inline clModuleLogger& endl(clModuleLogger& d)
{
    d.Flush();
    return d;
}

template <typename T> clModuleLogger& operator<<(clModuleLogger& logger, const T& obj)
{
    if(!logger.CanLog()) {
        return logger;
    }

    logger.Append(obj);
    return logger;
}

#define LOG_SYSTEM(LOG) LOG.SetCurrentLogLevel(FileLogger::System) << LOG.Prefix()
#define LOG_ERROR(LOG) LOG.SetCurrentLogLevel(FileLogger::Error) << LOG.Prefix()
#define LOG_WARNING(LOG) LOG.SetCurrentLogLevel(FileLogger::Warning) << LOG.Prefix()
#define LOG_DEBUG(LOG) LOG.SetCurrentLogLevel(FileLogger::Dbg) << LOG.Prefix()
#define LOG_TRACE(LOG) LOG.SetCurrentLogLevel(FileLogger::Developer) << LOG.Prefix()

#endif // CLMODULELOGGER_HPP