// File: /workspace/github_cpp_makefile_star2_O3/JianjiaoSQ[P]MyPoorWebServer/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);
    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);
        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);

    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);
    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);
        m_mutex.unlock();
    }

    va_end(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 0x64bc
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 0x3580
movq (%r15), %rax
leaq 0x78(%rsp), %rdi
movq %rax, (%rdi)
callq 0x32f0
movl (%rax), %edi
movl 0x4(%rax), %esi
movl 0x8(%rax), %edx
movl 0xc(%rax), %r12d
movl 0x10(%rax), %r13d
movl 0x14(%rax), %ebp
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
cmpl $0x3, %r14d
movq %rdx, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdi, 0x20(%rsp)
ja 0x653b
movl %r14d, %eax
leaq 0x2eb4(%rip), %rcx      # 0x93d0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movabsq $0x3a5d67756265645b, %rax # imm = 0x3A5D67756265645B
movq %rax, 0x10(%rsp)
movb $0x0, 0x18(%rsp)
jmp 0x6568
movabsq $0x3a5d6f666e695b, %rax # imm = 0x3A5D6F666E695B
jmp 0x6563
movabsq $0x3a5d6e7261775b, %rax # imm = 0x3A5D6E7261775B
jmp 0x6563
movabsq $0x3a5d6f666e695b, %rax # imm = 0x3A5D6F666E695B
addq $0xc03fc00, %rax        # imm = 0xC03FC00
movq %rax, 0x10(%rsp)
leaq 0x140(%rbx), %r14
movq %r14, %rdi
callq 0x3500
movq 0x110(%rbx), %rax
incq %rax
movq %rax, 0x110(%rbx)
cmpl %r12d, 0x118(%rbx)
jne 0x65b7
movslq 0x108(%rbx), %rcx
cqto
idivq %rcx
testq %rdx, %rdx
je 0x65b7
addl $0x76c, %ebp            # imm = 0x76C
movl %ebp, 0xc(%rsp)
incl %r13d
movl %r13d, %ebp
jmp 0x66bc
leaq 0x80(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x3220
movq 0x120(%rbx), %rdi
callq 0x3430
movq 0x120(%rbx), %rdi
callq 0x3380
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rdi
movaps %xmm0, (%rdi)
addl $0x76c, %ebp            # imm = 0x76C
incl %r13d
leaq 0x2e04(%rip), %rdx      # 0x9402
movl $0x10, %esi
movl %ebp, %ecx
movl %r13d, %r8d
movl %r12d, %r9d
xorl %eax, %eax
callq 0x3570
leaq 0x8(%rbx), %rcx
leaq 0x88(%rbx), %r9
cmpl %r12d, 0x118(%rbx)
movl %ebp, 0xc(%rsp)
jne 0x666c
movq 0x110(%rbx), %rax
movslq 0x108(%rbx), %rsi
cqto
idivq %rsi
movq %rax, %r10
subq $0x8, %rsp
leaq 0x2dcc(%rip), %rdx      # 0x9417
leaq 0x88(%rsp), %rdi
leaq 0x48(%rsp), %r8
movl $0xff, %esi
xorl %eax, %eax
pushq %r10
callq 0x3570
addq $0x10, %rsp
jmp 0x669e
leaq 0x2d9d(%rip), %rdx      # 0x9410
leaq 0x80(%rsp), %rdi
leaq 0x40(%rsp), %r8
movl $0xff, %esi
xorl %eax, %eax
callq 0x3570
movl %r12d, 0x118(%rbx)
movq $0x0, 0x110(%rbx)
movl %r13d, %ebp
leaq 0x2d4f(%rip), %rsi      # 0x93f7
leaq 0x80(%rsp), %rdi
callq 0x3440
movq %rax, 0x120(%rbx)
movq %r14, %rdi
callq 0x3300
leaq 0x180(%rsp), %rax
leaq 0x40(%rsp), %r15
movq %rax, 0x10(%r15)
leaq 0x270(%rsp), %rax
movq %rax, 0x8(%r15)
movabsq $0x3000000018, %rax     # imm = 0x3000000018
movq %rax, (%r15)
leaq 0x90(%rsp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
xorl %eax, %eax
movb %al, (%r13)
movq %r14, %rdi
callq 0x3500
movq 0x128(%rbx), %rdi
subq $0x8, %rsp
leaq 0x18(%rsp), %r10
leaq 0x2cfc(%rip), %rdx      # 0x9423
movl $0x30, %esi
movl 0x14(%rsp), %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 0x3570
addq $0x30, %rsp
movslq %eax, %r12
movq 0x128(%rbx), %rdi
addq %r12, %rdi
movslq 0x10c(%rbx), %rsi
decq %rsi
movq 0x38(%rsp), %rdx
movq %r15, %rcx
callq 0x3260
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), %r15
movq -0x8(%r13), %r12
movq %r15, %rdi
callq 0x3150
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r15, %rcx
movq %rax, %r8
callq 0x3540
movq %r14, %rdi
callq 0x3300
cmpb $0x1, 0x138(%rbx)
jne 0x680b
movq 0x130(%rbx), %r15
movq %r15, %rdi
callq 0x3500
movl 0x60(%r15), %ebp
movl 0x64(%r15), %r12d
movq %r15, %rdi
callq 0x3300
cmpl %r12d, %ebp
jge 0x680b
movq 0x130(%rbx), %rdi
leaq 0x80(%rsp), %rsi
callq 0x695a
jmp 0x682f
movq %r14, %rdi
callq 0x3500
movq 0x80(%rsp), %rdi
movq 0x120(%rbx), %rsi
callq 0x3400
movq %r14, %rdi
callq 0x3300
movq 0x80(%rsp), %rdi
cmpq %r13, %rdi
je 0x684c
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x33a0
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 0x687e
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x33a0
movq %rbx, %rdi
callq 0x35e0