C++日志记录类实例解析

所属分类: 软件编程 / C 语言 阅读数: 63
收藏 0 赞 0 分享

本文所述实例是从一个Red Hat开源项目里面扒出来的,非常实用!读者还可以根据自身需求加以修改!完整源码如下:

Log.h文件部分:

#ifndef __LOG_H__
#define __LOG_H__

#include <stdio.h>
#include <tchar.h>
#include <crtdbg.h>
#include <windows.h>
#include <time.h>
#include <sys/timeb.h>

class CLog {
public:
  ~CLog();
  static CLog* get(TCHAR* path = NULL);
  void printf(const char* format, ...);

private:
  CLog(FILE* handle);

private:
  static CLog* _log;
  FILE* _handle;
};

enum {
 LOG_DEBUG,
 LOG_INFO,
 LOG_WARN,
 LOG_ERROR,
 LOG_FATAL
};

#ifdef _DEBUG
static unsigned int log_level = LOG_DEBUG;
#else
static unsigned int log_level = LOG_INFO;
#endif

#define PRINT_LINE(type, format, datetime, ms, ...)           \
  printf("%lu::%s::%s,%.3d::%s::" format "\n", GetCurrentThreadId(), type, datetime, ms, \
      __FUNCTION__, ## __VA_ARGS__);

#define LOG(type, format, ...) do {                   \
  if (type >= log_level && type <= LOG_FATAL) {            \
    CLog* log = CLog::get();                   \
    const char *type_as_char[] = { "DEBUG", "INFO", "WARN", "ERROR", "FATAL" }; \
    struct _timeb now;                       \
    struct tm today;                        \
    char datetime_str[20];                     \
    _ftime_s(&now);                         \
    localtime_s(&today, &now.time);                 \
    strftime(datetime_str, 20, "%Y-%m-%d %H:%M:%S", &today);    \
    if (log) {                           \
      log->PRINT_LINE(type_as_char[type], format, datetime_str, now.millitm, ## __VA_ARGS__); \
    } else {                            \
      PRINT_LINE(type_as_char[type], format, datetime_str, now.millitm, ## __VA_ARGS__); \
    }                                \
  }                                  \
} while(0)


#define log_printf(format, ...) LOG(LOG_INFO, format, ## __VA_ARGS__)
#define LOG_INFO(format, ...) LOG(LOG_INFO, format, ## __VA_ARGS__)
#define LOG_WARN(format, ...) LOG(LOG_WARN, format, ## __VA_ARGS__)
#define LOG_ERROR(format, ...) LOG(LOG_ERROR, format, ## __VA_ARGS__)

#define DBGLEVEL 1000

#define DBG(level, format, ...) do {      \
  if (level <= DBGLEVEL) {          \
    LOG(LOG_DEBUG, format, ## __VA_ARGS__); \
  }                      \
} while(0)

#define ASSERT(x) _ASSERTE(x)
#endif

Log.cpp文件部分:

#include "Log.h"
#include <stdio.h>
#include <stdarg.h>
#include <share.h>

#define LOG_ROLL_SIZE (1024 * 1024)

CLog* CLog::_log = NULL;

CLog::CLog(FILE* handle)
  : _handle(handle)
{
  _log = this;
}

CLog::~CLog()
{
  if (_log && _handle) {
    fclose(_handle);
    _log = NULL;
  }
}

CLog* CLog::get(char* path)
{
  if (_log) {
    return _log;
  }
 if(!path)
 {
 path = "dll.log";
 }
  DWORD size = 0;
  HANDLE file = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
               NULL);
  if (file != INVALID_HANDLE_VALUE) {
    size = GetFileSize(file, NULL);
    CloseHandle(file);
  }
  if (size != INVALID_FILE_SIZE && size > LOG_ROLL_SIZE) {
    TCHAR roll_path[MAX_PATH];
    sprintf(roll_path, "%s.1", path);
    if (!MoveFileEx(path, roll_path, MOVEFILE_REPLACE_EXISTING)) {
      return NULL;
    }
  }
  FILE* handle = fopen(path, "a+");
  if (!handle) {
    return NULL;
  }
  _log = new CLog(handle);
  return _log;
}

void CLog::printf(const char* format, ...)
{
  va_list args;

  va_start(args, format);
  vfprintf(_handle, format, args);
  va_end(args);
  fflush(_handle);
}
更多精彩内容其他人还在看

C++广播通信实例

这篇文章主要介绍了C++实现广播通信的方法,实例讲述了C++ socket广播通信的原理与实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

C++计算ICMP头的校验和实例

这篇文章主要介绍了C++计算ICMP头的校验和的方法,代码简单实用,对于校验ICMP报文来说有不错的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++设置超时时间的简单实现方法

这篇文章主要介绍了C++设置超时时间的简单实现方法,涉及系统函数setsockopt对套接口的操作,具有一定的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++实现ping程序实例

这篇文章主要介绍了C++实现ping程序实例,涉及C++对于ICMP数据包的发送与回显处理,具有一定的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++之boost::array的用法

这篇文章主要介绍了C++之boost::array的用法,以实例的形式简单讲述了静态数组的容器boost::array的使用技巧,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++之Boost::array用法简介

这篇文章主要介绍了C++之Boost::array用法简介,较为详细的分析了Boost::array中的常见用法,并用实例的形式予以总结归纳,需要的朋友可以参考下
收藏 0 赞 0 分享

VC文件目录常见操作实例汇总

这篇文章主要介绍了VC文件目录常见操作实例汇总,总结了VC针对文件目录的各种常用操作,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

VC打印word,excel文本文件的方法

这篇文章主要介绍了VC打印word,excel文本文件的方法,是VC操作文本文件中非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

VC++获得当前进程运行目录的方法

这篇文章主要介绍了VC++获得当前进程运行目录的方法,可通过系统函数实现该功能,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

VC中SendMessage和PostMessage的区别

这篇文章主要介绍了VC中SendMessage和PostMessage的区别,较为全面的分析了SendMessage和PostMessage运行原理及用法上的不同之处,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多