-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
161 lines (139 loc) · 3.22 KB
/
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Since the point of this filesystem is to learn FUSE and its
// datastructures, I want to see *everything* that happens related to
// its data structures. This file contains macros and functions to
// accomplish this.
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/file.h>
#include "log.h"
#define SHOULD_LOG(_ll) (logging_on && _ll >= g_log_filtering_level)
static FILE *log_file = NULL;
static int logging_on = FALSE;
int g_log_filtering_level = 0;
/* Space is left over for additional names to be added at
* run-time by users. This hasn't been implemented
*/
const char _level_names[10][10] = {"DEBUG", "INFO", "WARN", "ERROR"};
void log_open(const char *name, int log_filter)
{
log_open0(fopen(name, "w"), log_filter);
}
void log_open0(FILE *f, int log_filter)
{
if (f == NULL)
{
perror("logfile");
exit(EXIT_FAILURE);
}
log_file = f;
setvbuf(log_file, NULL, _IOLBF, 0); // set to line buffering
logging_on = 1;
g_log_filtering_level = log_filter;
log_msg("============LOG_START===========\n");
}
void log_close()
{
log_msg("=============LOG_END============\n");
if (logging_on)
{
fclose(log_file);
}
logging_on = 0;
}
void log_msg0 (int log_level, const char *format, ...)
{
if (!SHOULD_LOG(log_level))
return;
va_list ap;
va_start(ap, format);
vlog_msg0(log_level, format, ap);
}
void vlog_msg0 (int log_level, const char *format, va_list ap)
{
/* this is the only method that does any real
* writing to the log file
*/
if (!SHOULD_LOG(log_level))
return;
vfprintf(log_file, format, ap);
}
void log_msg1 (int log_level, const char *file, int line_number, const char *format, ...)
{
#ifndef NO_LOGGING
/* Does some extra things like print the log level
* and line number
*/
if (!SHOULD_LOG(log_level))
return;
va_list ap;
va_start(ap, format);
lock_log();
log_msg0(log_level, "%s:%s:%d:", _level_names[log_level], file, line_number);
vlog_msg0(log_level, format, ap);
log_msg0(log_level, "\n");
unlock_log();
#endif
}
void _lock_log (int operation)
{
if (logging_on)
{
int fd = fileno(log_file);
if (fd > 0)
flock(fd, operation);
}
}
void lock_log ()
{
_lock_log(LOCK_EX);
}
void unlock_log ()
{
_lock_log(LOCK_UN);
}
int log_error (const char *str)
{
int ret = -errno;
error("%s: %s\n", str, strerror(errno));
return ret;
}
void log_pair (gpointer key, gpointer val, gpointer not_used)
{
log_msg("%p=>%p ", key, val);
}
void log_hash (GHashTable *hsh)
{
lock_log();
log_msg("{");
if (hsh != NULL)
g_hash_table_foreach(hsh, log_pair, NULL);
log_msg("}\n");
unlock_log();
}
void log_list (GList *l)
{
log_msg("(");
if (l != NULL)
{
log_msg("%p", l->data);
l = g_list_next(l);
while (l != NULL)
{
log_msg(" %p", l->data);
l = g_list_next(l);
}
}
log_msg(")\n");
}
void set_log_filter (int filter_level)
{
g_log_filtering_level = filter_level;
}
const char *log_level_name(int i)
{
return _level_names[i];
}