source: Main/trunk/Common/log.c@ 215

Last change on this file since 215 was 212, checked in by Nishi, on Oct 3, 2024 at 2:44:55 AM

compiles on vc6

  • Property svn:keywords set to Id
File size: 1.8 KB
Line 
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
7#include <time.h>
8#include <stdio.h>
9#include <stdbool.h>
10#include <string.h>
11#include <stdlib.h>
12#include <stdarg.h>
13
14#ifdef _PSP
15#include <pspdebug.h>
16#endif
17
18#ifdef __PPU__
19extern void tt_printf(const char* tmpl, ...);
20#endif
21
22FILE* logfile;
23
24bool cm_do_log = false;
25
26#define LOGNAME_LENGTH 12
27
28void 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);
33#ifdef _PSP
34 pspDebugScreenPrintf("[%s] %s\n", date, log);
35#elif defined(__PPU__)
36 tt_printf("[%s] %s\n", date, log);
37#else
38 fprintf(logfile, "[%s] %s\n", date, log);
39#endif
40 fflush(logfile);
41}
42
43void cm_log(const char* name, const char* log, ...) {
44 va_list args;
45 char namebuf[LOGNAME_LENGTH + 1];
46 int i;
47 char* result;
48 char cbuf[2];
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
57 result = malloc(1);
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;
67 char* c = va_arg(args, char*);
68 result = cm_strcat(tmp, c == NULL ? "(null)" : c);
69 free(tmp);
70 } else if(log[i] == 'd') {
71 int a = va_arg(args, int);
72 char buf[128];
73 char* tmp = result;
74 sprintf(buf, "%d", a);
75 result = cm_strcat(tmp, buf);
76 free(tmp);
77 }
78 } else {
79 char* tmp = result;
80 cbuf[0] = log[i];
81 result = cm_strcat(tmp, cbuf);
82 free(tmp);
83 }
84 }
85
86#ifdef _PSP
87 pspDebugScreenPrintf("%s %s\n", namebuf, result);
88#elif defined(__PPU__)
89 tt_printf("%s %s\n", namebuf, result);
90#else
91 fprintf(logfile, "%s %s\n", namebuf, result);
92#endif
93 va_end(args);
94
95 free(result);
96}
Note: See TracBrowser for help on using the repository browser.