logger.h

main 110 lines · 3.4 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1/*
2 * Copyright (c) 2010-2017 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
28#include <framework/stdext/thread.h>
29#include <fstream>
30
31struct LogMessage {
32 LogMessage(Fw::LogLevel level, const std::string& message, std::size_t when) : level(level), message(message), when(when) { }
33 Fw::LogLevel level;
34 std::string message;
35 std::size_t when;
36};
37
38// @bindsingleton g_logger
39class Logger
40{
41 enum {
42 MAX_LOG_HISTORY = 1000
43 };
44
45 typedef std::function<void(Fw::LogLevel, const std::string&, int64)> OnLogCallback;
46
47public:
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 std::string getLastLog() {
62 return m_lastLog;
63 }
64
65 void setTestingMode()
66 {
67 m_testingMode = true;
68 }
69
70private:
71 std::list<LogMessage> m_logMessages;
72 OnLogCallback m_onLog;
73 std::fstream m_outFile;
74 std::recursive_mutex m_mutex;
75 std::string m_lastLog;
76 bool m_testingMode = false;
77};
78
79extern Logger g_logger;
80
81#define trace() logFunc(Fw::LogDebug, "", __PRETTY_FUNCTION__)
82#define traceDebug(a) logFunc(Fw::LogDebug, a, __PRETTY_FUNCTION__)
83#define traceInfo(a) logFunc(Fw::LogInfo, a, __PRETTY_FUNCTION__)
84#define traceWarning(a) logFunc(Fw::LogWarning, a, __PRETTY_FUNCTION__)
85#define traceError(a) logFunc(Fw::LogError, a, __PRETTY_FUNCTION__)
86
87#define logTraceCounter() { \
88 static int __count = 0; \
89 static Timer __timer; \
90 __count++; \
91 if(__timer.ticksElapsed() >= 1000) { \
92 logTraceDebug(__count); \
93 __count = 0; \
94 __timer.restart(); \
95 } \
96}
97
98#define logTraceFrameCounter() { \
99 static int __count = 0; \
100 static Timer __timer; \
101 __count++; \
102 if(__timer.ticksElapsed() > 0) { \
103 logTraceDebug(__count); \
104 __count = 0; \
105 __timer.restart(); \
106 } \
107}
108
109#endif
110
Top