SphinxBase  5prealpha
err.c
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  */
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45 
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <stdarg.h>
49 #include <string.h>
50 #include <errno.h>
51 
52 #include "sphinxbase/err.h"
53 #include "sphinxbase/prim_type.h"
54 #include "sphinxbase/filename.h"
55 #include "sphinxbase/ckd_alloc.h"
56 
57 static FILE* logfp = NULL;
58 static int logfp_disabled = FALSE;
59 
60 #if defined(__ANDROID__)
61 #include <android/log.h>
62 static void
63 err_logcat_cb(void* user_data, err_lvl_t level, const char *fmt, ...);
64 #elif defined(_WIN32_WCE)
65 #include <windows.h>
66 #define vsnprintf _vsnprintf
67 static void
68 err_wince_cb(void* user_data, err_lvl_t level, const char *fmt, ...);
69 #endif
70 
71 #if defined(__ANDROID__)
72 static err_cb_f err_cb = err_logcat_cb;
73 #elif defined(_WIN32_WCE)
74 static err_cb_f err_cb = err_wince_cb;
75 #else
76 static err_cb_f err_cb = err_logfp_cb;
77 #endif
78 static void* err_user_data;
79 
80 void
81 err_msg(err_lvl_t lvl, const char *path, long ln, const char *fmt, ...)
82 {
83  static const char *err_prefix[ERR_MAX] = {
84  "DEBUG", "INFO", "INFOCONT", "WARN", "ERROR", "FATAL"
85  };
86 
87  char msg[1024];
88  va_list ap;
89 
90  if (!err_cb)
91  return;
92 
93  va_start(ap, fmt);
94  vsnprintf(msg, sizeof(msg), fmt, ap);
95  va_end(ap);
96 
97  if (path) {
98  const char *fname = path2basename(path);
99  if (lvl == ERR_INFOCONT)
100  err_cb(err_user_data, lvl, "%s(%ld): %s", fname, ln, msg);
101  else if (lvl == ERR_INFO)
102  err_cb(err_user_data, lvl, "%s: %s(%ld): %s", err_prefix[lvl], fname, ln, msg);
103  else
104  err_cb(err_user_data, lvl, "%s: \"%s\", line %ld: %s", err_prefix[lvl], fname, ln, msg);
105  } else {
106  err_cb(err_user_data, lvl, "%s", msg);
107  }
108 }
109 
110 #ifdef _WIN32_WCE /* No strerror for WinCE, so a separate implementation */
111 void
112 err_msg_system(err_lvl_t lvl, const char *path, long ln, const char *fmt, ...)
113 {
114  static const char *err_prefix[ERR_MAX] = {
115  "DEBUG", "INFO", "INFOCONT", "WARN", "ERROR", "FATAL"
116  };
117 
118  va_list ap;
119  LPVOID error_wstring;
120  DWORD error;
121  char msg[1024];
122  char error_string[1024];
123 
124  if (!err_cb)
125  return;
126 
127  error = GetLastError();
128  FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
129  FORMAT_MESSAGE_FROM_SYSTEM |
130  FORMAT_MESSAGE_IGNORE_INSERTS,
131  NULL,
132  error,
133  0, // Default language
134  (LPTSTR) &error_wstring,
135  0,
136  NULL);
137  wcstombs(error_string, error_wstring, 1023);
138  LocalFree(error_wstring);
139 
140  va_start(ap, fmt);
141  vsnprintf(msg, sizeof(msg), fmt, ap);
142  va_end(ap);
143 
144  if (path) {
145  const char *fname = path2basename(path);
146  if (lvl == ERR_INFOCONT)
147  err_cb(err_user_data, lvl, "%s(%ld): %s: %s\n", fname, ln, msg, error_string);
148  else if (lvl == ERR_INFO)
149  err_cb(err_user_data, lvl, "%s: %s(%ld): %s: %s\n", err_prefix[lvl], fname, ln, msg, error_string);
150  else
151  err_cb(err_user_data, lvl, "%s: \"%s\", line %ld: %s: %s\n", err_prefix[lvl], fname, ln, msg, error_string);
152  } else {
153  err_cb(err_user_data, lvl, "%s: %s\n", msg, error_string);
154  }
155 }
156 #else
157 void
158 err_msg_system(err_lvl_t lvl, const char *path, long ln, const char *fmt, ...)
159 {
160  int local_errno = errno;
161 
162  static const char *err_prefix[ERR_MAX] = {
163  "DEBUG", "INFO", "INFOCONT", "WARN", "ERROR", "FATAL"
164  };
165 
166  char msg[1024];
167  va_list ap;
168 
169  if (!err_cb)
170  return;
171 
172  va_start(ap, fmt);
173  vsnprintf(msg, sizeof(msg), fmt, ap);
174  va_end(ap);
175 
176  if (path) {
177  const char *fname = path2basename(path);
178  if (lvl == ERR_INFOCONT)
179  err_cb(err_user_data, lvl, "%s(%ld): %s: %s\n", fname, ln, msg, strerror(local_errno));
180  else if (lvl == ERR_INFO)
181  err_cb(err_user_data, lvl, "%s: %s(%ld): %s: %s\n", err_prefix[lvl], fname, ln, msg, strerror(local_errno));
182  else
183  err_cb(err_user_data, lvl, "%s: \"%s\", line %ld: %s: %s\n", err_prefix[lvl], fname, ln, msg, strerror(local_errno));
184  } else {
185  err_cb(err_user_data, lvl, "%s: %s\n", msg, strerror(local_errno));
186  }
187 }
188 #endif
189 
190 #if defined(__ANDROID__)
191 static void
192 err_logcat_cb(void *user_data, err_lvl_t lvl, const char *fmt, ...)
193 {
194  static const int android_level[ERR_MAX] = {ANDROID_LOG_DEBUG, ANDROID_LOG_INFO,
195  ANDROID_LOG_INFO, ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_ERROR};
196 
197  va_list ap;
198  va_start(ap, fmt);
199  __android_log_vprint(android_level[lvl], "cmusphinx", fmt, ap);
200  va_end(ap);
201 }
202 #elif defined(_WIN32_WCE)
203 static void
204 err_wince_cb(void *user_data, err_lvl_t lvl, const char *fmt, ...)
205 {
206  char msg[1024];
207  WCHAR *wmsg;
208  size_t size;
209  va_list ap;
210 
211  va_start(ap, fmt);
212  _vsnprintf(msg, sizeof(msg), fmt, ap);
213  va_end(ap);
214 
215  size = mbstowcs(NULL, msg, 0) + 1;
216  wmsg = ckd_calloc(size, sizeof(*wmsg));
217  mbstowcs(wmsg, msg, size);
218 
219  OutputDebugStringW(wmsg);
220  ckd_free(wmsg);
221 }
222 #else
223 void
224 err_logfp_cb(void *user_data, err_lvl_t lvl, const char *fmt, ...)
225 {
226  va_list ap;
227  FILE *fp = err_get_logfp();
228 
229  if (!fp)
230  return;
231 
232  va_start(ap, fmt);
233  vfprintf(fp, fmt, ap);
234  va_end(ap);
235  fflush(fp);
236 }
237 #endif
238 
239 int
240 err_set_logfile(const char *path)
241 {
242  FILE *newfp, *oldfp;
243 
244  if ((newfp = fopen(path, "a")) == NULL)
245  return -1;
246  oldfp = err_get_logfp();
247  err_set_logfp(newfp);
248  if (oldfp != NULL && oldfp != stdout && oldfp != stderr)
249  fclose(oldfp);
250  return 0;
251 }
252 
253 void
254 err_set_logfp(FILE *stream)
255 {
256  if (stream == NULL) {
257  logfp_disabled = TRUE;
258  logfp = NULL;
259  return;
260  }
261  logfp_disabled = FALSE;
262  logfp = stream;
263  return;
264 }
265 
266 FILE *
268 {
269  if (logfp_disabled)
270  return NULL;
271  if (logfp == NULL)
272  return stderr;
273 
274  return logfp;
275 }
276 
277 void
278 err_set_callback(err_cb_f cb, void* user_data)
279 {
280  err_cb = cb;
281  err_user_data= user_data;
282 }
Sphinx's memory allocation/deallocation routines.
SPHINXBASE_EXPORT void ckd_free(void *ptr)
Test and free a 1-D array.
Definition: ckd_alloc.c:244
#define ckd_calloc(n, sz)
Macros to simplify the use of above functions.
Definition: ckd_alloc.h:248
void err_set_logfp(FILE *stream)
Direct all logging to a given filehandle if default logfp callback is set.
Definition: err.c:254
void err_set_callback(err_cb_f cb, void *user_data)
Sets function to output error messages.
Definition: err.c:278
int err_set_logfile(const char *path)
Append all log messages to a given file.
Definition: err.c:240
FILE * err_get_logfp(void)
Get the current logging filehandle.
Definition: err.c:267
Implementation of logging routines.
File names related operation.
SPHINXBASE_EXPORT const char * path2basename(const char *path)
Returns the last part of the path, without modifying anything in memory.
Definition: filename.c:53
Basic type definitions used in Sphinx.