Log

来源:互联网 发布:数据大魔王txt离线下载 编辑:程序博客网 时间:2024/04/27 22:26
#include "rxlog/rxlog.h"#include <assert.h>#include <time.h>#include <iostream>#include <string>#include "rxcore/rxenv/rxruntimeenv.h"#ifdef RX_OS_WIN#include <Winsock2.h>#endifusing ::rxcore::Log;using ::rxcore::Mutex;using ::rxcore::MutexLocker;Log* Log::get_instance(const char* file_name, int log_buf_size, int split_lines){    static Log instance;    static int init_flag = 1;    if (init_flag) {        assert(file_name != NULL);        instance.init(file_name, log_buf_size, split_lines);        init_flag = 0;    }    return &instance;}Log::Log(){    m_count = 0;}Log::~Log(){    if (m_fp != NULL)    {        fclose(m_fp);    }    delete[]m_buf;}bool Log::init(const char* file_name, int log_buf_size, int split_lines){    char* envptr = NULL;    envptr = getenv("RUN_LEVEL");    if (!envptr) {        run_level = "RELEASE";    } else {        run_level = envptr;    }    assert(file_name[0] == '/');    m_log_buf_size = log_buf_size;    m_buf = new char[1024*1024];    //memset(m_buf, '\0', sizeof(m_buf));    m_split_lines = split_lines;    time_t t = time(NULL);    struct tm ptm = { 0 };    struct tm* sys_tm = localtime(&t); // NOLINT    struct tm my_tm = *sys_tm;    char log_full_name[512] = {0};    std::string log_fullbase_name = rxcore::RunTimeEnv::getInstance().getSystemLogDirectory() + file_name;    strcpy(m_log_base_name, log_fullbase_name.c_str());    _snprintf(log_full_name, 255, "%s_%d_%02d_%02d.log",        m_log_base_name, my_tm.tm_year + 1900, my_tm.tm_mon + 1, my_tm.tm_mday);    m_today = my_tm.tm_mday;    m_fp = fopen(log_full_name, "a");    if (m_fp == NULL)    {        return false;    }    return true;}void Log::write_log(int level, const char* format, ...){    struct timeval now_t = { 0, 0 };    struct timeval* now = &now_t;    time_t t = time(NULL);    struct tm* sys_tm = localtime(&t); // NOLINT    struct tm my_tm = *sys_tm;    char s[16] = {0};    switch (level)    {        case 0 : strcpy(s, "[debug]:"); break;        case 1 : strcpy(s, "[info ]:"); break;        case 2 : strcpy(s, "[warn ]:"); break;        case 3 : strcpy(s, "[erro ]:"); break;        default:            strcpy(s, "[info ]:");            break;    }    std::string type = s;    int n = _snprintf(m_buf, 48, "%d-%02d-%02d %02d:%02d:%02d.%03d %s ",        my_tm.tm_year+1900, my_tm.tm_mon+1, my_tm.tm_mday,        my_tm.tm_hour, my_tm.tm_min, my_tm.tm_sec, now->tv_usec, s);    //互斥访问区间    {        MutexLocker mutexLocker(m_mutex);        m_count++;        if (m_today != my_tm.tm_mday || m_count % m_split_lines == 0) //everyday log        {            char new_log[512] = { 0 };            flushWithLockHold();            fclose(m_fp);            char tail[16] = { 0 };            _snprintf(tail, 16, "%d_%02d_%02d", my_tm.tm_year + 1900, my_tm.tm_mon + 1, my_tm.tm_mday);            if (m_today != my_tm.tm_mday)            {                _snprintf(new_log, 255, "%s_%s.log", m_log_base_name, tail);                m_today = my_tm.tm_mday;                m_count = 0;            } else {                _snprintf(new_log, 255, "%s_%s.%d.log", m_log_base_name, tail, m_count / m_split_lines);            }            m_fp = fopen(new_log, "a");            if (!m_fp) {                std::cout << "log file open error!" << std::endl;            }        }    }    va_list valst;    va_start(valst, format);    {        MutexLocker mutexLocker(m_mutex);        int m = vsnprintf(m_buf + n, 1024*1024 - 1, format, valst);        // m_buf[n + m] = '\n';        if (run_level != "DEBUG") { // not debug model            if (type != "[debug]:") {                fputs(m_buf, m_fp);                fputs("\n", m_fp);                flushWithLockHold();            }        } else { //debug model            if (type == "[debug]:") {                //printf("%s\n", m_buf);                fputs(m_buf, m_fp);                fputs("\n", m_fp);                flushWithLockHold();            } else {                fputs(m_buf, m_fp);                fputs("\n", m_fp);                flushWithLockHold();            }        }    }    va_end(valst);}void Log::flush(void){    MutexLocker mutexLocker(m_mutex);    fflush(m_fp);}void Log::flushWithLockHold(){    fflush(m_fp);}<pre name="code" class="cpp">// Copyright 2015 Sprixin Inc. All rights reserved.#ifndef RXLOG_HEADER#define RXLOG_HEADER#include <stdio.h>#include <stdarg.h>#include <stdint.h>#include <stdlib.h>#include <string>#include "rxcore.h"#include "rxcore/rxthread/rxmutex.h"namespace rxcore {class RXCORE_API Log : boost::noncopyable{public:    enum LogType {        //! 调试时输出信息        RXDEBUG = 0,        //! 运行时输出信息        RXINFO = 1,        //! 警告信息        RXWARN = 2,        //! 错误信息        RXERROR = 3    };    static Log* get_instance(const char* file_name = NULL, int log_buf_size = 8192, int split_lines = 500000);    void write_log(int level, const char* format, ...);    void flush(void);private:    Log();    virtual ~Log();    void flushWithLockHold();    bool init(const char* file_name, int log_buf_size, int split_lines);private:    Mutex m_mutex;    char m_log_base_name[512];    int m_split_lines;    int m_log_buf_size;    int64_t m_count;    int m_today;    FILE *m_fp;    char *m_buf;    ::std::string run_level;};//! 定义的输出宏#define LOG_DEBUG(format, ...) Log::get_instance()->write_log(0, format " [%s][%s][%d]", \    __VA_ARGS__, __FUNCTION__, __FILE__ , __LINE__)#define LOG_INFO(format, ...) Log::get_instance()->write_log(1, format " [%s][%s][%d]", \    __VA_ARGS__, __FUNCTION__, __FILE__ , __LINE__)#define LOG_WARN(format, ...) Log::get_instance()->write_log(2, format " [%s][%s][%d]", \    __VA_ARGS__, __FUNCTION__, __FILE__ , __LINE__)#define LOG_ERROR(format, ...) Log::get_instance()->write_log(3, format " [%s][%s][%d]", \    __VA_ARGS__, __FUNCTION__, __FILE__ , __LINE__)} // namespace rxcore#endif

 

0 0