~ubuntu-branches/ubuntu/saucy/altermime/saucy

« back to all changes in this revision

Viewing changes to logger.c

  • Committer: Bazaar Package Importer
  • Author(s): Julien Valroff
  • Date: 2005-12-30 18:32:54 UTC
  • Revision ID: james.westby@ubuntu.com-20051230183254-vcdpwzgi9g1gooqe
Tags: upstream-0.3.6
ImportĀ upstreamĀ versionĀ 0.3.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
// Abstract logging system used to facilitate multiple modes
 
3
// of logging
 
4
 
 
5
#include <stdio.h>
 
6
#include <stdlib.h>
 
7
#include <string.h>
 
8
#ifndef WIN32
 
9
#include <syslog.h>
 
10
#endif
 
11
#include <errno.h>
 
12
#include <stdarg.h>
 
13
#include <ctype.h>
 
14
 
 
15
#include "logger.h"
 
16
 
 
17
#ifndef WIN32
 
18
static int _LOGGER_mode = _LOGGER_SYSLOG;
 
19
static int _LOGGER_syslog_mode = LOG_MAIL|LOG_INFO;
 
20
#else
 
21
static int _LOGGER_mode = _LOGGER_STDERR;
 
22
static int _LOGGER_syslog_mode = 0;
 
23
#endif
 
24
static FILE *_LOGGER_outf;
 
25
 
 
26
struct LOGGER_globals {
 
27
        int wrap;
 
28
        int wraplength;
 
29
};
 
30
 
 
31
        // Create and Initialise the global structure for LOGGER,
 
32
        //              we init it to have NO wrapping.
 
33
 
 
34
static struct LOGGER_globals LOGGER_glb={ 0, 0 };
 
35
 
 
36
 
 
37
/*------------------------------------------------------------------------
 
38
Procedure:     LOGGER_get_file ID:1
 
39
Purpose:       Returns the pointer to the file being used to output logs to
 
40
Input:
 
41
Output:
 
42
Errors:
 
43
------------------------------------------------------------------------*/
 
44
FILE *LOGGER_get_file( void )
 
45
{
 
46
        return _LOGGER_outf;
 
47
}
 
48
 
 
49
 
 
50
/*------------------------------------------------------------------------
 
51
Procedure:     LOGGER_set_output_mode ID:1
 
52
Purpose:       Sets the message/log output method, ie, stderr, stdout
 
53
or syslog
 
54
Input:
 
55
Output:
 
56
Errors:
 
57
------------------------------------------------------------------------*/
 
58
int LOGGER_set_output_mode( int modechoice )
 
59
{
 
60
        _LOGGER_mode = modechoice;
 
61
        return 0;
 
62
}
 
63
 
 
64
/*------------------------------------------------------------------------
 
65
Procedure:     LOGGER_set_output_file ID:1
 
66
Purpose:       Sets the output file for when _LOGGER_mode is set to
 
67
_LOGGER_file
 
68
Input:
 
69
Output:
 
70
Errors:
 
71
------------------------------------------------------------------------*/
 
72
int LOGGER_set_output_file( FILE *f )
 
73
{
 
74
        _LOGGER_outf = f;
 
75
        return 0;
 
76
}
 
77
 
 
78
/*------------------------------------------------------------------------
 
79
Procedure:     LOGGER_set_syslog_mode ID:1
 
80
Purpose:       Sets the mode that messaging to the syslog daemon will
 
81
be sent as (ie, LOG_MAIL|LOG_INFO)
 
82
Input:
 
83
Output:
 
84
Errors:
 
85
------------------------------------------------------------------------*/
 
86
int LOGGER_set_syslog_mode( int syslogmode )
 
87
{
 
88
        _LOGGER_syslog_mode = syslogmode;
 
89
        return 0;
 
90
}
 
91
 
 
92
 
 
93
 
 
94
 
 
95
/*------------------------------------------------------------------------
 
96
Procedure:     LOGGER_set_logfile ID:1
 
97
Purpose:       Opens and setups the internal Log file file pointer with the
 
98
log file as given by lfname
 
99
Input:
 
100
Output:
 
101
Errors:
 
102
------------------------------------------------------------------------*/
 
103
int LOGGER_set_logfile( char *lfname )
 
104
{
 
105
        int result = 0;
 
106
 
 
107
        _LOGGER_outf = fopen(lfname,"a");
 
108
        if (!_LOGGER_outf)
 
109
        {
 
110
#ifndef WIN32
 
111
                syslog(1,"LOGGER_set_logfile: ERROR - Cannot open logfile '%s' (%s)",lfname,strerror(errno));
 
112
#else
 
113
                fprintf(stderr, "LOGGER_set_logfile: ERROR - Cannot open logfile '%s' (%s)\n", lfname, strerror(errno));
 
114
#endif
 
115
                result = -1;
 
116
        }
 
117
 
 
118
        return result;
 
119
}
 
120
 
 
121
 
 
122
 
 
123
/*------------------------------------------------------------------------
 
124
Procedure:     LOGGER_set_wraplength ID:1
 
125
Purpose:       Sets the character count at which LOGGER will break a line
 
126
Input:         int length: Positive integer indicating number of chracters at which to wrap at
 
127
Output:
 
128
Errors:
 
129
------------------------------------------------------------------------*/
 
130
int LOGGER_set_wraplength( int length )
 
131
{
 
132
        if ( length >= 0 )
 
133
        {
 
134
                LOGGER_glb.wraplength = length;
 
135
        }
 
136
 
 
137
        return LOGGER_glb.wraplength;
 
138
}
 
139
 
 
140
/*------------------------------------------------------------------------
 
141
Procedure:     LOGGER_set_wrap ID:1
 
142
Purpose:       Set log output wrapping to on or off
 
143
Input:         int level: 0 = no wrap, > 0 = wrap.
 
144
Output:
 
145
Errors:
 
146
------------------------------------------------------------------------*/
 
147
int LOGGER_set_wrap( int level )
 
148
{
 
149
        if ( level >= 0 )
 
150
        {
 
151
                LOGGER_glb.wrap = level;
 
152
        }
 
153
 
 
154
        return LOGGER_glb.wrap;
 
155
}
 
156
 
 
157
 
 
158
 
 
159
/*------------------------------------------------------------------------
 
160
Procedure:     LOGGER_close_logfile ID:1
 
161
Purpose:       Closes the modules log file pointer.
 
162
Input:
 
163
Output:
 
164
Errors:
 
165
------------------------------------------------------------------------*/
 
166
int LOGGER_close_logfile( void )
 
167
{
 
168
        int result = 0;
 
169
 
 
170
        if (_LOGGER_outf) fclose(_LOGGER_outf);
 
171
 
 
172
        return result;
 
173
}
 
174
 
 
175
 
 
176
 
 
177
/*------------------------------------------------------------------------
 
178
Procedure:     LOGGER_clean_output ID:1
 
179
Purpose:       Checks through the output string for any characters which could cause
 
180
potential 'isssues' with the data writing calls, items such as stray non-escaped
 
181
% characters can cause havoc.
 
182
Input:         char *string: Raw string
 
183
int maxsize: Maximum available buffer size for this string to expand to
 
184
Output:
 
185
Errors:
 
186
------------------------------------------------------------------------*/
 
187
int LOGGER_clean_output( char *string, char **buffer )
 
188
{
 
189
        char *newstr;
 
190
        char *p, *q;
 
191
        char *next_space;
 
192
 
 
193
        int pc;
 
194
        int slen = strlen( string );
 
195
        int line_size;
 
196
        int maxsize = slen *2;
 
197
 
 
198
        // First up, allocate maxsize bytes for a temporary new string.
 
199
        newstr = malloc(slen *2 +1); 
 
200
        if ( newstr == NULL )
 
201
        {
 
202
                // FIXME - Report an error here ... to -somewhere-
 
203
                return -1;
 
204
        }
 
205
 
 
206
        p = newstr;
 
207
        q = string;
 
208
        pc = 0;
 
209
        line_size = 0;
 
210
 
 
211
        while (slen--)
 
212
        {
 
213
 
 
214
                // Do we need to apply any wrapping to the output? If so then we
 
215
                //              shall embark on a journey of strange space and distance
 
216
                //              evaluations to determine if we should wrap now or later
 
217
 
 
218
                if ( LOGGER_glb.wrap > 0 )
 
219
                {
 
220
                        if (isspace((int)*q))
 
221
                        {
 
222
                                next_space = strpbrk( (q+1), "\t\r\n\v " );
 
223
                                if (next_space != NULL)
 
224
                                {
 
225
                                        if ((line_size +(next_space -q)) >= LOGGER_glb.wraplength)
 
226
                                        {
 
227
                                                *p = '\n';
 
228
                                                p++;
 
229
                                                pc++;
 
230
                                                line_size = 0;
 
231
                                        }
 
232
                                }
 
233
                        }
 
234
 
 
235
                        if ( line_size >= LOGGER_glb.wraplength )
 
236
                        {
 
237
                                *p = '\n';
 
238
                                p++;
 
239
                                pc++;
 
240
                                line_size = 0;
 
241
                        }
 
242
                }
 
243
 
 
244
                // If the string has a % in it, then we need to encode it as
 
245
                //      a DOUBLE % symbol.
 
246
 
 
247
                if (*q == '%') {
 
248
//                      if (strchr("fdlsxXn",*(q+1)))
 
249
//                      {
 
250
                                *p = '%';
 
251
                                p++;
 
252
                                pc++;
 
253
//                      }
 
254
                }
 
255
 
 
256
                // Copy the character of the string in
 
257
                *p = *q;
 
258
 
 
259
                // Move everything along.
 
260
                q++;
 
261
                p++;
 
262
                pc++;
 
263
                line_size++;
 
264
 
 
265
                if ( pc > (maxsize -1) ) {
 
266
                        break;
 
267
                }
 
268
        }
 
269
 
 
270
        *p = '\0';
 
271
 
 
272
        // This will have to be deallocated later!
 
273
        if (newstr) *buffer = newstr;
 
274
 
 
275
        return 0;
 
276
}
 
277
 
 
278
/*------------------------------------------------------------------------
 
279
Procedure:     LOGGER_log ID:1
 
280
Purpose:       Logs the params as supplied to the required
 
281
output as defined by LOGGER_set_output
 
282
Input:
 
283
Output:
 
284
Errors:
 
285
------------------------------------------------------------------------*/
 
286
int LOGGER_log( char *format, ...)
 
287
{
 
288
        va_list ptr;
 
289
        char tmpoutput[10240];
 
290
        char linebreak[]="\n";
 
291
        char nolinebreak[]="";
 
292
        char *lineend;
 
293
        char *output;
 
294
 
 
295
 
 
296
        // get our variable arguments
 
297
        va_start(ptr,format);
 
298
 
 
299
        // produce output, and spit to the log file
 
300
#ifdef NO_SNPRINTF
 
301
        vsprintf(tmpoutput, format, ptr);
 
302
#else
 
303
        vsnprintf(tmpoutput,10240,format,ptr);
 
304
#endif
 
305
 
 
306
        LOGGER_clean_output( tmpoutput, &output );
 
307
 
 
308
        if ( output[strlen(output)-1] == '\n' ) {
 
309
                lineend = nolinebreak;
 
310
        }
 
311
        else {
 
312
                lineend = linebreak;
 
313
        }
 
314
 
 
315
        if ( output[strlen(output)-1] == '\n' ) { lineend = nolinebreak; } else { lineend = linebreak; }
 
316
 
 
317
        // Send the output to the appropriate output destination
 
318
        switch (_LOGGER_mode) {
 
319
                case _LOGGER_STDERR:
 
320
                        fprintf(stderr,"%s%s",output, lineend );
 
321
                        break;
 
322
                case _LOGGER_SYSLOG:
 
323
                        syslog(_LOGGER_syslog_mode,output);
 
324
                        break;
 
325
                case _LOGGER_STDOUT:
 
326
                        fprintf(stdout,"%s%s",output, lineend);
 
327
                        fflush(stdout);
 
328
                        break;
 
329
                case _LOGGER_FILE:
 
330
                        fprintf(_LOGGER_outf,"%s%s",output,lineend);
 
331
                        fflush(_LOGGER_outf);
 
332
                        break;
 
333
                default:
 
334
                        fprintf(stdout,"LOGGER-Default: %s%s",output,lineend);
 
335
        }
 
336
 
 
337
 
 
338
        if (output) free(output);
 
339
 
 
340
        return 0;
 
341
}
 
342
 
 
343
 
 
344
 
 
345