Jack2  1.9.10
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[256];
00063     size_t len;
00064     jack_log_function_t log_function;
00065 
00066     if (prefix != NULL) {
00067         len = strlen(prefix);
00068         assert(len < 256);
00069         memcpy(buffer, prefix, len);
00070     } else {
00071         len = 0;
00072     }
00073 
00074     vsnprintf(buffer + len, sizeof(buffer) - len, fmt, ap);
00075 
00076     log_function = (jack_log_function_t)jack_tls_get(JackGlobals::fKeyLogFunction);
00077 
00078     /* if log function is not overriden for thread, use default one */
00079     if (log_function == NULL)
00080     {
00081         log_function = jack_log_function;
00082         //log_function(LOG_LEVEL_INFO, "------ Using default log function");
00083     }
00084     else
00085     {
00086         //log_function(LOG_LEVEL_INFO, "++++++ Using thread-specific log function");
00087     }
00088 
00089     log_function(level, buffer);
00090 }
00091 
00092 SERVER_EXPORT void jack_error(const char *fmt, ...)
00093 {
00094         va_list ap;
00095         va_start(ap, fmt);
00096         jack_format_and_log(LOG_LEVEL_ERROR, NULL, fmt, ap);
00097         va_end(ap);
00098 }
00099 
00100 SERVER_EXPORT void jack_info(const char *fmt, ...)
00101 {
00102         va_list ap;
00103         va_start(ap, fmt);
00104         jack_format_and_log(LOG_LEVEL_INFO, NULL, fmt, ap);
00105         va_end(ap);
00106 }
00107 
00108 SERVER_EXPORT void jack_log(const char *fmt,...)
00109 {
00110         if (JackGlobals::fVerbose) {
00111                 va_list ap;
00112                 va_start(ap, fmt);
00113         jack_format_and_log(LOG_LEVEL_INFO, "Jack: ", fmt, ap);
00114                 va_end(ap);
00115         }
00116 }
00117 
00118 SERVER_EXPORT void default_jack_error_callback(const char *desc)
00119 {
00120     fprintf(stderr, "%s\n", desc);
00121     fflush(stderr);
00122 }
00123 
00124 SERVER_EXPORT void default_jack_info_callback(const char *desc)
00125 {
00126     fprintf(stdout, "%s\n", desc);
00127     fflush(stdout);
00128 }
00129 
00130 SERVER_EXPORT void silent_jack_error_callback(const char *desc)
00131 {}
00132 
00133 SERVER_EXPORT void silent_jack_info_callback(const char *desc)
00134 {}
00135 
00136 SERVER_EXPORT void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
00137 SERVER_EXPORT void (*jack_info_callback)(const char *desc) = &default_jack_info_callback;