Subversion Repositories Tewi

Rev

Rev 212 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 nishi 1
/* $Id: log.c 253 2024-10-04 03:13:36Z nishi $ */
2
 
3
#include "cm_log.h"
4
 
253 nishi 5
#include "../config.h"
3 nishi 6
#include "cm_string.h"
7
 
62 nishi 8
#include <time.h>
3 nishi 9
#include <stdio.h>
10
#include <stdbool.h>
11
#include <string.h>
12
#include <stdlib.h>
13
#include <stdarg.h>
14
 
182 nishi 15
#ifdef _PSP
16
#include <pspdebug.h>
17
#endif
18
 
191 nishi 19
#ifdef __PPU__
20
extern void tt_printf(const char* tmpl, ...);
21
#endif
22
 
62 nishi 23
FILE* logfile;
24
 
3 nishi 25
bool cm_do_log = false;
26
 
27
#define LOGNAME_LENGTH 12
28
 
253 nishi 29
#ifdef BUILD_GUI_VALID
30
void AddLog(const char* str);
31
#endif
32
 
62 nishi 33
void cm_force_log(const char* log) {
34
	time_t t = time(NULL);
35
	struct tm* tm = localtime(&t);
36
	char date[513];
253 nishi 37
	char* str;
62 nishi 38
	strftime(date, 512, "%a %b %d %H:%M:%S %Z %Y", tm);
182 nishi 39
#ifdef _PSP
40
	pspDebugScreenPrintf("[%s] %s\n", date, log);
191 nishi 41
#elif defined(__PPU__)
42
	tt_printf("[%s] %s\n", date, log);
253 nishi 43
#elif defined(BUILD_GUI_VALID)
44
	str = malloc(strlen(date) + strlen(log) + 3 + 1);
45
	str[strlen(date) + strlen(log) + 3] = 0;
46
	sprintf(str, "[%s] %s", date, log);
47
	AddLog(str);
48
	free(str);
182 nishi 49
#else
62 nishi 50
	fprintf(logfile, "[%s] %s\n", date, log);
253 nishi 51
	fflush(logfile);
182 nishi 52
#endif
62 nishi 53
}
54
 
3 nishi 55
void cm_log(const char* name, const char* log, ...) {
212 nishi 56
	va_list args;
57
	char namebuf[LOGNAME_LENGTH + 1];
58
	int i;
59
	char* result;
60
	char cbuf[2];
3 nishi 61
	if(!cm_do_log) return;
62
	va_start(args, log);
63
	memset(namebuf, '.', LOGNAME_LENGTH);
64
	namebuf[LOGNAME_LENGTH] = 0;
65
	for(i = 0; name[i] != 0 && i < LOGNAME_LENGTH; i++) {
66
		namebuf[i] = name[i];
67
	}
68
 
212 nishi 69
	result = malloc(1);
3 nishi 70
	result[0] = 0;
71
 
72
	cbuf[1] = 0;
73
 
74
	for(i = 0; log[i] != 0; i++) {
75
		if(log[i] == '%') {
76
			i++;
77
			if(log[i] == 's') {
78
				char* tmp = result;
12 nishi 79
				char* c = va_arg(args, char*);
80
				result = cm_strcat(tmp, c == NULL ? "(null)" : c);
3 nishi 81
				free(tmp);
6 nishi 82
			} else if(log[i] == 'd') {
83
				int a = va_arg(args, int);
84
				char buf[128];
212 nishi 85
				char* tmp = result;
6 nishi 86
				sprintf(buf, "%d", a);
87
				result = cm_strcat(tmp, buf);
88
				free(tmp);
3 nishi 89
			}
90
		} else {
212 nishi 91
			char* tmp = result;
3 nishi 92
			cbuf[0] = log[i];
93
			result = cm_strcat(tmp, cbuf);
94
			free(tmp);
95
		}
96
	}
97
 
182 nishi 98
#ifdef _PSP
99
	pspDebugScreenPrintf("%s %s\n", namebuf, result);
191 nishi 100
#elif defined(__PPU__)
101
	tt_printf("%s %s\n", namebuf, result);
182 nishi 102
#else
62 nishi 103
	fprintf(logfile, "%s %s\n", namebuf, result);
182 nishi 104
#endif
3 nishi 105
	va_end(args);
106
 
107
	free(result);
108
}