Sat Apr 26 2014 22:01:38

Asterisk developer's documentation


logger.h
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*!
00020   \file logger.h
00021   \brief Support for logging to various files, console and syslog
00022    Configuration in file logger.conf
00023 */
00024 
00025 #ifndef _ASTERISK_LOGGER_H
00026 #define _ASTERISK_LOGGER_H
00027 
00028 #include "asterisk/options.h" /* need option_debug */
00029 
00030 #if defined(__cplusplus) || defined(c_plusplus)
00031 extern "C" {
00032 #endif
00033 
00034 #define EVENTLOG "event_log"
00035 #define  QUEUELOG "queue_log"
00036 
00037 #define DEBUG_M(a) { \
00038    a; \
00039 }
00040 
00041 #define VERBOSE_PREFIX_1 " "
00042 #define VERBOSE_PREFIX_2 "  == "
00043 #define VERBOSE_PREFIX_3 "    -- "
00044 #define VERBOSE_PREFIX_4 "       > "
00045 
00046 #define AST_CALLID_BUFFER_LENGTH 13
00047 
00048 /*! \brief Used for sending a log message
00049    This is the standard logger function.  Probably the only way you will invoke it would be something like this:
00050    ast_log(AST_LOG_WHATEVER, "Problem with the %s Captain.  We should get some more.  Will %d be enough?\n", "flux capacitor", 10);
00051    where WHATEVER is one of ERROR, DEBUG, EVENT, NOTICE, or WARNING depending
00052    on which log you wish to output to. These are implemented as macros, that
00053    will provide the function with the needed arguments.
00054 
00055    \param level   Type of log event
00056    \param file Will be provided by the AST_LOG_* macro
00057    \param line Will be provided by the AST_LOG_* macro
00058    \param function   Will be provided by the AST_LOG_* macro
00059    \param fmt  This is what is important.  The format is the same as your favorite breed of printf.  You know how that works, right? :-)
00060  */
00061 
00062 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
00063    __attribute__((format(printf, 5, 6)));
00064 
00065 /* XXX needs documentation */
00066 struct ast_callid;
00067 
00068 /*! \brief Used for sending a log message with a known call_id
00069    This is a modified logger function which is functionally identical to the above logger function,
00070    it just include a call_id argument as well. If NULL is specified here, no attempt will be made to
00071    join the log message with a call_id.
00072 
00073    \param level   Type of log event
00074    \param file Will be provided by the AST_LOG_* macro
00075    \param line Will be provided by the AST_LOG_* macro
00076    \param function   Will be provided by the AST_LOG_* macro
00077    \param callid  This is the ast_callid that is associated with the log message. May be NULL.
00078    \param fmt  This is what is important.  The format is the same as your favorite breed of printf.  You know how that works, right? :-)
00079 */
00080 void ast_log_callid(int level, const char *file, int line, const char *function, struct ast_callid *callid, const char *fmt, ...)
00081    __attribute__((format(printf, 6, 7)));
00082 
00083 void ast_backtrace(void);
00084 
00085 /*! \brief Reload logger without rotating log files */
00086 int logger_reload(void);
00087 
00088 void __attribute__((format(printf, 5, 6))) ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...);
00089 
00090 /*!
00091  * \brief Send a verbose message (based on verbose level)
00092  *
00093  * \details This works like ast_log, but prints verbose messages to the console depending on verbosity level set.
00094  *
00095  * ast_verbose(VERBOSE_PREFIX_3 "Whatever %s is happening\n", "nothing");
00096  *
00097  * This will print the message to the console if the verbose level is set to a level >= 3
00098  *
00099  * Note the absence of a comma after the VERBOSE_PREFIX_3.  This is important.
00100  * VERBOSE_PREFIX_1 through VERBOSE_PREFIX_4 are defined.
00101  *
00102  * \version 11 added level parameter
00103  */
00104 void __attribute__((format(printf, 5, 6))) __ast_verbose(const char *file, int line, const char *func, int level, const char *fmt, ...);
00105 
00106 /*!
00107  * \brief Send a verbose message (based on verbose level) with deliberately specified callid
00108  *
00109  * \details just like __ast_verbose, only __ast_verbose_callid allows you to specify which callid is being used
00110  * for the log without needing to bind it to a thread. NULL is a valid argument for this function and will
00111  * allow you to specify that a log will never display a call id even when there is a call id bound to the
00112  * thread.
00113  */
00114 void __attribute__((format(printf, 6, 7))) __ast_verbose_callid(const char *file, int line, const char *func, int level, struct ast_callid *callid, const char *fmt, ...);
00115 
00116 #define ast_verbose(...) __ast_verbose(__FILE__, __LINE__, __PRETTY_FUNCTION__, -1, __VA_ARGS__)
00117 #define ast_verbose_callid(callid, ...) __ast_verbose_callid(__FILE__, __LINE__, __PRETTY_FUNCTION__, -1, callid, __VA_ARGS__)
00118 
00119 void __attribute__((format(printf, 6, 0))) __ast_verbose_ap(const char *file, int line, const char *func, int level, struct ast_callid *callid, const char *fmt, va_list ap);
00120 
00121 void __attribute__((format(printf, 2, 3))) ast_child_verbose(int level, const char *fmt, ...);
00122 
00123 int ast_register_verbose(void (*verboser)(const char *string)) attribute_warn_unused_result;
00124 int ast_unregister_verbose(void (*verboser)(const char *string)) attribute_warn_unused_result;
00125 
00126 /*
00127  * These gymnastics are due to platforms which designate char as unsigned by
00128  * default.  Level is the negative character -- offset by 1, because \0 is
00129  * the string terminator.
00130  */
00131 #define VERBOSE_MAGIC2LEVEL(x) (((char) -*(signed char *) (x)) - 1)
00132 #define VERBOSE_HASMAGIC(x)   (*(signed char *) (x) < 0)
00133 
00134 void ast_console_puts(const char *string);
00135 
00136 /*!
00137  * \brief log the string to the console, and all attached
00138  * console clients
00139  * \version 1.6.1 added level parameter
00140  */
00141 void ast_console_puts_mutable(const char *string, int level);
00142 void ast_console_toggle_mute(int fd, int silent);
00143 
00144 /*!
00145  * \brief enables or disables logging of a specified level to the console
00146  * fd specifies the index of the console receiving the level change
00147  * level specifies the index of the logging level being toggled
00148  * state indicates whether logging will be on or off (0 for off, 1 for on)
00149  */
00150 void ast_console_toggle_loglevel(int fd, int level, int state);
00151 
00152 /* Note: The AST_LOG_* macros below are the same as
00153  * the LOG_* macros and are intended to eventually replace
00154  * the LOG_* macros to avoid name collisions with the syslog(3)
00155  * log levels. However, please do NOT remove
00156  * the LOG_* macros from the source since these may be still
00157  * needed for third-party modules
00158  */
00159 
00160 #define _A_ __FILE__, __LINE__, __PRETTY_FUNCTION__
00161 
00162 #ifdef LOG_DEBUG
00163 #undef LOG_DEBUG
00164 #endif
00165 #define __LOG_DEBUG    0
00166 #define LOG_DEBUG      __LOG_DEBUG, _A_
00167 
00168 #ifdef AST_LOG_DEBUG
00169 #undef AST_LOG_DEBUG
00170 #endif
00171 #define AST_LOG_DEBUG      __LOG_DEBUG, _A_
00172 
00173 #ifdef LOG_NOTICE
00174 #undef LOG_NOTICE
00175 #endif
00176 #define __LOG_NOTICE   2
00177 #define LOG_NOTICE     __LOG_NOTICE, _A_
00178 
00179 #ifdef AST_LOG_NOTICE
00180 #undef AST_LOG_NOTICE
00181 #endif
00182 #define AST_LOG_NOTICE     __LOG_NOTICE, _A_
00183 
00184 #ifdef LOG_WARNING
00185 #undef LOG_WARNING
00186 #endif
00187 #define __LOG_WARNING  3
00188 #define LOG_WARNING    __LOG_WARNING, _A_
00189 
00190 #ifdef AST_LOG_WARNING
00191 #undef AST_LOG_WARNING
00192 #endif
00193 #define AST_LOG_WARNING    __LOG_WARNING, _A_
00194 
00195 #ifdef LOG_ERROR
00196 #undef LOG_ERROR
00197 #endif
00198 #define __LOG_ERROR    4
00199 #define LOG_ERROR      __LOG_ERROR, _A_
00200 
00201 #ifdef AST_LOG_ERROR
00202 #undef AST_LOG_ERROR
00203 #endif
00204 #define AST_LOG_ERROR      __LOG_ERROR, _A_
00205 
00206 #ifdef LOG_VERBOSE
00207 #undef LOG_VERBOSE
00208 #endif
00209 #define __LOG_VERBOSE  5
00210 #define LOG_VERBOSE    __LOG_VERBOSE, _A_
00211 
00212 #ifdef AST_LOG_VERBOSE
00213 #undef AST_LOG_VERBOSE
00214 #endif
00215 #define AST_LOG_VERBOSE    __LOG_VERBOSE, _A_
00216 
00217 #ifdef LOG_DTMF
00218 #undef LOG_DTMF
00219 #endif
00220 #define __LOG_DTMF  6
00221 #define LOG_DTMF    __LOG_DTMF, _A_
00222 
00223 #ifdef AST_LOG_DTMF
00224 #undef AST_LOG_DTMF
00225 #endif
00226 #define AST_LOG_DTMF    __LOG_DTMF, _A_
00227 
00228 #define NUMLOGLEVELS 32
00229 
00230 /*!
00231  * \brief Get the debug level for a module
00232  * \param module the name of module
00233  * \return the debug level
00234  */
00235 unsigned int ast_debug_get_by_module(const char *module);
00236 
00237 /*!
00238  * \brief Get the verbose level for a module
00239  * \param module the name of module
00240  * \return the verbose level
00241  * \version 11.0.0 deprecated
00242  */
00243 unsigned int ast_verbose_get_by_module(const char *module) __attribute__((deprecated));
00244 
00245 /*!
00246  * \brief Register a new logger level
00247  * \param name The name of the level to be registered
00248  * \retval -1 if an error occurs
00249  * \retval non-zero level to be used with ast_log for sending messages to this level
00250  * \since 1.8
00251  */
00252 int ast_logger_register_level(const char *name);
00253 
00254 /*!
00255  * \brief Unregister a previously registered logger level
00256  * \param name The name of the level to be unregistered
00257  * \return nothing
00258  * \since 1.8
00259  */
00260 void ast_logger_unregister_level(const char *name);
00261 
00262 /*!
00263  * \brief factory function to create a new uniquely identifying callid.
00264  *
00265  * \retval ast_callid struct pointer containing the call id
00266  *
00267  * \note The newly created callid will be referenced upon creation and this function should be
00268  * paired with a call to ast_callid_unref()
00269  */
00270 struct ast_callid *ast_create_callid(void);
00271 
00272 /*!
00273  * \brief extracts the callerid from the thread
00274  *
00275  * \retval ast_callid reference to call_id related to the thread
00276  * \retval NULL if no call_id is present in the thread
00277  *
00278  * This reference must be unreffed before it loses scope to prevent memory leaks.
00279  */
00280 struct ast_callid *ast_read_threadstorage_callid(void);
00281 
00282 /*!
00283  * \brief Increase callid reference count
00284  *
00285  * \param c the ast_callid
00286  *
00287  * \retval c always
00288  */
00289 #define ast_callid_ref(c) ({ ao2_ref(c, +1); (c); })
00290 
00291 /*!
00292  * \brief Decrease callid reference count
00293  *
00294  * \param c the ast_callid
00295  *
00296  * \retval NULL always
00297  */
00298 #define ast_callid_unref(c) ({ ao2_ref(c, -1); (NULL); })
00299 
00300 /*!
00301  * \brief Sets what is stored in the thread storage to the given
00302  *        callid if it does not match what is already there.
00303  *
00304  * \retval 0 - success
00305  * \retval non-zero - failure
00306  */
00307 int ast_callid_threadassoc_change(struct ast_callid *callid);
00308 
00309 /*!
00310  * \brief Adds a known callid to thread storage of the calling thread
00311  *
00312  * \retval 0 - success
00313  * \retval non-zero - failure
00314  */
00315 int ast_callid_threadassoc_add(struct ast_callid *callid);
00316 
00317 /*!
00318  * \brief Removes callid from thread storage of the calling thread
00319  *
00320  * \retval 0 - success
00321  * \retval non-zero - failure
00322  */
00323 int ast_callid_threadassoc_remove(void);
00324 
00325 /*!
00326  * \brief Checks thread storage for a callid and stores a reference if it exists.
00327  *        If not, then a new one will be created, bound to the thread, and a reference
00328  *        to it will be stored.
00329  *
00330  * \param callid pointer to struct pointer used to store the referenced callid
00331  * \retval 0 - callid was found
00332  * \retval 1 - callid was created
00333  * \retval -1 - the function failed somehow (presumably memory problems)
00334  */
00335 int ast_callid_threadstorage_auto(struct ast_callid **callid);
00336 
00337 /*!
00338  * \brief Use in conjunction with ast_callid_threadstorage_auto. Cleans up the
00339  *        references and if the callid was created by threadstorage_auto, unbinds
00340  *        the callid from the threadstorage
00341  * \param callid The callid set by ast_callid_threadstorage_auto
00342  * \param callid_created The integer returned through ast_callid_threadstorage_auto
00343  */
00344 void ast_callid_threadstorage_auto_clean(struct ast_callid *callid, int callid_created);
00345 
00346 /*!
00347  * \brief copy a string representation of the callid into a target string
00348  *
00349  * \param buffer destination of callid string (should be able to store 13 characters or more)
00350  * \param buffer_size maximum writable length of the string (Less than 13 will result in truncation)
00351  * \param callid Callid for which string is being requested
00352  */
00353 void ast_callid_strnprint(char *buffer, size_t buffer_size, struct ast_callid *callid);
00354 
00355 /*!
00356  * \brief Send a log message to a dynamically registered log level
00357  * \param level The log level to send the message to
00358  *
00359  * Like ast_log, the log message may include printf-style formats, and
00360  * the data for these must be provided as additional parameters after
00361  * the log message.
00362  *
00363  * \return nothing
00364  * \since 1.8
00365  */
00366 
00367 #define ast_log_dynamic_level(level, ...) ast_log(level, __FILE__, __LINE__, __PRETTY_FUNCTION__, __VA_ARGS__)
00368 
00369 /*!
00370  * \brief Log a DEBUG message
00371  * \param level The minimum value of option_debug for this message
00372  *        to get logged
00373  */
00374 #define ast_debug(level, ...) do {       \
00375    if (option_debug >= (level) || (ast_opt_dbg_module && ast_debug_get_by_module(AST_MODULE) >= (level)) ) \
00376       ast_log(AST_LOG_DEBUG, __VA_ARGS__); \
00377 } while (0)
00378 
00379 extern int ast_verb_sys_level;
00380 
00381 #define VERBOSITY_ATLEAST(level) ((level) <= ast_verb_sys_level)
00382 
00383 #define ast_verb(level, ...) \
00384    do { \
00385       if (VERBOSITY_ATLEAST(level) ) { \
00386          __ast_verbose(__FILE__, __LINE__, __PRETTY_FUNCTION__, level, __VA_ARGS__); \
00387       } \
00388    } while (0)
00389 
00390 #define ast_verb_callid(level, callid, ...) \
00391    do { \
00392       if (VERBOSITY_ATLEAST(level) ) { \
00393          __ast_verbose_callid(__FILE__, __LINE__, __PRETTY_FUNCTION__, level, callid, __VA_ARGS__); \
00394       } \
00395    } while (0)
00396 
00397 /*!
00398  * \brief Re-evaluate the system max verbosity level (ast_verb_sys_level).
00399  *
00400  * \return Nothing
00401  */
00402 void ast_verb_update(void);
00403 
00404 /*!
00405  * \brief Register this thread's console verbosity level pointer.
00406  *
00407  * \param level Where the verbose level value is.
00408  *
00409  * \return Nothing
00410  */
00411 void ast_verb_console_register(int *level);
00412 
00413 /*!
00414  * \brief Unregister this thread's console verbosity level.
00415  *
00416  * \return Nothing
00417  */
00418 void ast_verb_console_unregister(void);
00419 
00420 /*!
00421  * \brief Get this thread's console verbosity level.
00422  *
00423  * \retval verbosity level of the console.
00424  */
00425 int ast_verb_console_get(void);
00426 
00427 /*!
00428  * \brief Set this thread's console verbosity level.
00429  *
00430  * \param verb_level New level to set.
00431  *
00432  * \return Nothing
00433  */
00434 void ast_verb_console_set(int verb_level);
00435 
00436 #ifndef _LOGGER_BACKTRACE_H
00437 #define _LOGGER_BACKTRACE_H
00438 #ifdef HAVE_BKTR
00439 #define AST_MAX_BT_FRAMES 32
00440 /* \brief
00441  *
00442  * A structure to hold backtrace information. This structure provides an easy means to
00443  * store backtrace information or pass backtraces to other functions.
00444  */
00445 struct ast_bt {
00446    /*! The addresses of the stack frames. This is filled in by calling the glibc backtrace() function */
00447    void *addresses[AST_MAX_BT_FRAMES];
00448    /*! The number of stack frames in the backtrace */
00449    int num_frames;
00450    /*! Tells if the ast_bt structure was dynamically allocated */
00451    unsigned int alloced:1;
00452 };
00453 
00454 /* \brief
00455  * Allocates memory for an ast_bt and stores addresses and symbols.
00456  *
00457  * \return Returns NULL on failure, or the allocated ast_bt on success
00458  * \since 1.6.1
00459  */
00460 struct ast_bt *ast_bt_create(void);
00461 
00462 /* \brief
00463  * Fill an allocated ast_bt with addresses
00464  *
00465  * \retval 0 Success
00466  * \retval -1 Failure
00467  * \since 1.6.1
00468  */
00469 int ast_bt_get_addresses(struct ast_bt *bt);
00470 
00471 /* \brief
00472  *
00473  * Free dynamically allocated portions of an ast_bt
00474  *
00475  * \retval NULL.
00476  * \since 1.6.1
00477  */
00478 void *ast_bt_destroy(struct ast_bt *bt);
00479 
00480 /* \brief Retrieve symbols for a set of backtrace addresses
00481  *
00482  * \param addresses A list of addresses, such as the ->addresses structure element of struct ast_bt.
00483  * \param num_frames Number of addresses in the addresses list
00484  * \retval NULL Unable to allocate memory
00485  * \return List of strings. Free the entire list with a single ast_std_free call.
00486  * \since 1.6.2.16
00487  */
00488 char **ast_bt_get_symbols(void **addresses, size_t num_frames);
00489 
00490 #endif /* HAVE_BKTR */
00491 #endif /* _LOGGER_BACKTRACE_H */
00492 
00493 #if defined(__cplusplus) || defined(c_plusplus)
00494 }
00495 #endif
00496 
00497 #endif /* _ASTERISK_LOGGER_H */