corosync  2.3.2
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 
96 /*
97  * configuration bits that can only be done for the whole system
98  */
99 extern int logsys_format_set (
100  const char *format);
101 
102 extern char *logsys_format_get (void);
103 
104 /*
105  * per system/subsystem settings.
106  *
107  * NOTE: once a subsystem is created and configured, changing
108  * the default does NOT affect the subsystems.
109  *
110  * Pass a NULL subsystem to change them all
111  */
113  const char *subsys,
114  unsigned int facility);
115 
117  const char *subsys,
118  unsigned int priority);
119 
120 extern int logsys_config_mode_set (
121  const char *subsys,
122  unsigned int mode);
123 
124 extern unsigned int logsys_config_mode_get (
125  const char *subsys);
126 
127 void logsys_config_apply(void);
128 
129 /*
130  * to close a logfile, just invoke this function with a NULL
131  * file or if you want to change logfile, the old one will
132  * be closed for you.
133  */
134 extern int logsys_config_file_set (
135  const char *subsys,
136  const char **error_string,
137  const char *file);
138 
140  const char *subsys,
141  unsigned int priority);
142 
143 /*
144  * enabling debug, disable message priority filtering.
145  * everything is sent everywhere. priority values
146  * for file and syslog are not overwritten.
147  */
148 extern int logsys_config_debug_set (
149  const char *subsys,
150  unsigned int value);
151 
152 /*
153  * External API - helpers
154  *
155  * convert facility/priority to/from name/values
156  */
157 extern int logsys_priority_id_get (
158  const char *name);
159 
160 extern const char *logsys_priority_name_get (
161  unsigned int priority);
162 
163 extern int _logsys_system_setup(
164  const char *mainsystem,
165  unsigned int mode,
166  int syslog_facility,
167  int syslog_priority);
168 
169 extern void logsys_system_fini (void);
170 
171 extern int _logsys_config_subsys_get (
172  const char *subsys);
173 
174 extern int _logsys_subsys_create (const char *subsys, const char *filename);
175 
176 extern int logsys_thread_start (void);
177 
178 static int logsys_subsys_id __attribute__((unused)) = LOGSYS_MAX_SUBSYS_COUNT;
179 
180 #define LOGSYS_DECLARE_SYSTEM(name,mode,syslog_facility,syslog_priority)\
181 __attribute__ ((constructor)) \
182 static void logsys_system_init (void) \
183 { \
184  if (_logsys_system_setup (name,mode,syslog_facility,syslog_priority) < 0) { \
185  fprintf (stderr, \
186  "Unable to setup logging system: %s.\n", name); \
187  exit (-1); \
188  } \
189 }
190 
191 #ifdef QB_HAVE_ATTRIBUTE_SECTION
192 #define LOGSYS_DECLARE_SECTION assert(__start___verbose != __stop___verbose)
193 #else
194 #define LOGSYS_DECLARE_SECTION
195 #endif
196 
197 #define LOGSYS_DECLARE_SUBSYS(subsys) \
198 __attribute__ ((constructor)) \
199 static void logsys_subsys_init (void) \
200 { \
201  LOGSYS_DECLARE_SECTION; \
202  logsys_subsys_id = \
203  _logsys_subsys_create ((subsys), __FILE__); \
204  if (logsys_subsys_id == -1) { \
205  fprintf (stderr, \
206  "Unable to create logging subsystem: %s.\n", subsys); \
207  exit (-1); \
208  } \
209 }
210 
211 #define LOGSYS_PERROR(err_num, level, fmt, args...) do { \
212  char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
213  const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
214  qb_log(level, fmt ": %s (%d)", ##args, _error_ptr, err_num); \
215  } while(0)
216 
217 #define log_printf(level, format, args...) qb_log(level, format, ##args)
218 #define ENTER qb_enter
219 #define LEAVE qb_leave
220 #define TRACE1(format, args...) qb_log(LOG_TRACE, "TRACE1:" #format, ##args)
221 #define TRACE2(format, args...) qb_log(LOG_TRACE, "TRACE2:" #format, ##args)
222 #define TRACE3(format, args...) qb_log(LOG_TRACE, "TRACE3:" #format, ##args)
223 #define TRACE4(format, args...) qb_log(LOG_TRACE, "TRACE4:" #format, ##args)
224 #define TRACE5(format, args...) qb_log(LOG_TRACE, "TRACE5:" #format, ##args)
225 #define TRACE6(format, args...) qb_log(LOG_TRACE, "TRACE6:" #format, ##args)
226 #define TRACE7(format, args...) qb_log(LOG_TRACE, "TRACE7:" #format, ##args)
227 #define TRACE8(format, args...) qb_log(LOG_TRACE, "TRACE8:" #format, ##args)
228 
229 #endif /* LOGSYS_UTILS_ONLY */
230 
231 #ifdef __cplusplus
232 }
233 #endif
234 
235 #endif /* LOGSYS_H_DEFINED */
uint32_t value
const char * logsys_priority_name_get(unsigned int priority)
int logsys_config_file_set(const char *subsys, const char **error_string, const char *file)
Definition: logsys.c:531
int syslog_priority
Definition: logsys.c:92
int _logsys_subsys_create(const char *subsys, const char *filename)
Definition: logsys.c:427
int logsys_config_debug_set(const char *subsys, unsigned int value)
Definition: logsys.c:773
#define LOGSYS_MAX_SUBSYS_COUNT
Definition: logsys.h:83
int logsys_config_syslog_facility_set(const char *subsys, unsigned int facility)
Definition: logsys.c:640
int logsys_config_mode_set(const char *subsys, unsigned int mode)
Definition: logsys.c:497
char subsys[LOGSYS_MAX_SUBSYS_NAMELEN]
Definition: logsys.c:88
int logsys_thread_start(void)
Definition: logsys.c:811
char * logsys_format_get(void)
Definition: logsys.c:635
int logsys_config_syslog_priority_set(const char *subsys, unsigned int priority)
Definition: logsys.c:647
int _logsys_system_setup(const char *mainsystem, unsigned int mode, int syslog_facility, int syslog_priority)
Definition: logsys.c:290
unsigned int mode
Definition: logsys.c:90
void logsys_config_apply(void)
Definition: logsys.c:761
int logsys_priority_id_get(const char *name)
Definition: logsys.c:799
int _logsys_config_subsys_get(const char *subsys)
Definition: logsys.c:461
typedef __attribute__
unsigned int logsys_config_mode_get(const char *subsys)
Definition: logsys.c:519
int logsys_config_logfile_priority_set(const char *subsys, unsigned int priority)
Definition: logsys.c:674
int logsys_format_set(const char *format)
Definition: logsys.c:577
void logsys_system_fini(void)
Definition: logsys.c:272