// File: 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
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x198(%rbp)
cmpl $0x1, -0x28(%rbp)
jl 0x9111
movq -0x198(%rbp), %rax
movb $0x1, 0x138(%rax)
movl $0x70, %edi
callq 0x3450
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x1a0(%rbp)
movl -0x28(%rbp), %esi
callq 0x98c0
jmp 0x90c1
movq -0x198(%rbp), %rax
movq -0x1a0(%rbp), %rcx
movq %rcx, 0x130(%rax)
leaq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %ecx
leaq 0x91b(%rip), %rdx       # 0x9a00
movq %rcx, %rsi
callq 0x3780
jmp 0x9111
movq -0x1a0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
movl $0x70, %esi
callq 0x3460
jmp 0x92f1
movq -0x198(%rbp), %rax
movl -0x1c(%rbp), %ecx
movl %ecx, 0x168(%rax)
movl -0x20(%rbp), %ecx
movl %ecx, 0x10c(%rax)
movslq 0x10c(%rax), %rdi
callq 0x3030
movq %rax, %rcx
movq -0x198(%rbp), %rax
movq %rcx, 0x128(%rax)
movq 0x128(%rax), %rdi
movslq 0x10c(%rax), %rdx
xorl %esi, %esi
callq 0x3270
movq -0x198(%rbp), %rax
movl -0x24(%rbp), %ecx
movl %ecx, 0x108(%rax)
xorl %eax, %eax
movl %eax, %edi
callq 0x3420
movq %rax, -0x48(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x3370
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rsi
leaq -0x88(%rbp), %rdi
movl $0x38, %edx
callq 0x33c0
movq -0x18(%rbp), %rdi
movl $0x2f, %esi
callq 0x34f0
movq %rax, -0x90(%rbp)
leaq -0x190(%rbp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x3270
cmpq $0x0, -0x90(%rbp)
jne 0x9209
leaq -0x190(%rbp), %rdi
movl -0x74(%rbp), %ecx
addl $0x76c, %ecx            # imm = 0x76C
movl -0x78(%rbp), %r8d
addl $0x1, %r8d
movl -0x7c(%rbp), %r9d
movq -0x18(%rbp), %rax
movl $0xff, %esi
leaq 0x51e6(%rip), %rdx      # 0xe3df
movq %rax, (%rsp)
movb $0x0, %al
callq 0x3690
jmp 0x929b
movq -0x198(%rbp), %rdi
addq $0x88, %rdi
movq -0x90(%rbp), %rsi
addq $0x1, %rsi
callq 0x3430
movq -0x198(%rbp), %rdi
addq $0x8, %rdi
movq -0x18(%rbp), %rsi
movq -0x90(%rbp), %rdx
movq -0x18(%rbp), %rax
subq %rax, %rdx
addq $0x1, %rdx
callq 0x3240
movq -0x198(%rbp), %rax
leaq -0x190(%rbp), %rdi
movq %rax, %rcx
addq $0x8, %rcx
movl -0x74(%rbp), %r8d
addl $0x76c, %r8d            # imm = 0x76C
movl -0x78(%rbp), %r9d
addl $0x1, %r9d
movl -0x7c(%rbp), %r10d
addq $0x88, %rax
movl $0xff, %esi
leaq 0x5152(%rip), %rdx      # 0xe3dd
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x3690
movq -0x198(%rbp), %rax
movl -0x7c(%rbp), %ecx
movl %ecx, 0x118(%rax)
leaq -0x190(%rbp), %rdi
leaq 0x4df2(%rip), %rsi      # 0xe0ab
callq 0x3540
movq %rax, %rcx
movq -0x198(%rbp), %rax
movq %rcx, 0x120(%rax)
cmpq $0x0, 0x120(%rax)
jne 0x92df
movb $0x0, -0x1(%rbp)
jmp 0x92e3
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x3710
nopw (%rax,%rax)