1 /***************************************************************************
2  $RCSfile$
3                              -------------------
4     cvs         : $Id$
5     begin       : Sun Dec 05 2003
6     copyright   : (C) 2003 by Martin Preuss
7     email       : martin@libchipcard.de
8 
9  ***************************************************************************
10  *                                                                         *
11  *   This library is free software; you can redistribute it and/or         *
12  *   modify it under the terms of the GNU Lesser General Public            *
13  *   License as published by the Free Software Foundation; either          *
14  *   version 2.1 of the License, or (at your option) any later version.    *
15  *                                                                         *
16  *   This library is distributed in the hope that it will be useful,       *
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
19  *   Lesser General Public License for more details.                       *
20  *                                                                         *
21  *   You should have received a copy of the GNU Lesser General Public      *
22  *   License along with this library; if not, write to the Free Software   *
23  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *
24  *   MA  02111-1307  USA                                                   *
25  *                                                                         *
26  ***************************************************************************/
27 
28 #ifndef GWEN_LOGGER_H
29 #define GWEN_LOGGER_H
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 #define GWEN_LOGDOMAIN "gwenhywfar"
36 
37 #include <gwenhywfar/gwenhywfarapi.h>
38 #include <gwenhywfar/buffer.h>
39 
40 
41 typedef void GWENHYWFAR_CB(*GWEN_LOGGERFUNCTIONLOG)(const char *s);
42 
43 typedef enum {
44   GWEN_LoggerType_Console,
45   GWEN_LoggerType_File,
46   GWEN_LoggerType_Syslog,
47   GWEN_LoggerType_Function,
48 
49   GWEN_LoggerType_Unknown=9999
50 } GWEN_LOGGER_LOGTYPE;
51 
52 
53 typedef enum {
54   GWEN_LoggerFacility_Auth=0,
55   GWEN_LoggerFacility_Daemon,
56   GWEN_LoggerFacility_Mail,
57   GWEN_LoggerFacility_News,
58   GWEN_LoggerFacility_User,
59 
60   GWEN_LoggerFacility_Unknown=9999
61 } GWEN_LOGGER_FACILITY;
62 
63 
64 typedef enum {
65   GWEN_LoggerLevel_Emergency=0,
66   GWEN_LoggerLevel_Alert,
67   GWEN_LoggerLevel_Critical,
68   GWEN_LoggerLevel_Error,
69   GWEN_LoggerLevel_Warning,
70   GWEN_LoggerLevel_Notice,
71   GWEN_LoggerLevel_Info,
72   GWEN_LoggerLevel_Debug,
73   GWEN_LoggerLevel_Verbous,
74 
75   GWEN_LoggerLevel_Unknown=9999
76 } GWEN_LOGGER_LEVEL;
77 
78 
79 
80 
81 /**
82  * Checks whether a given log domain already exists.
83  * @return 1 if it exists, 0 otherwise
84  */
85 GWENHYWFAR_API int GWEN_Logger_Exists(const char *logDomain);
86 
87 
88 /**
89  * Sets up logging. It automatically enables logging.
90  * @author Martin Preuss<martin@libchipcard.de>
91  * @param ident this string is prepended to each message logged to identify
92  * the logging program
93  * @param file name of the file to log to. If this is empty and syslog is
94  * available, then all messages are logged via syslog. If syslog is not
95  * available, all messages are logged to the console.
96  * @param logtype how to log (via syslog, to a file, to the console etc)
97  * @param facility what kind of program the log message comes from
98  */
99 GWENHYWFAR_API int GWEN_Logger_Open(const char *logDomain,
100                                     const char *ident,
101                                     const char *file,
102                                     GWEN_LOGGER_LOGTYPE logtype,
103                                     GWEN_LOGGER_FACILITY facility);
104 
105 /**
106  * Shuts down logging. Automatically disables logging.
107  * @author Martin Preuss<martin@libchipcard.de>
108  */
109 GWENHYWFAR_API void GWEN_Logger_Close(const char *logDomain);
110 
111 /**
112  * Checks whether the logger for the given logDomain is open or not.
113  */
114 GWENHYWFAR_API int GWEN_Logger_IsOpen(const char *logDomain);
115 
116 
117 /**
118  * Log a message.
119  * @author Martin Preuss<martin@libchipcard.de>
120  * @param priority priority of the message
121  * @param s string to log. This string is cut at all occurences of a newline
122  * character thus splitting it into multiple log lines if necessary
123  */
124 GWENHYWFAR_API void GWEN_Logger_Log(const char *logDomain,
125                                     GWEN_LOGGER_LEVEL priority, const char *s);
126 
127 /**
128  * Enables or disables logging.
129  * @author Martin Preuss<martin@libchipcard.de>
130  * @param f if 0 then logging is disabled, otherwise it is enabled
131  */
132 GWENHYWFAR_API void GWEN_Logger_Enable(const char *logDomain,
133                                        int f);
134 
135 /**
136  * Checks whether logging is enabled.
137  * @author Martin Preuss<martin@libchipcard.de>
138  * @return 0 if disabled, 1 otherwise
139  */
140 GWENHYWFAR_API int GWEN_Logger_IsEnabled(const char *logDomain);
141 
142 /**
143  * Sets the logger level. All messages with a priority up to the given one
144  * will be logged, all others will not.
145  * @author Martin Preuss<martin@libchipcard.de>
146  * @param l maximum level to be logged
147  */
148 GWENHYWFAR_API void GWEN_Logger_SetLevel(const char *logDomain,
149                                          GWEN_LOGGER_LEVEL l);
150 
151 /**
152  * Returns the current log level.
153  * @author Martin Preuss<martin@libchipcard.de>
154  */
155 GWENHYWFAR_API int GWEN_Logger_GetLevel(const char *logDomain);
156 
157 
158 /**
159  * Set ident string. This string is prepended to every log message and
160  * should contain the name of the running program.
161  * @author Martin Preuss<martin@libchipcard.de>
162  */
163 GWENHYWFAR_API void GWEN_Logger_SetIdent(const char *logDomain,
164                                          const char *id);
165 
166 /**
167  * Set the name of the file to be used when logging to a file.
168  */
169 void GWEN_Logger_SetFilename(const char *logDomain, const char *name);
170 
171 
172 /**
173  * Set logging function. This function is used to log messages in mode
174  * LoggerTypeFunction.
175  * @author Martin Preuss<martin@libchipcard.de>
176  */
177 GWENHYWFAR_API
178 GWEN_LOGGERFUNCTIONLOG GWEN_Logger_SetLogFunction(const char *logDomain,
179                                                   GWEN_LOGGERFUNCTIONLOG fn);
180 
181 /**
182  * Transforms an ASCII string to a level value.
183  */
184 GWENHYWFAR_API
185 GWEN_LOGGER_LEVEL GWEN_Logger_Name2Level(const char *name);
186 
187 
188 /**
189  * Transforms a logger level to an ASCII string (for config files,
190  * command line options etc).
191  */
192 GWENHYWFAR_API
193 const char *GWEN_Logger_Level2Name(GWEN_LOGGER_LEVEL level);
194 
195 
196 /**
197  * Transforms an ASCII string into the corresponding log type.
198  */
199 GWENHYWFAR_API
200 GWEN_LOGGER_LOGTYPE GWEN_Logger_Name2Logtype(const char *name);
201 
202 /**
203  * Transforms a log type into an ASCII string.
204  */
205 GWENHYWFAR_API
206 const char *GWEN_Logger_Logtype2Name(GWEN_LOGGER_LOGTYPE lt);
207 
208 
209 /**
210  * This function can be used to generate log messages from within log hooks.
211  */
212 GWENHYWFAR_API
213 int GWEN_Logger_CreateLogMessage(const char *logDomain,
214                                  GWEN_LOGGER_LEVEL priority, const char *s,
215                                  GWEN_BUFFER *mbuf);
216 
217 
218 #ifdef __cplusplus
219 }
220 #endif
221 
222 
223 #endif /* #ifndef CH_LOGGER_H */
224 
225 
226