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

Last change on this file since 5 was 3, checked in by Nishi, on Sep 13, 2024 at 6:06:44 PM

string op works, log works too

  • Property svn:keywords set to Id
File size: 963 bytes
Line 
1/* $Id: log.c 3 2024-09-13 09:06:44Z nishi $ */
2
3#include "cm_log.h"
4
5#include "cm_string.h"
6
7#include <stdio.h>
8#include <stdbool.h>
9#include <string.h>
10#include <stdlib.h>
11#include <stdarg.h>
12
13bool cm_do_log = false;
14
15#define LOGNAME_LENGTH 12
16
17void cm_log(const char* name, const char* log, ...) {
18 if(!cm_do_log) return;
19 va_list args;
20 va_start(args, log);
21 char namebuf[LOGNAME_LENGTH + 1];
22 memset(namebuf, '.', LOGNAME_LENGTH);
23 namebuf[LOGNAME_LENGTH] = 0;
24 int i;
25 for(i = 0; name[i] != 0 && i < LOGNAME_LENGTH; i++) {
26 namebuf[i] = name[i];
27 }
28
29 char* result = malloc(1);
30 result[0] = 0;
31
32 char cbuf[2];
33 cbuf[1] = 0;
34
35 for(i = 0; log[i] != 0; i++) {
36 if(log[i] == '%') {
37 i++;
38 if(log[i] == 's') {
39 char* tmp = result;
40 result = cm_strcat(tmp, va_arg(args, char*));
41 free(tmp);
42 }
43 } else {
44 cbuf[0] = log[i];
45 char* tmp = result;
46 result = cm_strcat(tmp, cbuf);
47 free(tmp);
48 }
49 }
50
51 fprintf(stderr, "%s %s\n", namebuf, result);
52 va_end(args);
53
54 free(result);
55}
Note: See TracBrowser for help on using the repository browser.