-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.c
127 lines (105 loc) · 2.86 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
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2016 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: maben <[email protected]> |
+----------------------------------------------------------------------+
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include "log.h"
static char *cfc_error_titles[CFC_LOG_LEVEL_ERROR + 1] = {
"DEBUG", "NOTICE", "WARN", "ERROR"
};
static int cfc_log_fd = 0;
static int cfc_log_mark = CFC_LOG_LEVEL_DEBUG;
static int cfc_log_initialize = 0;
static char cfc_log_buffer[4096];
int cfc_init_log(char *file, int mark)
{
if (cfc_log_initialize) {
return 0;
}
if (mark < CFC_LOG_LEVEL_DEBUG
|| mark > CFC_LOG_LEVEL_ERROR)
{
return -1;
}
if (file) {
cfc_log_fd = open(file, O_WRONLY | O_CREAT | O_APPEND , 0666);
if (!cfc_log_fd) {
return -1;
}
} else {
dup2(cfc_log_fd, STDERR_FILENO);
}
cfc_log_mark = mark;
cfc_log_initialize = 1;
return 0;
}
void cfc_log(int level, char *fmt, ...)
{
va_list al;
time_t current;
struct tm *dt;
int off1, off2;
if (!cfc_log_initialize
|| level < cfc_log_mark
|| level > CFC_LOG_LEVEL_ERROR)
{
return;
}
/* Get current date and time */
time(¤t);
dt = localtime(¤t);
off1 = sprintf(cfc_log_buffer,
"[%04d-%02d-%02d %02d:%02d:%02d] %s: ",
dt->tm_year + 1900,
dt->tm_mon + 1,
dt->tm_mday,
dt->tm_hour,
dt->tm_min,
dt->tm_sec,
cfc_error_titles[level]);
va_start(al, fmt);
off2 = vsprintf(cfc_log_buffer + off1, fmt, al);
va_end(al);
cfc_log_buffer[off1 + off2] = '\n';
write(cfc_log_fd, cfc_log_buffer, off1 + off2 + 1);
}
void cfc_destroy_log()
{
if (!cfc_log_initialize) {
return;
}
if (cfc_log_fd && cfc_log_fd != STDERR_FILENO) {
close(cfc_log_fd);
}
}
int cfc_log_get_fd()
{
return cfc_log_fd;
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/