// File: /workspace/github_cpp_makefile_star2_O2/JianjiaoSQ[P]MyPoorWebServer/log/log.cpp
// Function: Log::init(char const*, int, int, int, int)
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);
    }
    
    m_close_log = close_log;
    m_log_buf_size = log_buf_size;
    m_buf = new char[m_log_buf_size];
    memset(m_buf, '\0', m_log_buf_size);
    m_split_lines = split_lines;

    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);
    }
    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);
    }

    m_today = my_tm.tm_mday;
    
    m_fp = fopen(log_full_name, "a");
    if (m_fp == NULL)
    {
        return false;
    }

    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movl %r8d, %ebp
movl %ecx, %r15d
movl %edx, %r12d
movq %rsi, %r13
movq %rdi, %rbx
testl %r9d, %r9d
jle 0x5d27
movl %r9d, %r14d
movq %r13, 0x8(%rsp)
movb $0x1, 0x138(%rbx)
pushq $0x70
popq %rdi
callq 0x33c0
movq %rax, %r13
movq %rax, %rdi
movl %r14d, %esi
callq 0x6278
movq %r13, 0x130(%rbx)
leaq 0x5fc(%rip), %rdx       # 0x6310
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x3690
movq 0x8(%rsp), %r13
movl %r12d, 0x168(%rbx)
movl %r15d, 0x10c(%rbx)
movslq %r15d, %r14
movq %r14, %rdi
callq 0x3030
movq %rax, 0x128(%rbx)
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x3240
movl %ebp, 0x108(%rbx)
xorl %edi, %edi
callq 0x3390
leaq 0x18(%rsp), %rdi
movq %rax, (%rdi)
callq 0x3310
movl 0xc(%rax), %r15d
movl 0x10(%rax), %ebp
movl 0x14(%rax), %r12d
pushq $0x2f
popq %rsi
movq %r13, %rdi
callq 0x3420
movq %rax, %r14
leaq 0x20(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x3240
testq %r14, %r14
je 0x5e06
leaq 0x88(%rbx), %rdi
movq %rdi, 0x10(%rsp)
leaq 0x1(%r14), %rsi
callq 0x33a0
movq %r15, 0x8(%rsp)
leaq 0x8(%rbx), %r15
subq %r13, %r14
incq %r14
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x3200
addl $0x76c, %r12d           # imm = 0x76C
incl %ebp
leaq 0x2606(%rip), %rdx      # 0x83e5
leaq 0x20(%rsp), %rdi
movl $0xff, %esi
movq %r15, %rcx
movq 0x8(%rsp), %r15
movl %r12d, %r8d
movl %ebp, %r9d
xorl %eax, %eax
pushq 0x10(%rsp)
pushq %r15
callq 0x35b0
jmp 0x5e36
addl $0x76c, %r12d           # imm = 0x76C
incl %ebp
subq $0x8, %rsp
leaq 0x25cd(%rip), %rdx      # 0x83e7
leaq 0x28(%rsp), %rdi
movl $0xff, %esi
movl %r12d, %ecx
movl %ebp, %r8d
movl %r15d, %r9d
xorl %eax, %eax
pushq %r13
callq 0x35b0
popq %rcx
popq %rdx
movl %r15d, 0x118(%rbx)
leaq 0x25b1(%rip), %rsi      # 0x83f7
leaq 0x20(%rsp), %rdi
callq 0x3470
movq %rax, 0x120(%rbx)
testq %rax, %rax
setne %al
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x70
popq %rsi
movq %r13, %rdi
callq 0x33d0
movq %rbx, %rdi
callq 0x3630
nop