My Project
Log.h
1 #pragma once
2 #include "util/mutex.h"
3 #include <string>
4 
5 #define MAX_DEBUG_STRING_LENGTH 1024
6 
7 namespace ParaEngine
8 {
9  class CLogger;
10 
11  namespace LogDetail
12  {
17  class PE_CORE_DECL LocationInfo
18  {
19  public:
20 
25  static const char * const NA;
26  static const char * const NA_METHOD;
27 
28  static const LocationInfo& getLocationUnavailable();
29 
30 
36  LocationInfo( const char * const fileName,
37  const char * const functionName,
38  int lineNumber);
39 
43  LocationInfo();
44 
49  LocationInfo( const LocationInfo & src );
50 
55  LocationInfo & operator = ( const LocationInfo & src );
56 
60  void clear();
61 
62 
64  const std::string getClassName();
65 
70  const char * getFileName();
71 
76  int getLineNumber();
77 
79  const std::string getMethodName();
80 
82  void write(CLogger* pOutput) const;
83 
84  private:
86  int lineNumber;
87 
89  const char * fileName;
90 
92  const char * methodName;
93  };
94  }
95 
96 #ifdef WIN32
97 #pragma warning( push )
98 // warning C4251: 'ParaEngine::CLogger::m_log_file_name' : class 'std::basic_string<_Elem,_Traits,_Ax>' needs to have dll-interface to be used by clients of class 'ParaEngine::CLogger'
99 #pragma warning( disable : 4251 )
100 #endif
101 
107  class PE_CORE_DECL CLogger
108  {
109  public:
110  CLogger();
111  ~CLogger();
112 
114  static CLogger& GetSingleton();
115 
119  void AddLogStr(const char * pStr);
120  void AddLogStr(const wchar_t * pStr);
121 
123  void AddLogStr_st(const char * pStr);
124  void AddLogStr_st(const wchar_t * pStr);
125 
127  void SetLogFile(const std::string& sFile);
128  const std::string& GetLogFile();
129 
133  void WriteFormated(const char *, ...);
134  void WriteFormatedVarList(const char *, va_list args);
135  void WriteFormated(const wchar_t *, ...);
136  void WriteFormated_WithTime(const char *, ...);
137 
139  void WriteFormated_st(const char *, ...);
140  void WriteFormated_st(const wchar_t *, ...);
141 
148  int Write(const char * buf, int nLength);
149 
151  int Write_st(const char * buf, int nLength);
152 
156  int GetPos();
157 
163  const char* GetLog(int fromPos, int nCount);
164 
166  void CloseLog();
167 
169  FILE* GetLogFileHandle();
170 
175  void WriteDebugStr(const char * zFormat,const char * str, const char* sFile,int nLine);
176 
177  public:
181  const std::string& GetName();
182 
183 
185  void SetAppendMode(bool bAppendToExistingFile);
186 
190  void SetForceFlush(bool bForceFlush);
191 
196  int GetLevel();
197 
199  void SetLevel(const int level1);
200 
205  bool IsEnabledFor(int level);
206 
214  void log(const int level, const std::string& message,
215  const ParaEngine::LogDetail::LocationInfo& location);
216  void log(const int level, const char* message,
217  const ParaEngine::LogDetail::LocationInfo& location);
225  void log(const int level, const std::string& message);
226  void log(const int level, const char* message);
227 
235  void ForcedLog(const int level, const std::string& message,
236  const ParaEngine::LogDetail::LocationInfo& location);
237  void ForcedLog(const int level, const char* message,
238  const ParaEngine::LogDetail::LocationInfo& location);
245  void ForcedLog(const int level, const std::string& message);
246  void ForcedLog(const int level, const char* message);
247 
248  protected:
249  // file name
250  std::string m_log_file_name;
251  // file handle
252  FILE* m_file_handle;
253  // debug string buffer.
254  char m_buffer[MAX_DEBUG_STRING_LENGTH*2+2];
255  // for multi-threaded logging.
256  mutex m_mutex;
257  // whether this is first time this logger is used. we will create new file if it is true, otherwise we will append to existing file.
258  bool m_is_first_time_open;
263 
265  std::string m_name;
266 
269  int m_level;
270 
271  };
272 #ifdef WIN32
273 #pragma warning( pop )
274 #endif
275 
276 }
277 
278 #ifndef OUTPUT_LOG
279 #define OUTPUT_LOG ParaEngine::CLogger::GetSingleton().WriteFormated
280 #endif
281 
283 #define OUTPUT_LOG1 ParaEngine::CLogger::GetSingleton().WriteFormated_WithTime
284 
285 #ifdef _DEBUG
286 #define OUTPUT_DEBUG(a) ParaEngine::CLogger::GetSingleton().WriteDebugStr("%s called from %s:%d\n",a, __FILE__,__LINE__);
287 #else
288 #define OUTPUT_DEBUG(a)
289 #endif
290 
291 
292 #if !defined(SERVICE_LOG_LOCATION)
293 #if defined(_MSC_VER)
294 #if _MSC_VER >= 1300
295 #define __SERVICE_LOG_FUNC__ __FUNCSIG__
296 #endif
297 #else
298 #if defined(__GNUC__)
299 #define __SERVICE_LOG_FUNC__ __PRETTY_FUNCTION__
300 #endif
301 #endif
302 #if !defined(__SERVICE_LOG_FUNC__)
303 #define __SERVICE_LOG_FUNC__ ""
304 #endif
305 #define SERVICE_LOG_LOCATION ParaEngine::LogDetail::LocationInfo(__FILE__, \
306  __SERVICE_LOG_FUNC__, \
307  __LINE__)
308 #endif
309 
310 
315 #define APP_LOG(message) ParaEngine::CLogger::GetSingleton().ForcedLog(0, message, SERVICE_LOG_LOCATION);
different physics engine has different winding order.
Definition: EventBinding.h:32
int m_level
The assigned level of this logger.
Definition: Log.h:269
bool m_bForceFlush
if true we will flush the new log to file immediately.
Definition: Log.h:262
static const char *const NA
When location information is not available the constant NA is returned.
Definition: Log.h:25
This class represents the location of a logging statement.
Definition: Log.h:17
cross platform mutex
Definition: mutex.h:95
std::string m_name
The name of this logger.
Definition: Log.h:265
a logger can only write to a given file.
Definition: Log.h:107