Otclient  14/8/2020
logger.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #ifndef LOGGER_H
24 #define LOGGER_H
25 
26 #include "../global.h"
27 
29 #include <fstream>
30 
31 struct LogMessage {
32  LogMessage(Fw::LogLevel level, const std::string& message, std::size_t when) : level(level), message(message), when(when) { }
34  std::string message;
35  std::size_t when;
36 };
37 
38 // @bindsingleton g_logger
39 class Logger
40 {
41  enum {
42  MAX_LOG_HISTORY = 1000
43  };
44 
45  typedef std::function<void(Fw::LogLevel, const std::string&, int64)> OnLogCallback;
46 
47 public:
48  void log(Fw::LogLevel level, const std::string& message);
49  void logFunc(Fw::LogLevel level, const std::string& message, std::string prettyFunction);
50 
51  void debug(const std::string& what) { log(Fw::LogDebug, what); }
52  void info(const std::string& what) { log(Fw::LogInfo, what); }
53  void warning(const std::string& what) { log(Fw::LogWarning, what); }
54  void error(const std::string& what) { log(Fw::LogError, what); }
55  void fatal(const std::string& what) { log(Fw::LogFatal, what); }
56 
57  void fireOldMessages();
58  void setLogFile(const std::string& file);
59  void setOnLog(const OnLogCallback& onLog) { m_onLog = onLog; }
60 
61 private:
62  std::list<LogMessage> m_logMessages;
63  OnLogCallback m_onLog;
64  std::ofstream m_outFile;
65  std::recursive_mutex m_mutex;
66 };
67 
68 extern Logger g_logger;
69 
70 #define trace() logFunc(Fw::LogDebug, "", __PRETTY_FUNCTION__)
71 #define traceDebug(a) logFunc(Fw::LogDebug, a, __PRETTY_FUNCTION__)
72 #define traceInfo(a) logFunc(Fw::LogInfo, a, __PRETTY_FUNCTION__)
73 #define traceWarning(a) logFunc(Fw::LogWarning, a, __PRETTY_FUNCTION__)
74 #define traceError(a) logFunc(Fw::LogError, a, __PRETTY_FUNCTION__)
75 
76 #define logTraceCounter() { \
77  static int __count = 0; \
78  static Timer __timer; \
79  __count++; \
80  if(__timer.ticksElapsed() >= 1000) { \
81  logTraceDebug(__count); \
82  __count = 0; \
83  __timer.restart(); \
84  } \
85 }
86 
87 #define logTraceFrameCounter() { \
88  static int __count = 0; \
89  static Timer __timer; \
90  __count++; \
91  if(__timer.ticksElapsed() > 0) { \
92  logTraceDebug(__count); \
93  __count = 0; \
94  __timer.restart(); \
95  } \
96 }
97 
98 #endif
g_logger
Logger g_logger
Definition: logger.cpp:35
Fw::LogWarning
@ LogWarning
Definition: const.h:181
Fw::LogLevel
LogLevel
Definition: const.h:178
Fw::LogError
@ LogError
Definition: const.h:182
LogMessage::message
std::string message
Definition: logger.h:34
Logger::error
void error(const std::string &what)
Definition: logger.h:54
int64
int64_t int64
Definition: types.h:38
Logger::setOnLog
void setOnLog(const OnLogCallback &onLog)
Definition: logger.h:59
Fw::LogDebug
@ LogDebug
Definition: const.h:179
Logger::fatal
void fatal(const std::string &what)
Definition: logger.h:55
LogMessage::when
std::size_t when
Definition: logger.h:35
Fw::LogInfo
@ LogInfo
Definition: const.h:180
Logger::log
void log(Fw::LogLevel level, const std::string &message)
Definition: logger.cpp:37
Logger::logFunc
void logFunc(Fw::LogLevel level, const std::string &message, std::string prettyFunction)
Definition: logger.cpp:99
Logger::debug
void debug(const std::string &what)
Definition: logger.h:51
Logger
Definition: logger.h:39
Logger::fireOldMessages
void fireOldMessages()
Definition: logger.cpp:120
thread.h
Logger::setLogFile
void setLogFile(const std::string &file)
Definition: logger.cpp:132
LogMessage::LogMessage
LogMessage(Fw::LogLevel level, const std::string &message, std::size_t when)
Definition: logger.h:32
Fw::LogFatal
@ LogFatal
Definition: const.h:183
LogMessage::level
Fw::LogLevel level
Definition: logger.h:33
LogMessage
Definition: logger.h:31
Logger::info
void info(const std::string &what)
Definition: logger.h:52
Logger::warning
void warning(const std::string &what)
Definition: logger.h:53