| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2014 Eran Ifrah |
| 5 |
// file name : file_logger.h |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
|
| 26 |
#ifndef FILELOGGER_H |
| 27 |
#define FILELOGGER_H |
| 28 |
|
| 29 |
#include "codelite_exports.h" |
| 30 |
#include "macros.h" |
| 31 |
|
| 32 |
#include <vector> |
| 33 |
#include <wx/colour.h> |
| 34 |
#include <wx/ffile.h> |
| 35 |
#include <wx/filename.h> |
| 36 |
#include <wx/stopwatch.h> |
| 37 |
#include <wx/thread.h> |
| 38 |
#include <wxStringHash.h> |
| 39 |
|
| 40 |
// manipulator function |
| 41 |
class FileLogger; |
| 42 |
typedef FileLogger& (*FileLoggerFunction)(FileLogger&); |
| 43 |
|
| 44 |
class WXDLLIMPEXP_CL FileLogger |
| 45 |
{ |
| 46 |
public: |
| 47 |
enum { System = -1, Error = 0, Warning = 1, Dbg = 2, Developer = 3 }; |
| 48 |
|
| 49 |
protected: |
| 50 |
static int m_verbosity; |
| 51 |
static wxString m_logfile; |
| 52 |
int _requestedLogLevel; |
| 53 |
FILE* m_fp; |
| 54 |
wxString m_buffer; |
| 55 |
static std::unordered_map<wxThreadIdType, wxString> m_threads; |
| 56 |
static wxCriticalSection m_cs; |
| 57 |
|
| 58 |
protected: |
| 59 |
static wxString GetCurrentThreadName(); |
| 60 |
|
| 61 |
public: |
| 62 |
FileLogger(int requestedVerbo); |
| 63 |
~FileLogger(); |
| 64 |
|
| 65 |
FileLogger& SetRequestedLogLevel(int level) |
| 66 |
{ |
| 67 |
_requestedLogLevel = level; |
| 68 |
return *this; |
| 69 |
} |
| 70 |
|
| 71 |
int GetRequestedLogLevel() const { return _requestedLogLevel; } |
| 72 |
static int GetCurrentLogLevel() { return m_verbosity; } |
| 73 |
|
| 74 |
/** |
| 75 |
* @brief return true if log_level is lower than the currently set log level |
| 76 |
*/ |
| 77 |
static bool CanLog(int log_level) { return log_level <= GetCurrentLogLevel(); } |
| 78 |
|
| 79 |
/** |
| 80 |
* @brief give a thread-id a unique name which will be displayed in log |
| 81 |
*/ |
| 82 |
static void RegisterThread(wxThreadIdType id, const wxString& name); |
| 83 |
static void UnRegisterThread(wxThreadIdType id); |
| 84 |
|
| 85 |
/** |
| 86 |
* @brief create log entry prefix |
| 87 |
*/ |
| 88 |
static wxString Prefix(int verbosity); |
| 89 |
|
| 90 |
void AddLogLine(const wxString& msg, int verbosity); |
| 91 |
/** |
| 92 |
* @brief print array into the log file |
| 93 |
* @param arr |
| 94 |
* @param verbosity |
| 95 |
*/ |
| 96 |
void AddLogLine(const wxArrayString& arr, int verbosity); |
| 97 |
static void SetVerbosity(int level); |
| 98 |
|
| 99 |
// Set the verbosity as string |
| 100 |
static void SetVerbosity(const wxString& verbosity); |
| 101 |
|
| 102 |
///---------------------------------- |
| 103 |
/// Statics |
| 104 |
///---------------------------------- |
| 105 |
/** |
| 106 |
* @brief open the log file |
| 107 |
*/ |
| 108 |
static void OpenLog(const wxString& fullName, int verbosity); |
| 109 |
// Various util methods |
| 110 |
static wxString GetVerbosityAsString(int verbosity); |
| 111 |
static int GetVerbosityAsNumber(const wxString& verbosity); |
| 112 |
|
| 113 |
inline FileLogger& operator<<(FileLoggerFunction f) |
| 114 |
{ |
| 115 |
Flush(); |
| 116 |
return *this; |
| 117 |
} |
| 118 |
|
| 119 |
// special types printing |
| 120 |
inline FileLogger& operator<<(const std::vector<wxString>& arr) |
| 121 |
{ |
| 122 |
if(GetRequestedLogLevel() > m_verbosity) { |
| 123 |
return *this; |
| 124 |
} |
| 125 |
if(!m_buffer.IsEmpty()) { |
| 126 |
m_buffer << " "; |
| 127 |
} |
| 128 |
m_buffer << "["; |
| 129 |
if(!arr.empty()) { |
| 130 |
for(size_t i = 0; i < arr.size(); ++i) { |
| 131 |
m_buffer << arr[i] << ", "; |
| 132 |
} |
| 133 |
m_buffer.RemoveLast(2); |
| 134 |
} |
| 135 |
m_buffer << "]"; |
| 136 |
return *this; |
| 137 |
} |
| 138 |
|
| 139 |
inline FileLogger& operator<<(const wxStringSet_t& S) |
| 140 |
{ |
| 141 |
if(GetRequestedLogLevel() > m_verbosity) { |
| 142 |
return *this; |
| 143 |
} |
| 144 |
if(!m_buffer.IsEmpty()) { |
| 145 |
m_buffer << " "; |
| 146 |
} |
| 147 |
m_buffer << "{"; |
| 148 |
if(!S.empty()) { |
| 149 |
for(const wxString& s : S) { |
| 150 |
m_buffer << s << ", "; |
| 151 |
} |
| 152 |
m_buffer.RemoveLast(2); |
| 153 |
} |
| 154 |
m_buffer << "}"; |
| 155 |
return *this; |
| 156 |
} |
| 157 |
|
| 158 |
inline FileLogger& operator<<(const wxStringMap_t& M) |
| 159 |
{ |
| 160 |
if(GetRequestedLogLevel() > m_verbosity) { |
| 161 |
return *this; |
| 162 |
} |
| 163 |
if(!m_buffer.IsEmpty()) { |
| 164 |
m_buffer << " "; |
| 165 |
} |
| 166 |
m_buffer << "{"; |
| 167 |
if(!M.empty()) { |
| 168 |
for(const auto& vt : M) { |
| 169 |
m_buffer << "{" << vt.first << ", " << vt.second << "}, "; |
| 170 |
} |
| 171 |
m_buffer.RemoveLast(2); |
| 172 |
} |
| 173 |
m_buffer << "}"; |
| 174 |
return *this; |
| 175 |
} |
| 176 |
|
| 177 |
inline FileLogger& operator<<(const wxArrayString& arr) |
| 178 |
{ |
| 179 |
if(GetRequestedLogLevel() > m_verbosity) { |
| 180 |
return *this; |
| 181 |
} |
| 182 |
std::vector<wxString> v{ arr.begin(), arr.end() }; |
| 183 |
*this << v; |
| 184 |
return *this; |
| 185 |
} |
| 186 |
|
| 187 |
inline FileLogger& operator<<(const wxColour& colour) |
| 188 |
{ |
| 189 |
if(GetRequestedLogLevel() > m_verbosity) { |
| 190 |
return *this; |
| 191 |
} |
| 192 |
|
| 193 |
*this << colour.GetAsString(wxC2S_HTML_SYNTAX); |
| 194 |
return *this; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* @brief special wxString printing |
| 199 |
* Without this overload operator, on some compilers, the "clDEBUG()<< wxString" might be "going" to the one |
| 200 |
* that handles wxFileName... |
| 201 |
*/ |
| 202 |
inline FileLogger& operator<<(const wxString& str) |
| 203 |
{ |
| 204 |
if(GetRequestedLogLevel() > m_verbosity) { |
| 205 |
return *this; |
| 206 |
} |
| 207 |
if(!m_buffer.IsEmpty()) { |
| 208 |
m_buffer << " "; |
| 209 |
} |
| 210 |
m_buffer << str; |
| 211 |
return *this; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* @brief special wxFileName printing |
| 216 |
*/ |
| 217 |
inline FileLogger& operator<<(const wxFileName& fn) |
| 218 |
{ |
| 219 |
if(GetRequestedLogLevel() > m_verbosity) { |
| 220 |
return *this; |
| 221 |
} |
| 222 |
if(!m_buffer.IsEmpty()) { |
| 223 |
m_buffer << " "; |
| 224 |
} |
| 225 |
m_buffer << fn.GetFullPath(); |
| 226 |
return *this; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* @brief append any type to the buffer, take log level into consideration |
| 231 |
*/ |
| 232 |
template <typename T> FileLogger& Append(const T& elem, int level) |
| 233 |
{ |
| 234 |
if(level > m_verbosity) { |
| 235 |
return *this; |
| 236 |
} |
| 237 |
if(!m_buffer.IsEmpty()) { |
| 238 |
m_buffer << " "; |
| 239 |
} |
| 240 |
m_buffer << elem; |
| 241 |
return *this; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* @brief flush the logger content |
| 246 |
*/ |
| 247 |
void Flush(); |
| 248 |
}; |
| 249 |
|
| 250 |
inline FileLogger& clEndl(FileLogger& d) |
| 251 |
{ |
| 252 |
d.Flush(); |
| 253 |
return d; |
| 254 |
} |
| 255 |
|
| 256 |
inline FileLogger& endl(FileLogger& d) |
| 257 |
{ |
| 258 |
d.Flush(); |
| 259 |
return d; |
| 260 |
} |
| 261 |
|
| 262 |
template <typename T> FileLogger& operator<<(FileLogger& logger, const T& obj) |
| 263 |
{ |
| 264 |
logger.Append(obj, logger.GetRequestedLogLevel()); |
| 265 |
return logger; |
| 266 |
} |
| 267 |
|
| 268 |
// New API |
| 269 |
#define clDEBUG() FileLogger(FileLogger::Dbg) << FileLogger::Prefix(FileLogger::Dbg) |
| 270 |
#define clDEBUG1() FileLogger(FileLogger::Developer) << FileLogger::Prefix(FileLogger::Developer) |
| 271 |
|
| 272 |
// alias |
| 273 |
#define clTRACE() clDEBUG1() |
| 274 |
|
| 275 |
#define clERROR() FileLogger(FileLogger::Error) << FileLogger::Prefix(FileLogger::Error) |
| 276 |
#define clWARNING() FileLogger(FileLogger::Warning) << FileLogger::Prefix(FileLogger::Warning) |
| 277 |
#define clSYSTEM() FileLogger(FileLogger::System) << FileLogger::Prefix(FileLogger::System) |
| 278 |
|
| 279 |
#define LOG_IF_DEBUG if(FileLogger::CanLog(FileLogger::Dbg)) |
| 280 |
#define LOG_IF_TRACE if(FileLogger::CanLog(FileLogger::Developer)) |
| 281 |
#define LOG_IF_WARN if(FileLogger::CanLog(FileLogger::Developer)) |
| 282 |
|
| 283 |
// A replacement for wxLogMessage |
| 284 |
#define clLogMessage(msg) clDEBUG() << msg |
| 285 |
|
| 286 |
#endif // FILELOGGER_H |