C Standard Library Extensions  1.0.5
cxmessages.h
00001 /* $Id: cxmessages.h,v 1.9 2009/12/07 15:48:15 llundin Exp $
00002  *
00003  * This file is part of the ESO C Extension Library
00004  * Copyright (C) 2001-2006 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 /*
00022  * $Author: llundin $
00023  * $Date: 2009/12/07 15:48:15 $
00024  * $Revision: 1.9 $
00025  * $Name: cpl-5_3_0-BRANCH $
00026  */
00027 
00028 #ifndef CX_MESSAGES_H
00029 #define CX_MESSAGES_H
00030 
00031 #include <stdarg.h>
00032 
00033 #include <cxmacros.h>
00034 #include <cxtypes.h>
00035 
00036 
00037 CX_BEGIN_DECLS
00038 
00039 /*
00040  * Message level offset for user defined message levels
00041  * (0 - 7 are used internally).
00042  */
00043 
00044 #define CX_LOG_LEVEL_USER_SHIFT  (8)
00045 
00046 
00047 /*
00048  * Log levels and flags
00049  */
00050 
00051 typedef enum
00052 {
00053     /* flags */
00054     CX_LOG_FLAG_RECURSION = 1 << 0,
00055     CX_LOG_FLAG_FATAL     = 1 << 1,
00056 
00057     /* levels */
00058     CX_LOG_LEVEL_ERROR    = 1 << 2,
00059     CX_LOG_LEVEL_CRITICAL = 1 << 3,
00060     CX_LOG_LEVEL_WARNING  = 1 << 4,
00061     CX_LOG_LEVEL_MESSAGE  = 1 << 5,
00062     CX_LOG_LEVEL_INFO     = 1 << 6,
00063     CX_LOG_LEVEL_DEBUG    = 1 << 7,
00064 
00065     CX_LOG_LEVEL_MASK     = ~(CX_LOG_FLAG_RECURSION | CX_LOG_FLAG_FATAL)
00066 } cx_log_level_flags;
00067 
00068 #define CX_LOG_FATAL_MASK (CX_LOG_FLAG_RECURSION | CX_LOG_LEVEL_ERROR)
00069 
00070 
00071 /*
00072  * Message handlers
00073  */
00074 
00075 typedef void (*cx_log_func) (const cxchar *, cx_log_level_flags,
00076                                 const cxchar *, cxptr);
00077 typedef void (*cx_print_func) (const cxchar *);
00078 
00079 
00080 /*
00081  * Messaging mechanisms
00082  */
00083 
00084 void cx_log_default_handler(const cxchar *, cx_log_level_flags,
00085                             const cxchar *, cxptr);
00086 cx_log_func cx_log_set_default_handler(cx_log_func);
00087 cxuint cx_log_set_handler(const cxchar *, cx_log_level_flags,
00088                           cx_log_func, cxptr);
00089 void cx_log_remove_handler(const cxchar *, cxuint);
00090 
00091 cx_log_level_flags cx_log_set_fatal_mask(const cxchar *, cx_log_level_flags);
00092 cx_log_level_flags cx_log_set_always_fatal(cx_log_level_flags);
00093 
00094 cxsize cx_log_get_domain_count(void);
00095 const cxchar *cx_log_get_domain_name(cxsize);
00096 
00097 void cx_log(const cxchar *, cx_log_level_flags, const cxchar *, ...)
00098 #ifdef __GNUC__
00099     __attribute__((format (printf, 3, 4)))
00100 #endif
00101 ;
00102 void cx_logv(const cxchar *, cx_log_level_flags, const cxchar *, va_list)
00103 #ifdef __GNUC__
00104     __attribute__((format (printf, 3, 0)))
00105 #endif
00106 ;
00107 
00108 cx_print_func cx_print_set_handler(cx_print_func);
00109 cx_print_func cx_printerr_set_handler(cx_print_func);
00110 
00111 void cx_print(const cxchar *, ...)
00112 #ifdef __GNUC__
00113     __attribute__((format (printf, 1, 2)))
00114 #endif
00115 ;
00116 void cx_printerr(const cxchar *, ...)
00117 #ifdef __GNUC__
00118     __attribute__((format (printf, 1, 0)))
00119 #endif
00120 ;
00121 
00122 
00123 /*
00124  * Convenience functions
00125  */
00126 
00127 void cx_error(const cxchar *, ...)
00128 #ifdef __GNUC__
00129     __attribute__((format (printf, 1, 2)))
00130 #endif
00131 ;
00132 void cx_critical(const cxchar *, ...)
00133 #ifdef __GNUC__
00134     __attribute__((format (printf, 1, 2)))
00135 #endif
00136 ;
00137 void cx_warning(const cxchar *, ...)
00138 #ifdef __GNUC__
00139     __attribute__((format (printf, 1, 2)))
00140 #endif
00141 ;
00142 void cx_message(const cxchar *, ...)
00143 #ifdef __GNUC__
00144     __attribute__((format (printf, 1, 2)))
00145 #endif
00146 ;
00147 
00148 
00149 #ifndef CX_LOG_DOMAIN
00150 #  define CX_LOG_DOMAIN  ((cxchar *)0)
00151 #endif
00152 
00153 
00154 /*
00155  * Macros for error handling.
00156  */
00157 
00158 #ifdef CX_DISABLE_ASSERT
00159 
00160 #  define cx_assert(expr)  /* empty */
00161 
00162 #else /* !CX_DISABLE_ASSERT */
00163 
00164 #  ifdef __GNUC__
00165 #    define cx_assert(expr)                                           \
00166      do {                                                             \
00167          if (expr) {                                                  \
00168              ;                                                        \
00169          }                                                            \
00170          else {                                                       \
00171              cx_log(CX_LOG_DOMAIN, CX_LOG_LEVEL_ERROR,                \
00172                     "file %s: line %d (%s): assertion failed: (%s)",  \
00173                     __FILE__, __LINE__, __PRETTY_FUNCTION__, #expr);  \
00174          }                                                            \
00175      } while (0)
00176 #  else /* !__GNUC__ */
00177 #    define cx_assert(expr)                                           \
00178      do {                                                             \
00179          if (expr) {                                                  \
00180              ;                                                        \
00181          }                                                            \
00182          else {                                                       \
00183              cx_log(CX_LOG_DOMAIN,CX_LOG_LEVEL_ERROR,                 \
00184                     "file %s: line %d: assertion failed: (%s)",       \
00185                     __FILE__, __LINE__, #expr);                       \
00186          }                                                            \
00187      } while (0)
00188 #  endif /* !__GNUC__ */
00189 
00190 #endif /* !CX_DISABLE_ASSERT */
00191 
00192 CX_END_DECLS
00193 
00194 #endif /* CX_MESSAGES_H */
00195