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

Last change on this file since 15 was 12, checked in by Nishi, on Sep 13, 2024 at 10:36:03 PM

vhost works

  • Property svn:keywords set to Id
File size: 1.2 KB
Line 
1/* $Id: log.c 12 2024-09-13 13:36:03Z 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 char* c = va_arg(args, char*);
41 result = cm_strcat(tmp, c == NULL ? "(null)" : c);
42 free(tmp);
43 } else if(log[i] == 'd') {
44 int a = va_arg(args, int);
45 char buf[128];
46 sprintf(buf, "%d", a);
47 char* tmp = result;
48 result = cm_strcat(tmp, buf);
49 free(tmp);
50 }
51 } else {
52 cbuf[0] = log[i];
53 char* tmp = result;
54 result = cm_strcat(tmp, cbuf);
55 free(tmp);
56 }
57 }
58
59 fprintf(stderr, "%s %s\n", namebuf, result);
60 va_end(args);
61
62 free(result);
63}
Note: See TracBrowser for help on using the repository browser.