PLog.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015-2018 Digital Barriers plc. All rights reserved.
3  * Contact: http://www.digitalbarriers.com/
4  *
5  * This file is part of the Papillon SDK.
6  *
7  * You can't use, modify or distribute any part of this file without
8  * the explicit written agreements of Digital Barriers.
9  */
10 
15 #pragma once
16 #include <PapillonCommon.h>
17 #include <PString.h>
18 #include <PStringConstants.h>
19 
20 PAPILLON_BEGIN_NAMESPACE
21 
31 #ifndef SWIG
32  typedef void (*PCallback_Logger)(const PString& logMessage);
33 #endif
34 
38 static const PString C_STR_DEFAULT_LOG_FORMAT = "date (sev): msg";
39 
72 class PAPILLON_API_CORE PLog
73 {
74 private:
75  PLog(); // cannot be instantiated
76 
77 public:
81  enum ELogLevel
82  {
83  E_LEVEL_TRACE = 0,
89  };
90 
95  {
97  E_REGEXP_WILDCARD
98  };
99 
103  static const PString& GetPrefix();
104 
108  static void SetPrefix(const PString& prefix);
109 
115  static PResult OpenConsoleLogger(ELogLevel level = E_LEVEL_INFO);
116 
127  static PResult OpenFileLogger(const PString& loggerName,
128  const PString& destinationFilename,
129  ELogLevel level = E_LEVEL_INFO,
130  int maximumNumberOfLogs = -1,
131  int maximumNumberOfLogFiles = 30);
132 
139 #ifndef SWIG
140  static PResult OpenCallbackLogger(const PString& loggerName, PCallback_Logger callback, ELogLevel level = E_LEVEL_INFO);
141 #endif
142 
147  static PResult Close(const PString& loggerName);
148 
153  static PResult CloseConsole();
154 
158  static void DisableConsoleLoggerOnDebug();
159 
166  static PResult SetLevel(const PString& loggerName, ELogLevel level);
167 
174  static PResult SetConsoleLevel(ELogLevel level);
175 
180  static ELogLevel GetLevel(const PString& loggerName);
181 
186  static ELogLevel GetConsoleLevel();
187 
191  static void EnableConsoleLogger();
192 
196  static void DisableConsoleLogger();
197 
213  static PResult SetFormat(const PString& loggerName, const PString& format);
214 
230  static PResult SetConsoleFormat(const PString& format);
231 
238  static PResult SetRegExpFilter(const PString& loggerName, const PString& regExpPattern, ERegExpPattern syntax = E_REGEXP_WILDCARD);
239 
248  static PResult Log(ELogLevel level, const PString& message, papillon::int32 lineNumber = 0, const PString& filename = PString::Empty());
249 
255  static ELogLevel LogLevelFromString(const char* stringLevel);
256 };
257 
263 class PAPILLON_API_CORE PLogStream
264 {
265 public:
266  PLogStream(PLog::ELogLevel logLevel, const PString& messagePrefix, papillon::int32 lineNumber = 0, const PString& filename = PString::Empty());
267  ~PLogStream();
268  PLog::ELogLevel GetLogLevel() const { return m_logLevel; }
269  void Push(const PString& string) const { m_message += string; m_hasMessage = true;}
270 
271 private:
272  PLog::ELogLevel m_logLevel ;
273  papillon::int32 m_lineNumber;
274  PString m_filename ;
275  mutable PString m_message ;
276  mutable bool m_hasMessage;
277 };
278 
279 inline const PLogStream& operator<<(const PLogStream& os, void* ptr) { os.Push(PString("0x%1").Arg(reinterpret_cast<papillon::uintp>(ptr), sizeof(papillon::uintp), 16, papillon::wchar('0'))); return os; }
280 inline const PLogStream& operator<<(const PLogStream& os, const char* val) { os.Push(PString(val)); return os; }
281 inline const PLogStream& operator<<(const PLogStream& os, const wchar_t* val) { os.Push(PString(val)); return os; }
282 inline const PLogStream& operator<<(const PLogStream& os, const std::string& val) { os.Push(PString(val)); return os; }
283 inline const PLogStream& operator<<(const PLogStream& os, bool val) { os.Push(PString(val ? "true" : "false")); return os; }
284 inline const PLogStream& operator<<(const PLogStream& os, double val) { os.Push(PString(val)); return os; }
285 inline const PLogStream& operator<<(const PLogStream& os, float val) { os.Push(PString(val)); return os; }
286 inline const PLogStream& operator<<(const PLogStream& os, char val) { os.Push(PString(val)); return os; }
287 inline const PLogStream& operator<<(const PLogStream& os, int8 val) { os.Push(PString(val)); return os; }
288 inline const PLogStream& operator<<(const PLogStream& os, uint8 val) { os.Push(PString(val)); return os; }
289 inline const PLogStream& operator<<(const PLogStream& os, int16 val) { os.Push(PString(val)); return os; }
290 inline const PLogStream& operator<<(const PLogStream& os, uint16 val) { os.Push(PString(val)); return os; }
291 inline const PLogStream& operator<<(const PLogStream& os, int32 val) { os.Push(PString(val)); return os; }
292 inline const PLogStream& operator<<(const PLogStream& os, uint32 val) { os.Push(PString(val)); return os; }
293 inline const PLogStream& operator<<(const PLogStream& os, int64 val) { os.Push(PString(val)); return os; }
294 inline const PLogStream& operator<<(const PLogStream& os, uint64 val) { os.Push(PString(val)); return os; }
295 #ifndef SWIG
296 # ifdef PAPILLON_INT64C
297 inline const PLogStream& operator<<(const PLogStream& os, int64c val) { os.Push(PString(val)); return os; }
298 inline const PLogStream& operator<<(const PLogStream& os, uint64c val) { os.Push(PString(val)); return os; }
299 # endif
300 #endif
301 
302 
303 template<class T> const PLogStream& operator<<(const PLogStream& os, const T& obj)
304 {
305  os.Push(obj.ToString());
306  return os;
307 }
308 
309 
310 #ifdef ECLIPSE_CDT
311 
312 std::cout P_LOG(int level);
313 std::cout P_LOG_SKIP_LINE;
314 std::cout P_LOG_TRACE;
315 std::cout P_LOG_DEBUG;
316 std::cout P_LOG_INFO;
317 std::cout P_LOG_WARNING;
318 std::cout P_LOG_ERROR;
319 std::cout P_LOG_FATAL;
320 
321 #else // ECLIPSE_CDT
322 
326 #define P_LOG(level) papillon::PLogStream(level, papillon::C_LOG_PREFIX, PAPILLON_SRC_POS)
327 
331 #define P_LOG_SKIP_LINE papillon::PLog::Log(papillon::PLog::E_LEVEL_INFO, "\n");
332 
336 #define P_LOG_TRACE papillon::PLogStream(papillon::PLog::E_LEVEL_TRACE, papillon::PLog::GetPrefix(), PAPILLON_SRC_POS)
337 
341 #define P_LOG_DEBUG papillon::PLogStream(papillon::PLog::E_LEVEL_DEBUG, papillon::PLog::GetPrefix(), PAPILLON_SRC_POS)
342 
346 #define P_LOG_INFO papillon::PLogStream(papillon::PLog::E_LEVEL_INFO, papillon::PLog::GetPrefix(), PAPILLON_SRC_POS)
347 
351 #define P_LOG_WARNING papillon::PLogStream(papillon::PLog::E_LEVEL_WARNING, papillon::PLog::GetPrefix(), PAPILLON_SRC_POS)
352 
356 #define P_LOG_ERROR papillon::PLogStream(papillon::PLog::E_LEVEL_ERROR, papillon::PLog::GetPrefix(), PAPILLON_SRC_POS)
357 
361 #define P_LOG_FATAL papillon::PLogStream(papillon::PLog::E_LEVEL_FATAL, papillon::PLog::GetPrefix(), PAPILLON_SRC_POS)
362 
363 #endif // ECLIPSE_CDT
364 
365 PAPILLON_END_NAMESPACE
PAPILLON_BEGIN_NAMESPACE typedef void(* PCallback_Logger)(const PString &logMessage)
Callback to be notified each time there is a new log message.
Definition: PLog.h:32
The PString class provides an Unicode character strings.
Definition: PString.h:37
String constants.
const PLogStream & operator<<(const PLogStream &os, void *ptr)
Definition: PLog.h:279
A debug severity message.
Definition: PLog.h:84
The PLogStream class represents a log stream used by log macros (should not be used directly...
Definition: PLog.h:263
void Push(const PString &string) const
Definition: PLog.h:269
#define P_LOG_SKIP_LINE
Writes a blank line in the log.
Definition: PLog.h:331
The PResult class represents the result returned by most of Papillon functions; this class is used to...
Definition: PResult.h:159
The PLog class represents the log system (singleton) used by Papillon; thread-safe.
Definition: PLog.h:72
ERegExpPattern
All the available way to interpret regular expression patterns.
Definition: PLog.h:94
ELogLevel
All the available log levels.
Definition: PLog.h:81
A warning severity message.
Definition: PLog.h:86
#define P_LOG_ERROR
Logs the specified message with ERROR level.
Definition: PLog.h:356
#define P_LOG_WARNING
Logs the specified message with WARNING level.
Definition: PLog.h:351
A informational severity message.
Definition: PLog.h:85
Class PString: an unicode character strings (use copy-on-write idiom).
static const PString & Empty()
Returns the constant holding the empty string "".
Definition: PString.h:64
#define P_LOG_TRACE
Logs the specified message with TRACE level.
Definition: PLog.h:336
A rich Perl-like pattern matching syntax.
Definition: PLog.h:96
#define P_LOG_INFO
Logs the specified message with INFO level.
Definition: PLog.h:346
An error severity message.
Definition: PLog.h:87
PLog::ELogLevel GetLogLevel() const
Definition: PLog.h:268
A fatal severity message.
Definition: PLog.h:88
#define P_LOG_FATAL
Logs the specified message with FATAL level.
Definition: PLog.h:361
#define P_LOG_DEBUG
Logs the specified message with DEBUG level.
Definition: PLog.h:341
#define P_LOG(level)
Logs a message with the specified log level.
Definition: PLog.h:326