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