-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.c
More file actions
executable file
·110 lines (91 loc) · 2.12 KB
/
Logger.c
File metadata and controls
executable file
·110 lines (91 loc) · 2.12 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
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
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
#include "Globals.h"
#include "Logger.h"
// $Id$
// $Name$
// $ProjectName$
//
#include <stdio.h>
#include "Buffer.h"
/*
* @Author: Hannes de Waal (c) 2006
*
* @Description:
* Logger module
*
* @log:
* 18 May 2004 - version 1.0
*
*
*/
char * log_file;
int param_log_level;
char *param_exclude_msg_source;
Logger* Logger_new()
{
return (Logger*) Malloc(sizeof(Logger));
}
int
Logger_log (const char *file, char *func, int l, int level, char *fmt, ...)
{
char msg[255];
va_list a;
va_start (a, fmt);
vsprintf (msg, fmt, a);
va_end (a);
FILE *fd;
char *levels[5] = { "DEBUG",
"INFO ",
"WARN ",
"ERROR",
"FATAL"
};
//2004-05-18 13:28:16,218 [main] DEBUG n1aa_server
time_t tcurrent;
struct tm ts;
char str[81];
char *pstr;
pstr = (char *) &str;
time (&tcurrent);
ts = *localtime (&tcurrent);
strftime (pstr, sizeof (str) - 1, "%Y-%m-%d %H:%M:%S", &ts);
if (log_file == NULL) {
fprintf (stderr, "%s, %s[%d] [%s()] %s[%d] - %s\n", pstr, levels[level],
getpid (), func, file, l, msg);
return 0;
}
fd = (FILE *) fopen (log_file, "a+");
if (fd == NULL)
{
// log error
fprintf (stderr, "Logger.c Can't open log file [%s], ", log_file);
perror ("open");
return FILE_OPEN_ERROR;
}
// fprintf(stderr,"%s, %d [%s()] %s %s - %s\n", pstr ,l,func,levels[level] ,file,msg);
if (level >= 4 - arg_verbose
&& (strstr (param_exclude_msg_source, file) == NULL))
fprintf (stderr, "%s, %s[%d] [%s()] %s[%d] - %s\n", pstr, levels[level],
getpid (), func, file, l, msg);
// fprintf(log_file,"%s, %d [%s()] %s %s - %s\n", pstr ,l,func,levels[level] ,file,msg);
if (level >= param_log_level
&& (strstr (param_exclude_msg_source, file) == NULL))
fprintf (fd, "%s, %s[%d] [%s()] %s[%d] - %s\n", pstr, levels[level],
getpid (), func, file, l, msg);
fclose (fd);
return 0;
}
#ifdef TEST_LOGGER
int
main (){
log(DEBUG, "hello");
loge( "hello %d",1);
logw( "hello");
logd( "hello");
logi( "hello");
logf( "hello");
}
#endif