240717班级,工业化控制系统,煤矿相关行业,昆仑系统
congmu
2024-10-30 79d067bcf500355e65e26a11c345a9427ed0128c
Merge branch 'master' of ssh://115.28.86.8:29418/~admin/昆仑_1025
5个文件已修改
1个文件已添加
4个文件已删除
281 ■■■■ 已修改文件
Server/王琨元/document/单例模式封装.txt 51 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Server/王琨元/document/建表.docx 补丁 | 查看 | 原始文档 | blame | 历史
Server/王琨元/document/数据库.docx 补丁 | 查看 | 原始文档 | blame | 历史
Server/王琨元/document/数据库模块需求分析.docx 补丁 | 查看 | 原始文档 | blame | 历史
Server/王琨元/document/数据库连接.txt 16 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Server/王琨元/document/数据库连接池类.txt 91 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Server/马丽萍/code/log/.vscode/settings.json 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
Server/马丽萍/code/log/README.md 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Server/马丽萍/code/log/log.cpp 86 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Server/马丽萍/code/log/log.h 30 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Server/ÍõçûÔª/document/µ¥Àýģʽ·â×°.txt
File was deleted
Server/ÍõçûÔª/document/½¨±í.docx
Binary files differ
Server/ÍõçûÔª/document/Êý¾Ý¿â.docx
Binary files differ
Server/ÍõçûÔª/document/Êý¾Ý¿âÄ£¿éÐèÇó·ÖÎö.docx
Binary files differ
Server/ÍõçûÔª/document/Êý¾Ý¿âÁ¬½Ó.txt
File was deleted
Server/ÍõçûÔª/document/Êý¾Ý¿âÁ¬½Ó³ØÀà.txt
File was deleted
Server/ÂíÀöƼ/code/log/.vscode/settings.json
@@ -4,7 +4,7 @@
  "C_Cpp_Runner.debuggerPath": "gdb",
  "C_Cpp_Runner.cStandard": "",
  "C_Cpp_Runner.cppStandard": "",
  "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat",
  "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/Build/vcvarsall.bat",
  "C_Cpp_Runner.useMsvc": false,
  "C_Cpp_Runner.warnings": [
    "-Wall",
Server/ÂíÀöƼ/code/log/README.md
@@ -1,9 +1,10 @@
同步/异步日志系统
异步日志系统
===============
同步/异步日志系统主要涉及了两个模块,一个是日志模块,一个是阻塞队列模块,其中加入阻塞队列模块主要是解决异步写入日志做准备.
异步日志系统主要涉及了两个模块,一个是日志模块,一个是阻塞队列模块,其中加入阻塞队列模块主要是解决异步写入日志做准备.
> * è‡ªå®šä¹‰é˜»å¡žé˜Ÿåˆ—
> * å•例模式创建日志
> * åŒæ­¥æ—¥å¿—
> * å¼‚步日志
> * å®žçŽ°æŒ‰å¤©ã€è¶…è¡Œåˆ†ç±»
> * æ–‡ä»¶é™åˆ¶å¤§å° é˜²æ­¢æ—¥å¿—文件过大
Server/ÂíÀöƼ/code/log/log.cpp
@@ -19,16 +19,14 @@
        fclose(m_fp);
    }
}
//异步需要设置阻塞队列的长度,同步不需要设置
bool Log::init(const char *file_name, int close_log, int log_buf_size, int split_lines, int max_queue_size)
{
    //如果设置了max_queue_size,则设置为异步
    if (max_queue_size >= 1)
    {
        m_is_async = true;
        m_log_queue = new block_queue<string>(max_queue_size);
        pthread_t tid;
        //flush_log_thread为回调函数,这里表示创建线程异步写日志
        pthread_create(&tid, NULL, flush_log_thread, NULL);
    }
    
@@ -40,25 +38,21 @@
    time_t t = time(NULL);
    struct tm *sys_tm = localtime(&t);
    struct tm my_tm = *sys_tm;
    const char *p = strrchr(file_name, '/');
    char log_full_name[256] = {0};
    if (p == NULL)
    {
        snprintf(log_full_name, 255, "%d_%02d_%02d_%s", my_tm.tm_year + 1900, my_tm.tm_mon + 1, my_tm.tm_mday, file_name);
        snprintf(log_full_name, 255, "%d_%02d_%02d_%s", sys_tm->tm_year + 1900, sys_tm->tm_mon + 1, sys_tm->tm_mday, file_name);
    }
    else
    {
        strcpy(log_name, p + 1);
        strncpy(dir_name, file_name, p - file_name + 1);
        snprintf(log_full_name, 255, "%s%d_%02d_%02d_%s", dir_name, my_tm.tm_year + 1900, my_tm.tm_mon + 1, my_tm.tm_mday, log_name);
        snprintf(log_full_name, 255, "%s%d_%02d_%02d_%s", dir_name, sys_tm->tm_year + 1900, sys_tm->tm_mon + 1, sys_tm->tm_mday, log_name);
    }
    m_today = my_tm.tm_mday;
    m_today = sys_tm->tm_mday;
    m_fp = fopen(log_full_name, "a");
    if (m_fp == NULL)
    {
@@ -70,48 +64,36 @@
void Log::write_log(int level, const char *format, ...)
{
    struct timeval now = {0, 0};
    struct timeval now;
    gettimeofday(&now, NULL);
    time_t t = now.tv_sec;
    struct tm *sys_tm = localtime(&t);
    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;
        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;
    }
    //写入一个log,对m_count++, m_split_lines最大行数
    m_mutex.lock();
    m_count++;
    if (m_today != my_tm.tm_mday || m_count % m_split_lines == 0) //everyday log
    if (m_today != sys_tm->tm_mday || m_count % m_split_lines == 0)
    {
        char new_log[256] = {0};
        fflush(m_fp);
        fclose(m_fp);
        char tail[16] = {0};
        snprintf(tail, 16, "%d_%02d_%02d_", sys_tm->tm_year + 1900, sys_tm->tm_mon + 1, sys_tm->tm_mday);
       
        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)
        if (m_today != sys_tm->tm_mday)
        {
            snprintf(new_log, 255, "%s%s%s", dir_name, tail, log_name);
            m_today = my_tm.tm_mday;
            m_today = sys_tm->tm_mday;
            m_count = 0;
        }
        else
@@ -121,19 +103,19 @@
        m_fp = fopen(new_log, "a");
    }
 
    m_mutex.unlock();
    // åœ¨å†™å…¥ä¹‹å‰æ£€æŸ¥æ—¥å¿—文件大小
    if (check_log_size())
    {
        rotate_logs(); // å¦‚果超过最大文件大小,进行日志轮换
    }
    va_list valst;
    va_start(valst, format);
    string log_str;
    m_mutex.lock();
    //写入的具体时间内容格式
    int n = snprintf(m_buf, 48, "%d-%02d-%02d %02d:%02d:%02d.%06ld %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);
                     sys_tm->tm_year + 1900, sys_tm->tm_mon + 1, sys_tm->tm_mday,
                     sys_tm->tm_hour, sys_tm->tm_min, sys_tm->tm_sec, now.tv_usec, s);
    int m = vsnprintf(m_buf + n, m_log_buf_size - n - 1, format, valst);
    m_buf[n + m] = '\n';
    m_buf[n + m + 1] = '\0';
@@ -158,7 +140,27 @@
void Log::flush(void)
{
    m_mutex.lock();
    //强制刷新写入流缓冲区
    fflush(m_fp);
    m_mutex.unlock();
}
// æ·»åŠ çš„æ–‡ä»¶å¤§å°æ£€æŸ¥å‡½æ•°
bool Log::check_log_size()
{
    if (m_fp)
    {
        fseek(m_fp, 0, SEEK_END);
        long file_size = ftell(m_fp);
        return file_size >= max_size; // è¿”回文件是否超出限制
    }
    return false;
}
void Log::rotate_logs()
{
    fclose(m_fp); // å…³é—­å½“前日志文件
    char new_log[256] = {0};
    snprintf(new_log, sizeof(new_log), "%s/%s.%lld", dir_name, log_name, m_count / m_split_lines);
    m_fp = fopen(new_log, "a"); // æ‰“开新的日志文件
    m_count = 0; // é‡ç½®æ—¥å¿—计数
}
Server/ÂíÀöƼ/code/log/log.h
@@ -24,16 +24,17 @@
    {
        Log::get_instance()->async_write_log();
    }
    //可选择的参数有日志文件、日志缓冲区大小、最大行数以及最长日志条队列
    bool init(const char *file_name, int close_log, int log_buf_size = 8192, int split_lines = 5000000, int max_queue_size = 0);
    void write_log(int level, const char *format, ...);
    void flush(void);
private:
    Log();
    virtual ~Log();
    void *async_write_log()
    {
        string single_log;
@@ -41,9 +42,35 @@
        while (m_log_queue->pop(single_log))
        {
            m_mutex.lock();
            if (check_log_size()) // æ£€æŸ¥æ–‡ä»¶å¤§å°
            {
                rotate_logs(); // è½®æ¢æ—¥å¿—
            }
            fputs(single_log.c_str(), m_fp);
            m_mutex.unlock();
        }
    }
    // æ£€æŸ¥æ—¥å¿—文件大小
    bool check_log_size()
    {
        if (m_fp)
        {
            fseek(m_fp, 0, SEEK_END);
            long file_size = ftell(m_fp);
            return file_size >= max_size; // è¿”回文件是否超出限制
        }
        return false;
    }
    // è½®æ¢æ—¥å¿—文件
    void rotate_logs()
    {
        fclose(m_fp); // å…³é—­å½“前日志文件
        char new_log[256] = {0};
        snprintf(new_log, sizeof(new_log), "%s/%s.%lld", dir_name, log_name, m_count / m_split_lines);
        m_fp = fopen(new_log, "a"); // æ‰“开新的日志文件
        m_count = 0; // é‡ç½®æ—¥å¿—计数
    }
private:
@@ -59,6 +86,7 @@
    bool m_is_async;                  //是否同步标志位
    locker m_mutex;
    int m_close_log; //关闭日志
    long max_size = 10 * 1024 * 1024; // 10MB
};
#define LOG_DEBUG(format, ...) if(0 == m_close_log) {Log::get_instance()->write_log(0, format, ##__VA_ARGS__); Log::get_instance()->flush();}