| 1 |
#ifndef CLMODULELOGGER_HPP |
| 2 |
#define CLMODULELOGGER_HPP |
| 3 |
|
| 4 |
#include "codelite_exports.h" |
| 5 |
#include "file_logger.h" |
| 6 |
|
| 7 |
#include <wx/colour.h> |
| 8 |
#include <wx/gdicmn.h> |
| 9 |
|
| 10 |
/// A small logger designed to be used by small subsystem of CodeLite |
| 11 |
class clModuleLogger; |
| 12 |
typedef clModuleLogger& (*clModuleLoggerFunction)(clModuleLogger&); |
| 13 |
|
| 14 |
class WXDLLIMPEXP_CL clModuleLogger |
| 15 |
{ |
| 16 |
int m_current_log_level = FileLogger::Error; |
| 17 |
wxString m_buffer; |
| 18 |
wxFileName m_logfile; |
| 19 |
|
| 20 |
public: |
| 21 |
clModuleLogger(); |
| 22 |
~clModuleLogger(); |
| 23 |
|
| 24 |
/// used to check if an entry added using the current log level |
| 25 |
/// will eventually be logged (we check this against the global |
| 26 |
/// logger debug level) |
| 27 |
bool CanLog() const; |
| 28 |
|
| 29 |
clModuleLogger& SetCurrentLogLevel(int level); |
| 30 |
|
| 31 |
/** |
| 32 |
* @brief return prefix string suitable for the current time and log level |
| 33 |
*/ |
| 34 |
wxString Prefix(); |
| 35 |
|
| 36 |
/** |
| 37 |
* @brief open log file |
| 38 |
*/ |
| 39 |
void Open(const wxFileName& filepath); |
| 40 |
|
| 41 |
inline clModuleLogger& operator<<(clModuleLoggerFunction f) |
| 42 |
{ |
| 43 |
Flush(); |
| 44 |
return *this; |
| 45 |
} |
| 46 |
|
| 47 |
// special types printing |
| 48 |
inline clModuleLogger& operator<<(const std::vector<wxString>& arr) |
| 49 |
{ |
| 50 |
if(!CanLog()) { |
| 51 |
return *this; |
| 52 |
} |
| 53 |
|
| 54 |
if(!m_buffer.empty()) { |
| 55 |
m_buffer << " "; |
| 56 |
} |
| 57 |
|
| 58 |
m_buffer << "["; |
| 59 |
if(!arr.empty()) { |
| 60 |
for(size_t i = 0; i < arr.size(); ++i) { |
| 61 |
m_buffer << arr[i] << ", "; |
| 62 |
} |
| 63 |
m_buffer.RemoveLast(2); |
| 64 |
} |
| 65 |
m_buffer << "]"; |
| 66 |
return *this; |
| 67 |
} |
| 68 |
|
| 69 |
inline clModuleLogger& operator<<(const wxStringSet_t& S) |
| 70 |
{ |
| 71 |
if(!CanLog()) { |
| 72 |
return *this; |
| 73 |
} |
| 74 |
if(!m_buffer.empty()) { |
| 75 |
m_buffer << " "; |
| 76 |
} |
| 77 |
m_buffer << "{"; |
| 78 |
if(!S.empty()) { |
| 79 |
for(const wxString& s : S) { |
| 80 |
m_buffer << s << ", "; |
| 81 |
} |
| 82 |
m_buffer.RemoveLast(2); |
| 83 |
} |
| 84 |
m_buffer << "}"; |
| 85 |
return *this; |
| 86 |
} |
| 87 |
|
| 88 |
inline clModuleLogger& operator<<(const wxStringMap_t& M) |
| 89 |
{ |
| 90 |
if(!CanLog()) { |
| 91 |
return *this; |
| 92 |
} |
| 93 |
if(!m_buffer.empty()) { |
| 94 |
m_buffer << " "; |
| 95 |
} |
| 96 |
m_buffer << "{"; |
| 97 |
if(!M.empty()) { |
| 98 |
for(const auto& vt : M) { |
| 99 |
m_buffer << "{" << vt.first << ", " << vt.second << "}, "; |
| 100 |
} |
| 101 |
m_buffer.RemoveLast(2); |
| 102 |
} |
| 103 |
m_buffer << "}"; |
| 104 |
return *this; |
| 105 |
} |
| 106 |
|
| 107 |
inline clModuleLogger& operator<<(const wxArrayString& arr) |
| 108 |
{ |
| 109 |
if(!CanLog()) { |
| 110 |
return *this; |
| 111 |
} |
| 112 |
std::vector<wxString> v{ arr.begin(), arr.end() }; |
| 113 |
*this << v; |
| 114 |
return *this; |
| 115 |
} |
| 116 |
|
| 117 |
inline clModuleLogger& operator<<(const wxColour& colour) |
| 118 |
{ |
| 119 |
if(!CanLog()) { |
| 120 |
return *this; |
| 121 |
} |
| 122 |
|
| 123 |
*this << colour.GetAsString(wxC2S_HTML_SYNTAX); |
| 124 |
return *this; |
| 125 |
} |
| 126 |
|
| 127 |
inline clModuleLogger& operator<<(const wxPoint& point) |
| 128 |
{ |
| 129 |
if(!CanLog()) { |
| 130 |
return *this; |
| 131 |
} |
| 132 |
|
| 133 |
wxString str; |
| 134 |
str << "{x:" << point.x << ", y:" << point.y << "}"; |
| 135 |
*this << str; |
| 136 |
return *this; |
| 137 |
} |
| 138 |
|
| 139 |
inline clModuleLogger& operator<<(const wxSize& size) |
| 140 |
{ |
| 141 |
if(!CanLog()) { |
| 142 |
return *this; |
| 143 |
} |
| 144 |
|
| 145 |
wxString str; |
| 146 |
str << "{w:" << size.GetWidth() << ", h:" << size.GetHeight() << "}"; |
| 147 |
*this << str; |
| 148 |
return *this; |
| 149 |
} |
| 150 |
|
| 151 |
inline clModuleLogger& operator<<(const wxRect& rect) |
| 152 |
{ |
| 153 |
if(!CanLog()) { |
| 154 |
return *this; |
| 155 |
} |
| 156 |
|
| 157 |
*this << "{" << rect.GetTopLeft() << "," << rect.GetSize() << "}"; |
| 158 |
return *this; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* @brief special wxString printing |
| 163 |
* Without this overload operator, on some compilers, the "clDEBUG()<< wxString" might be "going" to the one |
| 164 |
* that handles wxFileName... |
| 165 |
*/ |
| 166 |
inline clModuleLogger& operator<<(const wxString& str) |
| 167 |
{ |
| 168 |
if(!CanLog()) { |
| 169 |
return *this; |
| 170 |
} |
| 171 |
if(!m_buffer.empty()) { |
| 172 |
m_buffer << " "; |
| 173 |
} |
| 174 |
m_buffer << str; |
| 175 |
return *this; |
| 176 |
} |
| 177 |
/** |
| 178 |
* @brief handle char* |
| 179 |
*/ |
| 180 |
inline clModuleLogger& operator<<(const char* str) |
| 181 |
{ |
| 182 |
if(!CanLog()) { |
| 183 |
return *this; |
| 184 |
} |
| 185 |
wxString s(str); |
| 186 |
return *this << s; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* @brief special wxFileName printing |
| 191 |
*/ |
| 192 |
inline clModuleLogger& operator<<(const wxFileName& fn) |
| 193 |
{ |
| 194 |
if(!CanLog()) { |
| 195 |
return *this; |
| 196 |
} |
| 197 |
if(!m_buffer.empty()) { |
| 198 |
m_buffer << " "; |
| 199 |
} |
| 200 |
m_buffer << fn.GetFullPath(); |
| 201 |
return *this; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* @brief append any type to the buffer, take log level into consideration |
| 206 |
*/ |
| 207 |
template <typename T> clModuleLogger& Append(const T& elem) |
| 208 |
{ |
| 209 |
if(!m_buffer.empty()) { |
| 210 |
m_buffer << " "; |
| 211 |
} |
| 212 |
m_buffer << elem; |
| 213 |
return *this; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* @brief flush the logger content |
| 218 |
*/ |
| 219 |
void Flush(); |
| 220 |
}; |
| 221 |
|
| 222 |
inline clModuleLogger& endl(clModuleLogger& d) |
| 223 |
{ |
| 224 |
d.Flush(); |
| 225 |
return d; |
| 226 |
} |
| 227 |
|
| 228 |
template <typename T> clModuleLogger& operator<<(clModuleLogger& logger, const T& obj) |
| 229 |
{ |
| 230 |
if(!logger.CanLog()) { |
| 231 |
return logger; |
| 232 |
} |
| 233 |
|
| 234 |
logger.Append(obj); |
| 235 |
return logger; |
| 236 |
} |
| 237 |
|
| 238 |
#define LOG_SYSTEM(LOG) LOG.SetCurrentLogLevel(FileLogger::System) << LOG.Prefix() |
| 239 |
#define LOG_ERROR(LOG) LOG.SetCurrentLogLevel(FileLogger::Error) << LOG.Prefix() |
| 240 |
#define LOG_WARNING(LOG) LOG.SetCurrentLogLevel(FileLogger::Warning) << LOG.Prefix() |
| 241 |
#define LOG_DEBUG(LOG) LOG.SetCurrentLogLevel(FileLogger::Dbg) << LOG.Prefix() |
| 242 |
#define LOG_TRACE(LOG) LOG.SetCurrentLogLevel(FileLogger::Developer) << LOG.Prefix() |
| 243 |
|
| 244 |
#endif // CLMODULELOGGER_HPP |