// File: /workspace/github_cpp_makefile_star2_O3/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 0x62df
movl %r9d, %r14d
movq %r13, 0x8(%rsp)
movb $0x1, 0x138(%rbx)
movl $0x70, %edi
callq 0x3390
movq %rax, %r13
movq %rax, %rdi
movl %r14d, %esi
callq 0x68b8
movq %r13, 0x130(%rbx)
leaq -0x2bfc(%rip), %rdx     # 0x36d0
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x3640
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 0x3220
movl %ebp, 0x108(%rbx)
xorl %edi, %edi
callq 0x3360
leaq 0x18(%rsp), %rdi
movq %rax, (%rdi)
callq 0x32f0
movl 0xc(%rax), %r15d
movl 0x10(%rax), %ebp
movl 0x14(%rax), %r12d
movq %r13, %rdi
movl $0x2f, %esi
callq 0x33f0
movq %rax, %r14
leaq 0x20(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x3220
testq %r14, %r14
je 0x63c0
leaq 0x88(%rbx), %rdi
movq %rdi, 0x10(%rsp)
leaq 0x1(%r14), %rsi
callq 0x3370
movq %r15, 0x8(%rsp)
leaq 0x8(%rbx), %r15
subq %r13, %r14
incq %r14
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x31e0
addl $0x76c, %r12d           # imm = 0x76C
incl %ebp
leaq 0x304c(%rip), %rdx      # 0x93e5
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 0x3570
jmp 0x63f0
addl $0x76c, %r12d           # imm = 0x76C
incl %ebp
subq $0x8, %rsp
leaq 0x3013(%rip), %rdx      # 0x93e7
leaq 0x28(%rsp), %rdi
movl $0xff, %esi
movl %r12d, %ecx
movl %ebp, %r8d
movl %r15d, %r9d
xorl %eax, %eax
pushq %r13
callq 0x3570
addq $0x10, %rsp
movl %r15d, 0x118(%rbx)
leaq 0x2ff5(%rip), %rsi      # 0x93f7
leaq 0x20(%rsp), %rdi
callq 0x3440
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
movl $0x70, %esi
movq %r13, %rdi
callq 0x33a0
movq %rbx, %rdi
callq 0x35e0
nop