-
Notifications
You must be signed in to change notification settings - Fork 0
/
kssl_log.c
74 lines (59 loc) · 1.6 KB
/
kssl_log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// kssl_log.c: logging support functions
//
// Copyright (c) 2013 CloudFlare, Inc.
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "kssl_log.h"
#if PLATFORM_WINDOWS == 0
#include <syslog.h>
#endif
int silent = 0;
int verbose = 0;
#if PLATFORM_WINDOWS == 0
int use_syslog = 0;
#endif
// write_log: call to log a message. If syslog is not enabled then error
// message are written to STDERR, other messages are written to STDOUT. If
// syslog is enabled then error messages are sent with LOG_ERR, other messages
// with LOG_INFO. syslog messages are sent with the LOG_USER facility.
void write_log(int e, // If set this is an error message
const char *fmt, ...) // printf style
{
// Note the use of [] here. When syslogging, syslog will strip them off and
// create a message using that as the name of the program.
char * name = "[kssl_server] ";
char * newfmt;
va_list l;
#if PLATFORM_WINDOWS == 0
if (silent && !use_syslog) {
return;
}
#endif
if (!e && !verbose) {
return;
}
// +1 for the terminating 0
// +1 for the \n we append in non-syslog mode
newfmt = (char *)malloc(strlen(fmt)+1+strlen(name)+1);
if (newfmt == NULL) {
return;
}
strcpy(newfmt, name);
strcat(newfmt, fmt);
va_start(l, fmt);
// Note the syntax abuse below. Be careful to look at the dandling
// } else
#if PLATFORM_WINDOWS == 0
if (use_syslog) {
vsyslog(LOG_USER | (e?LOG_ERR:LOG_INFO), newfmt, l);
} else
#endif
{
strcat(newfmt, "\n");
vfprintf(e?stderr:stdout, newfmt, l);
}
va_end(l);
free(newfmt);
}