1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  | #include "log.h"
#include <stdarg.h>
#include <windows.h>
FILE* Logger::m_hLogFile = NULL;
LogLevel Logger::m_LogLevel = LOG_TRACE;
std::string Logger::m_strFilePath = LOG_FILE_PATH;
const char* LogLevelStr[] = {
    "TRACE",
    "INFO",
    "WARNING",
    "ERROR",
    "CRITICAL",
    "FATAL",
};
Logger& Logger::Instance() 
{
  static Logger LoggerInstance;
  return LoggerInstance;
}
void Logger::SetLogFilePath(const std::string& strFilePath, bool bAppend)
{
  m_strFilePath = strFilePath;
  Dispose();
  Initialise(bAppend);
}
void Logger::SetLogLevel(const LogLevel Level)
{
  m_LogLevel = Level;
}
Logger::Logger()
{
  Initialise(false);
}
Logger::~Logger()
{
  Dispose();
}
void Logger::Initialise(bool bAppend)
{
  if (m_strFilePath.length() > 0) {
      m_hLogFile = bAppend ? fopen(m_strFilePath.c_str(), "a+") 
                       : fopen(m_strFilePath.c_str(), "w+");  
  }
}
void Logger::Dispose()
{
  if (NULL != m_hLogFile) {
      fflush(m_hLogFile);
      fclose(m_hLogFile);
      m_hLogFile = NULL;
  }
}
void Logger::Log(const LogLevel Level, const char *Format, ...)
{
  if (m_LogLevel > Level) return;
  if (NULL == m_hLogFile) return; 
  char szBuffer[1024];
  va_list args;
  va_start(args, Format);
  vsprintf(szBuffer, Format, args);
  va_end(args);
  SYSTEMTIME st;      
  GetLocalTime(&st);
  if (0 > fprintf(m_hLogFile, "[%02u:%02u:%02u:%03u]\t[%s]\t%s\n", 
      st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, 
      LogLevelStr[Level], szBuffer)) {
      Dispose();
  }
  else {
      fflush(m_hLogFile);
  }
}
  |