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 
81 /******************************************************************************/
82 /* o o u c _ E r r o r */
83 /******************************************************************************/
84 
85 class XrdSysLogger;
86 
88 {
89 public:
90  XrdSysError(XrdSysLogger *lp, const char *ErrPrefix="sys")
91  : epfx(0),
92  epfxlen(0),
93  msgMask(-1),
94  Logger(lp)
95  { SetPrefix(ErrPrefix); }
96 
98 
99 // addTable allows you to add a new error table for errno handling. Any
100 // number of table may be added and must consist of statis message text
101 // since the table are deleted but the text is not freed. Error tables
102 // must be setup without multi-threading. There is only one global table.
103 //
104 static void addTable(XrdSysError_Table *etp) {etp->next = etab; etab = etp;}
105 
106 // baseFD() returns the original FD associated with this object.
107 //
108 int baseFD();
109 
110 // ec2text tyranslates an error code to the correspodning error text or returns
111 // null if matching text cannot be found.
112 //
113 static char *ec2text(int ecode);
114 
115 // Emsg() produces a message of various forms. The message is written to the
116 // constructor specified file descriptor. See variations below.
117 //
118 // <datetime> <epfx><esfx>: error <ecode> (syser[<ecode>]); <text1> <text2>"
119 // (returns abs(ecode)).
120 //
121 int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0);
122 
123 // <datetime> <epfx><esfx>: <text1> <text2> <text3>
124 //
125 void Emsg(const char *esfx, const char *text1,
126  const char *text2=0,
127  const char *text3=0);
128 
129 // <datetime> <epfx><esfx>: <text1> <text2> <text3>
130 //
131 inline void Log(int mask, const char *esfx,
132  const char *text1,
133  const char *text2=0,
134  const char *text3=0)
135  {if (mask & msgMask) Emsg(esfx, text1, text2, text3);}
136 
137 // logger() sets/returns the logger object for this message message handler.
138 //
140  {XrdSysLogger *oldp = Logger;
141  if (lp) Logger = lp;
142  return oldp;
143  }
144 
145 // Say() route a line without timestamp or prefix
146 //
147 void Say(const char *text1, const char *text2=0, const char *txt3=0,
148  const char *text4=0, const char *text5=0, const char *txt6=0);
149 
150 // Set the loging mask (only used by clients of this object)
151 //
152 void setMsgMask(int mask) {msgMask = mask;}
153 
154 // SetPrefix() dynamically changes the error prefix
155 //
156 inline const char *SetPrefix(const char *prefix)
157  {const char *oldpfx = epfx;
158  epfx = prefix; epfxlen = strlen(epfx);
159  return oldpfx;
160  }
161 
162 // TBeg() is used to start a trace on ostream cerr. The TEnd() ends the trace.
163 //
164 void TBeg(const char *txt1=0, const char *txt2=0, const char *txt3=0);
165 void TEnd();
166 
167 private:
168 
170 const char *epfx;
174 };
175 #endif
int epfxlen
Definition: XrdSysError.hh:171
const int SYS_LOG_06
Definition: XrdSysError.hh:77
static void addTable(XrdSysError_Table *etp)
Definition: XrdSysError.hh:104
XrdSysError_Table(int base, int last, const char **text)
Definition: XrdSysError.hh:54
const int SYS_LOG_07
Definition: XrdSysError.hh:78
const int SYS_LOG_02
Definition: XrdSysError.hh:73
Definition: XrdSysError.hh:45
Definition: XrdSysError.hh:87
~XrdSysError_Table()
Definition: XrdSysError.hh:59
XrdSysError_Table * next
Definition: XrdSysError.hh:62
XrdSysLogger * Logger
Definition: XrdSysError.hh:173
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:90
~XrdSysError()
Definition: XrdSysError.hh:97
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:139
void setMsgMask(int mask)
Definition: XrdSysError.hh:152
static XrdSysError_Table * etab
Definition: XrdSysError.hh:169
const char * epfx
Definition: XrdSysError.hh:170
void Log(int mask, const char *esfx, const char *text1, const char *text2=0, const char *text3=0)
Definition: XrdSysError.hh:131
int base_msgnum
Definition: XrdSysError.hh:63
const char * SetPrefix(const char *prefix)
Definition: XrdSysError.hh:156
const int SYS_LOG_05
Definition: XrdSysError.hh:76
int msgMask
Definition: XrdSysError.hh:172
const char ** msg_text
Definition: XrdSysError.hh:65
const int SYS_LOG_04
Definition: XrdSysError.hh:75