1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3 * Copyright (c) 1999-2004 Carnegie Mellon University. All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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
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.
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.
34 * ====================================================================
39 * @brief Somewhat antiquated logging and error interface.
50 #include "sphinxbase/err.h"
53 static int sphinx_debug_level;
55 err_set_debug_level(int level)
57 int prev = sphinx_debug_level;
58 sphinx_debug_level = level;
62 err_get_debug_level(void)
64 return sphinx_debug_level;
68 err_set_debug_level(int level)
74 err_get_debug_level(void)
80 #if defined(HAVE_PTHREAD_H)
82 static pthread_key_t logfp_index;
83 static pthread_once_t logfp_index_once = PTHREAD_ONCE_INIT;
86 logfp_index_alloc(void)
88 pthread_key_create(&logfp_index, NULL);
96 pthread_once(&logfp_index_once, logfp_index_alloc);
97 logfp = (FILE *)pthread_getspecific(logfp_index);
101 else if (logfp == (FILE*) -1)
108 internal_set_logfp(FILE *fh)
113 pthread_setspecific(logfp_index, (void *)fh);
116 #elif defined(_WIN32) || defined(__CYGWIN__) /* Use Windows TLS on Cygwin */
118 static DWORD logfp_index; /** TLS index for log file */
119 static LONG logfp_index_once = 0; /** True if we have initialized TLS */
122 logfp_index_alloc(void)
124 logfp_index = TlsAlloc();
132 if (InterlockedExchange(&logfp_index_once, 1) == 0)
134 logfp = (FILE *)TlsGetValue(logfp_index);
138 else if (logfp == (FILE*) -1)
145 internal_set_logfp(FILE *fh)
150 TlsSetValue(logfp_index, (void *)fh);
161 else if (logfp == (FILE*) -1)
168 internal_set_logfp(FILE *fh)
179 err_set_logfp(FILE *newfp)
183 oldfp = err_get_logfp();
184 internal_set_logfp(newfp);
190 err_set_logfile(char const *file)
194 if ((newfp = fopen(file, "a")) == NULL)
196 oldfp = err_get_logfp();
197 internal_set_logfp(newfp);
198 if (oldfp != NULL && oldfp != stdout && oldfp != stderr)
205 _E__pr_info_header_wofn(char const *msg)
209 logfp = err_get_logfp();
212 /* make different format so as not to be parsed by emacs compile */
213 fprintf(logfp, "%s:\t", msg);
218 _E__pr_header(char const *f, long ln, char const *msg)
223 logfp = err_get_logfp();
226 fname = strrchr(f,'\\');
228 fname = strrchr(f,'/');
229 fprintf(logfp, "%s: \"%s\", line %ld: ", msg, fname == NULL ? f : fname + 1, ln);
234 _E__pr_info_header(char const *f, long ln, char const *msg)
239 logfp = err_get_logfp();
242 fname = strrchr(f,'\\');
244 fname = strrchr(f,'/');
245 /* make different format so as not to be parsed by emacs compile */
246 fprintf(logfp, "%s: %s(%ld): ", msg, fname == NULL ? f : fname + 1, ln);
251 _E__pr_warn(char const *fmt, ...)
256 logfp = err_get_logfp();
260 vfprintf(logfp, fmt, pvar);
267 _E__pr_info(char const *fmt, ...)
272 logfp = err_get_logfp();
276 vfprintf(logfp, fmt, pvar);
283 _E__die_error(char const *fmt, ...)
288 logfp = err_get_logfp();
291 vfprintf(logfp, fmt, pvar);
296 #if defined(__ADSPBLACKFIN__) && !defined(__linux__)
304 _E__fatal_sys_error(char const *fmt, ...)
308 int local_errno = errno;
310 logfp = err_get_logfp();
313 vfprintf(logfp, fmt, pvar);
316 fprintf(logfp, ": %s\n", strerror(local_errno));
321 #if defined(__ADSPBLACKFIN__) && !defined(__linux__)
330 _E__sys_error(char const *fmt, ...)
334 int local_errno = errno;
336 logfp = err_get_logfp();
341 vfprintf(logfp, fmt, pvar);
344 fprintf(logfp, "; %s\n", strerror(local_errno));
349 _E__abort_error(char const *fmt, ...)
354 logfp = err_get_logfp();
357 vfprintf(logfp, fmt, pvar);
362 #if defined(__ADSPBLACKFIN__) && !defined(__linux__)
364 #elif defined(_WIN32_WCE)