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