Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CLogger.h
Go to the documentation of this file.
1 /* Copyright (C) 2010 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 <fstream>
22 #include <string>
23 #include <set>
24 #include <sstream>
25 
26 #include "ps/ThreadUtil.h"
27 
28 class CLogger;
29 extern CLogger* g_Logger;
30 
31 
32 #define LOGMESSAGE g_Logger->LogMessage
33 #define LOGMESSAGERENDER g_Logger->LogMessageRender
34 #define LOGWARNING g_Logger->LogWarning
35 #define LOGERROR g_Logger->LogError
36 
37 /**
38  * Error/warning/message logging class.
39  *
40  * Thread-safety:
41  * - Expected to be constructed/destructed in the main thread.
42  * - The message logging functions may be called from any thread
43  * while the object is alive.
44  */
45 class CLogger
46 {
48 public:
50  {
54  };
55 
56  // Default constructor - outputs to normal log files
57  CLogger();
58 
59  // Special constructor (mostly for testing) - outputs to provided streams.
60  // Can take ownership of streams and delete them in the destructor.
61  CLogger(std::ostream* mainLog, std::ostream* interestingLog, bool takeOwnership, bool useDebugPrintf);
62 
63  ~CLogger();
64 
65  // Functions to write different message types (Errors and warnings are placed
66  // both in mainLog and intrestingLog.)
67  void WriteMessage(const wchar_t* message, bool doRender);
68  void WriteError (const wchar_t* message);
69  void WriteWarning(const wchar_t* message);
70 
71  // Functions to write a message, warning or error to file.
72  void LogMessage(const wchar_t* fmt, ...) WPRINTF_ARGS(2);
73  void LogMessageRender(const wchar_t* fmt, ...) WPRINTF_ARGS(2);
74  void LogWarning(const wchar_t* fmt, ...) WPRINTF_ARGS(2);
75  void LogError(const wchar_t* fmt, ...) WPRINTF_ARGS(2);
76 
77  // Render recent log messages onto the screen
78  void Render();
79 
80 private:
81  void Init();
82 
83  void PushRenderMessage(ELogMethod method, const wchar_t* 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;
92 
93  // whether errors should be reported via debug_printf (default)
94  // or suppressed (for tests that intentionally trigger errors)
96 
97  // vars to hold message counts
101 
102  // Used for Render()
104  {
106  double time;
107  std::wstring message;
108  };
109  std::deque<RenderedMessage> m_RenderMessages;
111 
112  // Lock for all state modified by logging commands
114 };
115 
116 /**
117  * Helper class for unit tests - captures all log output while it is in scope,
118  * and returns it as a single string.
119  */
121 {
123 public:
124  TestLogger();
125  ~TestLogger();
126  std::wstring GetOutput();
127 private:
129  std::stringstream m_Stream;
130 };
131 
132 /**
133  * Helper class for unit tests - redirects all log output to stdout.
134  */
136 {
138 public:
141 private:
143 };
144 
145 #endif
Error/warning/message logging class.
Definition: CLogger.h:45
CMutex m_Mutex
Definition: CLogger.h:113
std::wstring message
Definition: CLogger.h:107
void PushRenderMessage(ELogMethod method, const wchar_t *message)
Definition: CLogger.cpp:337
void LogMessage(const wchar_t *fmt,...) WPRINTF_ARGS(2)
Definition: CLogger.cpp:213
void CleanupRenderQueue()
Definition: CLogger.cpp:361
NONCOPYABLE(CLogger)
void Init()
Definition: CLogger.cpp:99
ELogMethod
Definition: CLogger.h:49
static LogFn g_Logger
Definition: DLL.cpp:33
NONCOPYABLE(TestStdoutLogger)
void LogError(const wchar_t *fmt,...) WPRINTF_ARGS(2)
Definition: CLogger.cpp:261
std::deque< RenderedMessage > m_RenderMessages
Definition: CLogger.h:109
A non-recursive mutual exclusion lock.
Definition: ThreadUtil.h:45
void Render()
Definition: CLogger.cpp:277
bool m_UseDebugPrintf
Definition: CLogger.h:95
Helper class for unit tests - captures all log output while it is in scope, and returns it as a singl...
Definition: CLogger.h:120
double m_RenderLastEraseTime
Definition: CLogger.h:110
std::stringstream m_Stream
Definition: CLogger.h:129
void WriteError(const wchar_t *message)
Definition: CLogger.cpp:173
NONCOPYABLE(TestLogger)
std::ostream * m_InterestingLog
Definition: CLogger.h:90
void WriteWarning(const wchar_t *message)
Definition: CLogger.cpp:193
int m_NumberOfMessages
Definition: CLogger.h:98
int m_NumberOfErrors
Definition: CLogger.h:99
#define WPRINTF_ARGS(fmtpos)
~CLogger()
Definition: CLogger.cpp:116
CLogger * m_OldLogger
Definition: CLogger.h:128
void LogMessageRender(const wchar_t *fmt,...) WPRINTF_ARGS(2)
Definition: CLogger.cpp:229
bool m_OwnsStreams
Definition: CLogger.h:91
CLogger * m_OldLogger
Definition: CLogger.h:142
std::ostream * m_MainLog
Definition: CLogger.h:89
void WriteMessage(const wchar_t *message, bool doRender)
Definition: CLogger.cpp:152
std::wstring GetOutput()
Definition: CLogger.cpp:401
int m_NumberOfWarnings
Definition: CLogger.h:100
void LogWarning(const wchar_t *fmt,...) WPRINTF_ARGS(2)
Definition: CLogger.cpp:245
CLogger()
Definition: CLogger.cpp:75
Helper class for unit tests - redirects all log output to stdout.
Definition: CLogger.h:135