logger.c
1.2 KB
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
#define _ISOC99_SOURCE
#include <syslog.h>
#include <stdio.h>
#include <string.h>
#include "logger.h"
const int priority[] = {
LOG_USER | LOG_EMERG,
LOG_USER | LOG_ALERT,
LOG_USER | LOG_CRIT,
LOG_USER | LOG_ERR,
LOG_USER | LOG_WARNING,
LOG_USER | LOG_NOTICE,
LOG_USER | LOG_INFO,
LOG_USER | LOG_DEBUG
};
INIT_CLASS(LOGGER);
static void
logger_syslog(int level, const char * msg)
{
syslog(priority[level], "%s", msg);
}
__construct(LOGGER)
{
this->logfncts[0] = logger_syslog;
this->logfncts_count = 1;
}
__destruct(LOGGER) {}
__jsonConst(LOGGER) {}
__toJson(LOGGER) {}
__clear(LOGGER) {}
void
logger_log(LOGGER this, int level, const char * message, ...) {
va_list args;
char buffer[1025];
logger_logfnct * logfnct;
int maxBuf = sizeof(buffer)/sizeof(buffer[0]);
memset(buffer, 0, maxBuf);
va_start(args, message);
vsnprintf(buffer, 1024, message, args);
va_end(args);
logfnct = this->logfncts;
while (NULL != *logfnct) {
(*logfnct)(level, buffer);
logfnct++;
}
}
void
logger_add(LOGGER this, logger_logfnct logfunc) {
if (this->logfncts_count < MAX_LOG_FNCTS) {
this->logfncts[this->logfncts_count] = logfunc;
this->logfncts_count++;
}
}
// vim: set ts=4 sw=4: