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

Last change on this file since 206 was 191, checked in by Nishi, on Sep 29, 2024 at 2:11:20 PM

console works on ps3

  • Property svn:keywords set to Id
File size: 1.8 KB
Line 
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
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 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;
66 char* c = va_arg(args, char*);
67 result = cm_strcat(tmp, c == NULL ? "(null)" : c);
68 free(tmp);
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);
76 }
77 } else {
78 cbuf[0] = log[i];
79 char* tmp = result;
80 result = cm_strcat(tmp, cbuf);
81 free(tmp);
82 }
83 }
84
85#ifdef _PSP
86 pspDebugScreenPrintf("%s %s\n", namebuf, result);
87#elif defined(__PPU__)
88 tt_printf("%s %s\n", namebuf, result);
89#else
90 fprintf(logfile, "%s %s\n", namebuf, result);
91#endif
92 va_end(args);
93
94 free(result);
95}
Note: See TracBrowser for help on using the repository browser.