// File: /workspace/github_cpp_makefile_star2_O1/lszp[P]SmallWebServer/log/log.cpp
// Function: Log::write_log(int, char const*, ...)
void Log::write_log(int level, const char *format, ...)
{
    struct timeval now = {0, 0};
    gettimeofday(&now, NULL);  // 从1970年1月1日0:00:00起，经过的秒数及毫秒。
    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;
    }
    //写入一个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
    {
        
        char new_log[256] = {0};
        fflush(m_fp); //更新缓冲区，将缓冲区的内容写到 m_fp 指定的文件中。
        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%s", dir_name, tail, log_name);
            m_today = my_tm.tm_mday;
            m_count = 0;
        }
        else
        {
            snprintf(new_log, 255, "%s%s%s.%lld", dir_name, tail, log_name, m_count / m_split_lines);
        }
        m_fp = fopen(new_log, "a");
    }
 
    m_mutex.unlock();

    va_list valst;  // 定义指针，指向可选参数
    va_start(valst, format);  // 让valst指向参数列表中第一个可选参数

    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);
    
    int m = vsnprintf(m_buf + n, m_log_buf_size - 1, format, valst); // 将格式化数据从可变参数列表valst写入缓冲区，返回写入的字符数。
    m_buf[n + m] = '\n';
    m_buf[n + m + 1] = '\0';
    log_str = m_buf;

    m_mutex.unlock();

    if (m_is_async && !m_log_queue->full())
    {
        m_log_queue->push(log_str);
    }
    else
    {
        m_mutex.lock();
        fputs(log_str.c_str(), m_fp);  //队列满，则将log_str写入m_fp指向的文件
        m_mutex.unlock();
    }

    va_end(valst);  // 清空参数列表，并置valst无效。
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x238, %rsp            # imm = 0x238
movq %rdx, 0x38(%rsp)
movl %esi, %r14d
movq %rdi, %rbx
movq %rcx, 0x198(%rsp)
movq %r8, 0x1a0(%rsp)
movq %r9, 0x1a8(%rsp)
testb %al, %al
je 0x6e6c
movaps %xmm0, 0x1b0(%rsp)
movaps %xmm1, 0x1c0(%rsp)
movaps %xmm2, 0x1d0(%rsp)
movaps %xmm3, 0x1e0(%rsp)
movaps %xmm4, 0x1f0(%rsp)
movaps %xmm5, 0x200(%rsp)
movaps %xmm6, 0x210(%rsp)
movaps %xmm7, 0x220(%rsp)
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %r15
movaps %xmm0, (%r15)
movq %r15, %rdi
xorl %esi, %esi
callq 0x3680
movq (%r15), %rax
leaq 0x78(%rsp), %rdi
movq %rax, (%rdi)
callq 0x3340
movl (%rax), %edi
movl 0x4(%rax), %esi
movl 0x8(%rax), %edx
movl 0xc(%rax), %r12d
movl 0x10(%rax), %ebp
movl 0x14(%rax), %r15d
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
cmpl $0x3, %r14d
movq %rdx, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, 0x20(%rsp)
ja 0x6ee9
movl %r14d, %eax
leaq 0x35a5(%rip), %rcx      # 0xa470
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movabsq $0x3a5d67756265645b, %rax # imm = 0x3A5D67756265645B
movq %rax, (%rsp)
movb $0x0, 0x8(%rsp)
jmp 0x6f15
movabsq $0x3a5d6f666e695b, %rax # imm = 0x3A5D6F666E695B
jmp 0x6f11
movabsq $0x3a5d6e7261775b, %rax # imm = 0x3A5D6E7261775B
jmp 0x6f11
movabsq $0x3a5d6f666e695b, %rax # imm = 0x3A5D6F666E695B
addq $0xc03fc00, %rax        # imm = 0xC03FC00
movq %rax, (%rsp)
leaq 0x140(%rbx), %r14
movq %r14, %rdi
callq 0x35f0
movq 0x110(%rbx), %rax
incq %rax
movq %rax, 0x110(%rbx)
cmpl %r12d, 0x118(%rbx)
jne 0x6f53
movslq 0x108(%rbx), %rcx
cqto
idivq %rcx
testq %rdx, %rdx
jne 0x704e
leaq 0x80(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x3260
movq 0x120(%rbx), %rdi
callq 0x34e0
movq 0x120(%rbx), %rdi
callq 0x3410
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rdi
movaps %xmm0, (%rdi)
leal 0x76c(%r15), %ecx
leal 0x1(%rbp), %r8d
leaq 0x3504(%rip), %rdx      # 0xa4a0
movl $0x10, %esi
movl %r12d, %r9d
xorl %eax, %eax
callq 0x3670
leaq 0x8(%rbx), %rcx
leaq 0x88(%rbx), %r9
cmpl %r12d, 0x118(%rbx)
jne 0x7001
movq 0x110(%rbx), %rax
movslq 0x108(%rbx), %rsi
cqto
idivq %rsi
movq %rax, %r10
subq $0x8, %rsp
leaq 0x34d5(%rip), %rdx      # 0xa4b5
leaq 0x88(%rsp), %rdi
leaq 0x48(%rsp), %r8
movl $0xff, %esi
xorl %eax, %eax
pushq %r10
callq 0x3670
addq $0x10, %rsp
jmp 0x7033
leaq 0x34a6(%rip), %rdx      # 0xa4ae
leaq 0x80(%rsp), %rdi
leaq 0x40(%rsp), %r8
movl $0xff, %esi
xorl %eax, %eax
callq 0x3670
movl %r12d, 0x118(%rbx)
movq $0x0, 0x110(%rbx)
leaq 0x3074(%rip), %rsi      # 0xa0ae
leaq 0x80(%rsp), %rdi
callq 0x34f0
movq %rax, 0x120(%rbx)
movq %r14, %rdi
callq 0x3350
leaq 0x180(%rsp), %rax
movq %r14, %rdi
leaq 0x40(%rsp), %r14
movq %rax, 0x10(%r14)
leaq 0x270(%rsp), %rax
movq %rax, 0x8(%r14)
movabsq $0x3000000018, %rax     # imm = 0x3000000018
movq %rax, (%r14)
leaq 0x90(%rsp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
xorl %eax, %eax
movb %al, (%r13)
movq %rdi, 0x18(%rsp)
callq 0x35f0
movq 0x128(%rbx), %rdi
addl $0x76c, %r15d           # imm = 0x76C
incl %ebp
subq $0x8, %rsp
leaq 0x8(%rsp), %r10
leaq 0x33fa(%rip), %rdx      # 0xa4c1
movl $0x30, %esi
movl %r15d, %ecx
movl %ebp, %r8d
movl %r12d, %r9d
xorl %eax, %eax
pushq %r10
pushq 0x78(%rsp)
pushq 0x38(%rsp)
pushq 0x48(%rsp)
pushq 0x58(%rsp)
callq 0x3670
addq $0x30, %rsp
movslq %eax, %r12
movq 0x128(%rbx), %rdi
addq %r12, %rdi
movslq 0x10c(%rbx), %rsi
decq %rsi
movq 0x38(%rsp), %rdx
movq %r14, %rcx
callq 0x32a0
movq 0x128(%rbx), %rcx
addl %eax, %r12d
movslq %r12d, %rax
movb $0xa, (%rcx,%rax)
movq 0x128(%rbx), %rcx
xorl %edx, %edx
movb %dl, 0x1(%rcx,%rax)
movq 0x128(%rbx), %r14
movq -0x8(%r13), %r12
movq %r14, %rdi
callq 0x3180
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x3640
movq 0x18(%rsp), %r14
movq %r14, %rdi
callq 0x3350
cmpb $0x1, 0x138(%rbx)
jne 0x7185
movq 0x130(%rbx), %rdi
callq 0x72ea
testb %al, %al
je 0x71ab
movq %r14, %rdi
callq 0x35f0
movq 0x80(%rsp), %rdi
movq 0x120(%rbx), %rsi
callq 0x34a0
movq %r14, %rdi
callq 0x3350
jmp 0x71bf
movq 0x130(%rbx), %rdi
leaq 0x80(%rsp), %rsi
callq 0x7310
movq 0x80(%rsp), %rdi
cmpq %r13, %rdi
je 0x71dc
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x3430
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x80(%rsp), %rdi
cmpq %r13, %rdi
je 0x720e
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x3430
movq %rbx, %rdi
callq 0x36e0