// File: /workspace/github_cpp_makefile_star2_O1/lszp[P]SmallWebServer/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, '/');  // 在 file_name中搜索最后一次出现 '/'的位置， 找不到返回 NULL
    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);  
        // 将格式化的输入写入字符串log_full_name，255为写入字符的最大数目，超过则截断。
    }
    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 0x6c8f
movl %r9d, %r14d
movq %r13, 0x8(%rsp)
movb $0x1, 0x138(%rbx)
movl $0x70, %edi
callq 0x3420
movq %rax, %r13
movq %rax, %rdi
movl %r14d, %esi
callq 0x7248
movq %r13, 0x130(%rbx)
leaq -0x34ac(%rip), %rdx     # 0x37d0
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x3740
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 0x3260
movl %ebp, 0x108(%rbx)
xorl %edi, %edi
callq 0x33d0
leaq 0x18(%rsp), %rdi
movq %rax, (%rdi)
callq 0x3340
movl 0xc(%rax), %r15d
movl 0x10(%rax), %ebp
movl 0x14(%rax), %r12d
movq %r13, %rdi
movl $0x2f, %esi
callq 0x3490
movq %rax, %r14
leaq 0x20(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x3260
testq %r14, %r14
je 0x6d70
leaq 0x88(%rbx), %rdi
movq %rdi, 0x10(%rsp)
leaq 0x1(%r14), %rsi
callq 0x33f0
movq %r15, 0x8(%rsp)
leaq 0x8(%rbx), %r15
subq %r13, %r14
incq %r14
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x3220
addl $0x76c, %r12d           # imm = 0x76C
incl %ebp
leaq 0x373c(%rip), %rdx      # 0xa485
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 0x3670
jmp 0x6da0
addl $0x76c, %r12d           # imm = 0x76C
incl %ebp
subq $0x8, %rsp
leaq 0x3703(%rip), %rdx      # 0xa487
leaq 0x28(%rsp), %rdi
movl $0xff, %esi
movl %r12d, %ecx
movl %ebp, %r8d
movl %r15d, %r9d
xorl %eax, %eax
pushq %r13
callq 0x3670
addq $0x10, %rsp
movl %r15d, 0x118(%rbx)
leaq 0x32fc(%rip), %rsi      # 0xa0ae
leaq 0x20(%rsp), %rdi
callq 0x34f0
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 0x3430
movq %rbx, %rdi
callq 0x36e0
nop