Subversion Repositories Tewi

Rev

Rev 182 | Rev 212 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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