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