log.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Zebra logging funcions.
  3. * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #ifndef _ZEBRA_LOG_H
  23. #define _ZEBRA_LOG_H
  24. #include <syslog.h>
  25. #include <stdio.h>
  26. /* Here is some guidance on logging levels to use:
  27. *
  28. * LOG_DEBUG - For all messages that are enabled by optional debugging
  29. * features, typically preceded by "if (IS...DEBUG...)"
  30. * LOG_INFO - Information that may be of interest, but everything seems
  31. * to be working properly.
  32. * LOG_NOTICE - Only for message pertaining to daemon startup or shutdown.
  33. * LOG_WARNING - Warning conditions: unexpected events, but the daemon believes
  34. * it can continue to operate correctly.
  35. * LOG_ERR - Error situations indicating malfunctions. Probably require
  36. * attention.
  37. *
  38. * Note: LOG_CRIT, LOG_ALERT, and LOG_EMERG are currently not used anywhere,
  39. * please use LOG_ERR instead.
  40. */
  41. typedef enum
  42. {
  43. ZLOG_NONE,
  44. ZLOG_DEFAULT,
  45. ZLOG_ZEBRA,
  46. ZLOG_RIP,
  47. ZLOG_BGP,
  48. ZLOG_OSPF,
  49. ZLOG_RIPNG,
  50. ZLOG_BABEL,
  51. ZLOG_OSPF6,
  52. ZLOG_ISIS,
  53. ZLOG_PIM,
  54. ZLOG_MASC,
  55. ZLOG_NHRP,
  56. } zlog_proto_t;
  57. /* If maxlvl is set to ZLOG_DISABLED, then no messages will be sent
  58. to that logging destination. */
  59. #define ZLOG_DISABLED (LOG_EMERG-1)
  60. typedef enum
  61. {
  62. ZLOG_DEST_SYSLOG = 0,
  63. ZLOG_DEST_STDOUT,
  64. ZLOG_DEST_MONITOR,
  65. ZLOG_DEST_FILE
  66. } zlog_dest_t;
  67. #define ZLOG_NUM_DESTS (ZLOG_DEST_FILE+1)
  68. struct zlog
  69. {
  70. const char *ident; /* daemon name (first arg to openlog) */
  71. zlog_proto_t protocol;
  72. int maxlvl[ZLOG_NUM_DESTS]; /* maximum priority to send to associated
  73. logging destination */
  74. int default_lvl; /* maxlvl to use if none is specified */
  75. FILE *fp;
  76. char *filename;
  77. int facility; /* as per syslog facility */
  78. int record_priority; /* should messages logged through stdio include the
  79. priority of the message? */
  80. int syslog_options; /* 2nd arg to openlog */
  81. int timestamp_precision; /* # of digits of subsecond precision */
  82. };
  83. /* Message structure. */
  84. struct message
  85. {
  86. int key;
  87. const char *str;
  88. };
  89. /* Default logging strucutre. */
  90. extern struct zlog *zlog_default;
  91. /* Open zlog function */
  92. extern struct zlog *openzlog (const char *progname, zlog_proto_t protocol,
  93. int syslog_options, int syslog_facility);
  94. /* Close zlog function. */
  95. extern void closezlog (struct zlog *zl);
  96. /* GCC have printf type attribute check. */
  97. #ifdef __GNUC__
  98. #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
  99. #else
  100. #define PRINTF_ATTRIBUTE(a,b)
  101. #endif /* __GNUC__ */
  102. /* Generic function for zlog. */
  103. extern void zlog (struct zlog *zl, int priority, const char *format, ...)
  104. PRINTF_ATTRIBUTE(3, 4);
  105. /* Handy zlog functions. */
  106. extern void zlog_err (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
  107. extern void zlog_warn (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
  108. extern void zlog_info (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
  109. extern void zlog_notice (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
  110. extern void zlog_debug (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
  111. /* For bgpd's peer oriented log. */
  112. extern void plog_err (struct zlog *, const char *format, ...)
  113. PRINTF_ATTRIBUTE(2, 3);
  114. extern void plog_warn (struct zlog *, const char *format, ...)
  115. PRINTF_ATTRIBUTE(2, 3);
  116. extern void plog_info (struct zlog *, const char *format, ...)
  117. PRINTF_ATTRIBUTE(2, 3);
  118. extern void plog_notice (struct zlog *, const char *format, ...)
  119. PRINTF_ATTRIBUTE(2, 3);
  120. extern void plog_debug (struct zlog *, const char *format, ...)
  121. PRINTF_ATTRIBUTE(2, 3);
  122. extern void zlog_thread_info (int log_level);
  123. /* Set logging level for the given destination. If the log_level
  124. argument is ZLOG_DISABLED, then the destination is disabled.
  125. This function should not be used for file logging (use zlog_set_file
  126. or zlog_reset_file instead). */
  127. extern void zlog_set_level (struct zlog *zl, zlog_dest_t, int log_level);
  128. /* Set logging to the given filename at the specified level. */
  129. extern int zlog_set_file (struct zlog *zl, const char *filename, int log_level);
  130. /* Disable file logging. */
  131. extern int zlog_reset_file (struct zlog *zl);
  132. /* Rotate log. */
  133. extern int zlog_rotate (struct zlog *);
  134. /* For hackey message lookup and check */
  135. #define LOOKUP_DEF(x, y, def) mes_lookup(x, x ## _max, y, def, #x)
  136. #define LOOKUP(x, y) LOOKUP_DEF(x, y, "(no item found)")
  137. extern const char *lookup (const struct message *, int);
  138. extern const char *mes_lookup (const struct message *meslist,
  139. int max, int index,
  140. const char *no_item, const char *mesname);
  141. extern const char *zlog_priority[];
  142. extern const char *zlog_proto_names[];
  143. /* Safe version of strerror -- never returns NULL. */
  144. extern const char *safe_strerror(int errnum);
  145. /* To be called when a fatal signal is caught. */
  146. extern void zlog_signal(int signo, const char *action
  147. #ifdef SA_SIGINFO
  148. , siginfo_t *siginfo, void *program_counter
  149. #endif
  150. );
  151. /* Log a backtrace. */
  152. extern void zlog_backtrace(int priority);
  153. /* Log a backtrace, but in an async-signal-safe way. Should not be
  154. called unless the program is about to exit or abort, since it messes
  155. up the state of zlog file pointers. If program_counter is non-NULL,
  156. that is logged in addition to the current backtrace. */
  157. extern void zlog_backtrace_sigsafe(int priority, void *program_counter);
  158. /* Puts a current timestamp in buf and returns the number of characters
  159. written (not including the terminating NUL). The purpose of
  160. this function is to avoid calls to localtime appearing all over the code.
  161. It caches the most recent localtime result and can therefore
  162. avoid multiple calls within the same second. If buflen is too small,
  163. *buf will be set to '\0', and 0 will be returned. */
  164. #define QUAGGA_TIMESTAMP_LEN 40
  165. extern size_t quagga_timestamp(int timestamp_precision /* # subsecond digits */,
  166. char *buf, size_t buflen);
  167. extern void zlog_hexdump(void *mem, unsigned int len);
  168. /* structure useful for avoiding repeated rendering of the same timestamp */
  169. struct timestamp_control {
  170. size_t len; /* length of rendered timestamp */
  171. int precision; /* configuration parameter */
  172. int already_rendered; /* should be initialized to 0 */
  173. char buf[QUAGGA_TIMESTAMP_LEN]; /* will contain the rendered timestamp */
  174. };
  175. /* Defines for use in command construction: */
  176. #define LOG_LEVELS "(emergencies|alerts|critical|errors|warnings|notifications|informational|debugging)"
  177. #define LOG_LEVEL_DESC \
  178. "System is unusable\n" \
  179. "Immediate action needed\n" \
  180. "Critical conditions\n" \
  181. "Error conditions\n" \
  182. "Warning conditions\n" \
  183. "Normal but significant conditions\n" \
  184. "Informational messages\n" \
  185. "Debugging messages\n"
  186. #define LOG_FACILITIES "(kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7)"
  187. #define LOG_FACILITY_DESC \
  188. "Kernel\n" \
  189. "User process\n" \
  190. "Mail system\n" \
  191. "System daemons\n" \
  192. "Authorization system\n" \
  193. "Syslog itself\n" \
  194. "Line printer system\n" \
  195. "USENET news\n" \
  196. "Unix-to-Unix copy system\n" \
  197. "Cron/at facility\n" \
  198. "Local use\n" \
  199. "Local use\n" \
  200. "Local use\n" \
  201. "Local use\n" \
  202. "Local use\n" \
  203. "Local use\n" \
  204. "Local use\n" \
  205. "Local use\n"
  206. #endif /* _ZEBRA_LOG_H */