forked from bcxorg/onepixd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.c
147 lines (132 loc) · 2.44 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/********************************************************
** LOG -- All logging of errors is handled here.
**
** Initially uses only syslog for logging.
** May add logging to files later.
*/
#include "onepixd.h"
typedef struct {
char *name;
int value;
} SYSLOG_F;
static SYSLOG_F Facilitynames[] = {
#ifdef LOG_AUTH
{ "auth", LOG_AUTH },
#endif
#ifdef LOG_AUTHPRIV
{ "authpriv", LOG_AUTHPRIV },
#endif
#ifdef LOG_CRON
{ "cron", LOG_CRON },
#endif
#ifdef LOG_DAEMON
{ "daemon", LOG_DAEMON },
#endif
#ifdef LOG_FTP
{ "ftp", LOG_FTP },
#endif
#ifdef LOG_KERN
{ "kern", LOG_KERN },
#endif
#ifdef LOG_LPR
{ "lpr", LOG_LPR },
#endif
#ifdef LOG_MAIL
{ "mail", LOG_MAIL },
#endif
#ifdef LOG_NEWS
{ "news", LOG_NEWS },
#endif
#ifdef LOG_USER
{ "user", LOG_USER },
#endif
#ifdef LOG_UUCP
{ "uucp", LOG_UUCP },
#endif
#ifdef LOG_LOCAL0
{ "local0", LOG_LOCAL0 },
#endif
#ifdef LOG_LOCAL1
{ "local1", LOG_LOCAL1 },
#endif
#ifdef LOG_LOCAL2
{ "local2", LOG_LOCAL2 },
#endif
#ifdef LOG_LOCAL3
{ "local3", LOG_LOCAL3 },
#endif
#ifdef LOG_LOCAL4
{ "local4", LOG_LOCAL4 },
#endif
#ifdef LOG_LOCAL5
{ "local5", LOG_LOCAL5 },
#endif
#ifdef LOG_LOCAL6
{ "local6", LOG_LOCAL6 },
#endif
#ifdef LOG_LOCAL7
{ "local7", LOG_LOCAL7 },
#endif
{ NULL, -1 }
};
int
log_check_facility(char *facility)
{
SYSLOG_F *fptr;
for(fptr = Facilitynames; fptr->name != NULL; ++fptr)
{
if (strcasecmp(facility, fptr->name) == 0)
return fptr->value;
}
return -1;
}
int
log_init(char *facility, char *prog)
{
int fac = 0;
errno = 0;
if (prog == NULL)
prog = "undefined";
if (facility == NULL)
{
(void) openlog(prog, LOG_PID, LOG_MAIL);
return 0;
}
fac = log_check_facility(facility);
if (fac < 0)
{
(void) openlog(prog, LOG_PID, LOG_MAIL);
return 0;
}
(void) openlog(prog, LOG_PID, fac);
return 0;
}
void
log_emit(int level, char *ipnum, char *file, int line, char *msg)
{
char *s;
if (level == LOG_INFO)
s = "status";
else if (level == LOG_ERR)
s = "error";
else if (level == LOG_WARNING)
s = "warning";
else
s= "notice";
if (file == NULL)
file = __FILE__;
if (line <= 0)
line = __LINE__;
if (msg == NULL)
msg = "Undefined Error";
if (isdebug(BUG_LOGS) == TRUE)
{
printf("DEBUG: %s(%d): %s: %s\n", file, line, s, msg);
return;
}
if (ipnum == NULL || strcmp(ipnum, "0.0.0.0") == 0)
(void)syslog(level, "%s(%d): %s=%s", file, line, s, msg);
else
(void)syslog(level, "%s(%d): client_ip=%s, %s=%s", file, line, ipnum, s, msg);
return;
}