This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.c
99 lines (84 loc) · 1.93 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
#include "log.h"
#include <time.h>
#include <stdarg.h>
#include <string.h>
#include <syslog.h>
#include <apr_proc_mutex.h>
struct log
{
FILE *fp;
apr_proc_mutex_t *mutex;
};
static const char *TYPE_NAMES[] =
{
"INFO",
"WARN",
"ERROR",
"BLOCK",
};
log_t *log_open( const char *path, apr_pool_t *pool )
{
if (pool == NULL || path == NULL)
return NULL;
log_t *log = (log_t*) apr_pcalloc(pool, sizeof(log_t));
if (log == NULL)
return NULL;
if (apr_proc_mutex_create(&log->mutex, NULL, APR_LOCK_PROC_PTHREAD, pool) != APR_SUCCESS)
return NULL;
if ((log->fp = fopen(path, "a")) == NULL)
{
apr_proc_mutex_destroy(log->mutex);
return NULL;
}
return log;
}
void log_close( log_t *log )
{
if (log == NULL)
return;
if (log->fp)
{
fclose(log->fp);
log->fp = NULL;
}
if (log->mutex)
{
apr_proc_mutex_destroy(log->mutex);
log->mutex = NULL;
}
}
void log_print( log_t *log, log_type_t type, const char *format, ... )
{
va_list args;
time_t rawtime;
struct tm timeinfo;
char timeStr[28];
if (log == NULL || log->fp == NULL || log->mutex == NULL)
return;
if (apr_proc_mutex_lock(log->mutex) != APR_SUCCESS)
return;
time(&rawtime);
localtime_r(&rawtime, &timeinfo);
strftime(timeStr, sizeof(timeStr) - 1, "%Y-%m-%dT%H:%M:%S%z", &timeinfo);
fprintf(log->fp, "%s [%s] ", timeStr, TYPE_NAMES[type]);
va_start(args, format);
vfprintf(log->fp, format, args);
va_end(args);
fprintf(log->fp, "\n");
fflush(log->fp);
apr_proc_mutex_unlock(log->mutex);
}
void syslog_print( const char *server, const char *format, ... )
{
char tmp[512] = {0};
va_list args;
if (server == NULL || format == NULL) return;
openlog("mod_bouncer", LOG_PID, LOG_DAEMON);
va_start(args, format);
snprintf(tmp, sizeof(tmp) - 1, "[%s] ", server);
size_t len = strlen(tmp);
vsnprintf(tmp + len, sizeof(tmp) - 1 - len, format, args);
va_end(args);
syslog( LOG_DAEMON|LOG_ERR, "%s", tmp);
closelog();
}