Line data Source code
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 : 31 : class CCanvas2D; 32 : 33 : class CLogger; 34 : extern 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 : */ 54 : class CLogger 55 : { 56 : NONCOPYABLE(CLogger); 57 : public: 58 : class ScopedReplacement; 59 : 60 : enum ELogMethod 61 : { 62 : Normal, 63 : Error, 64 : Warning 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 : 80 : private: 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) 94 : bool m_UseDebugPrintf; 95 : 96 : // vars to hold message counts 97 : int m_NumberOfMessages{0}; 98 : int m_NumberOfErrors{0}; 99 : int m_NumberOfWarnings{0}; 100 : 101 : // Used for Render() 102 2408 : struct RenderedMessage 103 : { 104 : ELogMethod method; 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 112 : double m_RenderLastEraseTime{-1.0}; 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 : */ 121 : class CLogger::ScopedReplacement 122 : { 123 : public: 124 : ScopedReplacement(std::ostream& mainLog, std::ostream& interestingLog, const bool useDebugPrintf); 125 : 126 : ScopedReplacement(const ScopedReplacement&) = delete; 127 : ScopedReplacement& operator=(const ScopedReplacement&) = delete; 128 : ScopedReplacement(ScopedReplacement&&) = delete; 129 : ScopedReplacement& operator=(ScopedReplacement&&) = delete; 130 : ~ScopedReplacement(); 131 : 132 : private: 133 : CLogger m_ThisLogger; 134 : CLogger* m_OldLogger; 135 : }; 136 : 137 : /** 138 : * This is used in the engine to log the messages to the logfiles. 139 : */ 140 : class FileLogger 141 : { 142 : public: 143 : FileLogger(); 144 : private: 145 : std::ofstream m_MainLog; 146 : std::ofstream m_InterestingLog; 147 : CLogger::ScopedReplacement m_ScopedReplacement; 148 : }; 149 : 150 : /** 151 : * Helper class for unit tests - captures all log output, and returns it as a 152 : * single string. 153 : */ 154 161 : class TestLogger 155 : { 156 : public: 157 : TestLogger(); 158 : 159 : std::string GetOutput(); 160 : private: 161 : std::stringstream m_Stream; 162 : CLogger::ScopedReplacement m_ScopedReplacement; 163 : }; 164 : 165 : /** 166 : * Helper class for unit tests - redirects all log output to stdout. 167 : */ 168 : class TestStdoutLogger 169 : { 170 : public: 171 : TestStdoutLogger(); 172 : private: 173 : CLogger::ScopedReplacement m_ScopedReplacement; 174 : }; 175 : 176 : #endif