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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
/*
log.c - logging funtions
Copyright (C) 2002, 2003, 2008, 2010 Arthur de Jong
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <syslog.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include "log.h"
/* set the logname */
#undef PACKAGE
#define PACKAGE "nslcd"
/* storage for logging modes */
static struct cvsd_log {
FILE *fp; /* NULL==syslog */
int loglevel;
struct cvsd_log *next;
} *cvsd_loglist=NULL;
/* default loglevel when no logging is configured */
static int prelogging_loglevel=LOG_INFO;
/* the session id that is set for this thread */
static __thread char *sessionid=NULL;
/* the request identifier that is set for this thread */
static __thread char *requestid=NULL;
#define MAX_REQUESTID_LENGTH 40
/* set loglevel when no logging is configured */
void log_setdefaultloglevel(int loglevel)
{
prelogging_loglevel=loglevel;
}
/* add logging method to configuration list */
static void log_addlogging_fp(FILE *fp,int loglevel)
{
struct cvsd_log *tmp,*lst;
/* create new logstruct */
tmp=(struct cvsd_log *)malloc(sizeof(struct cvsd_log));
if (tmp==NULL)
{
fprintf(stderr,"malloc() failed: %s",strerror(errno));
/* since this is done during initialisation it's best to bail out */
exit(EXIT_FAILURE);
}
tmp->fp=fp;
tmp->loglevel=loglevel;
tmp->next=NULL;
/* save the struct in the list */
if (cvsd_loglist==NULL)
cvsd_loglist=tmp;
else
{
for (lst=cvsd_loglist;lst->next!=NULL;lst=lst->next);
lst->next=tmp;
}
}
/* configure logging to a file */
void log_addlogging_file(const char *filename,int loglevel)
{
FILE *fp;
fp=fopen(filename,"a");
if (fp==NULL)
{
log_log(LOG_ERR,"cannot open logfile (%s) for appending: %s",filename,strerror(errno));
exit(EXIT_FAILURE);
}
log_addlogging_fp(fp,loglevel);
}
/* configure logging to syslog */
void log_addlogging_syslog(int loglevel)
{
openlog(PACKAGE,LOG_PID,LOG_DAEMON);
log_addlogging_fp(NULL,loglevel);
}
/* configure a null logging mode (no logging) */
void log_addlogging_none()
{
/* this is a hack, but it's so easy */
log_addlogging_fp(NULL,LOG_EMERG);
}
/* start the logging with the configured logging methods
if no method is configured yet, logging is done to syslog */
void log_startlogging(void)
{
if (cvsd_loglist==NULL)
log_addlogging_syslog(LOG_INFO);
prelogging_loglevel=-1;
}
/* indicate that a session id should be included in the output
and set it to a new value */
void log_newsession(void)
{
/* ensure that sessionid can hold a string */
if (sessionid==NULL)
{
sessionid=(char *)malloc(7);
if (sessionid==NULL)
{
fprintf(stderr,"malloc() failed: %s",strerror(errno));
return; /* silently fail */
}
}
sprintf(sessionid,"%06x",(int)(rand()&0xffffff));
/* set the request id to empty */
if (requestid!=NULL)
requestid[0]='\0';
}
/* indicate that a request identifier should be included in the output
from this point on, until log_newsession() is called */
void log_setrequest(const char *format, ...)
{
va_list ap;
/* ensure that requestid can hold a string */
if (requestid==NULL)
{
requestid=(char *)malloc(MAX_REQUESTID_LENGTH);
if (requestid==NULL)
{
fprintf(stderr,"malloc() failed: %s",strerror(errno));
return; /* silently fail */
}
}
/* make the message */
va_start(ap,format);
vsnprintf(requestid,MAX_REQUESTID_LENGTH,format,ap);
requestid[MAX_REQUESTID_LENGTH-1]='\0';
va_end(ap);
}
/* log the given message using the configured logging method */
void log_log(int pri,const char *format, ...)
{
int res;
struct cvsd_log *lst;
/* TODO: make this something better */
#define maxbufferlen 200
char buffer[maxbufferlen];
va_list ap;
/* make the message */
va_start(ap,format);
res=vsnprintf(buffer,maxbufferlen,format,ap);
if ((res<0)||(res>=maxbufferlen))
{
/* truncate with "..." */
buffer[maxbufferlen-2]='.';
buffer[maxbufferlen-3]='.';
buffer[maxbufferlen-4]='.';
}
buffer[maxbufferlen-1]='\0';
va_end(ap);
/* do the logging */
if (prelogging_loglevel>=0)
{
/* if logging is not yet defined, log to stderr */
if (pri<=prelogging_loglevel)
{
if ((requestid!=NULL)&&(requestid[0]!='\0'))
fprintf(stderr,"%s: [%s] <%s> %s%s\n",PACKAGE,sessionid,requestid,pri==LOG_DEBUG?"DEBUG: ":"",buffer);
else if (sessionid)
fprintf(stderr,"%s: [%s] %s%s\n",PACKAGE,sessionid,pri==LOG_DEBUG?"DEBUG: ":"",buffer);
else
fprintf(stderr,"%s: %s%s\n",PACKAGE,pri==LOG_DEBUG?"DEBUG: ":"",buffer);
}
}
else
{
for (lst=cvsd_loglist;lst!=NULL;lst=lst->next)
{
if (pri<=lst->loglevel)
{
if (lst->fp==NULL) /* syslog */
{
if ((requestid!=NULL)&&(requestid[0]!='\0'))
syslog(pri,"[%s] <%s> %s",sessionid,requestid,buffer);
else if (sessionid)
syslog(pri,"[%s] %s",sessionid,buffer);
else
syslog(pri,"%s",buffer);
}
else /* file */
{
if ((requestid!=NULL)&&(requestid[0]!='\0'))
fprintf(lst->fp,"%s: [%s] <%s> %s\n",sessionid,requestid,PACKAGE,buffer);
else if (sessionid)
fprintf(lst->fp,"%s: [%s] %s\n",sessionid,PACKAGE,buffer);
else
fprintf(lst->fp,"%s: %s\n",PACKAGE,buffer);
fflush(lst->fp);
}
}
}
}
}
/* return the syslog loglevel represented by the string
return -1 on unknown */
int log_getloglevel(const char *lvl)
{
if ( strcmp(lvl,"crit")==0 )
return LOG_CRIT;
else if ( (strcmp(lvl,"error")==0) ||
(strcmp(lvl,"err")==0) )
return LOG_ERR;
else if ( strcmp(lvl,"warning")==0 )
return LOG_WARNING;
else if ( strcmp(lvl,"notice")==0 )
return LOG_NOTICE;
else if ( strcmp(lvl,"info")==0 )
return LOG_INFO;
else if ( strcmp(lvl,"debug")==0 )
return LOG_DEBUG;
else
return -1; /* unknown */
}
|