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

Last change on this file since 367 was 253, checked in by Nishi, on Oct 4, 2024 at 12:13:36 PM

fix stuff

  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
1/* $Id: log.c 253 2024-10-04 03:13:36Z nishi $ */
2
3#include "cm_log.h"
4
5#include "../config.h"
6#include "cm_string.h"
7
8#include <time.h>
9#include <stdio.h>
10#include <stdbool.h>
11#include <string.h>
12#include <stdlib.h>
13#include <stdarg.h>
14
15#ifdef _PSP
16#include <pspdebug.h>
17#endif
18
19#ifdef __PPU__
20extern void tt_printf(const char* tmpl, ...);
21#endif
22
23FILE* logfile;
24
25bool cm_do_log = false;
26
27#define LOGNAME_LENGTH 12
28
29#ifdef BUILD_GUI_VALID
30void AddLog(const char* str);
31#endif
32
33void cm_force_log(const char* log) {
34 time_t t = time(NULL);
35 struct tm* tm = localtime(&t);
36 char date[513];
37 char* str;
38 strftime(date, 512, "%a %b %d %H:%M:%S %Z %Y", tm);
39#ifdef _PSP
40 pspDebugScreenPrintf("[%s] %s\n", date, log);
41#elif defined(__PPU__)
42 tt_printf("[%s] %s\n", date, log);
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);
49#else
50 fprintf(logfile, "[%s] %s\n", date, log);
51 fflush(logfile);
52#endif
53}
54
55void cm_log(const char* name, const char* log, ...) {
56 va_list args;
57 char namebuf[LOGNAME_LENGTH + 1];
58 int i;
59 char* result;
60 char cbuf[2];
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
69 result = malloc(1);
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;
79 char* c = va_arg(args, char*);
80 result = cm_strcat(tmp, c == NULL ? "(null)" : c);
81 free(tmp);
82 } else if(log[i] == 'd') {
83 int a = va_arg(args, int);
84 char buf[128];
85 char* tmp = result;
86 sprintf(buf, "%d", a);
87 result = cm_strcat(tmp, buf);
88 free(tmp);
89 }
90 } else {
91 char* tmp = result;
92 cbuf[0] = log[i];
93 result = cm_strcat(tmp, cbuf);
94 free(tmp);
95 }
96 }
97
98#ifdef _PSP
99 pspDebugScreenPrintf("%s %s\n", namebuf, result);
100#elif defined(__PPU__)
101 tt_printf("%s %s\n", namebuf, result);
102#else
103 fprintf(logfile, "%s %s\n", namebuf, result);
104#endif
105 va_end(args);
106
107 free(result);
108}
Note: See TracBrowser for help on using the repository browser.