zoukankan      html  css  js  c++  java
  • 使用变长参数实现简单的日志类

    做项目时候经常需要打印日志来输出一些重要的信息,自己实现了一个简单的日志类,记录下。

    Log.h

     1 #pragma once
     2 
     3 class CVxILog
     4 {
     5 private:
     6     CVxILog();
     7     ~CVxILog();
     8 public:
     9     static    CVxILog*        Instance()
    10     {
    11         static CVxILog    log;
    12         return &log;
    13     }
    14 
    15     void    AddLog(const char* msg);
    16 
    17     void    AddLogEx(const char* format, ...);
    18 private:
    19     static const    char* m_pFileName;
    20 
    21     static CRITICAL_SECTION    s_cs;
    22 };
    23 
    24 #define WRITE_LOG(msg)                    CVxILog::Instance()->AddLog(msg)
    25 #define WRITE_LOG_EX(format,...)    CVxILog::Instance()->AddLogEx(format,__VA_ARGS__)

    Log.cpp

     1 #include "stdafx.h"
     2 #include "Log.h"
     3 #include <fstream>
     4 
     5 CRITICAL_SECTION    CVxILog::s_cs;
     6 
     7 const char* CVxILog::m_pFileName = _T("Test.log");
     8 
     9 #define MAX_LOG_LENGTH        1024        //最大Log长度
    10 
    11 static void    WriteHeadStr(std::ofstream& out)
    12 {
    13     SYSTEMTIME st = {0};
    14     GetLocalTime(&st);
    15     char timeStr[MAX_LOG_LENGTH] = {0};
    16     sprintf_s(timeStr,"[%d-%02d-%02d %02d:%02d:%02d] ",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
    17     out << timeStr;
    18 }
    19 
    20 CVxILog::CVxILog()
    21 {
    22     ::InitializeCriticalSection(&s_cs);
    23 }
    24 CVxILog::~CVxILog()
    25 {
    26     ::DeleteCriticalSection(&s_cs);
    27 }
    28 
    29 void    CVxILog::AddLog(const char* msg)
    30 {
    31     ::EnterCriticalSection(&s_cs);
    32     std::ofstream    out;
    33     out.open(m_pFileName,std::ios_base::app|std::ios_base::out);
    34     if(out.is_open())
    35     {
    36         WriteHeadStr(out);
    37         out << msg << std::endl;
    38         out.close();
    39     }
    40     ::LeaveCriticalSection(&s_cs);
    41 }
    42 
    43 void    CVxILog::AddLogEx(const char* format, ...)
    44 {
    45     ::EnterCriticalSection(&s_cs);
    46     std::ofstream    out;
    47     out.open(m_pFileName,std::ios_base::app|std::ios_base::out);
    48     if(out.is_open())
    49     {
    50         WriteHeadStr(out);
    51 
    52         va_list ap;
    53         va_start(ap,format);
    54         char msg[MAX_LOG_LENGTH];
    55         int size = vsnprintf_s(msg,MAX_LOG_LENGTH-1,format,ap);
    56         if(size > 0)
    57         {
    58             out << msg << std::endl;
    59         }
    60         va_end(ap);
    61         
    62         out.close();
    63     }
    64     ::LeaveCriticalSection(&s_cs);
    65 }

    使用时可以

    1 WRITE_LOG("log test");
    2 WRITE_LOG_EX("[%s],log test",__FUNCTION__);

    基本满足使用,但是还有很多不足,欢迎大家批评指正

  • 相关阅读:
    电赛小结
    markdown小结
    一元运算符重载
    二维数组作为函数参数传递剖析(转载)
    C语言内存(转载)
    Effective C++ chapter1:Accustiming Yourself to C++
    C++ 模板
    const
    命令行参数
    AStar算法
  • 原文地址:https://www.cnblogs.com/huangsitao/p/10551865.html
Copyright © 2011-2022 走看看