Main Page | Data Structures | Directories | File List | Data Fields | Globals

logerr.c

Go to the documentation of this file.
00001 
00024 /* This file is part of tclSadie.
00025 
00026    tclSadie is free software; you can redistribute it and/or modify it
00027    under the terms of the GNU General Public License as published by
00028    the Free Software Foundation; either version 2 of the License, or
00029    (at your option) any later version.
00030 
00031    tclSadie is distributed in the hope that it will be useful, but
00032    WITHOUT ANY WARRANTY; without even the implied warranty of
00033    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00034    General Public License for more details.
00035 
00036    You should have received a copy of the GNU General Public License
00037    along with tclSadie; if not, write to the Free Software Foundation,
00038    Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.  */
00039 
00040 #if HAVE_CONFIG_H
00041 #include <config.h>
00042 #endif /* HAVE_CONFIG_H */
00043 #include <stdio.h>
00044 #include <string.h>
00045 #include <stdlib.h>
00046 #include "logerr.h"
00047 #if HAVE_PTHREADS
00048 #include <pthread.h>
00049 #endif /* HAVE_PTHREADS */
00050 
00052 static logerr_message_poster_t logerr_messenger = NULL;
00053 
00055 static logerr_perror_poster_t logerr_perror_messenger = NULL;
00056 
00058 static int logerr_abort_threshold = LOGERR_ERR;
00059 
00060 #if HAVE_PTHREADS
00061 
00062 static pthread_mutex_t logerr_modlock = PTHREAD_MUTEX_INITIALIZER;
00063 
00065 #define logerr_lock pthread_mutex_lock (&logerr_modlock)
00066 
00068 #define logerr_unlock pthread_mutex_unlock (&logerr_modlock)
00069 
00070 #else /* !HAVE_PTHREADS */
00071 
00072 #define logerr_lock
00073 
00075 #define logerr_unlock
00076 #endif /* !HAVE_PTHREADS */
00077 
00085 static void
00086 default_messenger (int priority, const char * title, const char * message)
00087 {
00088   if (title != NULL)
00089     fprintf(stderr, "%s: ", title);
00090   else
00091     fprintf(stderr, "From unknown: ");
00092   if (message != NULL)
00093     fprintf(stderr, "%s\n", message);
00094   else
00095     fprintf(stderr, "(unspecified message)\n");    
00096 }
00097 
00106 static void
00107 default_perror_messenger (int priority, const char * title,
00108                           const char * message, const char * syserr)
00109 {
00110   if (title != NULL)
00111     fprintf(stderr, "%s: ", title);
00112   else
00113     fprintf(stderr, "From unknown: ");
00114   if (message != NULL)
00115     fprintf(stderr, "%s\n", message);
00116   else
00117     fprintf(stderr, "(unspecified message)\n");
00118   if (syserr != NULL)
00119     fprintf(stderr, "The system error message was:\n\t%s\n", syserr);
00120 }
00121 
00133 void
00134 logerr_vdispatch (int priority, const char * title,
00135                   const char * fmt, va_list ap)
00136 {
00137   char message[2048];
00138 
00139   vsnprintf (message, sizeof (message), fmt, ap);
00140   if (logerr_messenger == NULL)
00141     default_messenger (priority, title, message);
00142   else
00143     (*logerr_messenger) (priority, title, message);
00144   if (priority <= LOGERR_THRESHOLD_ABORT)
00145     abort();
00146 }
00147 
00155 void
00156 logerr_dispatch (int priority, const char * title, const char * fmt, ...)
00157 {
00158   va_list ap;
00159 
00160   va_start (ap, fmt);
00161   logerr_vdispatch (priority, title, fmt, ap);
00162   va_end (ap);
00163 }
00164 
00179 void
00180 logerr_vperror (int priority, int errnum, const char * title, const char * fmt, va_list ap)
00181 {
00182   char message[2048];
00183 
00184   vsnprintf (message, sizeof (message), fmt, ap);
00185   if (logerr_perror_messenger == NULL)
00186     default_perror_messenger (priority, title, message, strerror(errnum));
00187   else
00188     (*logerr_perror_messenger) (priority, title, message, strerror(errnum));
00189   if (priority <= LOGERR_THRESHOLD_ABORT)
00190     abort();
00191 }
00192 
00201 void
00202 logerr_perror (int priority, int errnum, const char * title, const char * fmt, ...)
00203 {
00204   va_list ap;
00205 
00206   va_start (ap, fmt);
00207   logerr_vperror (priority, errnum, title, fmt, ap);
00208   va_end (ap);
00209 }
00210 
00215 void
00216 logerr_set_abort_threshold (int threshold)
00217 {
00218   logerr_lock;
00219   logerr_abort_threshold = threshold;
00220   logerr_unlock;
00221 }
00222 
00227 void
00228 logerr_set_messenger (logerr_message_poster_t messenger)
00229 {
00230   logerr_lock;
00231   logerr_messenger = messenger;
00232   logerr_unlock;
00233 }
00234 
00239 void
00240 logerr_set_perror_messenger (logerr_perror_poster_t poster)
00241 {
00242   logerr_lock;
00243   logerr_perror_messenger = poster;
00244   logerr_unlock;
00245 }

Generated on Fri Jul 8 14:55:00 2005 for tclSadie by  doxygen 1.4.2