zoukankan      html  css  js  c++  java
  • C++实现简单的日志记录

    C++实现简单的日志记录

    //dlogger.h
    #ifndef DLOGGER_H
    #define DLOGGER_H
    
    #include <iostream>
    #include <Mutex>
    #include <ctime>
    #include <string>
    
    extern std::mutex mt;
    
    //用于获取运行时间
    class Duration
    {
    public:
        explicit Duration() : m_start(clock()){}
    
        inline double duration();
    
    private:
        clock_t m_start;
    };
    
    double Duration::duration()
    {
        return (double)(clock() - m_start) / CLOCKS_PER_SEC;
    }
    
    class DLogger
    {
    public:
        enum LogLevel
        {
            Log_Info = 0,
            Log_Debug,
            Log_Warning,
            Log_Error,
            Log_Fatal,
            Level_Num
        };
    
    public:
        explicit DLogger(const std::string &dir = "");
    
        void log(const char* msg, const char* fileName, int line, LogLevel level = Log_Info);
    
    private:
        std::string m_fileName;
        static const std::string m_levelMsg[Level_Num];
    };
    
    extern std::mutex mt;
    
    #endif
    
    //dlogger.cpp
    #include "dlogger.h"
    #include <cstring>
    
    std::mutex mt;
    
    
    const std::string DLogger::m_levelMsg[Level_Num] =
    {
        "Info",
        "Debug",
        "Warning",
        "Error",
        "Fatal"
    };
    
    /**
     * 为日志文件提供一个目录,不提供目录则表示当前工作目录
     */
    DLogger::DLogger(const std::string &dir)
    {
        char buf[64];
        time_t now = time(0);
        tm* ltm = localtime(&now);
    
        sprintf(buf, "%d-%d-%d.log", ltm->tm_year + 1900, ltm->tm_mon + 1, ltm->tm_mday);
    
        if(dir.empty())
        {
            m_fileName = buf;
        }
        else
        {
            m_fileName = dir + "\" + buf;
        }
    
    }
    
    /**
     * 记录日志
     * @param msg      [日志信息]
     * @param fileName [日志信息文件名]
     * @param line     [日志信息所在行]
     * @param level    [日志信息等级]
     */
    void DLogger::log(const char* msg, const char* fileName, int line, LogLevel level)
    {
        mt.lock();
    
        FILE* fp = ::fopen(m_fileName.data(), "a+");
        if(!fp)
        {
            mt.unlock();
            return;
        }
        char buf[1024];
    
        time_t now = time(0);
        tm *ltm = localtime(&now);
    
        ::sprintf(buf, "%d/%d/%d %d:%d:%d, %s, %d, %s, %s
    ",
                  1900+ltm->tm_year, ltm->tm_mon, ltm->tm_mday,
                  ltm->tm_hour, ltm->tm_min, ltm->tm_sec,
                  fileName, line, msg, m_levelMsg[level].data());
    
        ::fwrite(buf, sizeof(char),strlen(buf), fp);
        ::fclose(fp);
    
        mt.unlock();
    }
    
    
  • 相关阅读:
    springboot 实现 aop
    使用@import导入实现了ImportBeanDefinitionRegistrar接口的类,不能被注册为bean
    Spring Boot 动态数据源(多数据源自动切换)
    springboot2动态数据源的绑定
    三分钟学会@Autowired@Qualifier@Primary注解
    java复制文件的4种方式
    五款最好的免费同步软件
    springboot2多数据源完整示例
    WebSocket 结合 Nginx 实现域名及 WSS 协议访问
    Spring的注解@Qualifier注解
  • 原文地址:https://www.cnblogs.com/chengjundu/p/11939816.html
Copyright © 2011-2022 走看看