~ubuntu-branches/ubuntu/trusty/vanessa-logger/trusty-proposed

« back to all changes in this revision

Viewing changes to libvanessa_logger/vanessa_logger.c

  • Committer: Bazaar Package Importer
  • Author(s): Simon Horman
  • Date: 2001-06-28 18:14:14 UTC
  • Revision ID: james.westby@ubuntu.com-20010628181414-wm88moh520kfsqfc
Tags: 0.0.2-1
See ChangeLog

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**********************************************************************
 
2
 * vanessa_logger.c                                      September 2000
 
3
 * Horms                                             horms@vergenet.net
 
4
 *
 
5
 * vanessa_logger
 
6
 * Generic logging layer
 
7
 * Copyright (C) 2000  Horms
 
8
 * 
 
9
 * This library is free software; you can redistribute it and/or
 
10
 * modify it under the terms of the GNU Lesser General Public License
 
11
 * as published by the Free Software Foundation; either version 2 of
 
12
 * the License, or (at your option) any later version.
 
13
 * 
 
14
 * This library is distributed in the hope that it will be useful, but
 
15
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
 * Lesser General Public License for more details.
 
18
 * 
 
19
 * You should have received a copy of the GNU Lesser General Public
 
20
 * License along with this library; if not, write to the Free Software
 
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 
22
 * 02111-1307 USA
 
23
 *
 
24
 **********************************************************************/
 
25
 
 
26
 
 
27
#include <stdlib.h>
 
28
#include <string.h>
 
29
#include <stdarg.h>
 
30
#include <unistd.h>
 
31
 
 
32
#define SYSLOG_NAMES
 
33
#include <syslog.h>
 
34
 
 
35
 
 
36
/**********************************************************************
 
37
 * Sun Solaris doesn't seem to define facilitynames so the
 
38
 * following compatibility code is provided.
 
39
 **********************************************************************/
 
40
 
 
41
#ifdef sun
 
42
 
 
43
#define LOG_AUTHPRIV    (10<<3) /* security/authorization messages (private) */
 
44
#define LOG_FTP         (11<<3) /* ftp daemon */
 
45
 
 
46
#define LOG_MAKEPRI(fac, pri)   (((fac) << 3) | (pri))
 
47
 
 
48
#define INTERNAL_NOPRI  0x10    /* the "no priority" priority */
 
49
                                /* mark "facility" */
 
50
#define INTERNAL_MARK   LOG_MAKEPRI(LOG_NFACILITIES, 0)
 
51
typedef struct _code {
 
52
  char    *c_name;
 
53
  int     c_val;
 
54
} CODE;
 
55
 
 
56
CODE facilitynames[] =
 
57
  {
 
58
    { "auth", LOG_AUTH },
 
59
    { "authpriv", LOG_AUTHPRIV },
 
60
    { "cron", LOG_CRON },
 
61
    { "daemon", LOG_DAEMON },
 
62
    { "ftp", LOG_FTP },
 
63
    { "kern", LOG_KERN },
 
64
    { "lpr", LOG_LPR },
 
65
    { "mail", LOG_MAIL },
 
66
    { "mark", INTERNAL_MARK },          /* INTERNAL */
 
67
    { "news", LOG_NEWS },
 
68
    { "security", LOG_AUTH },           /* DEPRECATED */
 
69
    { "syslog", LOG_SYSLOG },
 
70
    { "user", LOG_USER },
 
71
    { "uucp", LOG_UUCP },
 
72
    { "local0", LOG_LOCAL0 },
 
73
    { "local1", LOG_LOCAL1 },
 
74
    { "local2", LOG_LOCAL2 },
 
75
    { "local3", LOG_LOCAL3 },
 
76
    { "local4", LOG_LOCAL4 },
 
77
    { "local5", LOG_LOCAL5 },
 
78
    { "local6", LOG_LOCAL6 },
 
79
    { "local7", LOG_LOCAL7 },
 
80
    { NULL, -1 }
 
81
  };
 
82
 
 
83
#endif /* sun */
 
84
 
 
85
 
 
86
#include "vanessa_logger.h"
 
87
 
 
88
 
 
89
/**********************************************************************
 
90
 * Internal data structures
 
91
 **********************************************************************/
 
92
 
 
93
typedef struct {
 
94
  FILE *filehandle;
 
95
  char *filename;
 
96
} __vanessa_logger_filename_data_t;
 
97
 
 
98
typedef union {
 
99
  void                             *d_any;
 
100
  FILE                             *d_filehandle;
 
101
  __vanessa_logger_filename_data_t *d_filename;
 
102
  int                              *d_syslog;
 
103
} __vanessa_logger_data_t;
 
104
 
 
105
typedef enum {
 
106
  __vanessa_logger_filehandle,
 
107
  __vanessa_logger_filename,
 
108
  __vanessa_logger_syslog,
 
109
  __vanessa_logger_none 
 
110
} __vanessa_logger_type_t;
 
111
 
 
112
typedef enum {
 
113
  __vanessa_logger_true,
 
114
  __vanessa_logger_false
 
115
} __vanessa_logger_bool_t;
 
116
 
 
117
typedef struct {
 
118
  __vanessa_logger_type_t  type;
 
119
  __vanessa_logger_data_t  data;
 
120
  __vanessa_logger_bool_t  ready;
 
121
  char                     *ident;
 
122
  char                     *buffer;
 
123
  size_t                   buffer_len;
 
124
  int                      max_priority;
 
125
} __vanessa_logger_t;
 
126
 
 
127
 
 
128
/**********************************************************************
 
129
 * Internal Defines
 
130
 **********************************************************************/
 
131
 
 
132
#define __VANESSA_LOGGER_BUF_SIZE (size_t)1024
 
133
 
 
134
#define __VANESSA_LOGGER_SAFE_FREE(ptr) \
 
135
  if(ptr!=NULL){ \
 
136
    free(ptr); \
 
137
    ptr=NULL; \
 
138
  }
 
139
 
 
140
 
 
141
/**********************************************************************
 
142
 * Prototype of internal functions
 
143
 **********************************************************************/
 
144
 
 
145
static __vanessa_logger_t *__vanessa_logger_create(void);
 
146
static void __vanessa_logger_destroy(__vanessa_logger_t *vl);
 
147
static void __vanessa_logger_reset(__vanessa_logger_t *vl);
 
148
static __vanessa_logger_t *__vanessa_logger_set(
 
149
   __vanessa_logger_t *vl,
 
150
  const char *ident,
 
151
  const int max_priority,
 
152
  const __vanessa_logger_type_t type,
 
153
  void *data,
 
154
  const int option
 
155
);
 
156
static void __vanessa_logger_log(
 
157
  __vanessa_logger_t *vl, 
 
158
  int priority, 
 
159
  char *fmt, 
 
160
  va_list ap
 
161
);
 
162
static int __vanessa_logger_reopen(__vanessa_logger_t *vl);
 
163
 
 
164
 
 
165
/**********************************************************************
 
166
 * __vanessa_logger_create
 
167
 * Internal function to create a new logger
 
168
 * pre: none
 
169
 * post: Memory for logger is allocated and values are initialised
 
170
 *       to null state
 
171
 *       NULL on error
 
172
 * return: pointer to new logger
 
173
 **********************************************************************/
 
174
 
 
175
static __vanessa_logger_t *__vanessa_logger_create(void){
 
176
  __vanessa_logger_t *vl;
 
177
 
 
178
  if((vl=(__vanessa_logger_t *)malloc(sizeof(__vanessa_logger_t)))==NULL){
 
179
    perror("__vanessa_logger_create: malloc");
 
180
    return(NULL);
 
181
  }
 
182
 
 
183
  vl->type=__vanessa_logger_none;
 
184
  vl->data.d_any=NULL;
 
185
  vl->ready=__vanessa_logger_false;
 
186
  vl->ident=NULL;
 
187
  vl->buffer=NULL;
 
188
  vl->buffer_len=0;
 
189
  vl->max_priority=0;
 
190
 
 
191
  return(vl);
 
192
}
 
193
 
 
194
 
 
195
/**********************************************************************
 
196
 * __vanessa_logger_destroy
 
197
 * Internal function to destroy a logger
 
198
 * pre: vl: pointer to logger to destroy
 
199
 * post: Memory for logger and any internal memory is freed
 
200
 *       Nothing if vl is NULL
 
201
 * return: none
 
202
 **********************************************************************/
 
203
 
 
204
static void __vanessa_logger_destroy(__vanessa_logger_t *vl){
 
205
  if(vl==NULL){
 
206
    return;
 
207
  }
 
208
  
 
209
  __vanessa_logger_reset(vl);
 
210
  __VANESSA_LOGGER_SAFE_FREE(vl);
 
211
}
 
212
 
 
213
 
 
214
/**********************************************************************
 
215
 * __vanessa_logger_reset
 
216
 * Internal function to set all the values of a logger to their null state
 
217
 * and free any allocated memory.
 
218
 * pre: vl: pointer to logger to reset
 
219
 * post: All internal memory is freed
 
220
 *       All internal values are set to null state
 
221
 *       Nothing if vl is NULL
 
222
 * return: none
 
223
 **********************************************************************/
 
224
 
 
225
static void __vanessa_logger_reset(__vanessa_logger_t *vl){
 
226
  __vanessa_logger_bool_t ready;
 
227
 
 
228
  if(vl==NULL){
 
229
    return;
 
230
  }
 
231
 
 
232
  /* 
 
233
   * Logger is no longer ready
 
234
   */
 
235
  ready=vl->ready;        /* Remember state logger _was_ in */
 
236
  vl->ready=__vanessa_logger_false;
 
237
 
 
238
  /*
 
239
   * Close filehandles or log facilities as neccessary
 
240
   * Free any memory used in storing data
 
241
   */
 
242
  switch(vl->type){
 
243
    case __vanessa_logger_filename:
 
244
      if(ready==__vanessa_logger_true){
 
245
        if(fclose(vl->data.d_filename->filehandle)){
 
246
          perror("__vanessa_logger_reset: fclose");
 
247
        }
 
248
      }
 
249
      if(vl->data.d_filename!=NULL){
 
250
        __VANESSA_LOGGER_SAFE_FREE(vl->data.d_filename->filename);
 
251
      } 
 
252
      __VANESSA_LOGGER_SAFE_FREE(vl->data.d_filename);
 
253
      break;
 
254
    case __vanessa_logger_syslog:
 
255
      __VANESSA_LOGGER_SAFE_FREE(vl->data.d_syslog);
 
256
      if(vl->ready==__vanessa_logger_true){
 
257
        closelog();
 
258
      }
 
259
      break;
 
260
    default:
 
261
      break;
 
262
  }
 
263
 
 
264
  /*
 
265
   * Reset type and data
 
266
   */
 
267
  vl->type=__vanessa_logger_none;
 
268
  vl->data.d_any=NULL;
 
269
 
 
270
  /*
 
271
   * Reset ident
 
272
   */
 
273
  __VANESSA_LOGGER_SAFE_FREE(vl->ident);
 
274
 
 
275
  /*
 
276
   * Reset buffer, buffer_len
 
277
   */
 
278
  __VANESSA_LOGGER_SAFE_FREE(vl->buffer);
 
279
  vl->buffer_len=0;
 
280
 
 
281
  /*
 
282
   * Reset max_priority
 
283
   */
 
284
  vl->max_priority=0;
 
285
}
 
286
 
 
287
 
 
288
/**********************************************************************
 
289
 * __vanessa_logger_set
 
290
 * Internal function to seed the values of a logger
 
291
 * pre: vl: pointer to logger to seed values of
 
292
 *      ident: Identity string to prepend to each log
 
293
 *      type: Type of logger to initialise
 
294
 *      data: Type specific data for logger typecast to (void *)
 
295
 * post: Values of logger are set to allow loging as per type
 
296
 *       Nothing if: vl is NULL or
 
297
 *                   type is __vanessa_logger_none or
 
298
 *                   data is NULL or
 
299
 *                   ident is NULL
 
300
 *       On error vl is destroyed as per __vanessa_logger_destroy
 
301
 * return: vl on success
 
302
 *         NULL on error
 
303
 **********************************************************************/
 
304
 
 
305
static __vanessa_logger_t *__vanessa_logger_set(
 
306
  __vanessa_logger_t *vl,
 
307
  const char *ident,
 
308
  const int max_priority,
 
309
  const __vanessa_logger_type_t type,
 
310
  void *data,
 
311
  const int option
 
312
){
 
313
  if(vl==NULL || type==__vanessa_logger_none || data==NULL || ident==NULL){
 
314
    return(NULL);
 
315
  }
 
316
 
 
317
  /*
 
318
   * Free any previously allocated memory
 
319
   */
 
320
  __vanessa_logger_reset(vl);
 
321
        
 
322
  /*
 
323
   * Set ident
 
324
   */
 
325
  if((vl->ident=strdup(ident))==NULL){
 
326
    perror("__vanessa_logger_set: strdup 1");
 
327
    __vanessa_logger_destroy(vl);
 
328
    return(NULL);
 
329
  }
 
330
 
 
331
  /*
 
332
   * Set buffer
 
333
   */
 
334
  if((vl->buffer=(char *)malloc(__VANESSA_LOGGER_BUF_SIZE))==NULL){
 
335
    perror("__vanessa_logger_set: malloc 1");
 
336
    __vanessa_logger_destroy(vl);
 
337
    return(NULL);
 
338
  }
 
339
  vl->buffer_len=__VANESSA_LOGGER_BUF_SIZE;
 
340
 
 
341
  /*
 
342
   * Set type
 
343
   */
 
344
  vl->type=type;
 
345
 
 
346
  /*
 
347
   * Set data
 
348
   */
 
349
  switch(vl->type){
 
350
    case __vanessa_logger_filehandle:
 
351
      vl->data.d_filehandle=(FILE *)data;
 
352
      break;
 
353
    case __vanessa_logger_filename:
 
354
      if((vl->data.d_filename=(__vanessa_logger_filename_data_t *)malloc(
 
355
        sizeof(__vanessa_logger_filename_data_t)
 
356
      ))==NULL){
 
357
        perror("__vanessa_logger_set: malloc 2");
 
358
        __vanessa_logger_destroy(vl);
 
359
        return(NULL);
 
360
      }
 
361
      if((vl->data.d_filename->filename=strdup((char *)data))==NULL){
 
362
        perror("__vanessa_logger_set: malloc strdup 2");
 
363
        __vanessa_logger_destroy(vl);
 
364
        return(NULL);
 
365
      }
 
366
      vl->data.d_filename->filehandle=fopen(vl->data.d_filename->filename, "a");
 
367
      if(vl->data.d_filename->filehandle==NULL){
 
368
        perror("__vanessa_logger_set: fopen");
 
369
        __vanessa_logger_destroy(vl);
 
370
        return(NULL);
 
371
      }
 
372
      break;
 
373
    case __vanessa_logger_syslog:
 
374
      if((vl->data.d_syslog=(int *)malloc(sizeof(int)))==NULL){
 
375
        perror("__vanessa_logger_set: malloc 3");
 
376
        __vanessa_logger_destroy(vl);
 
377
        return(NULL);
 
378
      }
 
379
      *(vl->data.d_syslog)=*((int *)data);
 
380
      openlog(vl->ident, LOG_PID|option, *(vl->data.d_syslog));
 
381
      break;
 
382
    case __vanessa_logger_none:
 
383
      break;
 
384
  }
 
385
 
 
386
  /*
 
387
   * Set max_priority
 
388
   */
 
389
  vl->max_priority=max_priority;
 
390
 
 
391
  /*
 
392
   * Set ready
 
393
   */
 
394
  vl->ready=__vanessa_logger_true;
 
395
 
 
396
  return(vl);
 
397
}
 
398
 
 
399
 
 
400
/**********************************************************************
 
401
 * __vanessa_logger_reopen
 
402
 * Internal function to reopen a logger
 
403
 * pre: vl: pointer to logger to reopen
 
404
 * post: In the calse of a filename logger the logger is closed
 
405
 *       if it was open and then opened regardless of weather it
 
406
 *       was originally open or not.
 
407
 *       In the case of a none, syslog or filehandle logger or if vl is NULL
 
408
 *       nothing is done.
 
409
 *       If an error occurs -1 is returned and vl->ready is set to
 
410
 *       __vanessa_logger_false
 
411
 * return: 0 on success
 
412
 *         -1 on error
 
413
 **********************************************************************/
 
414
 
 
415
static int __vanessa_logger_reopen(__vanessa_logger_t *vl){
 
416
  if(vl==NULL || vl->type==__vanessa_logger_none){
 
417
    return(0);
 
418
  }
 
419
 
 
420
  switch(vl->type){
 
421
    case __vanessa_logger_filename:
 
422
      if(vl->ready==__vanessa_logger_true){
 
423
        vl->ready=__vanessa_logger_false;
 
424
        if(fclose(vl->data.d_filename->filehandle)){
 
425
          perror("__vanessa_logger_reopen: fclose");
 
426
          return(-1);
 
427
        }
 
428
      }
 
429
      vl->data.d_filename->filehandle=fopen(vl->data.d_filename->filename, "a");
 
430
      if(vl->data.d_filename->filehandle==NULL){
 
431
        perror("__vanessa_logger_reopen: fopen");
 
432
        return(-1);
 
433
      }
 
434
      vl->ready=__vanessa_logger_true;
 
435
      break;
 
436
    default:
 
437
      break;
 
438
  }
 
439
 
 
440
  return(0);
 
441
}
 
442
 
 
443
 
 
444
/**********************************************************************
 
445
 * __vanessa_logger_log
 
446
 * Internal function to log a message
 
447
 * pre: vl: logger to use
 
448
 *      priority: priority to log with
 
449
 *                Only used if log type is __vanessa_logger_syslog
 
450
 *                Ignored otherwise
 
451
 *      fmt: format for log message
 
452
 *      ap: varargs for format
 
453
 * post: message is logged to appropriate logger
 
454
 *       vl->ident[pid]: will be prepended to each log
 
455
 *       '\n' will be appended to each log that doesn't already end with
 
456
 *       a '\n'
 
457
 *       Nothing on error
 
458
 * return: none
 
459
 **********************************************************************/
 
460
 
 
461
#define __VANESSA_LOGGER_DO_FH(_vl, _fmt, _fh, _ap) \
 
462
  { \
 
463
    int len; \
 
464
    if(snprintf( \
 
465
      _vl->buffer, \
 
466
      _vl->buffer_len-1, \
 
467
      "%s[%d]: %s",  \
 
468
      _vl->ident,  \
 
469
      getpid(),  \
 
470
      _fmt \
 
471
    )<0){ \
 
472
      fprintf(_fh, "__vanessa_logger_log: snprintf: output truncated\n"); \
 
473
      return; \
 
474
    } \
 
475
    len=strlen(_vl->buffer); \
 
476
    if(*((_vl->buffer)+len-1)!='\n'){ \
 
477
      *((_vl->buffer)+len)='\n'; \
 
478
      *((_vl->buffer)+len+1)='\0'; \
 
479
    } \
 
480
    vfprintf(_fh, _vl->buffer, _ap); \
 
481
  }
 
482
 
 
483
static void __vanessa_logger_log(
 
484
  __vanessa_logger_t *vl, 
 
485
  int priority, 
 
486
  char *fmt, 
 
487
  va_list ap
 
488
){
 
489
  if(vl==NULL||vl->ready==__vanessa_logger_false||priority>vl->max_priority){
 
490
    return;
 
491
  }
 
492
 
 
493
  switch(vl->type){
 
494
    case __vanessa_logger_filehandle:
 
495
      __VANESSA_LOGGER_DO_FH(vl, fmt, vl->data.d_filehandle, ap);
 
496
      break;
 
497
    case __vanessa_logger_filename:
 
498
      __VANESSA_LOGGER_DO_FH(vl, fmt, vl->data.d_filename->filehandle, ap);
 
499
      break;
 
500
    case __vanessa_logger_syslog:
 
501
      if(vsnprintf(vl->buffer, vl->buffer_len, fmt, ap)<0){
 
502
        syslog(priority, "__vanessa_logger_log: vsnprintf: output truncated");
 
503
        return;
 
504
      }
 
505
      syslog(priority, "%s", vl->buffer);
 
506
      /* syslog(priority, vl->buffer); */
 
507
      break;
 
508
    case __vanessa_logger_none:
 
509
      break;
 
510
  }
 
511
}
 
512
 
 
513
 
 
514
/**********************************************************************
 
515
 * __vanessa_logger_get_facility_byname
 
516
 * Given the name of a syslog facility as an ASCII string,
 
517
 * return the facility as an integer.
 
518
 * Relies on facilitynames[] being defined in syslog.h
 
519
 * pre: facility_name: syslog facility as an ASCII string
 
520
 * post: none
 
521
 * return: logger as an int
 
522
 *         -1 if facility_name cannot be found in facilitynames[],
 
523
 *            if facility_name is NULL
 
524
 *            or other error
 
525
 **********************************************************************/
 
526
 
 
527
static int __vanessa_logger_get_facility_byname(const char *facility_name){
 
528
  int i;
 
529
 
 
530
  extern CODE facilitynames[];
 
531
  
 
532
  if(facility_name==NULL){
 
533
    fprintf(
 
534
      stderr, 
 
535
      "__vanessa_logger_get_facility_byname: facility_name is NULL\n"
 
536
    );
 
537
    return(-1);
 
538
  }
 
539
 
 
540
  for(i=0; facilitynames[i].c_name!=NULL; i++){
 
541
    if(!strcmp(facility_name, facilitynames[i].c_name)){
 
542
      return(facilitynames[i].c_val);
 
543
    }
 
544
  }
 
545
 
 
546
  fprintf(
 
547
    stderr, 
 
548
    "__vanessa_logger_get_facility_byname: facility \"%s\" not found\n",
 
549
    facility_name
 
550
  );
 
551
  return(-1);
 
552
}
 
553
 
 
554
/**********************************************************************
 
555
 * vanessa_logger_openlog_syslog
 
556
 * Exported function to open a logger that will log to syslog
 
557
 * pre: facility: facility to log to syslog with
 
558
 *      ident: Identity to prepend to each log
 
559
 *      max_priority: Maximum priority no to log
 
560
 *                    Priorities are integers, the levels listed
 
561
 *                    in syslog(3) should be used for a syslog logger
 
562
 *      option: options to pass to the openlog command
 
563
 *              Will be logically ored with LOG_PID
 
564
 * post: Logger is opened
 
565
 * return: pointer to logger
 
566
 *         NULL on error
 
567
 **********************************************************************/
 
568
 
 
569
vanessa_logger_t *vanessa_logger_openlog_syslog(
 
570
  int facility,
 
571
  const char *ident,
 
572
  const int max_priority,
 
573
  const int option
 
574
){
 
575
  __vanessa_logger_t *vl;
 
576
 
 
577
  if((vl=__vanessa_logger_create())==NULL){
 
578
    fprintf(stderr, "vanessa_logger_openlog_syslog: __vanessa_logger_create\n");
 
579
    return(NULL);
 
580
  }
 
581
 
 
582
  if(__vanessa_logger_set(
 
583
    vl, 
 
584
    ident, 
 
585
    max_priority,
 
586
    __vanessa_logger_syslog, 
 
587
    (void *)&facility,
 
588
    option
 
589
  )==NULL){
 
590
    fprintf(stderr, "vanessa_logger_openlog_syslog: __vanessa_logger_set\n");
 
591
    return(NULL);
 
592
  }
 
593
 
 
594
  return((vanessa_logger_t *)vl);
 
595
}
 
596
 
 
597
 
 
598
/**********************************************************************
 
599
 * vanessa_logger_openlog_syslog_byname
 
600
 * Exported function to open a logger that will log to syslog
 
601
 * pre: facility_name: Name of facility to log to syslog with
 
602
 *      ident: Identity to prepend to each log
 
603
 *      max_priority: Maximum priority no to log
 
604
 *                    Priorities are integers, the levels listed
 
605
 *                    in syslog(3) should be used for a syslog logger
 
606
 *      option: options to pass to the openlog command
 
607
 *              Will be logically ored with LOG_PID
 
608
 * post: Logger is opened
 
609
 * return: pointer to logger
 
610
 *         NULL on error
 
611
 **********************************************************************/
 
612
 
 
613
vanessa_logger_t *vanessa_logger_openlog_syslog_byname(
 
614
  const char *facility_name,
 
615
  const char *ident,
 
616
  const int max_priority,
 
617
  const int option
 
618
){
 
619
  __vanessa_logger_t *vl;
 
620
  int facility;
 
621
 
 
622
  if((facility=__vanessa_logger_get_facility_byname(facility_name))<0){
 
623
    fprintf(
 
624
      stderr, 
 
625
      "vanessa_logger_open_syslog_byname: "
 
626
      "__vanessa_logger_get_facility_byname\n"
 
627
    );
 
628
    return(NULL);
 
629
  }
 
630
 
 
631
  vl=vanessa_logger_openlog_syslog(facility,ident,max_priority,option);
 
632
  if(vl==NULL){
 
633
    fprintf(
 
634
      stderr, 
 
635
      "vanessa_logger_openlog_syslog: vanessa_logger_openlog_syslog\n"
 
636
    );
 
637
    return(NULL);
 
638
  }
 
639
 
 
640
  return((vanessa_logger_t *)vl);
 
641
}
 
642
 
 
643
 
 
644
/**********************************************************************
 
645
 * vanessa_logger_openlog_filehandle
 
646
 * Exported function to open a logger that will log to a filehandle
 
647
 * pre: filehandle: open filehandle to log to
 
648
 *      ident: Identity to prepend to each log
 
649
 *      max_priority: Maximum priority number to log
 
650
 *                    Priorities are integers, the levels listed
 
651
 *                    in syslog(3) should be used for a syslog logger
 
652
 *      option: ignored
 
653
 * post: Logger is opened
 
654
 * return: pointer to logger
 
655
 *         NULL on error
 
656
 **********************************************************************/
 
657
 
 
658
vanessa_logger_t *vanessa_logger_openlog_filehandle(
 
659
  FILE *filehandle,
 
660
  const char *ident,
 
661
  const int max_priority,
 
662
  const int option
 
663
){
 
664
  __vanessa_logger_t *vl;
 
665
 
 
666
  if((vl=__vanessa_logger_create())==NULL){
 
667
    fprintf(
 
668
      stderr,
 
669
      "vanessa_logger_openlog_filehandle: __vanessa_logger_create\n"
 
670
    );
 
671
    return(NULL);
 
672
  }
 
673
 
 
674
  if(__vanessa_logger_set(
 
675
    vl, 
 
676
    ident, 
 
677
    max_priority,
 
678
    __vanessa_logger_filehandle, 
 
679
    (void *)filehandle,
 
680
    option
 
681
  )==NULL){
 
682
    fprintf(stderr,"vanessa_logger_openlog_filehandle: __vanessa_logger_set\n");
 
683
    return(NULL);
 
684
  }
 
685
 
 
686
  return((vanessa_logger_t *)vl);
 
687
}
 
688
 
 
689
 
 
690
/**********************************************************************
 
691
 * vanessa_logger_openlog_filename
 
692
 * Exported function to open a logger that will log to a filename
 
693
 *          that will be opened
 
694
 * pre: filename: filename to log to
 
695
 *      ident: Identity to prepend to each log
 
696
 *      max_priority: Maximum priority number to log
 
697
 *                    Priorities are integers, the levels listed
 
698
 *                    in syslog(3) should be used for a syslog logger
 
699
 *      option: ignored
 
700
 * post: Logger is opened
 
701
 * return: pointer to logger
 
702
 *         NULL on error
 
703
 **********************************************************************/
 
704
 
 
705
vanessa_logger_t *vanessa_logger_openlog_filename(
 
706
  char *filename,
 
707
  const char *ident,
 
708
  const int max_priority,
 
709
  const int option
 
710
){
 
711
  __vanessa_logger_t *vl;
 
712
 
 
713
  if((vl=__vanessa_logger_create())==NULL){
 
714
    fprintf(
 
715
      stderr,
 
716
      "vanessa_logger_openlog_filename: __vanessa_logger_create\n"
 
717
    );
 
718
    return(NULL);
 
719
  }
 
720
 
 
721
  if(__vanessa_logger_set(
 
722
    vl, 
 
723
    ident, 
 
724
    max_priority,
 
725
    __vanessa_logger_filename, 
 
726
    (void *)filename,
 
727
    option
 
728
  )==NULL){
 
729
    fprintf(stderr,"vanessa_logger_openlog_filename: __vanessa_logger_set\n");
 
730
    return(NULL);
 
731
  }
 
732
 
 
733
  return((vanessa_logger_t *)vl);
 
734
}
 
735
 
 
736
 
 
737
/**********************************************************************
 
738
 * vanessa_logger_change_max_priority
 
739
 * Exported function to change the maximum priority that the logger
 
740
 * will log.
 
741
 * pre: vl: logger to change the maximum priority of
 
742
 *      max_priority: Maximum priority number to log
 
743
 *                    Priorities are integers, the levels listed
 
744
 *                    in syslog(3) should be used for a syslog logger
 
745
 * post: maximum priority of logger is changed
 
746
 *       nothing if vl is NULL
 
747
 * return: none
 
748
 **********************************************************************/
 
749
 
 
750
void vanessa_logger_change_max_priority(
 
751
  vanessa_logger_t *vl,
 
752
  const int max_priority
 
753
){
 
754
  if(vl==NULL){
 
755
    return;
 
756
  }
 
757
 
 
758
  ((__vanessa_logger_t *)vl)->max_priority=max_priority;
 
759
}
 
760
 
 
761
 
 
762
/**********************************************************************
 
763
 * vanessa_logger_closelog
 
764
 * Exported function to close a logger
 
765
 * pre: vl: pointer to logger to close
 
766
 * post: logger is closed and memory is freed
 
767
 * return: none
 
768
 **********************************************************************/
 
769
 
 
770
void vanessa_logger_closelog(vanessa_logger_t *vl){
 
771
  __vanessa_logger_destroy((__vanessa_logger_t *)vl);
 
772
}
 
773
 
 
774
 
 
775
/**********************************************************************
 
776
 * vanessa_logger_log
 
777
 * Exported function to log a message
 
778
 * pre: vl: pointer to logger to log to
 
779
 *      priority: Priority to log with.
 
780
 *                If priority is more than max_priority as provided to
 
781
 *                vanessa_logger_openlog_filehandle, 
 
782
 *                vanessa_logger_openlog_filename or
 
783
 *                vanessa_logger_openlog_syslog. 
 
784
 *                Levels described in syslog(3) should be used for
 
785
 *                syslog loggers as the priority will be used when
 
786
 *                logging to syslog. These priorities may also be
 
787
 *                used for filehandle and filename loggers.
 
788
 *                Strangely with syslog higher priorities have
 
789
 *                _lower_ priority numbers. For this
 
790
 *                reason vanessa_logger regards messages with
 
791
 *                lower priority numbers as being higher priority
 
792
 *                than messages with lower priority. I suggest
 
793
 *                just using the syslog priorities to avoid confusion.
 
794
 *      fmt: format of message to log as per sprintf(3) for
 
795
 *           filename and filehandle loggers and as
 
796
 *           per syslog(3) for syslog loggers
 
797
 *      ...: data for fmt
 
798
 * post: Message is logged
 
799
 * return: none
 
800
 **********************************************************************/
 
801
 
 
802
void vanessa_logger_log(vanessa_logger_t *vl, int priority, char *fmt, ...){
 
803
  va_list ap;
 
804
 
 
805
  va_start(ap, fmt);
 
806
  __vanessa_logger_log((__vanessa_logger_t *)vl, priority, fmt, ap);
 
807
  va_end(ap);
 
808
}
 
809
 
 
810
 
 
811
/**********************************************************************
 
812
 * vanessa_logger_logv
 
813
 * Exported function to log a message
 
814
 * Same as vanessa_logger_logv but a va_list is given instead
 
815
 * of a variable number of arguments.
 
816
 **********************************************************************/
 
817
 
 
818
void vanessa_logger_logv(
 
819
  vanessa_logger_t *vl, 
 
820
  int priority, 
 
821
  char *fmt, 
 
822
  va_list ap
 
823
){
 
824
  __vanessa_logger_log((__vanessa_logger_t *)vl, priority, fmt, ap);
 
825
}
 
826
 
 
827
 
 
828
/**********************************************************************
 
829
 * vanessa_logger_reopen
 
830
 * Exported function to reopen a logger
 
831
 * pre: vl: pointer to logger to reopen
 
832
 * post: logger is reopened
 
833
 * return: 0 on success
 
834
 *         -1 on error
 
835
 *
 
836
 * Note: May be used as part of a signal handler to reopen logger
 
837
 *       on for instance receiving a SIGHUP
 
838
 **********************************************************************/
 
839
 
 
840
int vanessa_logger_reopen(vanessa_logger_t *vl){
 
841
  if(__vanessa_logger_reopen((__vanessa_logger_t *)vl)){
 
842
    fprintf(stderr, "vanessa_logger_reopen: __vanessa_logger_reopen\n");
 
843
    return(-1);
 
844
  }
 
845
  return(0);
 
846
}