xrootd
XrdSysError.hh
Go to the documentation of this file.
1 #ifndef __SYS_ERROR_H__
2 #define __SYS_ERROR_H__
3 /******************************************************************************/
4 /* */
5 /* X r d S y s E r r o r . h h */
6 /* */
7 /*(c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /*Produced by Andrew Hanushevsky for Stanford University under contract */
9 /* DE-AC02-76-SFO0515 with the Deprtment of Energy */
10 /* */
11 /* This file is part of the XRootD software suite. */
12 /* */
13 /* XRootD is free software: you can redistribute it and/or modify it under */
14 /* the terms of the GNU Lesser General Public License as published by the */
15 /* Free Software Foundation, either version 3 of the License, or (at your */
16 /* option) any later version. */
17 /* */
18 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
19 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
20 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
21 /* License for more details. */
22 /* */
23 /* You should have received a copy of the GNU Lesser General Public License */
24 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
25 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
26 /* */
27 /* The copyright holder's institutional names and contributor's names may not */
28 /* be used to endorse or promote products derived from this software without */
29 /* specific prior written permission of the institution or contributor. */
30 /******************************************************************************/
31 
32 #include <stdlib.h>
33 #ifndef WIN32
34 #include <unistd.h>
35 #include <string.h>
36 #include <strings.h>
37 #else
38 #include <string.h>
39 #endif
40 
41 /******************************************************************************/
42 /* o o u c _ E r r o r _ T a b l e */
43 /******************************************************************************/
44 
46 {
47 public:
48 friend class XrdSysError;
49 
50 char *Lookup(int mnum)
51  {return (char *)(mnum < base_msgnum || mnum > last_msgnum
52  ? 0 : msg_text[mnum - base_msgnum]);
53  }
54  XrdSysError_Table(int base, int last, const char **text)
55  : next(0),
56  base_msgnum(base),
57  last_msgnum(last),
58  msg_text(text) {}
60 
61 private:
62 XrdSysError_Table *next; // -> Next table or 0;
63 int base_msgnum; // Starting message number
64 int last_msgnum; // Ending message number
65 const char **msg_text; // Array of message text
66 };
67 
68 /******************************************************************************/
69 /* L o g M a s k D e f i n i t i o n s */
70 /******************************************************************************/
71 
72 const int SYS_LOG_01 = 1;
73 const int SYS_LOG_02 = 2;
74 const int SYS_LOG_03 = 4;
75 const int SYS_LOG_04 = 8;
76 const int SYS_LOG_05 = 16;
77 const int SYS_LOG_06 = 32;
78 const int SYS_LOG_07 = 64;
79 const int SYS_LOG_08 = 128;
80 // 0x00000100 to 0x0000ffff reseved for XRootD use
81 // 0x00010000 to 0xffff0000 reseved for non-XRootD use
82 
83 /******************************************************************************/
84 /* o o u c _ E r r o r */
85 /******************************************************************************/
86 
87 class XrdSysLogger;
88 
90 {
91 public:
92  XrdSysError(XrdSysLogger *lp, const char *ErrPrefix="sys")
93  : epfx(0),
94  epfxlen(0),
95  msgMask(-1),
96  Logger(lp)
97  { SetPrefix(ErrPrefix); }
98 
100 
101 // addTable allows you to add a new error table for errno handling. Any
102 // number of table may be added and must consist of statis message text
103 // since the table are deleted but the text is not freed. Error tables
104 // must be setup without multi-threading. There is only one global table.
105 //
106 static void addTable(XrdSysError_Table *etp) {etp->next = etab; etab = etp;}
107 
108 // baseFD() returns the original FD associated with this object.
109 //
110 int baseFD();
111 
112 // ec2text tyranslates an error code to the correspodning error text or returns
113 // null if matching text cannot be found.
114 //
115 static char *ec2text(int ecode);
116 
117 // Emsg() produces a message of various forms. The message is written to the
118 // constructor specified file descriptor. See variations below.
119 //
120 // <datetime> <epfx><esfx>: error <ecode> (syser[<ecode>]); <text1> <text2>"
121 // (returns abs(ecode)).
122 //
123 int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0);
124 
125 // <datetime> <epfx><esfx>: <text1> <text2> <text3>
126 //
127 void Emsg(const char *esfx, const char *text1,
128  const char *text2=0,
129  const char *text3=0);
130 
131 // <datetime> <epfx><esfx>: <text1> <text2> <text3>
132 //
133 inline void Log(int mask, const char *esfx,
134  const char *text1,
135  const char *text2=0,
136  const char *text3=0)
137  {if (mask & msgMask) Emsg(esfx, text1, text2, text3);}
138 
139 // logger() sets/returns the logger object for this message message handler.
140 //
142  {XrdSysLogger *oldp = Logger;
143  if (lp) Logger = lp;
144  return oldp;
145  }
146 
147 // Say() route a line without timestamp or prefix
148 //
149 void Say(const char *text1, const char *text2=0, const char *txt3=0,
150  const char *text4=0, const char *text5=0, const char *txt6=0);
151 
152 // Set/Get the loging mask (only used by clients of this object)
153 //
154 void setMsgMask(int mask) {msgMask = mask;}
155 
156 int getMsgMask() {return msgMask;}
157 
158 // SetPrefix() dynamically changes the error prefix
159 //
160 inline const char *SetPrefix(const char *prefix)
161  {const char *oldpfx = epfx;
162  epfx = prefix; epfxlen = strlen(epfx);
163  return oldpfx;
164  }
165 
166 // TBeg() is used to start a trace on ostream cerr. The TEnd() ends the trace.
167 //
168 void TBeg(const char *txt1=0, const char *txt2=0, const char *txt3=0);
169 void TEnd();
170 
171 private:
172 
174 const char *epfx;
178 };
179 #endif
int epfxlen
Definition: XrdSysError.hh:175
const int SYS_LOG_06
Definition: XrdSysError.hh:77
static char * ec2text(int ecode)
void TBeg(const char *txt1=0, const char *txt2=0, const char *txt3=0)
static void addTable(XrdSysError_Table *etp)
Definition: XrdSysError.hh:106
XrdSysError_Table(int base, int last, const char **text)
Definition: XrdSysError.hh:54
const int SYS_LOG_07
Definition: XrdSysError.hh:78
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
const int SYS_LOG_02
Definition: XrdSysError.hh:73
int getMsgMask()
Definition: XrdSysError.hh:156
Definition: XrdSysError.hh:45
Definition: XrdSysError.hh:89
void Say(const char *text1, const char *text2=0, const char *txt3=0, const char *text4=0, const char *text5=0, const char *txt6=0)
~XrdSysError_Table()
Definition: XrdSysError.hh:59
XrdSysError_Table * next
Definition: XrdSysError.hh:62
XrdSysLogger * Logger
Definition: XrdSysError.hh:177
int last_msgnum
Definition: XrdSysError.hh:64
char * Lookup(int mnum)
Definition: XrdSysError.hh:50
XrdSysError(XrdSysLogger *lp, const char *ErrPrefix="sys")
Definition: XrdSysError.hh:92
~XrdSysError()
Definition: XrdSysError.hh:99
const int SYS_LOG_08
Definition: XrdSysError.hh:79
const int SYS_LOG_03
Definition: XrdSysError.hh:74
const int SYS_LOG_01
Definition: XrdSysError.hh:72
Definition: XrdSysLogger.hh:52
XrdSysLogger * logger(XrdSysLogger *lp=0)
Definition: XrdSysError.hh:141
void setMsgMask(int mask)
Definition: XrdSysError.hh:154
static XrdSysError_Table * etab
Definition: XrdSysError.hh:173
const char * epfx
Definition: XrdSysError.hh:174
void Log(int mask, const char *esfx, const char *text1, const char *text2=0, const char *text3=0)
Definition: XrdSysError.hh:133
int base_msgnum
Definition: XrdSysError.hh:63
const char * SetPrefix(const char *prefix)
Definition: XrdSysError.hh:160
const int SYS_LOG_05
Definition: XrdSysError.hh:76
int msgMask
Definition: XrdSysError.hh:176
const char ** msg_text
Definition: XrdSysError.hh:65
const int SYS_LOG_04
Definition: XrdSysError.hh:75