Subversion Repositories Tewi

Rev

Rev 191 | 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 212 2024-10-02 17:44:55Z 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, ...) {
212 nishi 44
	va_list args;
45
	char namebuf[LOGNAME_LENGTH + 1];
46
	int i;
47
	char* result;
48
	char cbuf[2];
3 nishi 49
	if(!cm_do_log) return;
50
	va_start(args, log);
51
	memset(namebuf, '.', LOGNAME_LENGTH);
52
	namebuf[LOGNAME_LENGTH] = 0;
53
	for(i = 0; name[i] != 0 && i < LOGNAME_LENGTH; i++) {
54
		namebuf[i] = name[i];
55
	}
56
 
212 nishi 57
	result = malloc(1);
3 nishi 58
	result[0] = 0;
59
 
60
	cbuf[1] = 0;
61
 
62
	for(i = 0; log[i] != 0; i++) {
63
		if(log[i] == '%') {
64
			i++;
65
			if(log[i] == 's') {
66
				char* tmp = result;
12 nishi 67
				char* c = va_arg(args, char*);
68
				result = cm_strcat(tmp, c == NULL ? "(null)" : c);
3 nishi 69
				free(tmp);
6 nishi 70
			} else if(log[i] == 'd') {
71
				int a = va_arg(args, int);
72
				char buf[128];
212 nishi 73
				char* tmp = result;
6 nishi 74
				sprintf(buf, "%d", a);
75
				result = cm_strcat(tmp, buf);
76
				free(tmp);
3 nishi 77
			}
78
		} else {
212 nishi 79
			char* tmp = result;
3 nishi 80
			cbuf[0] = log[i];
81
			result = cm_strcat(tmp, cbuf);
82
			free(tmp);
83
		}
84
	}
85
 
182 nishi 86
#ifdef _PSP
87
	pspDebugScreenPrintf("%s %s\n", namebuf, result);
191 nishi 88
#elif defined(__PPU__)
89
	tt_printf("%s %s\n", namebuf, result);
182 nishi 90
#else
62 nishi 91
	fprintf(logfile, "%s %s\n", namebuf, result);
182 nishi 92
#endif
3 nishi 93
	va_end(args);
94
 
95
	free(result);
96
}