dlog.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef foodaemonloghfoo
  2. #define foodaemonloghfoo
  3. /***
  4. This file is part of libdaemon.
  5. Copyright 2003-2008 Lennart Poettering
  6. libdaemon is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as
  8. published by the Free Software Foundation, either version 2.1 of the
  9. License, or (at your option) any later version.
  10. libdaemon is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with libdaemon. If not, see
  16. <http://www.gnu.org/licenses/>.
  17. ***/
  18. #include <syslog.h>
  19. #include <stdarg.h>
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /** \file
  24. *
  25. * Contains a robust API for logging messages
  26. */
  27. /** Specifies where to send the log messages to. The global variable daemon_log_use takes values of this type.
  28. */
  29. enum daemon_log_flags {
  30. DAEMON_LOG_SYSLOG = 1, /**< Log messages are written to syslog */
  31. DAEMON_LOG_STDERR = 2, /**< Log messages are written to STDERR */
  32. DAEMON_LOG_STDOUT = 4, /**< Log messages are written to STDOUT */
  33. DAEMON_LOG_AUTO = 8 /**< If this is set a daemon_fork() will
  34. change this to DAEMON_LOG_SYSLOG in
  35. the daemon process. */
  36. };
  37. /** This variable is used to specify the log target(s) to
  38. * use. Defaults to DAEMON_LOG_STDERR|DAEMON_LOG_AUTO */
  39. extern enum daemon_log_flags daemon_log_use;
  40. /** Specifies the syslog identification, use daemon_ident_from_argv0()
  41. * to set this to a sensible value or generate your own. */
  42. extern const char* daemon_log_ident;
  43. #if defined(__GNUC__) && ! defined(DAEMON_GCC_PRINTF_ATTR)
  44. #define DAEMON_GCC_PRINTF_ATTR(a,b) __attribute__ ((format (printf, a, b)))
  45. #else
  46. /** A macro for making use of GCCs printf compilation warnings */
  47. #define DAEMON_GCC_PRINTF_ATTR(a,b)
  48. #endif
  49. /** Log a message using printf format strings using the specified syslog priority
  50. * @param prio The syslog priority (PRIO_xxx constants)
  51. * @param t,... The text message to log
  52. */
  53. void daemon_log(int prio, const char* t, ...) DAEMON_GCC_PRINTF_ATTR(2,3);
  54. /** This variable is defined to 1 iff daemon_logv() is supported.
  55. * @since 0.11
  56. * @see daemon_logv()
  57. */
  58. #define DAEMON_LOGV_AVAILABLE 1
  59. /** Same as daemon_log(), but without variadic arguments
  60. * @since 0.11
  61. * @see DAEMON_LOGV_AVAILABLE
  62. */
  63. void daemon_logv(int prio, const char* t, va_list ap);
  64. /** Return a sensible syslog identification for daemon_log_ident
  65. * generated from argv[0]. This will return a pointer to the file name
  66. * of argv[0], i.e. strrchr(argv[0], '\')+1
  67. * @param argv0 argv[0] as passed to main()
  68. * @return The identification string
  69. */
  70. char *daemon_ident_from_argv0(char *argv0);
  71. /** This variable is defined to 1 iff daemon_set_verbosity() is available.
  72. * @since 0.14
  73. * @see daemon_set_verbosity()
  74. */
  75. #define DAEMON_SET_VERBOSITY_AVAILABLE 1
  76. /** Setter for the verbosity level of standard output.
  77. *
  78. * @param verbosity_prio Minimum priority level for messages to output
  79. * on standard output/error
  80. *
  81. * Allows to decide which messages to output on standard output/error
  82. * streams. All messages are logged to syslog and this setting does
  83. * not influence that.
  84. *
  85. * The default value is LOG_WARNING.
  86. *
  87. * @since 0.14
  88. * @see DAEMON_SET_VERBOSITY_AVAILABLE
  89. */
  90. void daemon_set_verbosity(int verbosity_prio);
  91. #ifdef __cplusplus
  92. }
  93. #endif
  94. #endif