XRootD
Loading...
Searching...
No Matches
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{
48public:
49friend class XrdSysError;
50
51int 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
62private:
63XrdSysError_Table_Errno *next; // -> Next table or 0;
64int base_msgnum; // Starting message number
65int last_msgnum; // Ending message number
66const 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{
75public:
76friend class XrdSysError;
77
78char *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
89private:
90XrdSysError_Table *next; // -> Next table or 0;
91int base_msgnum; // Starting message number
92int last_msgnum; // Ending message number
93const 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
100const int SYS_LOG_01 = 1;
101const int SYS_LOG_02 = 2;
102const int SYS_LOG_03 = 4;
103const int SYS_LOG_04 = 8;
104const int SYS_LOG_05 = 16;
105const int SYS_LOG_06 = 32;
106const int SYS_LOG_07 = 64;
107const 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
115class XrdSysLogger;
116
118{
119public:
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//
134static void addTable(XrdSysError_Table *etp) {etp->next = etab; etab = etp;}
135
136static 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//
140int baseFD();
141
142// ec2text translates an error code to the correspodning error text or returns
143// null if matching text cannot be found.
144//
145static 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
149int 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//
157int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0);
158
159// <datetime> <epfx><esfx>: <text1> <text2> <text3>
160//
161void 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//
167inline 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//
183void 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//
188void setMsgMask(int mask) {msgMask = mask;}
189
190int getMsgMask() {return msgMask;}
191
192// SetPrefix() dynamically changes the error prefix
193//
194inline 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//
202void TBeg(const char *txt1=0, const char *txt2=0, const char *txt3=0);
203void TEnd();
204
205private:
206
207static XrdSysError_Table *etab;
208static XrdSysError_Table_Errno *etab_errno;
209const char *epfx;
210int epfxlen;
211int msgMask;
213};
214#endif
XrdOucPup XrdCmsParser::Pup & Say
static XrdSysLogger Logger
const int SYS_LOG_02
const int SYS_LOG_06
const int SYS_LOG_07
const int SYS_LOG_08
const int SYS_LOG_01
const int SYS_LOG_05
const int SYS_LOG_04
const int SYS_LOG_03
XrdSysError_Table_Errno(int base, int last, const int *codes)
friend class XrdSysError
char * Lookup(int mnum)
XrdSysError_Table(int base, int last, const char **text)
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
void TBeg(const char *txt1=0, const char *txt2=0, const char *txt3=0)
XrdSysError(XrdSysLogger *lp, const char *ErrPrefix="sys")
static void addTable(XrdSysError_Table *etp)
void setMsgMask(int mask)
static const char * ec2text(int ecode)
const char * SetPrefix(const char *prefix)
int ec2errno(int ecode)
static void addTable(XrdSysError_Table_Errno *etp)
XrdSysLogger * logger(XrdSysLogger *lp=0)
void Log(int mask, const char *esfx, const char *text1, const char *text2=0, const char *text3=0)