SphinxBase  5prealpha
err.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3  * Copyright (c) 1999-2004 Carnegie Mellon University. All rights
4  * reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * This work was supported in part by funding from the Defense Advanced
19  * Research Projects Agency and the National Science Foundation of the
20  * United States of America, and the CMU Sphinx Speech Consortium.
21  *
22  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * ====================================================================
35  *
36  */
37 
38 #ifndef _LIBUTIL_ERR_H_
39 #define _LIBUTIL_ERR_H_
40 
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <errno.h>
45 
46 /* Win32/WinCE DLL gunk */
47 #include <sphinxbase/sphinxbase_export.h>
48 
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69 #if 0
70 /* Fool Emacs. */
71 }
72 #endif
73 
74 #define E_SYSCALL(stmt, ...) if (stmt) E_FATAL_SYSTEM(__VA_ARGS__);
75 
76 #define FILELINE __FILE__ , __LINE__
77 
81 #define E_FATAL(...) \
82  do { \
83  err_msg(ERR_FATAL, FILELINE, __VA_ARGS__); \
84  exit(EXIT_FAILURE); \
85  } while (0)
86 
90 #define E_FATAL_SYSTEM(...) \
91  do { \
92  err_msg_system(ERR_FATAL, FILELINE, __VA_ARGS__); \
93  exit(EXIT_FAILURE); \
94  } while (0)
95 
99 #define E_ERROR_SYSTEM(...) err_msg_system(ERR_ERROR, FILELINE, __VA_ARGS__)
100 
104 #define E_ERROR(...) err_msg(ERR_ERROR, FILELINE, __VA_ARGS__)
105 
109 #define E_WARN(...) err_msg(ERR_WARN, FILELINE, __VA_ARGS__)
110 
114 #define E_INFO(...) err_msg(ERR_INFO, FILELINE, __VA_ARGS__)
115 
119 #define E_INFOCONT(...) err_msg(ERR_INFOCONT, NULL, 0, __VA_ARGS__)
120 
124 #define E_INFO_NOFN(...) err_msg(ERR_INFO, NULL, 0, __VA_ARGS__)
125 
136 #ifdef SPHINX_DEBUG
137 #define E_DEBUG(level, ...) \
138  if (err_get_debug_level() >= level) \
139  err_msg(ERR_DEBUG, FILELINE, __VA_ARGS__)
140 #define E_DEBUGCONT(level, ...) \
141  if (err_get_debug_level() >= level) \
142  err_msg(ERR_DEBUG, NULL, 0, __VA_ARGS__)
143 #else
144 #define E_DEBUG(level,x)
145 #define E_DEBUGCONT(level,x)
146 #endif
147 
148 typedef enum err_e {
149  ERR_DEBUG,
150  ERR_INFO,
151  ERR_INFOCONT,
152  ERR_WARN,
153  ERR_ERROR,
154  ERR_FATAL,
155  ERR_MAX
156 } err_lvl_t;
157 
158 SPHINXBASE_EXPORT void
159 err_msg(err_lvl_t lvl, const char *path, long ln, const char *fmt, ...);
160 
161 SPHINXBASE_EXPORT void
162 err_msg_system(err_lvl_t lvl, const char *path, long ln, const char *fmt, ...);
163 
164 SPHINXBASE_EXPORT void
165 err_logfp_cb(void * user_data, err_lvl_t level, const char *fmt, ...);
166 
167 typedef void (*err_cb_f)(void* user_data, err_lvl_t, const char *, ...);
168 
177 SPHINXBASE_EXPORT void
178 err_set_callback(err_cb_f callback, void *user_data);
179 
185 SPHINXBASE_EXPORT void
186 err_set_logfp(FILE *stream);
187 
194 SPHINXBASE_EXPORT FILE *
195 err_get_logfp(void);
196 
205 SPHINXBASE_EXPORT int
206 err_set_logfile(const char *path);
207 
215 SPHINXBASE_EXPORT
216 int err_set_debug_level(int level);
217 
223 SPHINXBASE_EXPORT
224 int err_get_debug_level(void);
225 
226 #ifdef __cplusplus
227 }
228 #endif
229 
230 #endif /* !_ERR_H */
SPHINXBASE_EXPORT void err_set_logfp(FILE *stream)
Direct all logging to a given filehandle if default logfp callback is set.
Definition: err.c:255
SPHINXBASE_EXPORT FILE * err_get_logfp(void)
Get the current logging filehandle.
Definition: err.c:268
SPHINXBASE_EXPORT int err_set_debug_level(int level)
Set debugging verbosity level.
Definition: err.c:279
SPHINXBASE_EXPORT void err_set_callback(err_cb_f callback, void *user_data)
Sets function to output error messages.
Definition: err.c:293
SPHINXBASE_EXPORT int err_get_debug_level(void)
Get debugging verbosity level.
Definition: err.c:287
SPHINXBASE_EXPORT int err_set_logfile(const char *path)
Append all log messages to a given file.
Definition: err.c:241