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

Last change on this file since 6 was 6, checked in by Nishi, on Sep 13, 2024 at 7:28:20 PM

vhost

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