Jack2 1.9.6

JackError.cpp

00001 /*
00002  Copyright (C) 2001 Paul Davis
00003  Copyright (C) 2004-2008 Grame
00004  Copyright (C) 2008 Nedko Arnaudov
00005  
00006  This program is free software; you can redistribute it and/or modify
00007  it under the terms of the GNU Lesser General Public License as published by
00008  the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
00015  
00016  You should have received a copy of the GNU Lesser General Public License
00017  along with this program; if not, write to the Free Software
00018  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019  
00020  */
00021 
00022 #include <stdarg.h>
00023 #include <stdio.h>
00024 #include "JackError.h"
00025 #include "JackGlobals.h"
00026 #include "JackMessageBuffer.h"
00027 
00028 using namespace Jack;
00029 
00030 static bool change_thread_log_function(jack_log_function_t log_function)
00031 {
00032     return (jack_tls_get(JackGlobals::fKeyLogFunction) == NULL 
00033             && jack_tls_set(JackGlobals::fKeyLogFunction, (void*)log_function));
00034 }
00035 
00036 SERVER_EXPORT int set_threaded_log_function()
00037 {
00038     return change_thread_log_function(JackMessageBufferAdd);
00039 }
00040 
00041 void jack_log_function(int level, const char *message)
00042 {
00043     void (* log_callback)(const char *);
00044 
00045     switch (level)
00046     {
00047     case LOG_LEVEL_INFO:
00048         log_callback = jack_info_callback;
00049         break;
00050     case LOG_LEVEL_ERROR:
00051         log_callback = jack_error_callback;
00052         break;
00053     default:
00054         return;
00055     }
00056 
00057     log_callback(message);
00058 }
00059 
00060 static void jack_format_and_log(int level, const char *prefix, const char *fmt, va_list ap)
00061 {
00062     char buffer[300];
00063     size_t len;
00064     jack_log_function_t log_function;
00065 
00066     if (prefix != NULL) {
00067         len = strlen(prefix);
00068         memcpy(buffer, prefix, len);
00069     } else {
00070         len = 0;
00071     }
00072 
00073     vsnprintf(buffer + len, sizeof(buffer) - len, fmt, ap);
00074 
00075     log_function = (jack_log_function_t)jack_tls_get(JackGlobals::fKeyLogFunction);
00076 
00077     /* if log function is not overriden for thread, use default one */
00078     if (log_function == NULL)
00079     {
00080         log_function = jack_log_function;
00081         //log_function(LOG_LEVEL_INFO, "------ Using default log function");
00082     }
00083     else
00084     {
00085         //log_function(LOG_LEVEL_INFO, "++++++ Using thread-specific log function");
00086     }
00087 
00088     log_function(level, buffer);
00089 }
00090 
00091 SERVER_EXPORT void jack_error(const char *fmt, ...)
00092 {
00093         va_list ap;
00094         va_start(ap, fmt);
00095         jack_format_and_log(LOG_LEVEL_ERROR, NULL, fmt, ap);
00096         va_end(ap);
00097 }
00098 
00099 SERVER_EXPORT void jack_info(const char *fmt, ...)
00100 {
00101         va_list ap;
00102         va_start(ap, fmt);
00103         jack_format_and_log(LOG_LEVEL_INFO, NULL, fmt, ap);
00104         va_end(ap);
00105 }
00106 
00107 SERVER_EXPORT void jack_log(const char *fmt,...)
00108 {
00109         if (JackGlobals::fVerbose) {
00110                 va_list ap;
00111                 va_start(ap, fmt);
00112         jack_format_and_log(LOG_LEVEL_INFO, "Jack: ", fmt, ap);
00113                 va_end(ap);
00114         }
00115 }
00116 
00117 SERVER_EXPORT void default_jack_error_callback(const char *desc)
00118 {
00119     fprintf(stderr, "%s\n", desc);
00120     fflush(stderr);
00121 }
00122 
00123 SERVER_EXPORT void default_jack_info_callback(const char *desc)
00124 {
00125     fprintf(stdout, "%s\n", desc);
00126     fflush(stdout);
00127 }
00128 
00129 SERVER_EXPORT void silent_jack_error_callback(const char *desc)
00130 {}
00131 
00132 SERVER_EXPORT void silent_jack_info_callback(const char *desc)
00133 {}
00134 
00135 SERVER_EXPORT void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
00136 SERVER_EXPORT void (*jack_info_callback)(const char *desc) = &default_jack_info_callback;