-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.c
205 lines (170 loc) · 4.22 KB
/
debug.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*********************************************************************************
* Copyright:
*
*
* Filename:
* Description:
*
* Version:
* Author: swiftchen
* ChangeLog:
*
********************************************************************************/
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include "logger.h"
/*
* Program name variable is provided by the libc
*/
extern const char* __progname;
#define PROGRAM_NAME __progname
#define FILENAME_LEN 32
#define LOG_ROLLBACK_SIZE 4 /* Default rollback log size */
#define LOG_ROLLBACK_NONE 0 /* Set rollback size to 0 will not rollback */
static unsigned long log_rollback_size = LOG_ROLLBACK_SIZE*1024;
/*
* Logger internal sctructure
*/
typedef struct logger_s {
FILE *fp;
int loglevel;
int use_stdout;
char file[FILENAME_LEN];
} logger_t;
static struct logger_s g_logger;
static const char* LOG_LEVELS[] = {
LOG_STRING_ERROR,
LOG_STRING_WARN,
LOG_STRING_INFO,
LOG_STRING_DEBUG
};
/*
* initial logger system
*/
int logger_init(char *filename, int loglevel)
{
logger_term();
g_logger.loglevel = loglevel>LOG_LEVEL_MAX ? LOG_LEVEL_MAX : loglevel;
/* $filename is NULL or match "stdout" will use standard output */
if( !filename || !strcasecmp(filename, "stdout"))
{
g_logger.use_stdout = 1;
g_logger.fp = stderr;
}
else
{
g_logger.use_stdout = 0;
g_logger.fp = fopen(filename, "a");
if( !g_logger.fp )
{
fprintf(stderr, "Failed to open file '%s': %s", filename, strerror(errno));
return -1;
}
strcpy(g_logger.file, filename);
}
return 0;
}
/*
* terminate logger system
*/
void logger_term(void)
{
if( !g_logger.fp )
{
return ;
}
if( !g_logger.use_stdout )
{
fclose(g_logger.fp);
}
g_logger.use_stdout = 0;
g_logger.fp = NULL;
return ;
}
/*
* Logging functions
*/
void log_generic(const int level, const char* format, va_list args)
{
char message[256];
struct tm* current_tm;
time_t time_now;
vsprintf(message, format, args);
time(&time_now);
current_tm = localtime(&time_now);
int res = fprintf(g_logger.fp,
"%s : %02i:%02i:%02i [%s] %s\n"
, PROGRAM_NAME
, current_tm->tm_hour
, current_tm->tm_min
, current_tm->tm_sec
, LOG_LEVELS[level]
, message );
fflush(g_logger.fp);
}
void log_error(char *format, ...)
{
va_list args;
va_start(args, format);
log_generic(LOG_LEVEL_ERROR, format, args);
va_end(args);
check_and_rollback();
}
void log_warn(char *format, ...)
{
if (g_logger.loglevel < LOG_LEVEL_WARN) {
return;
}
va_list args;
va_start(args, format);
log_generic(LOG_LEVEL_WARN, format, args);
va_end(args);
check_and_rollback();
}
void log_info(char *format, ...)
{
if (g_logger.loglevel < LOG_LEVEL_INFO) {
return;
}
va_list args;
va_start(args, format);
log_generic(LOG_LEVEL_INFO, format, args);
va_end(args);
check_and_rollback();
}
void log_debug(char *format, ...)
{
if (g_logger.loglevel < LOG_LEVEL_DEBUG) {
return;
}
va_list args;
va_start(args, format);
log_generic(LOG_LEVEL_DEBUG, format, args);
va_end(args);
check_and_rollback();
}
static void check_and_rollback(void)
{
if( !g_logger.fp )
return ;
if (log_rollback_size != LOG_ROLLBACK_NONE)
{
long curOffset = ftell(g_logger.fp);
if ((curOffset != -1) && (curOffset >= log_rollback_size))
{
char cmd[512];
snprintf(cmd, sizeof(cmd), "cp -f %s %s.roll", g_logger.file, g_logger.file);
system(cmd);
if (-1 == fseek(g_logger.fp, 0L, SEEK_SET))
fprintf(g_logger.fp, "log rollback fseek failed \n");
rewind(g_logger.fp);
truncate(g_logger.file, 0);
}
}
}