klog.h

00001 /*
00002  * Copyright (c) 2005-2012 by KoanLogic s.r.l. <http://www.koanlogic.com>
00003  * All rights reserved.
00004  *
00005  * This file is part of KLone, and as such it is subject to the license stated
00006  * in the LICENSE file which you have received as part of this distribution.
00007  *
00008  * $Id: klog.h,v 1.19 2006/01/09 12:38:37 tat Exp $
00009  */
00010 
00011 #ifndef _KLONE_LOG_H_
00012 #define _KLONE_LOG_H_
00013 
00014 #include <sys/types.h>
00015 #include <stdarg.h>
00016 #include <u/libu.h>
00017 
00018 #ifdef __cplusplus
00019 extern "C" {
00020 #endif 
00021 
00022 /* log levels, from low to high */
00023 enum {
00024     KLOG_DEBUG,
00025     KLOG_INFO,
00026     KLOG_NOTICE,
00027     KLOG_WARNING,
00028     KLOG_ERR,
00029     KLOG_CRIT,
00030     KLOG_ALERT,
00031     KLOG_EMERG,
00032     KLOG_LEVEL_UNKNOWN    /* stopper */
00033 };
00034 
00035 /* internal representation of a 'log' config section */
00036 struct klog_args_s
00037 {
00038     int type;           /* one of KLOG_TYPEs */
00039     char *ident;        /* string prepended to each log msg */
00040     int threshold;      /* filter log msgs lower than this level */
00041     int mlimit;         /* max number of log messages (memory) */
00042     char *fbasename;    /* basename of log files (postfix varies) */
00043     int fsplits;        /* number of split files (file) */
00044     int flimit;         /* number of log msgs per file (file) */
00045     int soptions;       /* log options (syslog) */
00046     int sfacility;      /* default facility (syslog's LOG_LOCAL[0-7]) */
00047 #define KLOG_FACILITY_UNKNOWN   -1
00048 };  
00049     
00050 typedef struct klog_args_s klog_args_t;
00051 
00052 #define KLOG_LN_SZ          512 /* maximum log line size */
00053 #define KLOG_ID_SZ          8   /* maximum log id size */
00054 #define KLOG_MLIMIT_DFL     250 /* default number of log lines (mem) */
00055 #define KLOG_FLIMIT_DFL     250 /* default number of log lines (file) */
00056 #define KLOG_FSPLITS_DFL    4   /* default number of log files (file) */
00057 
00058 /* a log line is at most KLOG_LN_SZ + 1 bytes long (including encoded
00059  * timestamp and severity) */
00060 struct klog_mem_msg_s
00061 {
00062     int level;          /* log severity */
00063     time_t timestamp;   /* message timestamp */
00064     char *line;         /* log line */
00065     TAILQ_ENTRY(klog_mem_msg_s) next;
00066 };
00067 
00068 typedef struct klog_mem_msg_s klog_mem_msg_t;
00069 
00070 /* klog_mem_msg_s' organised in a fixed size buffer with FIFO discard policy */
00071 struct klog_mem_s
00072 {
00073     size_t bound;                           /* FIFO buffer max size */
00074     size_t nmsgs;                           /* # of msgs in buffer */
00075 #define KLOG_MEM_FULL(klm)  ((klm)->nmsgs >= (klm)->bound)
00076     TAILQ_HEAD(mh, klog_mem_msg_s) msgs;    /* the list of msgs */
00077 };
00078 
00079 typedef struct klog_mem_s klog_mem_t;
00080 
00081 struct klog_file_s
00082 {
00083     size_t npages;  /* number of available log pages */
00084     size_t nlines;  /* number of available log lines per page */
00085     size_t wpageid; /* working page id */
00086     size_t offset;  /* write offset in working page */
00087     char basename[U_FILENAME_MAX];
00088 #define KLOG_PAGE_FULL(klf)  ((klf)->offset >= (klf)->nlines)
00089     FILE *wfp;      /* working page file pointer */
00090 };
00091 
00092 typedef struct klog_file_s klog_file_t;
00093 
00094 /* a syslog(3) wrapper  */
00095 struct klog_syslog_s
00096 {
00097     int facility;   /* default syslog(3) facility */
00098     int logopt;     /* log options bit field */
00099 };
00100 
00101 typedef struct klog_syslog_s klog_syslog_t;
00102 
00103 struct klog_s
00104 {
00105     enum { 
00106         KLOG_TYPE_UNKNOWN, 
00107         KLOG_TYPE_MEM, 
00108         KLOG_TYPE_FILE, 
00109         KLOG_TYPE_SYSLOG 
00110     } type;
00111 
00112 #define IS_KLOG_TYPE(t) (t >= KLOG_TYPE_MEM && t <= KLOG_TYPE_SYSLOG)
00113 
00114     int threshold;                  /* min unfiltered level */
00115     char ident[KLOG_ID_SZ + 1];     /* id string prepended to each log msg */
00116 
00117     /* data private to each log type */
00118     union
00119     {
00120         klog_mem_t *m;
00121         klog_syslog_t *s;
00122         klog_file_t *f; 
00123     } u;
00124 
00125     /* availability of the following depends on klog_s type */
00126     int (*cb_log) (struct klog_s *, int, const char *, va_list);
00127     void (*cb_close) (struct klog_s *);
00128     int (*cb_getln) (struct klog_s *, size_t, char[]);
00129     ssize_t (*cb_countln) (struct klog_s *);
00130     int (*cb_clear) (struct klog_s *);
00131     int (*cb_flush) (struct klog_s *);
00132 };
00133 
00134 typedef struct klog_s klog_t;
00135 
00136 int klog_open (klog_args_t *ka, klog_t **pkl);
00137 int klog (klog_t *kl, int level, const char *msg, ...);
00138 void klog_close (klog_t *kl);
00139 
00140 /* file device specific */
00141 int klog_flush (klog_t *kl);
00142 
00143 /* mem device specific */
00144 int klog_getln (klog_t *kl, size_t nth, char ln[]);
00145 ssize_t klog_countln (klog_t *kl);
00146 int klog_clear (klog_t *kl);
00147 
00148 int klog_args (u_config_t *logsect, klog_args_t **pka);
00149 void klog_args_free (klog_args_t *ka);
00150 int klog_open_from_config (u_config_t *ls, klog_t **pkl);
00151 
00152 #ifdef __cplusplus
00153 }
00154 #endif 
00155 
00156 #endif  /* _KLONE_LOG_H_ */

←Products
Copyright © 2005-2012 - KoanLogic S.r.l. - All rights reserved