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 /* All Rights Reserved. See XrdInfo.cc for complete License Terms */
9 /*Produced by Andrew Hanushevsky for Stanford University under contract */
10 /* DE-AC03-76-SFO0515 with the Deprtment of Energy */
11 /******************************************************************************/
12 
13 // $Id$
14 
15 #include <stdlib.h>
16 #ifndef WIN32
17 #include <unistd.h>
18 #include <string.h>
19 #include <strings.h>
20 #else
21 #include <string.h>
22 #endif
23 
24 /******************************************************************************/
25 /* o o u c _ E r r o r _ T a b l e */
26 /******************************************************************************/
27 
29 {
30 public:
31 friend class XrdSysError;
32 
33 char *Lookup(int mnum)
34  {return (char *)(mnum < base_msgnum || mnum > last_msgnum
35  ? 0 : msg_text[mnum - base_msgnum]);
36  }
37  XrdSysError_Table(int base, int last, const char **text)
38  : next(0),
39  base_msgnum(base),
40  last_msgnum(last),
41  msg_text(text) {}
43 
44 private:
45 XrdSysError_Table *next; // -> Next table or 0;
46 int base_msgnum; // Starting message number
47 int last_msgnum; // Ending message number
48 const char **msg_text; // Array of message text
49 };
50 
51 /******************************************************************************/
52 /* L o g M a s k D e f i n i t i o n s */
53 /******************************************************************************/
54 
55 const int SYS_LOG_01 = 1;
56 const int SYS_LOG_02 = 2;
57 const int SYS_LOG_03 = 4;
58 const int SYS_LOG_04 = 8;
59 const int SYS_LOG_05 = 16;
60 const int SYS_LOG_06 = 32;
61 const int SYS_LOG_07 = 64;
62 const int SYS_LOG_08 = 128;
63 
64 /******************************************************************************/
65 /* o o u c _ E r r o r */
66 /******************************************************************************/
67 
68 class XrdSysLogger;
69 
71 {
72 public:
73  XrdSysError(XrdSysLogger *lp, const char *ErrPrefix="sys")
74  : epfx(0),
75  epfxlen(0),
76  msgMask(-1),
77  Logger(lp)
78  { SetPrefix(ErrPrefix); }
79 
81 
82 // addTable allows you to add a new error table for errno handling. Any
83 // number of table may be added and must consist of statis message text
84 // since the table are deleted but the text is not freed. Error tables
85 // must be setup without multi-threading. There is only one global table.
86 //
87 static void addTable(XrdSysError_Table *etp) {etp->next = etab; etab = etp;}
88 
89 // baseFD() returns the original FD associated with this object.
90 //
91 int baseFD();
92 
93 // ec2text tyranslates an error code to the correspodning error text or returns
94 // null if matching text cannot be found.
95 //
96 static char *ec2text(int ecode);
97 
98 // Emsg() produces a message of various forms. The message is written to the
99 // constructor specified file descriptor. See variations below.
100 //
101 // <datetime> <epfx><esfx>: error <ecode> (syser[<ecode>]); <text1> <text2>"
102 // (returns abs(ecode)).
103 //
104 int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0);
105 
106 // <datetime> <epfx><esfx>: <text1> <text2> <text3>
107 //
108 void Emsg(const char *esfx, const char *text1,
109  const char *text2=0,
110  const char *text3=0);
111 
112 // <datetime> <epfx><esfx>: <text1> <text2> <text3>
113 //
114 inline void Log(int mask, const char *esfx,
115  const char *text1,
116  const char *text2=0,
117  const char *text3=0)
118  {if (mask & msgMask) Emsg(esfx, text1, text2, text3);}
119 
120 // logger() sets/returns the logger object for this message message handler.
121 //
123  {XrdSysLogger *oldp = Logger;
124  if (lp) Logger = lp;
125  return oldp;
126  }
127 
128 // Say() route a line without timestamp or prefix
129 //
130 void Say(const char *text1, const char *text2=0, const char *txt3=0,
131  const char *text4=0, const char *text5=0, const char *txt6=0);
132 
133 // Set the loging mask (only used by clients of this object)
134 //
135 void setMsgMask(int mask) {msgMask = mask;}
136 
137 // SetPrefix() dynamically changes the error prefix
138 //
139 inline const char *SetPrefix(const char *prefix)
140  {const char *oldpfx = epfx;
141  epfx = prefix; epfxlen = strlen(epfx);
142  return oldpfx;
143  }
144 
145 // TBeg() is used to start a trace on ostream cerr. The TEnd() ends the trace.
146 //
147 void TBeg(const char *txt1=0, const char *txt2=0, const char *txt3=0);
148 void TEnd();
149 
150 private:
151 
153 const char *epfx;
157 };
158 #endif