Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
CLogger.h
Go to the documentation of this file.
1/* Copyright (C) 2026 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 "lib/code_annotation.h"
22
23#include <deque>
24#include <fmt/format.h>
25#include <fmt/printf.h>
26#include <fstream>
27#include <mutex>
28#include <string>
29#include <sstream>
30
31class CCanvas2D;
32
33class CLogger;
34extern CLogger* g_Logger;
35
36
37#define LOGMESSAGE(fmtstring, ...) g_Logger->WriteMessage( \
38 fmt::format(FMT_STRING(fmtstring), ## __VA_ARGS__).c_str(), false)
39#define LOGMESSAGERENDER(fmtstring, ...) g_Logger->WriteMessage( \
40 fmt::format(FMT_STRING(fmtstring), ## __VA_ARGS__).c_str(), true)
41#define LOGWARNING(fmtstring, ...) g_Logger->WriteWarning( \
42 fmt::format(FMT_STRING(fmtstring), ## __VA_ARGS__).c_str())
43#define LOGERROR(fmtstring, ...) g_Logger->WriteError( \
44 fmt::format(FMT_STRING(fmtstring), ## __VA_ARGS__).c_str())
45
46/**
47 * Error/warning/message logging class.
48 *
49 * Thread-safety:
50 * - Expected to be constructed/destructed in the main thread.
51 * - The message logging functions may be called from any thread
52 * while the object is alive.
53 */
55{
57public:
59
61 {
65 };
66
67 CLogger(std::ostream& mainLog, std::ostream& interestingLog, const bool useDebugPrintf);
68
69 ~CLogger();
70
71 // Functions to write different message types (Errors and warnings are placed
72 // both in mainLog and intrestingLog.)
73 void WriteMessage(const char* message, bool doRender);
74 void WriteError (const char* message);
75 void WriteWarning(const char* message);
76
77 // Render recent log messages onto the screen
78 void Render(CCanvas2D& canvas);
79
80private:
81 void Init();
82
83 void PushRenderMessage(ELogMethod method, const char* message);
84
85 // Delete old timed-out entries from the list of text to render
86 void CleanupRenderQueue();
87
88 // the output streams
89 std::ostream& m_MainLog;
90 std::ostream& m_InterestingLog;
91
92 // whether errors should be reported via debug_printf (default)
93 // or suppressed (for tests that intentionally trigger errors)
95
96 // vars to hold message counts
100
101 // Used for Render()
103 {
105 double time;
106 std::string message;
107 };
108 std::deque<RenderedMessage> m_RenderMessages;
109
110 // The logger might be constructed too early to allow us to call timer_Time(),
111 // so we'll fill in the initial value later
113
114 // Lock for all state modified by logging commands
115 std::mutex m_Mutex;
116};
117
118/**
119 * Replaces `g_Logger` for as long as it's in scope.
120 */
122{
123public:
124 ScopedReplacement(std::ostream& mainLog, std::ostream& interestingLog, const bool useDebugPrintf);
125
131
132private:
135};
136
137/**
138 * This is used in the engine to log the messages to the logfiles.
139 */
141{
142public:
143 FileLogger();
144private:
145 std::ofstream m_MainLog;
146 std::ofstream m_InterestingLog;
148};
149
150/**
151 * Helper class for unit tests - captures all log output, and returns it as a
152 * single string.
153 */
155{
156public:
157 TestLogger();
158
159 std::string GetOutput();
160private:
161 std::stringstream m_Stream;
163};
164
165/**
166 * Helper class for unit tests - redirects all log output to stdout.
167 */
169{
170public:
172private:
174};
175
176#endif
CLogger * g_Logger
Definition: CLogger.cpp:59
Definition: Canvas2D.h:36
Replaces g_Logger for as long as it's in scope.
Definition: CLogger.h:122
~ScopedReplacement()
Definition: CLogger.cpp:287
ScopedReplacement(ScopedReplacement &&)=delete
ScopedReplacement & operator=(ScopedReplacement &&)=delete
ScopedReplacement(const ScopedReplacement &)=delete
ScopedReplacement & operator=(const ScopedReplacement &)=delete
CLogger * m_OldLogger
Definition: CLogger.h:134
ScopedReplacement(std::ostream &mainLog, std::ostream &interestingLog, const bool useDebugPrintf)
Definition: CLogger.cpp:281
CLogger m_ThisLogger
Definition: CLogger.h:133
Error/warning/message logging class.
Definition: CLogger.h:55
int m_NumberOfMessages
Definition: CLogger.h:97
bool m_UseDebugPrintf
Definition: CLogger.h:94
void WriteError(const char *message)
Definition: CLogger.cpp:134
void CleanupRenderQueue()
Definition: CLogger.cpp:253
ELogMethod
Definition: CLogger.h:61
@ Normal
Definition: CLogger.h:62
@ Error
Definition: CLogger.h:63
@ Warning
Definition: CLogger.h:64
std::ostream & m_MainLog
Definition: CLogger.h:89
std::ostream & m_InterestingLog
Definition: CLogger.h:90
void PushRenderMessage(ELogMethod method, const char *message)
Definition: CLogger.cpp:229
int m_NumberOfErrors
Definition: CLogger.h:98
NONCOPYABLE(CLogger)
void Init()
std::mutex m_Mutex
Definition: CLogger.h:115
std::deque< RenderedMessage > m_RenderMessages
Definition: CLogger.h:108
void WriteMessage(const char *message, bool doRender)
Definition: CLogger.cpp:113
~CLogger()
Definition: CLogger.cpp:84
void WriteWarning(const char *message)
Definition: CLogger.cpp:154
CLogger(std::ostream &mainLog, std::ostream &interestingLog, const bool useDebugPrintf)
Definition: CLogger.cpp:75
void Render(CCanvas2D &canvas)
Definition: CLogger.cpp:174
double m_RenderLastEraseTime
Definition: CLogger.h:112
int m_NumberOfWarnings
Definition: CLogger.h:99
This is used in the engine to log the messages to the logfiles.
Definition: CLogger.h:141
std::ofstream m_MainLog
Definition: CLogger.h:145
std::ofstream m_InterestingLog
Definition: CLogger.h:146
FileLogger()
Definition: CLogger.cpp:302
CLogger::ScopedReplacement m_ScopedReplacement
Definition: CLogger.h:147
Helper class for unit tests - captures all log output, and returns it as a single string.
Definition: CLogger.h:155
TestLogger()
Definition: CLogger.cpp:308
std::stringstream m_Stream
Definition: CLogger.h:161
CLogger::ScopedReplacement m_ScopedReplacement
Definition: CLogger.h:162
std::string GetOutput()
Definition: CLogger.cpp:312
Helper class for unit tests - redirects all log output to stdout.
Definition: CLogger.h:169
TestStdoutLogger()
Definition: CLogger.cpp:317
CLogger::ScopedReplacement m_ScopedReplacement
Definition: CLogger.h:173
Definition: CLogger.h:103
std::string message
Definition: CLogger.h:106
ELogMethod method
Definition: CLogger.h:104
double time
Definition: CLogger.h:105