zoukankan      html  css  js  c++  java
  • c++封装简单日志操作

    logger.h

    /**
     * logger.h
     * @brief 简易日志模块
     */
    #ifndef __LOGGER_H__
    #define __LOGGER_H__
    
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    #include <cstdlib>
    #include <stdint.h>
    
    typedef enum log_rank{
            INFO = 0,
            WARNING,
            ERROR,
            FATAL,
    } log_rank_t;
    
    void initLogger(const std::string &info_filename, const std::string &warn_filename,
                                    const std::string &error_filename);
    
    class Logger{
            friend void initLogger(const std::string &info_filename, const std::string &warn_filename,
                                    const std::string &error_filename);
    public:
            Logger(log_rank_t log_rank):m_log_rank(log_rank){};
            ~Logger();
            static std::ostream &start(log_rank_t log_rank, const int line, const std::string &fun);
    
    private:
            static std::ostream &getStream(log_rank_t log_rank);
    
            static std::ofstream m_info_file;
            static std::ofstream m_warn_file;
            static std::ofstream m_error_file;
            log_rank_t m_log_rank;
    };
    
    #define LOG(log_rank) 
            Logger(log_rank).start(log_rank, __LINE__, __FUNCTION__)
    
    #endif
    #include <cstdlib>
    #include <ctime>
    #include "logger.h"
    
    std::ofstream Logger::m_info_file;
    std::ofstream Logger::m_warn_file;
    std::ofstream Logger::m_error_file;
    
    void initLogger(const std::string &info_filename, const std::string &warn_filename, 
                                    const std::string &error_filename){
            Logger::m_info_file.open(info_filename.c_str(), std::ofstream::app);
            Logger::m_warn_file.open(warn_filename.c_str(), std::ofstream::app);
            Logger::m_error_file.open(error_filename.c_str(), std::ofstream::app);
    }
    
    std::ostream &Logger::getStream(log_rank_t log_rank){
            if(log_rank == INFO){
                    return m_info_file.is_open() ? m_info_file : std::cout;
            }
            if(log_rank == WARNING){
                    return m_warn_file.is_open() ? m_warn_file : std::cout;
            }
            if(log_rank == ERROR){
                    return m_error_file.is_open() ? m_error_file : std::cout;
            }
    }
    
    std::ostream &Logger::start(log_rank_t log_rank, const int line, const std::string &fun){
            time_t tm;
            time(&tm);
            char time_string[128];
            strftime(time_string, sizeof(time_string),"[%Y.%m.%d %X] ", localtime(&tm));
            return getStream(log_rank) << time_string 
                                                               << "function[" << fun <<"] " 
                                                               << "line[" << line <<"] " 
                                                               << std::flush;
    }
    
    Logger::~Logger(){
            getStream(m_log_rank) << std::endl << std::flush;
            if(m_log_rank == FATAL){
                    m_info_file.close();
                    m_warn_file.close();
                    m_error_file.close();
                    abort();
            }
    }

    测试文件

    #include <iostream>
    #include "logger.h"
    
    int main(){
            initLogger("log/info.log", "log/warning.log", "log/error.log");
            LOG(INFO) << "info " << "test";
            LOG(INFO) << "info " << "test";
            LOG(WARNING) << "warning " << "test";
            LOG(WARNING) << "warning " << "test";
            LOG(ERROR) << "error " << "test";
            LOG(ERROR) << "error " << "test";
    }
  • 相关阅读:
    Eclipse去掉js错误校验
    教学平台详细设计
    CentOS 6.3下部署LVS(NAT)+keepalived实现高性能高可用负载均衡
    通过CentOS克隆虚拟机后发现无法启动网卡或无法上网的解决办法
    使用U盘引导安装CentOS操作系统
    项目的热加载
    【转载】SQLServer全文搜索
    【转载】Lucence.Net
    【转载】Discuz!NT企业版之Sphinx全文搜索
    【转载】MySQL主从复制(MasterSlave)与读写分离(MySQLProxy)实践
  • 原文地址:https://www.cnblogs.com/bai-jimmy/p/6856336.html
Copyright © 2011-2022 走看看