思考的轨迹

人若无名 专心练剑

A Simple Log Tool

| Comments

对于程序员来说,debug是再正常不过的事。

在遇到问题时,通过分析程序的产生的Log文件来查找和验证问题发生的原因。

下面是一个简单的log tool,可以满足一般的log需求。

代码已经放到Github上了,需要的可到这里下载。

该log tool实现了log分级,也支持自己指定log file的存放位置,缺省情况下,log文件为C:\DebugInfo.log

因为log没有加锁,所以在多线程下产生的log会有错乱的现象,因此,在多线程中,可以在Log()函数的开始处加锁来解决。


log.h

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
#ifndef _LOG_H
#define _LOG_H

#include <string>

#define TEST
#define LOG_FILE_PATH "C:\\DebugInfo.log"

#ifdef TEST
  #define LogData Logger::Instance().Log
  #define SetLogFile Logger::SetLogFilePath
#else
  #define LogData
  #define SetLogFile
#endif

typedef enum tagLogLevel
{
  LOG_TRACE,
  LOG_INFO,
  LOG_WARNING,
  LOG_ERROR,
  LOG_FATAL,
  LOG_NONE = 10,
}LogLevel;

class Logger
{
public:
  static Logger& Instance();

  static void SetLogFilePath(const std::string& strFilePath, bool bAppend = false);
  static void SetLogLevel(const LogLevel Level);
  static void Initialise(bool bAppend = false);
  static void Dispose();

  void Log(const LogLevel Level, const char *Format, ...);

private:
  Logger();
  Logger(Logger const&);
  Logger& operator=(Logger const&);
  ~Logger();

  static FILE*        m_hLogFile;
  static std::string  m_strFilePath;
  static LogLevel     m_LogLevel;
};

#endif//_LOG_H

log.cpp

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);
  }
}

log的使用示例

1
2
3
4
5
6
7
8
9
10
11
#include "Log.h"

LogData(LOG_TRACE, "log information\n");

int i = 100;
LogData(LOG_TRACE, "i = %d\n", i);

int j = 10;
LogData(LOG_TRACE, "i = %d, j = %d\n", i, j);

SetLogFile("c:\\log.txt"); //set log file path

Comments