Pyrogenesis trunk
CLogger.h
Go to the documentation of this file.
1/* Copyright (C) 2024 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_CLOGGER
19#define INCLUDED_CLOGGER
20
21#include <deque>
22#include <fmt/printf.h>
23#include <fstream>
24#include <mutex>
25#include <string>
26#include <sstream>
27
28class CCanvas2D;
29
30class CLogger;
31extern CLogger* g_Logger;
32
33
34#define LOGMESSAGE(...) g_Logger->WriteMessage(fmt::sprintf(__VA_ARGS__).c_str(), false)
35#define LOGMESSAGERENDER(...) g_Logger->WriteMessage(fmt::sprintf(__VA_ARGS__).c_str(), true)
36#define LOGWARNING(...) g_Logger->WriteWarning(fmt::sprintf(__VA_ARGS__).c_str())
37#define LOGERROR(...) g_Logger->WriteError (fmt::sprintf(__VA_ARGS__).c_str())
38
39/**
40 * Error/warning/message logging class.
41 *
42 * Thread-safety:
43 * - Expected to be constructed/destructed in the main thread.
44 * - The message logging functions may be called from any thread
45 * while the object is alive.
46 */
48{
50public:
52
54 {
58 };
59
60 CLogger(std::ostream& mainLog, std::ostream& interestingLog, const bool useDebugPrintf);
61
62 ~CLogger();
63
64 // Functions to write different message types (Errors and warnings are placed
65 // both in mainLog and intrestingLog.)
66 void WriteMessage(const char* message, bool doRender);
67 void WriteError (const char* message);
68 void WriteWarning(const char* message);
69
70 // Render recent log messages onto the screen
71 void Render(CCanvas2D& canvas);
72
73private:
74 void Init();
75
76 void PushRenderMessage(ELogMethod method, const char* message);
77
78 // Delete old timed-out entries from the list of text to render
79 void CleanupRenderQueue();
80
81 // the output streams
82 std::ostream& m_MainLog;
83 std::ostream& m_InterestingLog;
84
85 // whether errors should be reported via debug_printf (default)
86 // or suppressed (for tests that intentionally trigger errors)
88
89 // vars to hold message counts
93
94 // Used for Render()
96 {
98 double time;
99 std::string message;
100 };
101 std::deque<RenderedMessage> m_RenderMessages;
102
103 // The logger might be constructed too early to allow us to call timer_Time(),
104 // so we'll fill in the initial value later
106
107 // Lock for all state modified by logging commands
108 std::mutex m_Mutex;
109};
110
111/**
112 * Replaces `g_Logger` for as long as it's in scope.
113 */
115{
116public:
117 ScopedReplacement(std::ostream& mainLog, std::ostream& interestingLog, const bool useDebugPrintf);
118
124
125private:
128};
129
130/**
131 * This is used in the engine to log the messages to the logfiles.
132 */
134{
135public:
136 FileLogger();
137private:
138 std::ofstream m_MainLog;
139 std::ofstream m_InterestingLog;
141};
142
143/**
144 * Helper class for unit tests - captures all log output, and returns it as a
145 * single string.
146 */
148{
149public:
150 TestLogger();
151
152 std::string GetOutput();
153private:
154 std::stringstream m_Stream;
156};
157
158/**
159 * Helper class for unit tests - redirects all log output to stdout.
160 */
162{
163public:
165private:
167};
168
169#endif
CLogger * g_Logger
Definition: CLogger.cpp:55
Definition: Canvas2D.h:36
Replaces g_Logger for as long as it's in scope.
Definition: CLogger.h:115
~ScopedReplacement()
Definition: CLogger.cpp:283
ScopedReplacement(ScopedReplacement &&)=delete
ScopedReplacement & operator=(ScopedReplacement &&)=delete
ScopedReplacement(const ScopedReplacement &)=delete
ScopedReplacement & operator=(const ScopedReplacement &)=delete
CLogger * m_OldLogger
Definition: CLogger.h:127
ScopedReplacement(std::ostream &mainLog, std::ostream &interestingLog, const bool useDebugPrintf)
Definition: CLogger.cpp:277
CLogger m_ThisLogger
Definition: CLogger.h:126
Error/warning/message logging class.
Definition: CLogger.h:48
int m_NumberOfMessages
Definition: CLogger.h:90
bool m_UseDebugPrintf
Definition: CLogger.h:87
void WriteError(const char *message)
Definition: CLogger.cpp:130
void CleanupRenderQueue()
Definition: CLogger.cpp:249
ELogMethod
Definition: CLogger.h:54
@ Normal
Definition: CLogger.h:55
@ Error
Definition: CLogger.h:56
@ Warning
Definition: CLogger.h:57
std::ostream & m_MainLog
Definition: CLogger.h:82
std::ostream & m_InterestingLog
Definition: CLogger.h:83
void PushRenderMessage(ELogMethod method, const char *message)
Definition: CLogger.cpp:225
int m_NumberOfErrors
Definition: CLogger.h:91
NONCOPYABLE(CLogger)
void Init()
std::mutex m_Mutex
Definition: CLogger.h:108
std::deque< RenderedMessage > m_RenderMessages
Definition: CLogger.h:101
void WriteMessage(const char *message, bool doRender)
Definition: CLogger.cpp:109
~CLogger()
Definition: CLogger.cpp:80
void WriteWarning(const char *message)
Definition: CLogger.cpp:150
CLogger(std::ostream &mainLog, std::ostream &interestingLog, const bool useDebugPrintf)
Definition: CLogger.cpp:71
void Render(CCanvas2D &canvas)
Definition: CLogger.cpp:170
double m_RenderLastEraseTime
Definition: CLogger.h:105
int m_NumberOfWarnings
Definition: CLogger.h:92
This is used in the engine to log the messages to the logfiles.
Definition: CLogger.h:134
std::ofstream m_MainLog
Definition: CLogger.h:138
std::ofstream m_InterestingLog
Definition: CLogger.h:139
FileLogger()
Definition: CLogger.cpp:298
CLogger::ScopedReplacement m_ScopedReplacement
Definition: CLogger.h:140
Helper class for unit tests - captures all log output, and returns it as a single string.
Definition: CLogger.h:148
TestLogger()
Definition: CLogger.cpp:304
std::stringstream m_Stream
Definition: CLogger.h:154
CLogger::ScopedReplacement m_ScopedReplacement
Definition: CLogger.h:155
std::string GetOutput()
Definition: CLogger.cpp:308
Helper class for unit tests - redirects all log output to stdout.
Definition: CLogger.h:162
TestStdoutLogger()
Definition: CLogger.cpp:313
CLogger::ScopedReplacement m_ScopedReplacement
Definition: CLogger.h:166
Definition: CLogger.h:96
std::string message
Definition: CLogger.h:99
ELogMethod method
Definition: CLogger.h:97
double time
Definition: CLogger.h:98