// File: roberty65[P]bServer/frame/share/src/Log.cpp
// Function: LogHandle::doLog(int, char const*, ...)
int LogHandle::doLog(int level, const char *fmt...)
{
	struct timeval ts;
	struct tm tmbuf, *ptm;
	va_list ap; 
	char buf[8192];

	gettimeofday(&ts, NULL);
	ptm = localtime_r(&ts.tv_sec, &tmbuf);
	int mlen = snprintf(buf, sizeof buf, "%04d-%03d-%02dT%02d:%02d:%02d.%03d %s ",
		ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday,
		ptm->tm_hour, ptm->tm_min, ptm->tm_sec,
		(int)(ts.tv_usec / 1000),
		level >= LOG_LEVEL_FATAL && level <= LOG_LEVEL_DEBUG
			? strLevels[level] : strLevels[0]);
	
	va_start(ap, fmt);
	int slen = vsnprintf(buf + mlen, sizeof(buf) - mlen, fmt, ap);
	if (slen >= (int)(sizeof(buf) - mlen - 2)) {
		/* discard the rest */
		slen = (int)(sizeof(buf) - mlen) - 2;
	}

	mlen += slen;
	buf[mlen++] = '\n';
	buf[mlen] = 0;

	va_end(ap);

	// log to stdout
	if (basePath[0] == '\0' or (basePath[0] == '-' && basePath[1] == '\0')) {
		fprintf(stdout, "%s", buf);
		return 0;
	}

	int fd = open(basePath, O_CREAT | O_APPEND | O_WRONLY, 0664);
	if (fd < 0) return -1;

	ssize_t wlen = write(fd, buf, mlen);
	if (wlen != mlen) {}

	close(fd);

	//
	// write console for fatal log too
	// then exit?
	if (level == LOG_LEVEL_FATAL) fprintf(stderr, "%s", buf);

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x21c0, %rsp           # imm = 0x21C0
testb %al, %al
je 0xbf8a
movaps %xmm0, -0x2140(%rbp)
movaps %xmm1, -0x2130(%rbp)
movaps %xmm2, -0x2120(%rbp)
movaps %xmm3, -0x2110(%rbp)
movaps %xmm4, -0x2100(%rbp)
movaps %xmm5, -0x20f0(%rbp)
movaps %xmm6, -0x20e0(%rbp)
movaps %xmm7, -0x20d0(%rbp)
movq %r9, -0x2148(%rbp)
movq %r8, -0x2150(%rbp)
movq %rcx, -0x2158(%rbp)
movq %rdi, -0x20(%rbp)
movl %esi, -0x24(%rbp)
movq %rdx, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x21a0(%rbp)
leaq -0x40(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x3630
leaq -0x40(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x31c0
movq %rax, -0x80(%rbp)
leaq -0x20a0(%rbp), %rax
movq %rax, -0x2198(%rbp)
movq -0x80(%rbp), %rax
movl 0x14(%rax), %eax
addl $0x76c, %eax            # imm = 0x76C
movl %eax, -0x218c(%rbp)
movq -0x80(%rbp), %rax
movl 0x10(%rax), %eax
addl $0x1, %eax
movl %eax, -0x2188(%rbp)
movq -0x80(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x2184(%rbp)
movq -0x80(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x2180(%rbp)
movq -0x80(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x217c(%rbp)
movq -0x80(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x2178(%rbp)
movq -0x38(%rbp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
movl %eax, -0x2174(%rbp)
cmpl $0x1, -0x24(%rbp)
jl 0xc06e
cmpl $0x5, -0x24(%rbp)
jg 0xc06e
movslq -0x24(%rbp), %rcx
leaq 0xe50f(%rip), %rax      # 0x1a570
movq (%rax,%rcx,8), %rax
movq %rax, -0x21a8(%rbp)
jmp 0xc07c
movq 0xe4fb(%rip), %rax      # 0x1a570
movq %rax, -0x21a8(%rbp)
movl -0x2174(%rbp), %r10d
movl -0x2178(%rbp), %r11d
movl -0x217c(%rbp), %ebx
movl -0x2180(%rbp), %r14d
movl -0x2184(%rbp), %r9d
movl -0x2188(%rbp), %r8d
movl -0x218c(%rbp), %ecx
movq -0x2198(%rbp), %rdi
movq -0x21a8(%rbp), %rax
movl $0x2000, %esi           # imm = 0x2000
leaq 0x741b(%rip), %rdx      # 0x134e0
movl %r14d, (%rsp)
movl %ebx, 0x8(%rsp)
movl %r11d, 0x10(%rsp)
movl %r10d, 0x18(%rsp)
movq %rax, 0x20(%rsp)
movb $0x0, %al
callq 0x3620
movl %eax, -0x20a4(%rbp)
leaq -0xa0(%rbp), %rax
leaq -0x2170(%rbp), %rcx
movq %rcx, 0x10(%rax)
leaq 0x10(%rbp), %rcx
movq %rcx, 0x8(%rax)
movl $0x30, 0x4(%rax)
movl $0x18, (%rax)
leaq -0x20a0(%rbp), %rdi
movslq -0x20a4(%rbp), %rax
addq %rax, %rdi
movslq -0x20a4(%rbp), %rax
movl $0x2000, %esi           # imm = 0x2000
subq %rax, %rsi
movq -0x30(%rbp), %rdx
leaq -0xa0(%rbp), %rcx
callq 0x3240
movl %eax, -0x20a8(%rbp)
movl -0x20a8(%rbp), %eax
movslq -0x20a4(%rbp), %rdx
movl $0x2000, %ecx           # imm = 0x2000
subq %rdx, %rcx
subq $0x2, %rcx
cmpl %ecx, %eax
jl 0xc17b
movslq -0x20a4(%rbp), %rcx
movl $0x2000, %eax           # imm = 0x2000
subq %rcx, %rax
subl $0x2, %eax
movl %eax, -0x20a8(%rbp)
movq -0x21a0(%rbp), %rax
movl -0x20a8(%rbp), %ecx
addl -0x20a4(%rbp), %ecx
movl %ecx, -0x20a4(%rbp)
movl -0x20a4(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x20a4(%rbp)
movslq %ecx, %rcx
movb $0xa, -0x20a0(%rbp,%rcx)
movslq -0x20a4(%rbp), %rcx
movb $0x0, -0x20a0(%rbp,%rcx)
leaq -0xa0(%rbp), %rcx
movsbl 0x18(%rax), %eax
cmpl $0x0, %eax
je 0xc1ef
movq -0x21a0(%rbp), %rax
movsbl 0x18(%rax), %eax
cmpl $0x2d, %eax
jne 0xc21a
movq -0x21a0(%rbp), %rax
movsbl 0x19(%rax), %eax
cmpl $0x0, %eax
jne 0xc21a
movq 0xddb2(%rip), %rax      # 0x19fa8
movq (%rax), %rdi
leaq -0x20a0(%rbp), %rdx
leaq 0x5ea9(%rip), %rsi      # 0x120b0
movb $0x0, %al
callq 0x35a0
movl $0x0, -0x14(%rbp)
jmp 0xc2ba
movq -0x21a0(%rbp), %rdi
addq $0x18, %rdi
movl $0x441, %esi            # imm = 0x441
movl $0x1b4, %edx            # imm = 0x1B4
movb $0x0, %al
callq 0x32b0
movl %eax, -0x20ac(%rbp)
cmpl $0x0, -0x20ac(%rbp)
jge 0xc24e
movl $0xffffffff, -0x14(%rbp) # imm = 0xFFFFFFFF
jmp 0xc2ba
movl -0x20ac(%rbp), %edi
leaq -0x20a0(%rbp), %rsi
movslq -0x20a4(%rbp), %rdx
callq 0x3100
movq %rax, -0x20b8(%rbp)
movq -0x20b8(%rbp), %rax
movslq -0x20a4(%rbp), %rcx
cmpq %rcx, %rax
je 0xc283
jmp 0xc283
movl -0x20ac(%rbp), %edi
callq 0x3710
cmpl $0x1, -0x24(%rbp)
jne 0xc2b3
movq 0xdd3d(%rip), %rax      # 0x19fd8
movq (%rax), %rdi
leaq -0x20a0(%rbp), %rdx
leaq 0x5e04(%rip), %rsi      # 0x120b0
movb $0x0, %al
callq 0x35a0
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
addq $0x21c0, %rsp           # imm = 0x21C0
popq %rbx
popq %r14
popq %rbp
retq
nopl (%rax)