// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2128, %rsp           # imm = 0x2128
movq %rdx, 0x8(%rsp)
movl %esi, %r15d
movq %rdi, %rbp
leaq 0x70(%rsp), %r12
movq %rcx, 0x18(%r12)
movq %r8, 0x20(%r12)
movq %r9, 0x28(%r12)
testb %al, %al
je 0x6d8e
movaps %xmm0, 0xa0(%rsp)
movaps %xmm1, 0xb0(%rsp)
movaps %xmm2, 0xc0(%rsp)
movaps %xmm3, 0xd0(%rsp)
movaps %xmm4, 0xe0(%rsp)
movaps %xmm5, 0xf0(%rsp)
movaps %xmm6, 0x100(%rsp)
movaps %xmm7, 0x110(%rsp)
leaq 0x10(%rsp), %r13
movq %r13, %rdi
xorl %esi, %esi
callq 0x35a0
leaq 0x38(%rsp), %rsi
movq %r13, %rdi
callq 0x31a0
movl $0x76c, %ecx            # imm = 0x76C
addl 0x14(%rax), %ecx
movl 0x10(%rax), %r8d
incl %r8d
movl 0xc(%rax), %r9d
movl 0x8(%rax), %r11d
movl (%rax), %r14d
movl 0x4(%rax), %ebx
movq 0x8(%r13), %rax
movl $0x3e8, %esi            # imm = 0x3E8
cqto
idivq %rsi
movq %rax, %r10
leal -0x1(%r15), %eax
cmpl $0x5, %eax
movq %r15, (%rsp)
movl %r15d, %eax
leaq 0x96d3(%rip), %rdx      # 0x104c0
leaq (%rdx,%rax,8), %r15
cmovaeq %rdx, %r15
subq $0x8, %rsp
leaq 0x45e9(%rip), %rdx      # 0xb3e9
leaq 0x128(%rsp), %rdi
movl $0x2000, %r13d          # imm = 0x2000
movl $0x2000, %esi           # imm = 0x2000
movl $0x0, %eax
pushq (%r15)
pushq %r10
pushq %r14
pushq %rbx
pushq %r11
callq 0x3590
addq $0x30, %rsp
movabsq $0x3000000018, %rdx     # imm = 0x3000000018
leaq 0x20(%rsp), %rcx
movq %rdx, (%rcx)
leaq 0x2160(%rsp), %rdx
movq %rdx, 0x8(%rcx)
movq %r12, 0x10(%rcx)
movslq %eax, %rbx
leaq (%rsp,%rbx), %rdi
addq $0x120, %rdi            # imm = 0x120
subq %rbx, %r13
movq %r13, %rsi
movq 0x8(%rsp), %rdx
callq 0x31f0
movl $0x1ffe, %ecx           # imm = 0x1FFE
subl %ebx, %ecx
addl $-0x2, %r13d
cmpl %ecx, %eax
cmovll %eax, %r13d
addl %ebx, %r13d
movslq %r13d, %r12
movw $0xa, 0x120(%rsp,%r12)
movzbl 0x18(%rbp), %eax
testl %eax, %eax
je 0x6efb
cmpl $0x2d, %eax
jne 0x6ea0
cmpb $0x0, 0x19(%rbp)
je 0x6efb
addq $0x18, %rbp
xorl %ebx, %ebx
movq %rbp, %rdi
movl $0x441, %esi            # imm = 0x441
movl $0x1b4, %edx            # imm = 0x1B4
xorl %eax, %eax
callq 0x3250
testl %eax, %eax
js 0x6f16
movl %eax, %ebp
incq %r12
leaq 0x120(%rsp), %rsi
movl %eax, %edi
movq %r12, %rdx
callq 0x30f0
movl %ebp, %edi
callq 0x3660
cmpl $0x1, (%rsp)
jne 0x6f19
movq 0x90ef(%rip), %rax      # 0xffd8
movq (%rax), %rsi
leaq 0x120(%rsp), %rdi
callq 0x33c0
jmp 0x6f19
movq 0x90a6(%rip), %rax      # 0xffa8
movq (%rax), %rsi
leaq 0x120(%rsp), %rdi
callq 0x33c0
xorl %ebx, %ebx
jmp 0x6f19
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0x2128, %rsp           # imm = 0x2128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)