-
Notifications
You must be signed in to change notification settings - Fork 507
/
beast_log.c
86 lines (67 loc) · 1.49 KB
/
beast_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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#ifdef PHP_WIN32
#else
#include <unistd.h>
#endif
#include "main/php_reentrancy.h"
#include "beast_log.h"
static FILE *beast_log_fp = NULL;
static int log_level = beast_log_notice;
int beast_log_init(char *log_file, int level)
{
if (!log_file || strlen(log_file) == 0) {
return 0;
}
beast_log_fp = fopen(log_file, "a+");
if (!beast_log_fp)
return -1;
log_level = level;
return 0;
}
int beast_log_chown(uid_t uid, gid_t gid)
{
#ifdef PHP_WIN32
return 1;
#else
int fd;
if (!beast_log_fp) {
return 0;
}
fd = fileno(beast_log_fp);
return fchown(fd, uid, gid);
#endif
}
void beast_write_log(beast_log_level level, const char *fmt, ...)
{
struct tm local_tm, *result_tm;
time_t the_time;
char buf[64];
char *headers[] = {"DEBUG", "NOTICE", "ERROR"};
va_list ap;
if (beast_log_fp == NULL ||
level > beast_log_error ||
level < log_level)
{
return;
}
va_start(ap, fmt);
the_time = time(NULL);
result_tm = php_localtime_r(&the_time, &local_tm);
strftime(buf, 64, "%d %b %H:%M:%S", result_tm);
fprintf(beast_log_fp, "[%s] %s: ", buf, headers[level]);
vfprintf(beast_log_fp, fmt, ap);
fprintf(beast_log_fp, "\n");
fflush(beast_log_fp);
va_end(ap);
return;
}
void beast_log_destroy()
{
if (beast_log_fp) {
fclose(beast_log_fp);
beast_log_fp = NULL;
}
}