Home | History | Annotate | Download | only in libdaemon
      1 #ifndef foodaemonloghfoo
      2 #define foodaemonloghfoo
      3 
      4 /***
      5   This file is part of libdaemon.
      6 
      7   Copyright 2003-2008 Lennart Poettering
      8 
      9   Permission is hereby granted, free of charge, to any person obtaining a copy
     10   of this software and associated documentation files (the "Software"), to deal
     11   in the Software without restriction, including without limitation the rights
     12   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     13   copies of the Software, and to permit persons to whom the Software is
     14   furnished to do so, subject to the following conditions:
     15 
     16   The above copyright notice and this permission notice shall be included in
     17   all copies or substantial portions of the Software.
     18 
     19   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     20   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     21   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     22   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     23   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     24   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     25   SOFTWARE.
     26 
     27 ***/
     28 
     29 #include <syslog.h>
     30 #include <stdarg.h>
     31 
     32 #ifdef __cplusplus
     33 extern "C" {
     34 #endif
     35 
     36 /** \file
     37  *
     38  * Contains a robust API for logging messages
     39  */
     40 
     41 /** Specifies where to send the log messages to. The global variable daemon_log_use takes values of this type.
     42  */
     43 enum daemon_log_flags {
     44     DAEMON_LOG_SYSLOG = 1,   /**< Log messages are written to syslog */
     45     DAEMON_LOG_STDERR = 2,   /**< Log messages are written to STDERR */
     46     DAEMON_LOG_STDOUT = 4,   /**< Log messages are written to STDOUT */
     47     DAEMON_LOG_AUTO = 8      /**< If this is set a daemon_fork() will
     48                                   change this to DAEMON_LOG_SYSLOG in
     49                                   the daemon process. */
     50 };
     51 
     52 /** This variable is used to specify the log target(s) to
     53  * use. Defaults to DAEMON_LOG_STDERR|DAEMON_LOG_AUTO */
     54 extern enum daemon_log_flags daemon_log_use;
     55 
     56 /** Specifies the syslog identification, use daemon_ident_from_argv0()
     57  * to set this to a sensible value or generate your own. */
     58 extern const char* daemon_log_ident;
     59 
     60 #if defined(__GNUC__) && ! defined(DAEMON_GCC_PRINTF_ATTR)
     61 #define DAEMON_GCC_PRINTF_ATTR(a,b) __attribute__ ((format (printf, a, b)))
     62 #else
     63 /** A macro for making use of GCCs printf compilation warnings */
     64 #define DAEMON_GCC_PRINTF_ATTR(a,b)
     65 #endif
     66 
     67 /** Log a message using printf format strings using the specified syslog priority
     68  * @param prio The syslog priority (PRIO_xxx constants)
     69  * @param t,... The text message to log
     70  */
     71 void daemon_log(int prio, const char* t, ...) DAEMON_GCC_PRINTF_ATTR(2,3);
     72 
     73 /** This variable is defined to 1 iff daemon_logv() is supported.
     74  * @since 0.11
     75  * @see daemon_logv()
     76  */
     77 #define DAEMON_LOGV_AVAILABLE 1
     78 
     79 /** Same as daemon_log(), but without variadic arguments
     80  * @since 0.11
     81  * @see DAEMON_LOGV_AVAILABLE
     82  */
     83 void daemon_logv(int prio, const char* t, va_list ap);
     84 
     85 /** Return a sensible syslog identification for daemon_log_ident
     86  * generated from argv[0]. This will return a pointer to the file name
     87  * of argv[0], i.e. strrchr(argv[0], '\')+1
     88  * @param argv0 argv[0] as passed to main()
     89  * @return The identification string
     90  */
     91 char *daemon_ident_from_argv0(char *argv0);
     92 
     93 /** This variable is defined to 1 iff daemon_set_verbosity() is available.
     94  * @since 0.14
     95  * @see daemon_set_verbosity()
     96  */
     97 #define DAEMON_SET_VERBOSITY_AVAILABLE 1
     98 
     99 /** Setter for the verbosity level of standard output.
    100  *
    101  * @param verbosity_prio Minimum priority level for messages to output
    102  * on standard output/error
    103  *
    104  * Allows to decide which messages to output on standard output/error
    105  * streams. All messages are logged to syslog and this setting does
    106  * not influence that.
    107  *
    108  * The default value is LOG_WARNING.
    109  *
    110  * @since 0.14
    111  * @see DAEMON_SET_VERBOSITY_AVAILABLE
    112  */
    113 void daemon_set_verbosity(int verbosity_prio);
    114 
    115 #ifdef __cplusplus
    116 }
    117 #endif
    118 
    119 #endif
    120