UniMRCP  1.7.0
apt_log.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2015 Arsen Chaloyan
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef APT_LOG_H
18 #define APT_LOG_H
19 
20 /**
21  * @file apt_log.h
22  * @brief Basic Logger
23  */
24 
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include "apt.h"
28 
30 
31 /** Default max size of the log file (8Mb) */
32 #define MAX_LOG_FILE_SIZE (8 * 1024 * 1024)
33 /** Default max number of log files used in rotation */
34 #define MAX_LOG_FILE_COUNT 100
35 
36 /** Opaque log source declaration */
38 
39 /** Declaration of log mark to be used by custom log sources */
40 #define APT_LOG_MARK_DECLARE(LOG_SOURCE) LOG_SOURCE,__FILE__,__LINE__
41 
42 /** Use this macro in a header file to declare a custom log source */
43 #define APT_LOG_SOURCE_DECLARE(SCOPE,LOG_SOURCE) \
44  extern apt_log_source_t *LOG_SOURCE; \
45  SCOPE##_DECLARE(void) LOG_SOURCE##_init();
46 
47 /** Use this macro in a source file to implement a custom log source */
48 #define APT_LOG_SOURCE_IMPLEMENT(SCOPE, LOG_SOURCE, LOG_SOURCE_TAG) \
49  apt_log_source_t *LOG_SOURCE = &def_log_source; \
50  SCOPE##_DECLARE(void) LOG_SOURCE##_init() {apt_log_source_assign(LOG_SOURCE_TAG,&LOG_SOURCE);}
51 
52 /** Default (globally available) log source */
54 /** Default log mark providing log source, file and line information */
55 #define APT_LOG_MARK APT_LOG_MARK_DECLARE(&def_log_source)
56 
57 /*
58  * Definition of common formats used with apt_log().
59  *
60  * Note that the generic %p format can not be used for pointers
61  * since apr_vformatter doesn't accept it. The format %pp introduced
62  * by apr_vformatter can not be used either since it breaks compatibility
63  * with generic printf style loggers.
64  */
65 #if defined(WIN32) && APR_SIZEOF_VOIDP == 8
66 /** Format to log pointer values on Win x64 */
67 #define APT_PTR_FMT "0x%I64x"
68 #else
69 /** Format to log pointer values */
70 #define APT_PTR_FMT "0x%lx"
71 #endif
72 /** Format to log string identifiers */
73 #define APT_SID_FMT "<%s>"
74 /** Format to log string identifiers and resources */
75 #define APT_SIDRES_FMT "<%s@%s>"
76 /** Format to log pointers and identifiers */
77 #define APT_PTRSID_FMT APT_PTR_FMT " " APT_SID_FMT
78 /** Format to log names and identifiers */
79 #define APT_NAMESID_FMT "%s " APT_SID_FMT
80 /** Format to log names, identifiers and resources */
81 #define APT_NAMESIDRES_FMT "%s " APT_SIDRES_FMT
82 
83 /** Priority of log messages ordered from highest priority to lowest (rfc3164) */
84 typedef enum {
85  APT_PRIO_EMERGENCY, /**< system is unusable */
86  APT_PRIO_ALERT, /**< action must be taken immediately */
87  APT_PRIO_CRITICAL, /**< critical condition */
88  APT_PRIO_ERROR, /**< error condition */
89  APT_PRIO_WARNING, /**< warning condition */
90  APT_PRIO_NOTICE, /**< normal, but significant condition */
91  APT_PRIO_INFO, /**< informational message */
92  APT_PRIO_DEBUG, /**< debug-level message */
93 
94  APT_PRIO_COUNT /**< number of priorities */
96 
97 /** Header (format) of log messages */
98 typedef enum {
99  APT_LOG_HEADER_NONE = 0x00, /**< disable optional headers output */
100  APT_LOG_HEADER_DATE = 0x01, /**< enable date output */
101  APT_LOG_HEADER_TIME = 0x02, /**< enable time output */
102  APT_LOG_HEADER_PRIORITY = 0x04, /**< enable priority name output */
103  APT_LOG_HEADER_MARK = 0x08, /**< enable file:line mark output */
104  APT_LOG_HEADER_THREAD = 0x10, /**< enable thread identifier output */
105 
108 
109 /** Mode of log output */
110 typedef enum {
111  APT_LOG_OUTPUT_NONE = 0x00, /**< disable logging */
112  APT_LOG_OUTPUT_CONSOLE = 0x01, /**< enable console output */
113  APT_LOG_OUTPUT_FILE = 0x02, /**< enable log file output */
114  APT_LOG_OUTPUT_SYSLOG = 0x04 /**< enable syslog output */
116 
117 /** Masking mode of private data */
118 typedef enum {
119  APT_LOG_MASKING_NONE, /**< log everything as is */
120  APT_LOG_MASKING_COMPLETE, /**< mask private data completely */
121  APT_LOG_MASKING_ENCRYPTED /**< encrypt private data */
123 
124 /** Opaque logger declaration */
125 typedef struct apt_logger_t apt_logger_t;
126 
127 /** Prototype of extended log handler function */
128 typedef apt_bool_t (*apt_log_ext_handler_f)(const char *file, int line,
129  const char *obj, apt_log_priority_e priority,
130  const char *format, va_list arg_ptr);
131 
132 /**
133  * Create the singleton instance of the logger.
134  * @param mode the log output mode
135  * @param priority the log priority level
136  * @param pool the memory pool to use
137  */
139 
140 /**
141  * Create and load the singleton instance of the logger.
142  * @param config_file the path to configuration file to load settings from
143  * @param pool the memory pool to use
144  */
145 APT_DECLARE(apt_bool_t) apt_log_instance_load(const char *config_file, apr_pool_t *pool);
146 
147 /**
148  * Destroy the singleton instance of the logger.
149  */
151 
152 /**
153  * Get the singleton instance of the logger.
154  */
156 
157 /**
158  * Set the singleton instance of the logger.
159  * @param logger the logger to set
160  */
162 
163 /**
164  * Set the default log source.
165  * @param log_source the log source to set
166  */
168 
169 /**
170  * Find and assign log source by its name.
171  * @param name the unique name associated to the log source
172  * @param log_source the log source to be returned, if found
173  */
174 APT_DECLARE(apt_bool_t) apt_log_source_assign(const char *name, apt_log_source_t **log_source);
175 
176 /**
177  * Open the log file.
178  * @param dir_path the path to the log directory
179  * @param file_name the name of the log file
180  * @param max_file_size the max size of the log file
181  * @param max_file_count the max number of files used in log rotation
182  * @param append whether to append or to truncate (start over) the log file
183  * @param pool the memory pool to use
184  * @deprecated @see apt_log_file_open_ex()
185  */
187  const char *dir_path,
188  const char *file_name,
189  apr_size_t max_file_size,
190  apr_size_t max_file_count,
191  apt_bool_t append,
192  apr_pool_t *pool);
193 
194 /**
195  * Open the log file (extended version).
196  * @param dir_path the path to the log directory
197  * @param prefix the prefix used to compose the log file name
198  * @param config_file the path to configuration file to load settings from
199  * @param pool the memory pool to use
200  */
201 APT_DECLARE(apt_bool_t) apt_log_file_open_ex(const char *dir_path, const char *prefix, const char *config_file, apr_pool_t *pool);
202 
203 /**
204  * Close the log file.
205  */
207 
208 /**
209  * Open the syslog.
210  * @param prefix the prefix used to compose the log file name
211  * @param config_file the path to configuration file to load settings from
212  * @param pool the memory pool to use
213  */
214 APT_DECLARE(apt_bool_t) apt_syslog_open(const char *prefix, const char *config_file, apr_pool_t *pool);
215 
216 /**
217  * Close the syslog.
218  */
220 
221 /**
222  * Set the logging output mode.
223  * @param mode the mode to set
224  */
226 
227 /**
228  * Check the logging output mode to be enabled (set) or not.
229  * @param mode the mode to check
230  */
232 
233 /**
234  * Translate the output mode string to bitmask of apt_log_output_e values.
235  * @param str the string to translate
236  */
238 
239 /**
240  * Set the logging priority (log level).
241  * @param priority the priority to set
242  */
244 
245 /**
246  * Translate the priority (log level) string to enum.
247  * @param str the string to translate
248  */
250 
251 /**
252  * Set the header (format) for log messages.
253  * @param header the header to set (used as bitmask)
254  */
256 
257 /**
258  * Translate the header string to bitmask of apt_log_header_e values.
259  * @param str the string to translate
260  */
261 APT_DECLARE(int) apt_log_header_translate(char *str);
262 
263 /**
264  * Set the masking mode of private data.
265  * @param masking the masking mode to set
266  */
268 
269 /**
270  * Get the current masking mode of private data.
271  */
273 
274 /**
275  * Translate the masking mode string to enum.
276  * @param str the string to translate
277  */
279 
280 /**
281  * Mask private data based on the masking mode.
282  * @param data_in the data to mask
283  * @param length the length of the data to mask on input, the length of the masked data on output
284  * @param pool the memory pool to use if needed
285  * @return The masked data.
286  */
287 APT_DECLARE(const char*) apt_log_data_mask(const char *data_in, apr_size_t *length, apr_pool_t *pool);
288 
289 /**
290  * Set the extended external log handler.
291  * @param handler the handler to pass log events to
292  * @remark default logger is used to output the logs to stdout and/or log file,
293  * if external log handler isn't set
294  */
296 
297 /**
298  * Do logging.
299  * @param log_source the log source
300  * @param file the file name log entry is generated from
301  * @param line the line number log entry is generated from
302  * @param priority the priority of the entire log entry
303  * @param format the format of the entire log entry
304  */
305 APT_DECLARE(apt_bool_t) apt_log(apt_log_source_t *log_source, const char *file, int line, apt_log_priority_e priority, const char *format, ...);
306 
307 /**
308  * Do logging (this version uses an object externally associated with the logger).
309  * @param log_source the log source
310  * @param file the file name log entry is generated from
311  * @param line the line number log entry is generated from
312  * @param priority the priority of the entire log entry
313  * @param obj the associated object
314  * @param format the format of the entire log entry
315  */
316 APT_DECLARE(apt_bool_t) apt_obj_log(apt_log_source_t *log_source, const char *file, int line, apt_log_priority_e priority, void *obj, const char *format, ...);
317 
318 /**
319  * Do logging (this version accepts va_list argument).
320  * @param log_source the log source
321  * @param file the file name log entry is generated from
322  * @param line the line number log entry is generated from
323  * @param priority the priority of the entire log entry
324  * @param format the format of the entire log entry
325  * @param arg_ptr the arguments
326  */
327 APT_DECLARE(apt_bool_t) apt_va_log(apt_log_source_t *log_source, const char *file, int line, apt_log_priority_e priority, const char *format, va_list arg_ptr);
328 
330 
331 #endif /* APT_LOG_H */
Definition: apt_log.h:86
Definition: apt_log.h:119
apt_bool_t apt_log_instance_set(apt_logger_t *logger)
apt_bool_t apt_log_file_open_ex(const char *dir_path, const char *prefix, const char *config_file, apr_pool_t *pool)
apt_bool_t apt_log(apt_log_source_t *log_source, const char *file, int line, apt_log_priority_e priority, const char *format,...)
apt_bool_t(* apt_log_ext_handler_f)(const char *file, int line, const char *obj, apt_log_priority_e priority, const char *format, va_list arg_ptr)
Definition: apt_log.h:128
#define APT_END_EXTERN_C
Definition: apt.h:38
Definition: apt_log.h:100
int apt_bool_t
Definition: apt.h:57
Definition: apt_log.h:112
Definition: apt_log.h:90
apt_bool_t apt_log_priority_set(apt_log_priority_e priority)
Definition: apt_log.h:102
apt_log_masking_e
Definition: apt_log.h:118
Definition: apt_log.h:113
Definition: apt_log.h:88
apt_bool_t apt_log_file_close(void)
apt_log_header_e
Definition: apt_log.h:98
int apt_log_output_mode_translate(char *str)
apt_bool_t apt_log_instance_destroy(void)
apt_bool_t apt_syslog_close(void)
void apt_def_log_source_set(apt_log_source_t *log_source)
#define APT_BEGIN_EXTERN_C
Definition: apt.h:36
apt_bool_t apt_log_header_set(int header)
Definition: apt_log.h:111
#define APT_DECLARE(type)
Definition: apt.h:53
apt_bool_t apt_va_log(apt_log_source_t *log_source, const char *file, int line, apt_log_priority_e priority, const char *format, va_list arg_ptr)
Definition: apt_log.h:92
APR Toolkit Definitions.
apt_bool_t apt_log_output_mode_set(apt_log_output_e mode)
apt_log_source_t def_log_source
struct apt_log_source_t apt_log_source_t
Definition: apt_log.h:37
Definition: apt_log.h:85
apt_bool_t apt_syslog_open(const char *prefix, const char *config_file, apr_pool_t *pool)
apt_bool_t apt_log_source_assign(const char *name, apt_log_source_t **log_source)
apt_bool_t apt_obj_log(apt_log_source_t *log_source, const char *file, int line, apt_log_priority_e priority, void *obj, const char *format,...)
Definition: apt_log.h:89
apt_bool_t apt_log_file_open(const char *dir_path, const char *file_name, apr_size_t max_file_size, apr_size_t max_file_count, apt_bool_t append, apr_pool_t *pool)
Definition: apt_log.h:114
apt_bool_t apt_log_instance_load(const char *config_file, apr_pool_t *pool)
apt_log_masking_e apt_log_masking_translate(const char *str)
apt_log_priority_e apt_log_priority_translate(const char *str)
apt_logger_t * apt_log_instance_get(void)
Definition: apt_log.h:87
Definition: apt_log.h:103
Definition: apt_log.h:104
int apt_log_header_translate(char *str)
apt_bool_t apt_log_ext_handler_set(apt_log_ext_handler_f handler)
apt_bool_t apt_log_instance_create(apt_log_output_e mode, apt_log_priority_e priority, apr_pool_t *pool)
Definition: apt_log.h:99
Definition: apt_log.h:120
struct apt_logger_t apt_logger_t
Definition: apt_log.h:125
apt_bool_t apt_log_output_mode_check(apt_log_output_e mode)
const char * apt_log_data_mask(const char *data_in, apr_size_t *length, apr_pool_t *pool)
apt_log_output_e
Definition: apt_log.h:110
Definition: apt_log.h:121
Definition: apt_log.h:101
apt_bool_t apt_log_masking_set(apt_log_masking_e masking)
Definition: apt_log.h:91
apt_log_masking_e apt_log_masking_get(void)
apt_log_priority_e
Definition: apt_log.h:84
Definition: apt_log.h:94