error.cxx

Go to the documentation of this file.
00001 // @(#)root/rpdutils:$Id: error.cxx 36126 2010-10-06 15:14:52Z ganis $
00002 // Author: Fons Rademakers   11/08/97
00003 // Modifified: Gerardo Ganis 8/04/2003
00004 
00005 /*************************************************************************
00006  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00007  * All rights reserved.                                                  *
00008  *                                                                       *
00009  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00010  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00011  *************************************************************************/
00012 
00013 //////////////////////////////////////////////////////////////////////////
00014 //                                                                      //
00015 // error                                                                //
00016 //                                                                      //
00017 // Set of error handling routines for daemon process.                   //
00018 // Merging of rootd and proofd/src/error.cxx                            //
00019 //                                                                      //
00020 //////////////////////////////////////////////////////////////////////////
00021 
00022 #include <stdio.h>
00023 #include <unistd.h>
00024 #include <stdlib.h>
00025 #include <syslog.h>
00026 #include <errno.h>
00027 #include <string.h>
00028 #include "snprintf.h"
00029 
00030 #if defined(hpux9)
00031 extern "C" {
00032    extern void openlog(const char *, int, int);
00033    extern void syslog(int, const char *, ...);
00034 }
00035 #endif
00036 
00037 #ifdef __sun
00038 #   ifndef _REENTRANT
00039 #      if __SUNPRO_CC > 0x420
00040 #         define GLOBAL_ERRNO
00041 #      endif
00042 #   endif
00043 #endif
00044 
00045 #include "rpderr.h"
00046 #include "rpdp.h"
00047 
00048 // This is the only really global 
00049 extern int     gDebug;
00050 
00051 namespace ROOT {
00052 
00053 extern bool gSysLog;
00054 
00055 //______________________________________________________________________________
00056 int GetErrno()
00057 {
00058 #ifdef GLOBAL_ERRNO
00059    return ::errno;
00060 #else
00061    return errno;
00062 #endif
00063 }
00064 
00065 //______________________________________________________________________________
00066 void ResetErrno()
00067 {
00068 #ifdef GLOBAL_ERRNO
00069    ::errno = 0;
00070 #else
00071    errno = 0;
00072 #endif
00073 }
00074 
00075 //______________________________________________________________________________
00076 void ErrorInfo(const char *va_(fmt), ...)
00077 {
00078    // Write info message to syslog.
00079 
00080    char    buf[kMAXSECBUF];
00081    va_list ap;
00082 
00083    va_start(ap,va_(fmt));
00084    vsnprintf(buf, sizeof(buf), fmt, ap);
00085    va_end(ap);
00086 
00087    if (gSysLog) {
00088       syslog(LOG_INFO, "%s\n", buf);
00089    } else {
00090       fprintf(stderr, "%s\n", buf);
00091    }
00092 }
00093 
00094 //______________________________________________________________________________
00095 void ErrorInit(const char *ident)
00096 {
00097    // Open syslog.
00098 
00099    openlog(ident, (LOG_PID | LOG_CONS), LOG_DAEMON);
00100 }
00101 
00102 //______________________________________________________________________________
00103 void Perror(char *buf, int size)
00104 {
00105    // Return in buf the message belonging to errno.
00106 
00107    int len = strlen(buf);
00108 #if (defined(__sun) && defined (__SVR4)) || defined (__linux) || \
00109    defined(_AIX) || defined(__MACH__)
00110    snprintf(buf+len, size, " (%s)", strerror(GetErrno()));
00111 #else
00112    if (GetErrno() >= 0 && GetErrno() < sys_nerr)
00113       snprintf(buf+len, size, " (%s)", sys_errlist[GetErrno()]);
00114 #endif
00115 }
00116 
00117 //______________________________________________________________________________
00118 void Error(ErrorHandler_t func, int code, const char *va_(fmt), ...)
00119 {
00120    // Write fatal message to syslog and exit.
00121 
00122    char    buf[kMAXSECBUF];
00123    va_list ap;
00124 
00125    va_start(ap,va_(fmt));
00126    vsnprintf(buf, sizeof(buf), fmt, ap);
00127    va_end(ap);
00128 
00129    if (gSysLog) {
00130       syslog(LOG_ERR, "%s\n", buf);
00131    } else {
00132       fprintf(stderr, "%s\n", buf);
00133    }
00134 
00135    // Actions are defined by the specific error handler function
00136    // (see rootd.cxx and proofd.cxx)
00137    if (func) (*func)(code,(const char *)buf, sizeof(buf));
00138 }
00139 
00140 } // namespace ROOT

Generated on Tue Jul 5 14:46:13 2011 for ROOT_528-00b_version by  doxygen 1.5.1