C Standard Library Extensions 1.3.1
cxmessages.h
1/*
2 * This file is part of the ESO C Extension Library
3 * Copyright (C) 2001-2026 European Southern Observatory
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library 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 GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
17 */
18
19#ifndef CX_MESSAGES_H
20#define CX_MESSAGES_H
21
22#include <stdarg.h>
23
24#include "cxmacros.h"
25#include "cxtypes.h"
26
27
28CX_BEGIN_DECLS
29
30/*
31 * Message level offset for user defined message levels
32 * (0 - 7 are used internally).
33 */
34
35#define CX_LOG_LEVEL_USER_SHIFT (8)
36
37
38/*
39 * Log levels and flags
40 */
41
42typedef enum
43{
44 /* flags */
45 CX_LOG_FLAG_RECURSION = 1 << 0,
46 CX_LOG_FLAG_FATAL = 1 << 1,
47
48 /* levels */
49 CX_LOG_LEVEL_ERROR = 1 << 2,
50 CX_LOG_LEVEL_CRITICAL = 1 << 3,
51 CX_LOG_LEVEL_WARNING = 1 << 4,
52 CX_LOG_LEVEL_MESSAGE = 1 << 5,
53 CX_LOG_LEVEL_INFO = 1 << 6,
54 CX_LOG_LEVEL_DEBUG = 1 << 7,
55
56 CX_LOG_LEVEL_MASK = ~(CX_LOG_FLAG_RECURSION | CX_LOG_FLAG_FATAL)
57} cx_log_level_flags;
58
59#define CX_LOG_FATAL_MASK (CX_LOG_FLAG_RECURSION | CX_LOG_LEVEL_ERROR)
60
61
62/*
63 * Message handlers
64 */
65
66typedef void (*cx_log_func)(const cxchar *, cx_log_level_flags, const cxchar *,
67 cxptr);
68typedef void (*cx_print_func)(const cxchar *);
69
70
71/*
72 * Messaging mechanisms
73 */
74
75void cx_log_default_handler(const cxchar *, cx_log_level_flags, const cxchar *,
76 cxptr);
77cx_log_func cx_log_set_default_handler(cx_log_func);
78cxuint cx_log_set_handler(const cxchar *, cx_log_level_flags, cx_log_func,
79 cxptr);
80void cx_log_remove_handler(const cxchar *, cxuint);
81
82cx_log_level_flags cx_log_set_fatal_mask(const cxchar *, cx_log_level_flags);
83cx_log_level_flags cx_log_set_always_fatal(cx_log_level_flags);
84
85cxsize cx_log_get_domain_count(void);
86const cxchar *cx_log_get_domain_name(cxsize);
87
88void cx_log(const cxchar *, cx_log_level_flags, const cxchar *, ...)
89 CX_GNUC_PRINTF(3, 4);
90void cx_logv(const cxchar *, cx_log_level_flags, const cxchar *, va_list)
91 CX_GNUC_PRINTF(3, 0);
92
93cx_print_func cx_print_set_handler(cx_print_func);
94cx_print_func cx_printerr_set_handler(cx_print_func);
95
96void cx_print(const cxchar *, ...) CX_GNUC_PRINTF(1, 2);
97void cx_printerr(const cxchar *, ...) CX_GNUC_PRINTF(1, 0);
98
99
100/*
101 * Convenience functions
102 */
103
104void cx_error(const cxchar *, ...) CX_GNUC_PRINTF(1, 2);
105void cx_critical(const cxchar *, ...) CX_GNUC_PRINTF(1, 2);
106void cx_warning(const cxchar *, ...) CX_GNUC_PRINTF(1, 2);
107void cx_message(const cxchar *, ...) CX_GNUC_PRINTF(1, 2);
108
109
110#ifndef CX_LOG_DOMAIN
111# define CX_LOG_DOMAIN ((cxchar *)0)
112#endif
113
114
115/*
116 * Macros for error handling.
117 */
118
119#ifdef CX_DISABLE_ASSERT
120
121# define cx_assert(expr) /* empty */
122
123#else /* !CX_DISABLE_ASSERT */
124
125# ifdef __GNUC__
126# define cx_assert(expr) \
127 do { \
128 if (expr) { \
129 ; \
130 } \
131 else { \
132 cx_log(CX_LOG_DOMAIN, CX_LOG_LEVEL_ERROR, \
133 "file %s: line %d (%s): assertion failed: (%s)", \
134 __FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \
135 } \
136 } while (0)
137# else /* !__GNUC__ */
138# define cx_assert(expr) \
139 do { \
140 if (expr) { \
141 ; \
142 } \
143 else { \
144 cx_log(CX_LOG_DOMAIN, CX_LOG_LEVEL_ERROR, \
145 "file %s: line %d: assertion failed: (%s)", __FILE__, \
146 __LINE__, #expr); \
147 } \
148 } while (0)
149# endif /* !__GNUC__ */
150
151#endif /* !CX_DISABLE_ASSERT */
152
153CX_END_DECLS
154
155#endif /* CX_MESSAGES_H */
void cx_log_remove_handler(const cxchar *, cxuint)
Remove a log handler from a domain.
Definition cxmessages.c:858
cx_log_level_flags cx_log_set_always_fatal(cx_log_level_flags)
Set log levels to be always fatal.
Definition cxmessages.c:617
cxsize cx_log_get_domain_count(void)
Get the number of registered log domains.
Definition cxmessages.c:652
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:719
const cxchar * cx_log_get_domain_name(cxsize)
Get the name of a log domain.
Definition cxmessages.c:686
void cx_log(const cxchar *name, cx_log_level_flags level, const cxchar *format,...)
Log a formatted message.
Definition cxmessages.c:1098
void cx_log_default_handler(const cxchar *, cx_log_level_flags, const cxchar *, cxptr)
Default log handler.
Definition cxmessages.c:1132
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:803
void void cx_print_func cx_print_set_handler(cx_print_func)
Set handler for message output.
Definition cxmessages.c:1218
cx_print_func cx_printerr_set_handler(cx_print_func)
Set handler for error message output.
Definition cxmessages.c:1307
cx_log_func cx_log_set_default_handler(cx_log_func)
Set the default log handler.
Definition cxmessages.c:769