00001 /* 00002 * Copyright 2008-2010 Arsen Chaloyan 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 * 00016 * $Id: apt_log.h 1792 2011-01-10 21:08:52Z achaloyan $ 00017 */ 00018 00019 #ifndef APT_LOG_H 00020 #define APT_LOG_H 00021 00022 /** 00023 * @file apt_log.h 00024 * @brief Basic Logger 00025 */ 00026 00027 #include <stdio.h> 00028 #include <stdarg.h> 00029 #include "apt.h" 00030 00031 APT_BEGIN_EXTERN_C 00032 00033 /** Default max size of the log file (8Mb) */ 00034 #define MAX_LOG_FILE_SIZE (8 * 1024 * 1024) 00035 /** Default max number of rotated log files */ 00036 #define MAX_LOG_FILE_COUNT 10 00037 00038 /** File:line mark */ 00039 #define APT_LOG_MARK __FILE__,__LINE__ 00040 00041 /** Format to log pointer values */ 00042 #define APT_PTR_FMT "0x%x" 00043 /** Format to log string identifiers */ 00044 #define APT_SID_FMT "<%s>" 00045 /** Format to log string identifiers and resources */ 00046 #define APT_SIDRES_FMT "<%s@%s>" 00047 /** Format to log pointers and identifiers */ 00048 #define APT_PTRSID_FMT APT_PTR_FMT" "APT_SID_FMT 00049 /** Format to log pointers and identifiers */ 00050 #define APT_NAMESID_FMT "%s "APT_SID_FMT 00051 /** Format to log names, identifiers and resources */ 00052 #define APT_NAMESIDRES_FMT "%s "APT_SIDRES_FMT 00053 00054 00055 /** Priority of log messages ordered from highest priority to lowest (rfc3164) */ 00056 typedef enum { 00057 APT_PRIO_EMERGENCY, /**< system is unusable */ 00058 APT_PRIO_ALERT, /**< action must be taken immediately */ 00059 APT_PRIO_CRITICAL, /**< critical condition */ 00060 APT_PRIO_ERROR, /**< error condition */ 00061 APT_PRIO_WARNING, /**< warning condition */ 00062 APT_PRIO_NOTICE, /**< normal, but significant condition */ 00063 APT_PRIO_INFO, /**< informational message */ 00064 APT_PRIO_DEBUG, /**< debug-level message */ 00065 00066 APT_PRIO_COUNT /**< number of priorities */ 00067 } apt_log_priority_e; 00068 00069 /** Header (format) of log messages */ 00070 typedef enum { 00071 APT_LOG_HEADER_NONE = 0x00, /**< disable optional headers output */ 00072 APT_LOG_HEADER_DATE = 0x01, /**< enable date output */ 00073 APT_LOG_HEADER_TIME = 0x02, /**< enable time output */ 00074 APT_LOG_HEADER_PRIORITY = 0x04, /**< enable priority name output */ 00075 APT_LOG_HEADER_MARK = 0x08, /**< enable file:line mark output */ 00076 APT_LOG_HEADER_THREAD = 0x10, /**< enable thread identifier output */ 00077 00078 APT_LOG_HEADER_DEFAULT = APT_LOG_HEADER_DATE | APT_LOG_HEADER_TIME | APT_LOG_HEADER_PRIORITY 00079 } apt_log_header_e; 00080 00081 /** Mode of log output */ 00082 typedef enum { 00083 APT_LOG_OUTPUT_NONE = 0x00, /**< disable logging */ 00084 APT_LOG_OUTPUT_CONSOLE = 0x01, /**< enable console output */ 00085 APT_LOG_OUTPUT_FILE = 0x02 /**< enable log file output */ 00086 } apt_log_output_e; 00087 00088 /** Masking mode of private data */ 00089 typedef enum { 00090 APT_LOG_MASKING_NONE, /**< log everything as is */ 00091 APT_LOG_MASKING_COMPLETE, /**< mask private data completely */ 00092 APT_LOG_MASKING_ENCRYPTED /**< encrypt private data */ 00093 } apt_log_masking_e; 00094 00095 /** Opaque logger declaration */ 00096 typedef struct apt_logger_t apt_logger_t; 00097 00098 /** Prototype of extended log handler function */ 00099 typedef apt_bool_t (*apt_log_ext_handler_f)(const char *file, int line, const char *obj, 00100 apt_log_priority_e priority, const char *format, va_list arg_ptr); 00101 00102 /** 00103 * Create the singleton instance of the logger. 00104 * @param mode the log output mode 00105 * @param priority the log priority level 00106 * @param pool the memory pool to use 00107 */ 00108 APT_DECLARE(apt_bool_t) apt_log_instance_create(apt_log_output_e mode, apt_log_priority_e priority, apr_pool_t *pool); 00109 00110 /** 00111 * Create and load the singleton instance of the logger. 00112 * @param config_file the path to configuration file to load settings from 00113 * @param pool the memory pool to use 00114 */ 00115 APT_DECLARE(apt_bool_t) apt_log_instance_load(const char *config_file, apr_pool_t *pool); 00116 00117 /** 00118 * Destroy the singleton instance of the logger. 00119 */ 00120 APT_DECLARE(apt_bool_t) apt_log_instance_destroy(void); 00121 00122 /** 00123 * Get the singleton instance of the logger. 00124 */ 00125 APT_DECLARE(apt_logger_t*) apt_log_instance_get(void); 00126 00127 /** 00128 * Set the singleton instance of the logger. 00129 */ 00130 APT_DECLARE(apt_bool_t) apt_log_instance_set(apt_logger_t *logger); 00131 00132 /** 00133 * Open the log file. 00134 * @param dir_path the path to the log directory 00135 * @param file_name the name of the log file 00136 * @param max_file_size the max size of the log file 00137 * @param max_file_count the max number of files used in log rotation 00138 * @param append whether to append or to truncate (start over) the log file 00139 * @param pool the memory pool to use 00140 */ 00141 APT_DECLARE(apt_bool_t) apt_log_file_open( 00142 const char *dir_path, 00143 const char *file_name, 00144 apr_size_t max_file_size, 00145 apr_size_t max_file_count, 00146 apt_bool_t append, 00147 apr_pool_t *pool); 00148 00149 /** 00150 * Close the log file. 00151 */ 00152 APT_DECLARE(apt_bool_t) apt_log_file_close(void); 00153 00154 /** 00155 * Set the logging output mode. 00156 * @param mode the mode to set 00157 */ 00158 APT_DECLARE(apt_bool_t) apt_log_output_mode_set(apt_log_output_e mode); 00159 00160 /** 00161 * Check the logging output mode to be enabled (set) or not. 00162 * @param mode the mode to check 00163 */ 00164 APT_DECLARE(apt_bool_t) apt_log_output_mode_check(apt_log_output_e mode); 00165 00166 /** 00167 * Translate the output mode string to bitmask of apt_log_output_e values. 00168 * @param str the string to translate 00169 */ 00170 APT_DECLARE(int) apt_log_output_mode_translate(char *str); 00171 00172 /** 00173 * Set the logging priority (log level). 00174 * @param priority the priority to set 00175 */ 00176 APT_DECLARE(apt_bool_t) apt_log_priority_set(apt_log_priority_e priority); 00177 00178 /** 00179 * Translate the priority (log level) string to enum. 00180 * @param str the string to translate 00181 */ 00182 APT_DECLARE(apt_log_priority_e) apt_log_priority_translate(const char *str); 00183 00184 /** 00185 * Set the header (format) for log messages. 00186 * @param header the header to set (used as bitmask) 00187 */ 00188 APT_DECLARE(apt_bool_t) apt_log_header_set(int header); 00189 00190 /** 00191 * Translate the header string to bitmask of apt_log_header_e values. 00192 * @param str the string to translate 00193 */ 00194 APT_DECLARE(int) apt_log_header_translate(char *str); 00195 00196 /** 00197 * Set the masking mode of private data. 00198 * @param masking the masking mode to set 00199 */ 00200 APT_DECLARE(apt_bool_t) apt_log_masking_set(apt_log_masking_e masking); 00201 00202 /** 00203 * Get the current masking mode of private data. 00204 */ 00205 APT_DECLARE(apt_log_masking_e) apt_log_masking_get(); 00206 00207 /** 00208 * Translate the masking mode string to enum. 00209 * @param str the string to translate 00210 */ 00211 APT_DECLARE(apt_log_masking_e) apt_log_masking_translate(const char *str); 00212 00213 /** 00214 * Mask private data based on the masking mode 00215 * @param data_in the data to mask 00216 * @param length the length of the data to mask on input, the length of the masked data on output 00217 * @param pool the memory pool to use if needed 00218 * @return The masked data. 00219 */ 00220 APT_DECLARE(const char*) apt_log_data_mask(const char *data_in, apr_size_t *length, apr_pool_t *pool); 00221 00222 /** 00223 * Set the extended external log handler. 00224 * @param handler the handler to pass log events to 00225 * @remark default logger is used to output the logs to stdout and/or log file, 00226 * if external log handler isn't set 00227 */ 00228 APT_DECLARE(apt_bool_t) apt_log_ext_handler_set(apt_log_ext_handler_f handler); 00229 00230 /** 00231 * Do logging. 00232 * @param file the file name log entry is generated from 00233 * @param line the line number log entry is generated from 00234 * @param priority the priority of the entire log entry 00235 * @param format the format of the entire log entry 00236 */ 00237 APT_DECLARE(apt_bool_t) apt_log(const char *file, int line, apt_log_priority_e priority, const char *format, ...); 00238 00239 /** 00240 * Do logging. 00241 * @param file the file name log entry is generated from 00242 * @param line the line number log entry is generated from 00243 * @param priority the priority of the entire log entry 00244 * @param obj the associated object 00245 * @param format the format of the entire log entry 00246 */ 00247 APT_DECLARE(apt_bool_t) apt_obj_log(const char *file, int line, apt_log_priority_e priority, void *obj, const char *format, ...); 00248 00249 APT_END_EXTERN_C 00250 00251 #endif /* APT_LOG_H */