-
Notifications
You must be signed in to change notification settings - Fork 110
/
logging.c
64 lines (53 loc) · 1.55 KB
/
logging.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
/*
* Copyright (c) 2010, Gerard Lledó Vives, [email protected]
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. See README and COPYING for
* more details.
*/
#include <stdio.h>
#include <stdarg.h>
#include <pthread.h>
#include "logging.h"
static int loglevel = DEFAULT_LOG_LEVEL;
static FILE *logfile = NULL;
static const char *loglevel_str[] = {
[LOG_EMERG] = "[emerg]",
[LOG_ALERT] = "[alert]",
[LOG_CRIT] = "[crit] ",
[LOG_ERR] = "[err] ",
[LOG_WARNING] = "[warn] ",
[LOG_NOTICE] = "[notic]",
[LOG_INFO] = "[info] ",
[LOG_DEBUG] = "[debug]",
};
void __LOG(int level, const char *func, int line, const char *format, ...)
{
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
va_list ap;
if (!logfile) return;
if (level < 0) return;
if (level > loglevel) return;
/* We have a lock here so different threads don interleave the log output */
pthread_mutex_lock(&lock);
va_start(ap, format);
fprintf(logfile, "%s[%s:%d] ", loglevel_str[level], func, line);
vfprintf(logfile, format, ap);
fprintf(logfile, "\n");
va_end(ap);
pthread_mutex_unlock(&lock);
}
void logging_setlevel(int new_level)
{
loglevel = new_level;
}
int logging_open(const char *path)
{
if (path == NULL) return 0;
if ((logfile = fopen(path, "w")) == NULL) {
perror("open");
return -1;
}
return 0;
}