1 /*
2  * Copyright 2008-2014 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  * $Id: apt_log.h 2136 2014-07-04 06:33:36Z achaloyan@gmail.com $
17  */
18 
19 #ifndef APT_LOG_H
20 #define APT_LOG_H
21 
22 /**
23  * @file apt_log.h
24  * @brief Basic Logger
25  */
26 
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include "apt.h"
30 
31 APT_BEGIN_EXTERN_C
32 
33 /** Default max size of the log file (8Mb) */
34 #define MAX_LOG_FILE_SIZE (8 * 1024 * 1024)
35 /** Default max number of log files used in rotation */
36 #define MAX_LOG_FILE_COUNT 100
37 
38 /** File:line mark */
39 #define APT_LOG_MARK   __FILE__,__LINE__
40 
41 /*
42  * Definition of common formats used with apt_log().
43  *
44  * Note that the generic %p format can not be used for pointers
45  * since apr_vformatter doesn't accept it. The format %pp introduced
46  * by apr_vformatter can not be used either since it breaks compatibility
47  * with generic printf style loggers.
48  */
49 #if defined(WIN32) && APR_SIZEOF_VOIDP == 8
50 /** Format to log pointer values on Win x64 */
51 #define APT_PTR_FMT       "0x%I64x"
52 #else
53 /** Format to log pointer values */
54 #define APT_PTR_FMT       "0x%lx"
55 #endif
56 /** Format to log string identifiers */
57 #define APT_SID_FMT       "<%s>"
58 /** Format to log string identifiers and resources */
59 #define APT_SIDRES_FMT    "<%s@%s>"
60 /** Format to log pointers and identifiers */
61 #define APT_PTRSID_FMT    APT_PTR_FMT" " APT_SID_FMT
62 /** Format to log pointers and identifiers */
63 #define APT_NAMESID_FMT   "%s " APT_SID_FMT
64 /** Format to log names, identifiers and resources */
65 #define APT_NAMESIDRES_FMT "%s " APT_SIDRES_FMT
66 
67 /** Priority of log messages ordered from highest priority to lowest (rfc3164) */
68 typedef enum {
69 	APT_PRIO_EMERGENCY, /**< system is unusable */
70 	APT_PRIO_ALERT,     /**< action must be taken immediately */
71 	APT_PRIO_CRITICAL,  /**< critical condition */
72 	APT_PRIO_ERROR,     /**< error condition */
73 	APT_PRIO_WARNING,   /**< warning condition */
74 	APT_PRIO_NOTICE,    /**< normal, but significant condition */
75 	APT_PRIO_INFO,      /**< informational message */
76 	APT_PRIO_DEBUG,     /**< debug-level message */
77 
78 	APT_PRIO_COUNT     	/**< number of priorities */
79 } apt_log_priority_e;
80 
81 /** Header (format) of log messages */
82 typedef enum {
83 	APT_LOG_HEADER_NONE     = 0x00, /**< disable optional headers output */
84 	APT_LOG_HEADER_DATE     = 0x01, /**< enable date output */
85 	APT_LOG_HEADER_TIME     = 0x02, /**< enable time output */
86 	APT_LOG_HEADER_PRIORITY = 0x04, /**< enable priority name output */
87 	APT_LOG_HEADER_MARK     = 0x08, /**< enable file:line mark output */
88 	APT_LOG_HEADER_THREAD   = 0x10, /**< enable thread identifier output */
89 
90 	APT_LOG_HEADER_DEFAULT  = APT_LOG_HEADER_DATE | APT_LOG_HEADER_TIME | APT_LOG_HEADER_PRIORITY
91 } apt_log_header_e;
92 
93 /** Mode of log output */
94 typedef enum {
95 	APT_LOG_OUTPUT_NONE     = 0x00, /**< disable logging */
96 	APT_LOG_OUTPUT_CONSOLE  = 0x01, /**< enable console output */
97 	APT_LOG_OUTPUT_FILE     = 0x02  /**< enable log file output */
98 } apt_log_output_e;
99 
100 /** Masking mode of private data */
101 typedef enum {
102 	APT_LOG_MASKING_NONE,      /**< log everything as is */
103 	APT_LOG_MASKING_COMPLETE,  /**< mask private data completely */
104 	APT_LOG_MASKING_ENCRYPTED  /**< encrypt private data */
105 } apt_log_masking_e;
106 
107 /** Opaque logger declaration */
108 typedef struct apt_logger_t apt_logger_t;
109 
110 /** Prototype of extended log handler function */
111 typedef apt_bool_t (*apt_log_ext_handler_f)(const char *file, int line,
112 											const char *obj, apt_log_priority_e priority,
113 											const char *format, va_list arg_ptr);
114 
115 /**
116  * Create the singleton instance of the logger.
117  * @param mode the log output mode
118  * @param priority the log priority level
119  * @param pool the memory pool to use
120  */
121 APT_DECLARE(apt_bool_t) apt_log_instance_create(apt_log_output_e mode, apt_log_priority_e priority, apr_pool_t *pool);
122 
123 /**
124  * Create and load the singleton instance of the logger.
125  * @param config_file the path to configuration file to load settings from
126  * @param pool the memory pool to use
127  */
128 APT_DECLARE(apt_bool_t) apt_log_instance_load(const char *config_file, apr_pool_t *pool);
129 
130 /**
131  * Destroy the singleton instance of the logger.
132  */
133 APT_DECLARE(apt_bool_t) apt_log_instance_destroy(void);
134 
135 /**
136  * Get the singleton instance of the logger.
137  */
138 APT_DECLARE(apt_logger_t*) apt_log_instance_get(void);
139 
140 /**
141  * Set the singleton instance of the logger.
142  */
143 APT_DECLARE(apt_bool_t) apt_log_instance_set(apt_logger_t *logger);
144 
145 /**
146  * Open the log file.
147  * @param dir_path the path to the log directory
148  * @param file_name the name of the log file
149  * @param max_file_size the max size of the log file
150  * @param max_file_count the max number of files used in log rotation
151  * @param append whether to append or to truncate (start over) the log file
152  * @param pool the memory pool to use
153  */
154 APT_DECLARE(apt_bool_t) apt_log_file_open(
155 							const char *dir_path,
156 							const char *file_name,
157 							apr_size_t max_file_size,
158 							apr_size_t max_file_count,
159 							apt_bool_t append,
160 							apr_pool_t *pool);
161 
162 /**
163  * Close the log file.
164  */
165 APT_DECLARE(apt_bool_t) apt_log_file_close(void);
166 
167 /**
168  * Set the logging output mode.
169  * @param mode the mode to set
170  */
171 APT_DECLARE(apt_bool_t) apt_log_output_mode_set(apt_log_output_e mode);
172 
173 /**
174  * Check the logging output mode to be enabled (set) or not.
175  * @param mode the mode to check
176  */
177 APT_DECLARE(apt_bool_t) apt_log_output_mode_check(apt_log_output_e mode);
178 
179 /**
180  * Translate the output mode string to bitmask of apt_log_output_e values.
181  * @param str the string to translate
182  */
183 APT_DECLARE(int) apt_log_output_mode_translate(char *str);
184 
185 /**
186  * Set the logging priority (log level).
187  * @param priority the priority to set
188  */
189 APT_DECLARE(apt_bool_t) apt_log_priority_set(apt_log_priority_e priority);
190 
191 /**
192  * Translate the priority (log level) string to enum.
193  * @param str the string to translate
194  */
195 APT_DECLARE(apt_log_priority_e) apt_log_priority_translate(const char *str);
196 
197 /**
198  * Set the header (format) for log messages.
199  * @param header the header to set (used as bitmask)
200  */
201 APT_DECLARE(apt_bool_t) apt_log_header_set(int header);
202 
203 /**
204  * Translate the header string to bitmask of apt_log_header_e values.
205  * @param str the string to translate
206  */
207 APT_DECLARE(int) apt_log_header_translate(char *str);
208 
209 /**
210  * Set the masking mode of private data.
211  * @param masking the masking mode to set
212  */
213 APT_DECLARE(apt_bool_t) apt_log_masking_set(apt_log_masking_e masking);
214 
215 /**
216  * Get the current masking mode of private data.
217  */
218 APT_DECLARE(apt_log_masking_e) apt_log_masking_get(void);
219 
220 /**
221  * Translate the masking mode string to enum.
222  * @param str the string to translate
223  */
224 APT_DECLARE(apt_log_masking_e) apt_log_masking_translate(const char *str);
225 
226 /**
227  * Mask private data based on the masking mode
228  * @param data_in the data to mask
229  * @param length the length of the data to mask on input, the length of the masked data on output
230  * @param pool the memory pool to use if needed
231  * @return The masked data.
232  */
233 APT_DECLARE(const char*) apt_log_data_mask(const char *data_in, apr_size_t *length, apr_pool_t *pool);
234 
235 /**
236  * Set the extended external log handler.
237  * @param handler the handler to pass log events to
238  * @remark default logger is used to output the logs to stdout and/or log file,
239  *         if external log handler isn't set
240  */
241 APT_DECLARE(apt_bool_t) apt_log_ext_handler_set(apt_log_ext_handler_f handler);
242 
243 /**
244  * Do logging.
245  * @param file the file name log entry is generated from
246  * @param line the line number log entry is generated from
247  * @param priority the priority of the entire log entry
248  * @param format the format of the entire log entry
249  */
250 APT_DECLARE(apt_bool_t) apt_log(const char *file, int line, apt_log_priority_e priority, const char *format, ...);
251 
252 /**
253  * Do logging (this version uses an object externally associated with the logger).
254  * @param file the file name log entry is generated from
255  * @param line the line number log entry is generated from
256  * @param priority the priority of the entire log entry
257  * @param obj the associated object
258  * @param format the format of the entire log entry
259  */
260 APT_DECLARE(apt_bool_t) apt_obj_log(const char *file, int line, apt_log_priority_e priority, void *obj, const char *format, ...);
261 
262 /**
263  * Do logging (this version accepts va_list argument).
264  * @param file the file name log entry is generated from
265  * @param line the line number log entry is generated from
266  * @param priority the priority of the entire log entry
267  * @param format the format of the entire log entry
268  * @param arg_ptr the arguments
269  */
270 APT_DECLARE(apt_bool_t) apt_va_log(const char *file, int line, apt_log_priority_e priority, const char *format, va_list arg_ptr);
271 
272 APT_END_EXTERN_C
273 
274 #endif /* APT_LOG_H */
275