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 <cstdlib>
33 #ifndef WIN32
34 #include <unistd.h>
35 #include <cstring>
36 #include <strings.h>
37 #else
38 #include <cstring>
39 #endif
40 
41 
42 /******************************************************************************/
43 /* X r d S y s E r r o r _ T a b l e _ E r r n o */
44 /******************************************************************************/
45 
47 {
48 public:
49 friend class XrdSysError;
50 
51 int Lookup(int mnum)
52  {return (mnum < base_msgnum || mnum > last_msgnum
53  ? 0 : translations[mnum - base_msgnum]);
54  }
55  XrdSysError_Table_Errno(int base, int last, const int* codes)
56  : next(0),
57  base_msgnum(base),
58  last_msgnum(last),
59  translations(codes) {}
61 
62 private:
63 XrdSysError_Table_Errno *next; // -> Next table or 0;
64 int base_msgnum; // Starting message number
65 int last_msgnum; // Ending message number
66 const int* translations; // Array of linux error code mappings
67 };
68 
69 /******************************************************************************/
70 /* o o u c _ E r r o r _ T a b l e */
71 /******************************************************************************/
72 
74 {
75 public:
76 friend class XrdSysError;
77 
78 char *Lookup(int mnum)
79  {return (char *)(mnum < base_msgnum || mnum > last_msgnum
80  ? 0 : msg_text[mnum - base_msgnum]);
81  }
82  XrdSysError_Table(int base, int last, const char **text)
83  : next(0),
84  base_msgnum(base),
85  last_msgnum(last),
86  msg_text(text) {}
88 
89 private:
90 XrdSysError_Table *next; // -> Next table or 0;
91 int base_msgnum; // Starting message number
92 int last_msgnum; // Ending message number
93 const char **msg_text; // Array of message text
94 };
95 
96 /******************************************************************************/
97 /* L o g M a s k D e f i n i t i o n s */
98 /******************************************************************************/
99 
100 const int SYS_LOG_01 = 1;
101 const int SYS_LOG_02 = 2;
102 const int SYS_LOG_03 = 4;
103 const int SYS_LOG_04 = 8;
104 const int SYS_LOG_05 = 16;
105 const int SYS_LOG_06 = 32;
106 const int SYS_LOG_07 = 64;
107 const int SYS_LOG_08 = 128;
108 // 0x00000100 to 0x0000ffff reseved for XRootD use
109 // 0x00010000 to 0xffff0000 reseved for non-XRootD use
110 
111 /******************************************************************************/
112 /* o o u c _ E r r o r */
113 /******************************************************************************/
114 
115 class XrdSysLogger;
116 
118 {
119 public:
120  XrdSysError(XrdSysLogger *lp, const char *ErrPrefix="sys")
121  : epfx(0),
122  epfxlen(0),
123  msgMask(-1),
124  Logger(lp)
125  { SetPrefix(ErrPrefix); }
126 
128 
129 // addTable allows you to add a new error table for errno handling. Any
130 // number of table may be added and must consist of statis message text
131 // since the table are deleted but the text is not freed. Error tables
132 // must be setup without multi-threading. There is only one global table.
133 //
134 static void addTable(XrdSysError_Table *etp) {etp->next = etab; etab = etp;}
135 
136 static void addTable(XrdSysError_Table_Errno *etp) {etp->next = etab_errno; etab_errno = etp;}
137 
138 // baseFD() returns the original FD associated with this object.
139 //
140 int baseFD();
141 
142 // ec2text translates an error code to the correspodning error text or returns
143 // null if matching text cannot be found.
144 //
145 static const char *ec2text(int ecode);
146 
147 // ec2errno maps a extended error code to a linux error code as defined by
148 // the translation table provided
149 int ec2errno(int ecode);
150 
151 // Emsg() produces a message of various forms. The message is written to the
152 // constructor specified file descriptor. See variations below.
153 //
154 // <datetime> <epfx><esfx>: error <ecode> (syser[<ecode>]); <text1> <text2>"
155 // (returns abs(ecode)).
156 //
157 int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0);
158 
159 // <datetime> <epfx><esfx>: <text1> <text2> <text3>
160 //
161 void Emsg(const char *esfx, const char *text1,
162  const char *text2=0,
163  const char *text3=0);
164 
165 // <datetime> <epfx><esfx>: <text1> <text2> <text3>
166 //
167 inline void Log(int mask, const char *esfx,
168  const char *text1,
169  const char *text2=0,
170  const char *text3=0)
171  {if (mask & msgMask) Emsg(esfx, text1, text2, text3);}
172 
173 // logger() sets/returns the logger object for this message message handler.
174 //
176  {XrdSysLogger *oldp = Logger;
177  if (lp) Logger = lp;
178  return oldp;
179  }
180 
181 // Say() route a line without timestamp or prefix
182 //
183 void Say(const char *text1, const char *text2=0, const char *txt3=0,
184  const char *text4=0, const char *text5=0, const char *txt6=0);
185 
186 // Set/Get the loging mask (only used by clients of this object)
187 //
188 void setMsgMask(int mask) {msgMask = mask;}
189 
190 int getMsgMask() {return msgMask;}
191 
192 // SetPrefix() dynamically changes the error prefix
193 //
194 inline const char *SetPrefix(const char *prefix)
195  {const char *oldpfx = epfx;
196  epfx = prefix; epfxlen = strlen(epfx);
197  return oldpfx;
198  }
199 
200 // TBeg() is used to start a trace on std::ostream std::cerr. The TEnd() ends the trace.
201 //
202 void TBeg(const char *txt1=0, const char *txt2=0, const char *txt3=0);
203 void TEnd();
204 
205 private:
206 
207 static XrdSysError_Table *etab;
208 static XrdSysError_Table_Errno *etab_errno;
209 const char *epfx;
210 int epfxlen;
211 int msgMask;
212 XrdSysLogger *Logger;
213 };
214 #endif
const int SYS_LOG_02
Definition: XrdSysError.hh:101
const int SYS_LOG_06
Definition: XrdSysError.hh:105
const int SYS_LOG_07
Definition: XrdSysError.hh:106
const int SYS_LOG_08
Definition: XrdSysError.hh:107
const int SYS_LOG_01
Definition: XrdSysError.hh:100
const int SYS_LOG_05
Definition: XrdSysError.hh:104
const int SYS_LOG_04
Definition: XrdSysError.hh:103
const int SYS_LOG_03
Definition: XrdSysError.hh:102
int Lookup(int mnum)
Definition: XrdSysError.hh:51
XrdSysError_Table_Errno(int base, int last, const int *codes)
Definition: XrdSysError.hh:55
char * Lookup(int mnum)
Definition: XrdSysError.hh:78
XrdSysError_Table(int base, int last, const char **text)
Definition: XrdSysError.hh:82
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:116
void TBeg(const char *txt1=0, const char *txt2=0, const char *txt3=0)
Definition: XrdSysError.cc:182
XrdSysError(XrdSysLogger *lp, const char *ErrPrefix="sys")
Definition: XrdSysError.hh:120
static void addTable(XrdSysError_Table *etp)
Definition: XrdSysError.hh:134
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)
Definition: XrdSysError.cc:162
void setMsgMask(int mask)
Definition: XrdSysError.hh:188
int baseFD()
Definition: XrdSysError.cc:74
static const char * ec2text(int ecode)
Definition: XrdSysError.cc:80
XrdSysLogger * logger(XrdSysLogger *lp=0)
Definition: XrdSysError.hh:175
int getMsgMask()
Definition: XrdSysError.hh:190
int ec2errno(int ecode)
Definition: XrdSysError.cc:96
static void addTable(XrdSysError_Table_Errno *etp)
Definition: XrdSysError.hh:136
const char * SetPrefix(const char *prefix)
Definition: XrdSysError.hh:194
void Log(int mask, const char *esfx, const char *text1, const char *text2=0, const char *text3=0)
Definition: XrdSysError.hh:167