00001 #ifndef __SYS_ERROR_H__ 00002 #define __SYS_ERROR_H__ 00003 /******************************************************************************/ 00004 /* */ 00005 /* X r d S y s E r r o r . h h */ 00006 /* */ 00007 /*(c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */ 00008 /*Produced by Andrew Hanushevsky for Stanford University under contract */ 00009 /* DE-AC02-76-SFO0515 with the Deprtment of Energy */ 00010 /* */ 00011 /* This file is part of the XRootD software suite. */ 00012 /* */ 00013 /* XRootD is free software: you can redistribute it and/or modify it under */ 00014 /* the terms of the GNU Lesser General Public License as published by the */ 00015 /* Free Software Foundation, either version 3 of the License, or (at your */ 00016 /* option) any later version. */ 00017 /* */ 00018 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */ 00019 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 00020 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */ 00021 /* License for more details. */ 00022 /* */ 00023 /* You should have received a copy of the GNU Lesser General Public License */ 00024 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */ 00025 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */ 00026 /* */ 00027 /* The copyright holder's institutional names and contributor's names may not */ 00028 /* be used to endorse or promote products derived from this software without */ 00029 /* specific prior written permission of the institution or contributor. */ 00030 /******************************************************************************/ 00031 00032 #include <stdlib.h> 00033 #ifndef WIN32 00034 #include <unistd.h> 00035 #include <string.h> 00036 #include <strings.h> 00037 #else 00038 #include <string.h> 00039 #endif 00040 00041 /******************************************************************************/ 00042 /* o o u c _ E r r o r _ T a b l e */ 00043 /******************************************************************************/ 00044 00045 class XrdSysError_Table 00046 { 00047 public: 00048 friend class XrdSysError; 00049 00050 char *Lookup(int mnum) 00051 {return (char *)(mnum < base_msgnum || mnum > last_msgnum 00052 ? 0 : msg_text[mnum - base_msgnum]); 00053 } 00054 XrdSysError_Table(int base, int last, const char **text) 00055 : next(0), 00056 base_msgnum(base), 00057 last_msgnum(last), 00058 msg_text(text) {} 00059 ~XrdSysError_Table() {} 00060 00061 private: 00062 XrdSysError_Table *next; // -> Next table or 0; 00063 int base_msgnum; // Starting message number 00064 int last_msgnum; // Ending message number 00065 const char **msg_text; // Array of message text 00066 }; 00067 00068 /******************************************************************************/ 00069 /* L o g M a s k D e f i n i t i o n s */ 00070 /******************************************************************************/ 00071 00072 const int SYS_LOG_01 = 1; 00073 const int SYS_LOG_02 = 2; 00074 const int SYS_LOG_03 = 4; 00075 const int SYS_LOG_04 = 8; 00076 const int SYS_LOG_05 = 16; 00077 const int SYS_LOG_06 = 32; 00078 const int SYS_LOG_07 = 64; 00079 const int SYS_LOG_08 = 128; 00080 // 0x00000100 to 0x0000ffff reseved for XRootD use 00081 // 0x00010000 to 0xffff0000 reseved for non-XRootD use 00082 00083 /******************************************************************************/ 00084 /* o o u c _ E r r o r */ 00085 /******************************************************************************/ 00086 00087 class XrdSysLogger; 00088 00089 class XrdSysError 00090 { 00091 public: 00092 XrdSysError(XrdSysLogger *lp, const char *ErrPrefix="sys") 00093 : epfx(0), 00094 epfxlen(0), 00095 msgMask(-1), 00096 Logger(lp) 00097 { SetPrefix(ErrPrefix); } 00098 00099 ~XrdSysError() {} 00100 00101 // addTable allows you to add a new error table for errno handling. Any 00102 // number of table may be added and must consist of statis message text 00103 // since the table are deleted but the text is not freed. Error tables 00104 // must be setup without multi-threading. There is only one global table. 00105 // 00106 static void addTable(XrdSysError_Table *etp) {etp->next = etab; etab = etp;} 00107 00108 // baseFD() returns the original FD associated with this object. 00109 // 00110 int baseFD(); 00111 00112 // ec2text tyranslates an error code to the correspodning error text or returns 00113 // null if matching text cannot be found. 00114 // 00115 static const char *ec2text(int ecode); 00116 00117 // Emsg() produces a message of various forms. The message is written to the 00118 // constructor specified file descriptor. See variations below. 00119 // 00120 // <datetime> <epfx><esfx>: error <ecode> (syser[<ecode>]); <text1> <text2>" 00121 // (returns abs(ecode)). 00122 // 00123 int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0); 00124 00125 // <datetime> <epfx><esfx>: <text1> <text2> <text3> 00126 // 00127 void Emsg(const char *esfx, const char *text1, 00128 const char *text2=0, 00129 const char *text3=0); 00130 00131 // <datetime> <epfx><esfx>: <text1> <text2> <text3> 00132 // 00133 inline void Log(int mask, const char *esfx, 00134 const char *text1, 00135 const char *text2=0, 00136 const char *text3=0) 00137 {if (mask & msgMask) Emsg(esfx, text1, text2, text3);} 00138 00139 // logger() sets/returns the logger object for this message message handler. 00140 // 00141 XrdSysLogger *logger(XrdSysLogger *lp=0) 00142 {XrdSysLogger *oldp = Logger; 00143 if (lp) Logger = lp; 00144 return oldp; 00145 } 00146 00147 // Say() route a line without timestamp or prefix 00148 // 00149 void Say(const char *text1, const char *text2=0, const char *txt3=0, 00150 const char *text4=0, const char *text5=0, const char *txt6=0); 00151 00152 // Set/Get the loging mask (only used by clients of this object) 00153 // 00154 void setMsgMask(int mask) {msgMask = mask;} 00155 00156 int getMsgMask() {return msgMask;} 00157 00158 // SetPrefix() dynamically changes the error prefix 00159 // 00160 inline const char *SetPrefix(const char *prefix) 00161 {const char *oldpfx = epfx; 00162 epfx = prefix; epfxlen = strlen(epfx); 00163 return oldpfx; 00164 } 00165 00166 // TBeg() is used to start a trace on ostream cerr. The TEnd() ends the trace. 00167 // 00168 void TBeg(const char *txt1=0, const char *txt2=0, const char *txt3=0); 00169 void TEnd(); 00170 00171 private: 00172 00173 static XrdSysError_Table *etab; 00174 const char *epfx; 00175 int epfxlen; 00176 int msgMask; 00177 XrdSysLogger *Logger; 00178 }; 00179 #endif