-
Notifications
You must be signed in to change notification settings - Fork 48
/
logger.cc
84 lines (68 loc) · 1.6 KB
/
logger.cc
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
75
76
77
78
79
80
81
82
83
84
/*
* logging utilities
*/
#include <stdarg.h>
#include <stdio.h> /* vsnprintf */
#include <string.h> /* memset */
#include "logger.hh"
#define BUFSIZE 1024
static char buf[BUFSIZE + 1];
static int init = 0;
void initlogger();
void
initlogger()
{
if (init != 0)
return;
// FIXME
// this is lame; make new build system and fix this.
// should be using -DDEBUG=0 or -DDEBUG=1 and then use
// if () or #if instead of #ifdef
#if !(DEBUG)
// not debug build ==> no debug messages
setlogmask(LOG_UPTO(LOG_INFO));
#endif
openlog("ViewTouch ", LOG_PERROR | LOG_PID, LOG_USER);
init = 1;
}
void
setident(const char* ident)
{
closelog();
if (init == 0)
initlogger();
openlog(ident ? "VT" : ident, LOG_PERROR | LOG_PID, LOG_USER);
}
/*
* return: 0 : success
* -1 : message truncated (didn't fit into buffer)
* -2 : error returned by vsnprintf(3)
*/
int
logmsg(int priority, const char* fmt, ...)
{
va_list ap;
int retval = 0;
if (init == 0)
initlogger();
memset(buf, 0, sizeof(buf));
va_start(ap, fmt);
retval = vsnprintf(buf, BUFSIZE, fmt, ap);
va_end(ap);
// ensure termination
buf[BUFSIZE] = '\0';
if (retval >= BUFSIZE) {
retval = -1;
// ellipsis at the end because we truncated
buf[BUFSIZE - 1] = buf[BUFSIZE - 2] = buf[BUFSIZE - 3] = '.';
} else if (retval < 0)
retval = -2;
else
retval = 0;
// made the string -- log it
// (even if it was truncated)
if (retval >= -1)
syslog(priority, "%s",buf);
return retval;
}
/* done */