// File: 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
movq %rsp, %rbp
pushq %rbx
subq $0x308, %rsp            # imm = 0x308
testb %al, %al
je 0x9348
movaps %xmm0, -0x280(%rbp)
movaps %xmm1, -0x270(%rbp)
movaps %xmm2, -0x260(%rbp)
movaps %xmm3, -0x250(%rbp)
movaps %xmm4, -0x240(%rbp)
movaps %xmm5, -0x230(%rbp)
movaps %xmm6, -0x220(%rbp)
movaps %xmm7, -0x210(%rbp)
movq %r9, -0x288(%rbp)
movq %r8, -0x290(%rbp)
movq %rcx, -0x298(%rbp)
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x2d8(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x2d0(%rbp)
movaps %xmm0, -0x30(%rbp)
xorl %eax, %eax
movl %eax, %esi
leaq -0x30(%rbp), %rdi
callq 0x36a0
movq -0x30(%rbp), %rax
movq %rax, -0x38(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x3370
movaps -0x2d0(%rbp), %xmm0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq 0x30(%rax), %rcx
movq %rcx, -0x50(%rbp)
movups (%rax), %xmm1
movups 0x10(%rax), %xmm2
movups 0x20(%rax), %xmm3
movaps %xmm3, -0x60(%rbp)
movaps %xmm2, -0x70(%rbp)
movaps %xmm1, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
movl -0x14(%rbp), %eax
movq %rax, -0x2b8(%rbp)
subq $0x3, %rax
ja 0x944f
movq -0x2b8(%rbp), %rax
leaq 0x4fd6(%rip), %rcx      # 0xe3c8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq -0x90(%rbp), %rdi
leaq 0x4fe6(%rip), %rsi      # 0xe3ef
callq 0x3430
jmp 0x9462
leaq -0x90(%rbp), %rdi
leaq 0x4fda(%rip), %rsi      # 0xe3f8
callq 0x3430
jmp 0x9462
leaq -0x90(%rbp), %rdi
leaq 0x4fcd(%rip), %rsi      # 0xe400
callq 0x3430
jmp 0x9462
leaq -0x90(%rbp), %rdi
leaq 0x4fc0(%rip), %rsi      # 0xe408
callq 0x3430
jmp 0x9462
leaq -0x90(%rbp), %rdi
leaq 0x4f9b(%rip), %rsi      # 0xe3f8
callq 0x3430
movq -0x2d8(%rbp), %rdi
addq $0x140, %rdi            # imm = 0x140
callq 0x7010
movq -0x2d8(%rbp), %rax
movq 0x110(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x110(%rax)
movl 0x118(%rax), %eax
cmpl -0x74(%rbp), %eax
jne 0x94bd
movq -0x2d8(%rbp), %rcx
movq 0x110(%rcx), %rax
movslq 0x108(%rcx), %rcx
cqto
idivq %rcx
cmpq $0x0, %rdx
jne 0x9611
leaq -0x190(%rbp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x3270
movq -0x2d8(%rbp), %rax
movq 0x120(%rax), %rdi
callq 0x3530
movq -0x2d8(%rbp), %rax
movq 0x120(%rax), %rdi
callq 0x3440
leaq -0x1a0(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x3270
leaq -0x1a0(%rbp), %rdi
movl -0x6c(%rbp), %ecx
addl $0x76c, %ecx            # imm = 0x76C
movl -0x70(%rbp), %r8d
addl $0x1, %r8d
movl -0x74(%rbp), %r9d
movl $0x10, %esi
leaq 0x4edf(%rip), %rdx      # 0xe410
movb $0x0, %al
callq 0x3690
movq -0x2d8(%rbp), %rax
movl 0x118(%rax), %eax
cmpl -0x74(%rbp), %eax
je 0x959d
movq -0x2d8(%rbp), %r9
leaq -0x190(%rbp), %rdi
movq %r9, %rcx
addq $0x8, %rcx
leaq -0x1a0(%rbp), %r8
addq $0x88, %r9
movl $0xff, %esi
leaq 0x4ea5(%rip), %rdx      # 0xe41e
movb $0x0, %al
callq 0x3690
movq -0x2d8(%rbp), %rax
movl -0x74(%rbp), %ecx
movl %ecx, 0x118(%rax)
movq $0x0, 0x110(%rax)
jmp 0x95ed
movq -0x2d8(%rbp), %rdx
leaq -0x190(%rbp), %rdi
movq %rdx, %rcx
addq $0x8, %rcx
leaq -0x1a0(%rbp), %r8
movq %rdx, %r9
addq $0x88, %r9
movq 0x110(%rdx), %rax
movslq 0x108(%rdx), %rsi
cqto
idivq %rsi
movl $0xff, %esi
leaq 0x4e43(%rip), %rdx      # 0xe425
movq %rax, (%rsp)
movb $0x0, %al
callq 0x3690
leaq -0x190(%rbp), %rdi
leaq 0x4ab0(%rip), %rsi      # 0xe0ab
callq 0x3540
movq %rax, %rcx
movq -0x2d8(%rbp), %rax
movq %rcx, 0x120(%rax)
movq -0x2d8(%rbp), %rdi
addq $0x140, %rdi            # imm = 0x140
movq %rdi, -0x2e0(%rbp)
callq 0x7190
leaq -0x2b0(%rbp), %rax
movq %rax, -0x1b0(%rbp)
leaq 0x10(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movl $0x30, -0x1bc(%rbp)
movl $0x18, -0x1c0(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0x3610
movq -0x2e0(%rbp), %rdi
callq 0x7010
jmp 0x9672
movq -0x2d8(%rbp), %rax
movq 0x128(%rax), %rdi
movl -0x6c(%rbp), %ecx
addl $0x76c, %ecx            # imm = 0x76C
movl -0x70(%rbp), %r8d
incl %r8d
movl -0x74(%rbp), %r9d
movl -0x78(%rbp), %edx
movl -0x80(%rbp), %r10d
movl -0x7c(%rbp), %esi
movq -0x28(%rbp), %r11
movq %rsp, %rax
leaq -0x90(%rbp), %rbx
movq %rbx, 0x20(%rax)
movq %r11, 0x18(%rax)
movl %r10d, 0x10(%rax)
movl %esi, 0x8(%rax)
movl %edx, (%rax)
leaq 0x4d6d(%rip), %rdx      # 0xe431
xorl %eax, %eax
movl $0x30, %esi
callq 0x3690
movl %eax, %ecx
movq -0x2d8(%rbp), %rax
movl %ecx, -0x1f0(%rbp)
movq 0x128(%rax), %rdi
movslq -0x1f0(%rbp), %rcx
addq %rcx, %rdi
movl 0x10c(%rax), %eax
decl %eax
movslq %eax, %rsi
movq -0x20(%rbp), %rdx
leaq -0x1c0(%rbp), %rcx
callq 0x32d0
movl %eax, %ecx
movq -0x2d8(%rbp), %rax
movl %ecx, -0x1f4(%rbp)
movq 0x128(%rax), %rcx
movl -0x1f0(%rbp), %edx
movl -0x1f4(%rbp), %esi
addl %esi, %edx
movslq %edx, %rdx
movb $0xa, (%rcx,%rdx)
movq 0x128(%rax), %rdx
movl -0x1f0(%rbp), %ecx
movl -0x1f4(%rbp), %esi
addl %esi, %ecx
movslq %ecx, %rcx
movb $0x0, 0x1(%rcx,%rdx)
movq 0x128(%rax), %rsi
leaq -0x1e0(%rbp), %rdi
callq 0x35f0
jmp 0x9768
movq -0x2d8(%rbp), %rdi
addq $0x140, %rdi            # imm = 0x140
callq 0x7190
jmp 0x977d
movq -0x2d8(%rbp), %rax
testb $0x1, 0x138(%rax)
je 0x97f2
movq -0x2d8(%rbp), %rax
movq 0x130(%rax), %rdi
callq 0x9a20
movb %al, -0x2e1(%rbp)
jmp 0x97a8
movb -0x2e1(%rbp), %al
testb $0x1, %al
jne 0x97f2
jmp 0x97b4
movq -0x2d8(%rbp), %rax
movq 0x130(%rax), %rdi
leaq -0x1e0(%rbp), %rsi
callq 0x9a70
jmp 0x97d0
jmp 0x9842
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1e8(%rbp)
movl %eax, -0x1ec(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0x32c0
jmp 0x9858
movq -0x2d8(%rbp), %rdi
addq $0x140, %rdi            # imm = 0x140
callq 0x7010
jmp 0x9807
leaq -0x1e0(%rbp), %rdi
callq 0x3180
movq %rax, %rdi
movq -0x2d8(%rbp), %rax
movq 0x120(%rax), %rsi
callq 0x3500
jmp 0x982b
movq -0x2d8(%rbp), %rdi
addq $0x140, %rdi            # imm = 0x140
callq 0x7190
jmp 0x9840
jmp 0x9842
leaq -0x1e0(%rbp), %rdi
callq 0x32c0
addq $0x308, %rsp            # imm = 0x308
popq %rbx
popq %rbp
retq
movq -0x1e8(%rbp), %rdi
callq 0x3710
nopw %cs:(%rax,%rax)