zoukankan      html  css  js  c++  java
  • C++ 最简单的日志类

    最近搞一个 C++ 项目的二次开发,没玩过 C++,可谓步履维艰。自己写个简单的日志类都被各种坑折磨。终于搞定了。

    参考了这篇博客,并且进一步简化:https://www.cnblogs.com/DswCnblog/p/5459539.html

    代码如下:

    #pragma once
    
    #include <ctime>  
    #include <iostream>  
    #include <fstream>  
    #include <direct.h>
    
    using namespace std;
    
    #ifndef __EASYLOG_PIPI_0813
    #define __EASYLOG_PIPI_0813
    
    class EasyLog
    {
    public:
        static void Write(std::string log) {  
            try
            {    
                std::ofstream ofs;  
                time_t t = time(0);  
                char tmp[64];  
                strftime(tmp, sizeof(tmp), "[%Y-%m-%d %X]", localtime(&t));  
                ofs.open("D:\PipeLog.log", std::ofstream::app);  
                
                ofs << tmp << " - ";  
                ofs.write(log.c_str(), log.size());  
                ofs << std::endl;  
                ofs.close();         
            }
            catch(...)
            {
            } 
        }
    };
    
    #endif

    使用也很简单:

    EasyLog::Write("hello Log");

    发个博客记一下,省得忘了。

  • 相关阅读:
    Disharmony Trees HDU
    Xenia and Bit Operations CodeForces
    Gym
    背包入门
    搜索入门
    Farm Tour POJ
    Flow Problem HDU
    Hie with the Pie POJ
    Building a Space Station POJ
    kuangbin 最短路集合
  • 原文地址:https://www.cnblogs.com/zhhh/p/9470255.html
Copyright © 2011-2022 走看看