cxmessages.h
/*
* This file is part of the ESO C Extension Library
* Copyright (C) 2001-2026 European Southern Observatory
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <https://www.gnu.org/licenses/>.
*/
#ifndef CX_MESSAGES_H
#define CX_MESSAGES_H
#include <stdarg.h>
#include "cxmacros.h"
#include "cxtypes.h"
CX_BEGIN_DECLS
/*
* Message level offset for user defined message levels
* (0 - 7 are used internally).
*/
#define CX_LOG_LEVEL_USER_SHIFT (8)
/*
* Log levels and flags
*/
typedef enum
{
/* flags */
CX_LOG_FLAG_RECURSION = 1 << 0,
CX_LOG_FLAG_FATAL = 1 << 1,
/* levels */
CX_LOG_LEVEL_ERROR = 1 << 2,
CX_LOG_LEVEL_CRITICAL = 1 << 3,
CX_LOG_LEVEL_WARNING = 1 << 4,
CX_LOG_LEVEL_MESSAGE = 1 << 5,
CX_LOG_LEVEL_INFO = 1 << 6,
CX_LOG_LEVEL_DEBUG = 1 << 7,
CX_LOG_LEVEL_MASK = ~(CX_LOG_FLAG_RECURSION | CX_LOG_FLAG_FATAL)
} cx_log_level_flags;
#define CX_LOG_FATAL_MASK (CX_LOG_FLAG_RECURSION | CX_LOG_LEVEL_ERROR)
/*
* Message handlers
*/
typedef void (*cx_log_func)(const cxchar *, cx_log_level_flags, const cxchar *,
cxptr);
typedef void (*cx_print_func)(const cxchar *);
/*
* Messaging mechanisms
*/
void cx_log_default_handler(const cxchar *, cx_log_level_flags, const cxchar *,
cxptr);
cx_log_func cx_log_set_default_handler(cx_log_func);
cxuint cx_log_set_handler(const cxchar *, cx_log_level_flags, cx_log_func,
cxptr);
void cx_log_remove_handler(const cxchar *, cxuint);
cx_log_level_flags cx_log_set_fatal_mask(const cxchar *, cx_log_level_flags);
cx_log_level_flags cx_log_set_always_fatal(cx_log_level_flags);
cxsize cx_log_get_domain_count(void);
const cxchar *cx_log_get_domain_name(cxsize);
void cx_log(const cxchar *, cx_log_level_flags, const cxchar *, ...)
CX_GNUC_PRINTF(3, 4);
void cx_logv(const cxchar *, cx_log_level_flags, const cxchar *, va_list)
CX_GNUC_PRINTF(3, 0);
cx_print_func cx_print_set_handler(cx_print_func);
cx_print_func cx_printerr_set_handler(cx_print_func);
void cx_print(const cxchar *, ...) CX_GNUC_PRINTF(1, 2);
void cx_printerr(const cxchar *, ...) CX_GNUC_PRINTF(1, 0);
/*
* Convenience functions
*/
void cx_error(const cxchar *, ...) CX_GNUC_PRINTF(1, 2);
void cx_critical(const cxchar *, ...) CX_GNUC_PRINTF(1, 2);
void cx_warning(const cxchar *, ...) CX_GNUC_PRINTF(1, 2);
void cx_message(const cxchar *, ...) CX_GNUC_PRINTF(1, 2);
#ifndef CX_LOG_DOMAIN
# define CX_LOG_DOMAIN ((cxchar *)0)
#endif
/*
* Macros for error handling.
*/
#ifdef CX_DISABLE_ASSERT
# define cx_assert(expr) /* empty */
#else /* !CX_DISABLE_ASSERT */
# ifdef __GNUC__
# define cx_assert(expr) \
do { \
if (expr) { \
; \
} \
else { \
cx_log(CX_LOG_DOMAIN, CX_LOG_LEVEL_ERROR, \
"file %s: line %d (%s): assertion failed: (%s)", \
__FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \
} \
} while (0)
# else /* !__GNUC__ */
# define cx_assert(expr) \
do { \
if (expr) { \
; \
} \
else { \
cx_log(CX_LOG_DOMAIN, CX_LOG_LEVEL_ERROR, \
"file %s: line %d: assertion failed: (%s)", __FILE__, \
__LINE__, #expr); \
} \
} while (0)
# endif /* !__GNUC__ */
#endif /* !CX_DISABLE_ASSERT */
CX_END_DECLS
#endif /* CX_MESSAGES_H */
cx_message
void cx_message(const cxchar *format,...)
Log a normal message.
Definition cxmessages.c:1492
cx_log_remove_handler
void cx_log_remove_handler(const cxchar *, cxuint)
Remove a log handler from a domain.
Definition cxmessages.c:858
cx_log_set_always_fatal
cx_log_level_flags cx_log_set_always_fatal(cx_log_level_flags)
Set log levels to be always fatal.
Definition cxmessages.c:617
cx_log_get_domain_count
cxsize cx_log_get_domain_count(void)
Get the number of registered log domains.
Definition cxmessages.c:652
cx_print
void cx_print(const cxchar *format,...)
Output a formatted message via the print handler.
Definition cxmessages.c:1258
cx_log_set_fatal_mask
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
cx_warning
void cx_warning(const cxchar *format,...)
Log a warning.
Definition cxmessages.c:1464
cx_logv
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:931
cx_log_get_domain_name
const cxchar * cx_log_get_domain_name(cxsize)
Get the name of a log domain.
Definition cxmessages.c:686
cx_log
void cx_log(const cxchar *name, cx_log_level_flags level, const cxchar *format,...)
Log a formatted message.
Definition cxmessages.c:1098
cx_log_default_handler
void cx_log_default_handler(const cxchar *, cx_log_level_flags, const cxchar *, cxptr)
Default log handler.
Definition cxmessages.c:1132
cx_log_set_handler
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
cx_print_set_handler
cx_print_func cx_print_set_handler(cx_print_func func)
Set handler for message output.
Definition cxmessages.c:1218
cx_printerr_set_handler
cx_print_func cx_printerr_set_handler(cx_print_func func)
Set handler for error message output.
Definition cxmessages.c:1307
cx_log_set_default_handler
cx_log_func cx_log_set_default_handler(cx_log_func)
Set the default log handler.
Definition cxmessages.c:769
cx_error
void cx_error(const cxchar *format,...)
Log an error message.
Definition cxmessages.c:1402
cx_printerr
void cx_printerr(const cxchar *format,...)
Output a formatted message via the error message handler.
Definition cxmessages.c:1347
cx_critical
void cx_critical(const cxchar *format,...)
Log a "critical" warning.
Definition cxmessages.c:1435