// File: /workspace/github_cpp_makefile_star2_O1/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 0x7384
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 0x3570
leaq 0x38(%rsp), %rsi
movq %r13, %rdi
callq 0x3190
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
movabsq $0x20c49ba5e353f7cf, %rax # imm = 0x20C49BA5E353F7CF
imulq 0x8(%r13)
movq %rdx, %r10
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0x7, %r10
addl %eax, %r10d
leal -0x1(%r15), %eax
cmpl $0x5, %eax
movq %r15, (%rsp)
movl %r15d, %eax
leaq 0x90af(%rip), %rdx      # 0x104a0
leaq (%rdx,%rax,8), %r15
cmovaeq %rdx, %r15
subq $0x8, %rsp
leaq 0x3fe5(%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 0x3560
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 0x31e0
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 0x74ff
cmpl $0x2d, %eax
jne 0x74a4
cmpb $0x0, 0x19(%rbp)
je 0x74ff
addq $0x18, %rbp
xorl %ebx, %ebx
movq %rbp, %rdi
movl $0x441, %esi            # imm = 0x441
movl $0x1b4, %edx            # imm = 0x1B4
xorl %eax, %eax
callq 0x3240
testl %eax, %eax
js 0x751a
movl %eax, %ebp
incq %r12
leaq 0x120(%rsp), %rsi
movl %eax, %edi
movq %r12, %rdx
callq 0x30f0
movl %ebp, %edi
callq 0x3630
cmpl $0x1, (%rsp)
jne 0x751f
movq 0x8aeb(%rip), %rax      # 0xffd8
movq (%rax), %rsi
leaq 0x120(%rsp), %rdi
callq 0x33b0
jmp 0x751f
movq 0x8aa2(%rip), %rax      # 0xffa8
movq (%rax), %rsi
leaq 0x120(%rsp), %rdi
callq 0x33b0
xorl %ebx, %ebx
jmp 0x751f
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x2128, %rsp           # imm = 0x2128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop