// File: /workspace/github_cpp_makefile_star2_O2/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 $0x228, %rsp            # imm = 0x228
movq %rdx, 0x28(%rsp)
movl %esi, %r14d
movq %rdi, %rbx
movq %rcx, 0x188(%rsp)
movq %r8, 0x190(%rsp)
movq %r9, 0x198(%rsp)
testb %al, %al
je 0x5efe
movaps %xmm0, 0x1a0(%rsp)
movaps %xmm1, 0x1b0(%rsp)
movaps %xmm2, 0x1c0(%rsp)
movaps %xmm3, 0x1d0(%rsp)
movaps %xmm4, 0x1e0(%rsp)
movaps %xmm5, 0x1f0(%rsp)
movaps %xmm6, 0x200(%rsp)
movaps %xmm7, 0x210(%rsp)
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %r15
movaps %xmm0, (%r15)
movq %r15, %rdi
xorl %esi, %esi
callq 0x35c0
movq (%r15), %rax
leaq 0x68(%rsp), %rdi
movq %rax, (%rdi)
callq 0x3310
movl (%rax), %edi
movl 0x4(%rax), %esi
movl 0x8(%rax), %edx
movl 0xc(%rax), %ebp
movl 0x10(%rax), %r12d
movl 0x14(%rax), %r13d
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
cmpl $0x3, %r14d
movq %rdx, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
ja 0x5f7b
movl %r14d, %eax
leaq 0x2473(%rip), %rcx      # 0x83d0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movabsq $0x3a5d67756265645b, %rax # imm = 0x3A5D67756265645B
movq %rax, (%rsp)
movb $0x0, 0x8(%rsp)
jmp 0x5fa7
movabsq $0x3a5d6f666e695b, %rax # imm = 0x3A5D6F666E695B
jmp 0x5fa3
movabsq $0x3a5d6e7261775b, %rax # imm = 0x3A5D6E7261775B
jmp 0x5fa3
movabsq $0x3a5d6f666e695b, %rax # imm = 0x3A5D6F666E695B
addq $0xc03fc00, %rax        # imm = 0xC03FC00
movq %rax, (%rsp)
leaq 0x140(%rbx), %r15
movq %r15, %rdi
callq 0x54be
movq 0x110(%rbx), %rax
incq %rax
movq %rax, 0x110(%rbx)
cmpl %ebp, 0x118(%rbx)
jne 0x5fef
movslq 0x108(%rbx), %rcx
cqto
idivq %rcx
testq %rdx, %rdx
je 0x5fef
addl $0x76c, %r13d           # imm = 0x76C
incl %r12d
jmp 0x60da
leaq 0x70(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x3240
movq 0x120(%rbx), %rdi
callq 0x3460
movq 0x120(%rbx), %rdi
callq 0x33b0
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rdi
movaps %xmm0, (%rdi)
addl $0x76c, %r13d           # imm = 0x76C
incl %r12d
leaq 0x23ce(%rip), %rdx      # 0x8402
pushq $0x10
popq %rsi
movl %r13d, %ecx
movl %r12d, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x35b0
leaq 0x8(%rbx), %rcx
leaq 0x88(%rbx), %r9
cmpl %ebp, 0x118(%rbx)
jne 0x6097
movq 0x110(%rbx), %rax
movslq 0x108(%rbx), %rsi
cqto
idivq %rsi
movq %rax, %r10
subq $0x8, %rsp
leaq 0x239c(%rip), %rdx      # 0x8417
leaq 0x78(%rsp), %rdi
leaq 0x38(%rsp), %r8
movl $0xff, %esi
xorl %eax, %eax
pushq %r10
callq 0x35b0
popq %rcx
popq %rdx
jmp 0x60c2
leaq 0x2372(%rip), %rdx      # 0x8410
leaq 0x70(%rsp), %rdi
leaq 0x30(%rsp), %r8
movl $0xff, %esi
xorl %eax, %eax
callq 0x35b0
movl %ebp, 0x118(%rbx)
andq $0x0, 0x110(%rbx)
leaq 0x232e(%rip), %rsi      # 0x83f7
leaq 0x70(%rsp), %rdi
callq 0x3470
movq %rax, 0x120(%rbx)
movq %r15, %rdi
callq 0x5522
leaq 0x170(%rsp), %rax
leaq 0x30(%rsp), %r14
movq %rax, 0x10(%r14)
leaq 0x260(%rsp), %rax
movq %rax, 0x8(%r14)
leaq 0x80(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movabsq $0x3000000018, %rcx     # imm = 0x3000000018
movq %rcx, (%r14)
xorl %ecx, %ecx
movb %cl, (%rax)
movq %r15, %rdi
callq 0x54be
movq 0x128(%rbx), %rdi
subq $0x8, %rsp
leaq 0x8(%rsp), %r10
leaq 0x22e3(%rip), %rdx      # 0x8423
pushq $0x30
popq %rsi
movl %r13d, %ecx
movl %r12d, %r8d
movl %ebp, %r9d
movl $0x0, %eax
pushq %r10
pushq 0x68(%rsp)
pushq 0x28(%rsp)
pushq 0x38(%rsp)
pushq 0x48(%rsp)
callq 0x35b0
addq $0x30, %rsp
movslq %eax, %r12
movq 0x128(%rbx), %rdi
addq %r12, %rdi
movslq 0x10c(%rbx), %rsi
decq %rsi
movq 0x28(%rsp), %rdx
movq %r14, %rcx
callq 0x3280
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), %rsi
leaq 0x70(%rsp), %rdi
callq 0x36d0
movq %r15, %rdi
callq 0x5522
cmpb $0x1, 0x138(%rbx)
jne 0x61e0
movq 0x130(%rbx), %rdi
callq 0x631e
testb %al, %al
je 0x6203
movq %r15, %rdi
callq 0x54be
movq 0x70(%rsp), %rdi
movq 0x120(%rbx), %rsi
callq 0x3430
movq %r15, %rdi
callq 0x5522
jmp 0x6214
movq 0x130(%rbx), %rdi
leaq 0x70(%rsp), %rsi
callq 0x6344
leaq 0x70(%rsp), %rdi
callq 0x3190
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x3190
movq %rbx, %rdi
callq 0x3630
nop