corosync  2.4.3
logsys.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2004 MontaVista Software, Inc.
3  * Copyright (c) 2006-2012 Red Hat, Inc.
4  *
5  * Author: Steven Dake (sdake@redhat.com)
6  * Author: Lon Hohberger (lhh@redhat.com)
7  * Author: Fabio M. Di Nitto (fdinitto@redhat.com)
8  *
9  * All rights reserved.
10  *
11  * This software licensed under BSD license, the text of which follows:
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions are met:
15  *
16  * - Redistributions of source code must retain the above copyright notice,
17  * this list of conditions and the following disclaimer.
18  * - Redistributions in binary form must reproduce the above copyright notice,
19  * this list of conditions and the following disclaimer in the documentation
20  * and/or other materials provided with the distribution.
21  * - Neither the name of the MontaVista Software, Inc. nor the names of its
22  * contributors may be used to endorse or promote products derived from this
23  * software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35  * THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 #ifndef LOGSYS_H_DEFINED
38 #define LOGSYS_H_DEFINED
39 
40 #include <stdarg.h>
41 #include <stdlib.h>
42 #include <syslog.h>
43 #include <pthread.h>
44 #include <limits.h>
45 
46 #include <qb/qbconfig.h>
47 #include <qb/qblog.h>
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 /*
54  * All of the LOGSYS_MODE's can be ORed together for combined behavior
55  *
56  * FORK and THREADED are ignored for SUBSYSTEMS
57  */
58 #define LOGSYS_MODE_OUTPUT_FILE (1<<0)
59 #define LOGSYS_MODE_OUTPUT_STDERR (1<<1)
60 #define LOGSYS_MODE_OUTPUT_SYSLOG (1<<2)
61 #define LOGSYS_MODE_FORK (1<<3)
62 #define LOGSYS_MODE_THREADED (1<<4)
63 
64 /*
65  * Log priorities, compliant with syslog and SA Forum Log spec.
66  */
67 #define LOGSYS_LEVEL_EMERG LOG_EMERG
68 #define LOGSYS_LEVEL_ALERT LOG_ALERT
69 #define LOGSYS_LEVEL_CRIT LOG_CRIT
70 #define LOGSYS_LEVEL_ERROR LOG_ERR
71 #define LOGSYS_LEVEL_WARNING LOG_WARNING
72 #define LOGSYS_LEVEL_NOTICE LOG_NOTICE
73 #define LOGSYS_LEVEL_INFO LOG_INFO
74 #define LOGSYS_LEVEL_DEBUG LOG_DEBUG
75 #define LOGSYS_LEVEL_TRACE LOG_TRACE
76 
77 /*
78  * logsys_logger bits
79  *
80  * SUBSYS_COUNT defines the maximum number of subsystems
81  * SUBSYS_NAMELEN defines the maximum len of a subsystem name
82  */
83 #define LOGSYS_MAX_SUBSYS_COUNT 32
84 #define LOGSYS_MAX_SUBSYS_NAMELEN 64
85 #define LOGSYS_MAX_PERROR_MSG_LEN 128
86 
87 /*
88  * Debug levels
89  */
90 #define LOGSYS_DEBUG_OFF 0
91 #define LOGSYS_DEBUG_ON 1
92 #define LOGSYS_DEBUG_TRACE 2
93 
94 #ifndef LOGSYS_UTILS_ONLY
95 
101 extern int logsys_format_set (
102  const char *format);
103 
108 extern char *logsys_format_get (void);
109 
123  const char *subsys,
124  unsigned int facility);
125 
133  const char *subsys,
134  unsigned int priority);
135 
142 extern int logsys_config_mode_set (
143  const char *subsys,
144  unsigned int mode);
145 
151 extern unsigned int logsys_config_mode_get (
152  const char *subsys);
153 
157 void logsys_config_apply(void);
158 
169 extern int logsys_config_file_set (
170  const char *subsys,
171  const char **error_string,
172  const char *file);
173 
181  const char *subsys,
182  unsigned int priority);
183 
193 extern int logsys_config_debug_set (
194  const char *subsys,
195  unsigned int value);
196 
197 /*
198  * External API - helpers
199  *
200  * convert facility/priority to/from name/values
201  */
207 extern int logsys_priority_id_get (
208  const char *name);
209 
215 extern const char *logsys_priority_name_get (
216  unsigned int priority);
217 
226 extern int _logsys_system_setup(
227  const char *mainsystem,
228  unsigned int mode,
229  int syslog_facility,
230  int syslog_priority);
231 
235 extern void logsys_system_fini (void);
236 
242 extern int _logsys_config_subsys_get (
243  const char *subsys);
244 
251 extern int _logsys_subsys_create (const char *subsys, const char *filename);
252 
257 extern int logsys_thread_start (void);
258 
262 static int logsys_subsys_id __attribute__((unused)) = LOGSYS_MAX_SUBSYS_COUNT;
263 
271 #define LOGSYS_DECLARE_SYSTEM(name,mode,syslog_facility,syslog_priority)\
272 __attribute__ ((constructor)) \
273 static void logsys_system_init (void) \
274 { \
275  if (_logsys_system_setup (name,mode,syslog_facility,syslog_priority) < 0) { \
276  fprintf (stderr, \
277  "Unable to setup logging system: %s.\n", name); \
278  exit (-1); \
279  } \
280 }
281 
282 #ifdef QB_HAVE_ATTRIBUTE_SECTION
283 #define LOGSYS_DECLARE_SECTION assert(__start___verbose != __stop___verbose)
284 #else
285 #define LOGSYS_DECLARE_SECTION
286 #endif
287 
292 #define LOGSYS_DECLARE_SUBSYS(subsys) \
293 __attribute__ ((constructor)) \
294 static void logsys_subsys_init (void) \
295 { \
296  LOGSYS_DECLARE_SECTION; \
297  logsys_subsys_id = \
298  _logsys_subsys_create ((subsys), __FILE__); \
299  if (logsys_subsys_id == -1) { \
300  fprintf (stderr, \
301  "Unable to create logging subsystem: %s.\n", subsys); \
302  exit (-1); \
303  } \
304 }
305 
313 #define LOGSYS_PERROR(err_num, level, fmt, args...) do { \
314  char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
315  const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
316  qb_log(level, fmt ": %s (%d)", ##args, _error_ptr, err_num); \
317  } while(0)
318 
319 #define log_printf(level, format, args...) qb_log(level, format, ##args)
320 #define ENTER qb_enter
321 #define LEAVE qb_leave
322 #define TRACE1(format, args...) qb_log(LOG_TRACE, "TRACE1:" #format, ##args)
323 #define TRACE2(format, args...) qb_log(LOG_TRACE, "TRACE2:" #format, ##args)
324 #define TRACE3(format, args...) qb_log(LOG_TRACE, "TRACE3:" #format, ##args)
325 #define TRACE4(format, args...) qb_log(LOG_TRACE, "TRACE4:" #format, ##args)
326 #define TRACE5(format, args...) qb_log(LOG_TRACE, "TRACE5:" #format, ##args)
327 #define TRACE6(format, args...) qb_log(LOG_TRACE, "TRACE6:" #format, ##args)
328 #define TRACE7(format, args...) qb_log(LOG_TRACE, "TRACE7:" #format, ##args)
329 #define TRACE8(format, args...) qb_log(LOG_TRACE, "TRACE8:" #format, ##args)
330 
331 #endif /* LOGSYS_UTILS_ONLY */
332 
333 #ifdef __cplusplus
334 }
335 #endif
336 
337 #endif /* LOGSYS_H_DEFINED */
uint32_t value
const char * logsys_priority_name_get(unsigned int priority)
logsys_priority_name_get
int logsys_config_file_set(const char *subsys, const char **error_string, const char *file)
to close a logfile, just invoke this function with a NULL file or if you want to change logfile...
Definition: logsys.c:540
int syslog_priority
Definition: logsys.c:92
int _logsys_subsys_create(const char *subsys, const char *filename)
_logsys_subsys_create
Definition: logsys.c:436
int logsys_config_debug_set(const char *subsys, unsigned int value)
enabling debug, disable message priority filtering.
Definition: logsys.c:782
#define LOGSYS_MAX_SUBSYS_COUNT
Definition: logsys.h:83
int logsys_config_syslog_facility_set(const char *subsys, unsigned int facility)
per system/subsystem settings.
Definition: logsys.c:649
int logsys_config_mode_set(const char *subsys, unsigned int mode)
logsys_config_mode_set
Definition: logsys.c:506
char subsys[LOGSYS_MAX_SUBSYS_NAMELEN]
Definition: logsys.c:88
int logsys_thread_start(void)
logsys_thread_start
Definition: logsys.c:820
char * logsys_format_get(void)
logsys_format_get
Definition: logsys.c:644
typedef __attribute__
int logsys_config_syslog_priority_set(const char *subsys, unsigned int priority)
logsys_config_syslog_priority_set
Definition: logsys.c:656
int _logsys_system_setup(const char *mainsystem, unsigned int mode, int syslog_facility, int syslog_priority)
_logsys_system_setup
Definition: logsys.c:303
unsigned int mode
Definition: logsys.c:90
void logsys_config_apply(void)
logsys_config_apply
Definition: logsys.c:770
int logsys_priority_id_get(const char *name)
logsys_priority_id_get
Definition: logsys.c:808
int _logsys_config_subsys_get(const char *subsys)
_logsys_config_subsys_get
Definition: logsys.c:470
unsigned int logsys_config_mode_get(const char *subsys)
logsys_config_mode_get
Definition: logsys.c:528
int logsys_config_logfile_priority_set(const char *subsys, unsigned int priority)
logsys_config_logfile_priority_set
Definition: logsys.c:683
int logsys_format_set(const char *format)
configuration bits that can only be done for the whole system
Definition: logsys.c:586
void logsys_system_fini(void)
logsys_system_fini
Definition: logsys.c:285