C Standard Library Extensions  1.2.3
cxmessages.h
1 /*
2  * This file is part of the ESO C Extension Library
3  * Copyright (C) 2001-2017 European Southern Observatory
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #ifndef CX_MESSAGES_H
21 #define CX_MESSAGES_H
22 
23 #include <stdarg.h>
24 
25 #include <cxmacros.h>
26 #include <cxtypes.h>
27 
28 
29 CX_BEGIN_DECLS
30 
31 /*
32  * Message level offset for user defined message levels
33  * (0 - 7 are used internally).
34  */
35 
36 #define CX_LOG_LEVEL_USER_SHIFT (8)
37 
38 
39 /*
40  * Log levels and flags
41  */
42 
43 typedef enum
44 {
45  /* flags */
46  CX_LOG_FLAG_RECURSION = 1 << 0,
47  CX_LOG_FLAG_FATAL = 1 << 1,
48 
49  /* levels */
50  CX_LOG_LEVEL_ERROR = 1 << 2,
51  CX_LOG_LEVEL_CRITICAL = 1 << 3,
52  CX_LOG_LEVEL_WARNING = 1 << 4,
53  CX_LOG_LEVEL_MESSAGE = 1 << 5,
54  CX_LOG_LEVEL_INFO = 1 << 6,
55  CX_LOG_LEVEL_DEBUG = 1 << 7,
56 
57  CX_LOG_LEVEL_MASK = ~(CX_LOG_FLAG_RECURSION | CX_LOG_FLAG_FATAL)
58 } cx_log_level_flags;
59 
60 #define CX_LOG_FATAL_MASK (CX_LOG_FLAG_RECURSION | CX_LOG_LEVEL_ERROR)
61 
62 
63 /*
64  * Message handlers
65  */
66 
67 typedef void (*cx_log_func) (const cxchar *, cx_log_level_flags,
68  const cxchar *, cxptr);
69 typedef void (*cx_print_func) (const cxchar *);
70 
71 
72 /*
73  * Messaging mechanisms
74  */
75 
76 void cx_log_default_handler(const cxchar *, cx_log_level_flags,
77  const cxchar *, cxptr);
78 cx_log_func cx_log_set_default_handler(cx_log_func);
79 cxuint cx_log_set_handler(const cxchar *, cx_log_level_flags,
80  cx_log_func, cxptr);
81 void cx_log_remove_handler(const cxchar *, cxuint);
82 
83 cx_log_level_flags cx_log_set_fatal_mask(const cxchar *, cx_log_level_flags);
84 cx_log_level_flags cx_log_set_always_fatal(cx_log_level_flags);
85 
86 cxsize cx_log_get_domain_count(void);
87 const cxchar *cx_log_get_domain_name(cxsize);
88 
89 void cx_log(const cxchar *, cx_log_level_flags,
90  const cxchar *, ...) CX_GNUC_PRINTF(3, 4);
91 void cx_logv(const cxchar *, cx_log_level_flags,
92  const cxchar *, va_list) CX_GNUC_PRINTF(3, 0);
93 
94 cx_print_func cx_print_set_handler(cx_print_func);
95 cx_print_func cx_printerr_set_handler(cx_print_func);
96 
97 void cx_print(const cxchar *, ...) CX_GNUC_PRINTF(1, 2);
98 void cx_printerr(const cxchar *, ...) CX_GNUC_PRINTF(1, 0);
99 
100 
101 /*
102  * Convenience functions
103  */
104 
105 void cx_error(const cxchar *, ...) CX_GNUC_PRINTF(1, 2);
106 void cx_critical(const cxchar *, ...) CX_GNUC_PRINTF(1, 2);
107 void cx_warning(const cxchar *, ...) CX_GNUC_PRINTF(1, 2);
108 void cx_message(const cxchar *, ...) CX_GNUC_PRINTF(1, 2);
109 
110 
111 #ifndef CX_LOG_DOMAIN
112 # define CX_LOG_DOMAIN ((cxchar *)0)
113 #endif
114 
115 
116 /*
117  * Macros for error handling.
118  */
119 
120 #ifdef CX_DISABLE_ASSERT
121 
122 # define cx_assert(expr) /* empty */
123 
124 #else /* !CX_DISABLE_ASSERT */
125 
126 # ifdef __GNUC__
127 # define cx_assert(expr) \
128  do { \
129  if (expr) { \
130  ; \
131  } \
132  else { \
133  cx_log(CX_LOG_DOMAIN, CX_LOG_LEVEL_ERROR, \
134  "file %s: line %d (%s): assertion failed: (%s)", \
135  __FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \
136  } \
137  } while (0)
138 # else /* !__GNUC__ */
139 # define cx_assert(expr) \
140  do { \
141  if (expr) { \
142  ; \
143  } \
144  else { \
145  cx_log(CX_LOG_DOMAIN,CX_LOG_LEVEL_ERROR, \
146  "file %s: line %d: assertion failed: (%s)", \
147  __FILE__, __LINE__, #expr); \
148  } \
149  } while (0)
150 # endif /* !__GNUC__ */
151 
152 #endif /* !CX_DISABLE_ASSERT */
153 
154 CX_END_DECLS
155 
156 #endif /* CX_MESSAGES_H */
157 
cx_log_level_flags cx_log_set_fatal_mask(const cxchar *, cx_log_level_flags)
Sets the log message level which are fatal for a given domain.
Definition: cxmessages.c:740
void cx_critical(const cxchar *format,...)
Log a "critical" warning.
Definition: cxmessages.c:1458
void cx_log(const cxchar *name, cx_log_level_flags level, const cxchar *format,...)
Log a formatted message.
Definition: cxmessages.c:1120
const cxchar * cx_log_get_domain_name(cxsize)
Get the name of a log domain.
Definition: cxmessages.c:705
cx_log_func cx_log_set_default_handler(cx_log_func)
Set the default log handler.
Definition: cxmessages.c:790
void void cx_print_func cx_print_set_handler(cx_print_func)
Set handler for message output.
Definition: cxmessages.c:1238
void cx_warning(const cxchar *format,...)
Log a warning.
Definition: cxmessages.c:1486
void cx_print(const cxchar *format,...)
Output a formatted message via the print handler.
Definition: cxmessages.c:1280
cxsize cx_log_get_domain_count(void)
Get the number of registered log domains.
Definition: cxmessages.c:669
void cx_error(const cxchar *format,...)
Log an error message.
Definition: cxmessages.c:1425
cx_print_func cx_printerr_set_handler(cx_print_func)
Set handler for error message output.
Definition: cxmessages.c:1329
void cx_logv(const cxchar *name, cx_log_level_flags level, const cxchar *format, va_list args)
Log a formatted message using a variable-length argument.
Definition: cxmessages.c:949
cx_log_level_flags cx_log_set_always_fatal(cx_log_level_flags)
Set log levels to be always fatal.
Definition: cxmessages.c:632
void cx_printerr(const cxchar *format,...)
Output a formatted message via the error message handler.
Definition: cxmessages.c:1371
void cx_log_remove_handler(const cxchar *, cxuint)
Remove a log handler from a domain.
Definition: cxmessages.c:878
cxuint cx_log_set_handler(const cxchar *, cx_log_level_flags, cx_log_func, cxptr)
Set the log handler for a log domain.
Definition: cxmessages.c:825
void cx_log_default_handler(const cxchar *, cx_log_level_flags, const cxchar *, cxptr)
Default log handler.
Definition: cxmessages.c:1156
void cx_message(const cxchar *format,...)
Log a normal message.
Definition: cxmessages.c:1513