h5_log.h 5.43 KB
Newer Older
1
/*
gsell's avatar
gsell committed
2
  Copyright (c) 2006-2016, The Regents of the University of California,
3 4 5 6 7 8 9
  through Lawrence Berkeley National Laboratory (subject to receipt of any
  required approvals from the U.S. Dept. of Energy) and the Paul Scherrer
  Institut (Switzerland).  All rights reserved.

  License: see file COPYING in top level of source distribution.
*/

10 11
#ifndef __H5CORE_H5_LOG_H
#define __H5CORE_H5_LOG_H
12

gsell's avatar
gsell committed
13 14 15
#include <stdio.h>
#include <stddef.h>

16
extern const char* H5_VER_STRING;
17

18 19
#include "h5core/h5_types.h"
#include "h5core/h5_err.h"
20

21 22 23 24
#define H5_VERBOSE_NONE    	(0)
#define H5_VERBOSE_ERROR   	(1)
#define H5_VERBOSE_WARN    	(2)
#define H5_VERBOSE_INFO    	(3)
25
#define H5_VERBOSE_DEBUG    	(4)
26

gsell's avatar
gsell committed
27 28
#define H5_VERBOSE_DEFAULT      H5_VERBOSE_ERROR

29 30 31 32 33 34 35 36 37 38
#define H5_DEBUG_USER		(1<<2)
#define H5_DEBUG_API		(1<<3)
#define H5_DEBUG_CORE_API	(1<<4)
#define H5_DEBUG_PRIV_API	(1<<5)
#define H5_DEBUG_PRIV_FUNC	(1<<6)
#define H5_DEBUG_HDF5		(1<<7)
#define H5_DEBUG_MPI		(1<<8)
#define H5_DEBUG_MALLOC		(1<<9)
#define H5_DEBUG_CLIB		(1<<10)

gsell's avatar
gsell committed
39
#define H5_DEBUG_NONE		(0)
40
#define H5_DEBUG_ALL		(-1 & ~0x3)
41 42

extern char* h5_rfmts[];
gsell's avatar
gsell committed
43 44 45 46 47 48
enum h5_rtypes {
	e_int = 0,
	e_ssize_t,
	e_char_p,
	e_void_p,
	e_h5_err_t,
49 50
	e_h5_int32_t,
	e_h5_uint32_t,
gsell's avatar
gsell committed
51
	e_h5_int64_t,
52
	e_h5_uint64_t,
gsell's avatar
gsell committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	e_h5_id_t,
	e_h5_ssize_t,
	e_h5_errorhandler_t,
	e_h5_file_p,
	e_h5_file_t,
	e_h5_lvl_idx_t,
	e_h5t_iterator_p,
	e_h5_loc_id_t,
	e_h5_loc_idx_t,
	e_hid_t,
	e_H5O_type_t,
	e_h5_glb_elem_p,
    	e_h5_prop_p,
        e_h5_prop_t,
    	e_h5_prop_file_p,
        e_h5_prop_file_t,
	e_herr_t
};

struct call_stack_entry {
	char* name;
	enum h5_rtypes type;
};

struct call_stack {
	int level;
	struct call_stack_entry entry[1024];
};

Gsell Achim's avatar
* C-API  
Gsell Achim committed
82 83
extern h5_int64_t __h5_log_level;
extern h5_int64_t __h5_debug_mask;
gsell's avatar
gsell committed
84 85
extern struct call_stack h5_call_stack;

86 87 88 89 90
// :FIXME: Should go to another header file
h5_err_t
h5_initialize (void);


91 92 93 94
#ifdef __cplusplus
extern "C" {
#endif

gsell's avatar
gsell committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
static inline void
h5_call_stack_push (
        const char* fname,
        enum h5_rtypes type
        ) {
	h5_call_stack.entry[h5_call_stack.level].name = (char *)fname;
	h5_call_stack.entry[h5_call_stack.level].type = type;
	h5_call_stack.level++;
}

static inline const char*
h5_call_stack_pop (
        void
        ) {
	return h5_call_stack.entry[--h5_call_stack.level].name;
}

static inline const char*
h5_call_stack_get_name (
        void
        ) {
	return h5_call_stack.entry[h5_call_stack.level-1].name;
}

static inline const char*
h5_get_funcname (
        void
        ) {
	return h5_call_stack.entry[0].name;
}

static inline enum h5_rtypes
h5_call_stack_get_type (
        void
        ) {
	return h5_call_stack.entry[h5_call_stack.level-1].type;
}

static inline int
h5_call_stack_get_level (
        void
        ) {
	return h5_call_stack.level;
}

static inline const char*
h5_call_stack_reset (
        void
        ) {
	h5_call_stack.level = 0;
	return h5_call_stack.entry[0].name;
}

/*!
   \ingroup h5_core_errorhandling

gsell's avatar
gsell committed
151
   Print a debug message to \c stdout.
gsell's avatar
gsell committed
152
 */
gsell's avatar
gsell committed
153 154
void
h5_debug (
gsell's avatar
gsell committed
155 156 157 158 159 160 161 162 163 164 165
        const char *fmt,
        ...
        )
#ifdef __GNUC__
__attribute__ ((format (printf, 1, 2)))
#endif
;

/*!
   \ingroup h5_core_errorhandling

gsell's avatar
gsell committed
166
   Print a warning message to \c stderr.
gsell's avatar
gsell committed
167
 */
gsell's avatar
gsell committed
168 169
h5_err_t
h5_warn (
gsell's avatar
gsell committed
170 171 172 173 174 175 176 177 178 179 180
        const char *fmt,
        ...
        )
#ifdef __GNUC__
__attribute__ ((format (printf, 1, 2)))
#endif
;

/*!
   \ingroup h5_core_errorhandling

gsell's avatar
gsell committed
181
   Print an informational message to \c stdout.
gsell's avatar
gsell committed
182
 */
gsell's avatar
gsell committed
183 184
void
h5_info (
gsell's avatar
gsell committed
185 186 187 188 189 190 191 192
        const char *fmt,
        ...
        )
#ifdef __GNUC__
__attribute__ ((format (printf, 1, 2)))
#endif
;

gsell's avatar
gsell committed
193 194
h5_err_t
h5_set_loglevel (
195
	const h5_int64_t);
gsell's avatar
gsell committed
196 197

h5_err_t
198 199 200 201
h5_set_debug_mask (
	const h5_int64_t);

h5_int64_t
gsell's avatar
gsell committed
202 203 204 205 206 207 208
h5_get_loglevel (
	void);

#ifdef __cplusplus
}
#endif

209 210 211 212
//////////////////////////////////////////////////////////////////////////////
// function enter macro
#if defined(NDEBUG)

213 214 215
#define H5_API_ENTER(type, fmt, ...)					\
	type ret_value = (type)H5_ERR;					\
	h5_initialize();						\
216
	h5_call_stack_reset ();						\
217
	h5_call_stack_push (__func__,e_##type);
218 219 220

#else   // NDEBUG not defined

gsell's avatar
gsell committed
221
#define H5_API_ENTER(type, fmt, ...)					\
222
	type ret_value = (type)H5_ERR;					\
223 224 225
	h5_initialize();						\
	h5_call_stack_reset ();						\
	h5_call_stack_push (__func__,e_##type);				\
Gsell Achim's avatar
* C-API  
Gsell Achim committed
226
	int __log__ = __h5_debug_mask & H5_DEBUG_API;			\
gsell's avatar
gsell committed
227
	if (__log__) {							\
228
		h5_debug ("(" fmt ")", __VA_ARGS__);			\
gsell's avatar
gsell committed
229
	}
230 231 232 233 234

#endif
//
//////////////////////////////////////////////////////////////////////////////

gsell's avatar
gsell committed
235
#define H5_LEAVE(expr) {						\
236 237 238 239 240
		ret_value = expr;					\
		goto done;						\
	}

#define H5_RETURN_ERROR(errno, fmt, ...) {				\
241
		ret_value = h5_error (errno, fmt, __VA_ARGS__);		\
242 243
		goto done;						\
	}
244 245 246 247 248

//////////////////////////////////////////////////////////////////////////////
// function return macro
#if defined(NDEBUG)

gsell's avatar
gsell committed
249
#define H5_RETURN(expr)							\
250 251 252 253 254 255 256
	ret_value = expr;						\
	goto done;							\
done:									\
	return ret_value;

#else  // NDEBUG not defined

gsell's avatar
gsell committed
257
#define H5_RETURN(expr)							\
258 259 260
	ret_value = expr;						\
	goto done;							\
done:									\
261
	if (__log__ ) {							\
262 263 264 265 266 267 268 269 270 271 272 273
		char fmt[256];						\
		snprintf (fmt, sizeof(fmt), "return: %s",		\
			  h5_rfmts[h5_call_stack_get_type()]);		\
		h5_debug (fmt, ret_value);				\
		h5_call_stack_pop();					\
	}								\
	return ret_value;

#endif
//
//////////////////////////////////////////////////////////////////////////////

gsell's avatar
gsell committed
274 275
#define H5_API_LEAVE(expr)		H5_LEAVE(expr)
#define H5_API_RETURN(expr)		H5_RETURN(expr);
276 277 278 279 280 281 282 283 284


#define TRY( func )							\
	if ((int64_t)(ptrdiff_t)(func) <= (int64_t)H5_ERR) {		\
		goto done;						\
	}


#endif