XRootD
XrdOucERoute.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d O u c E R o u t e . h h */
4 /* */
5 /* (c) 2012 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* All Rights Reserved */
7 /* Produced by Andrew Hanushevsky for Stanford University under contract */
8 /* DE-AC02-76-SFO0515 with the Department of Energy */
9 /* */
10 /* This file is part of the XRootD software suite. */
11 /* */
12 /* XRootD is free software: you can redistribute it and/or modify it under */
13 /* the terms of the GNU Lesser General Public License as published by the */
14 /* Free Software Foundation, either version 3 of the License, or (at your */
15 /* option) any later version. */
16 /* */
17 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20 /* License for more details. */
21 /* */
22 /* You should have received a copy of the GNU Lesser General Public License */
23 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25 /* */
26 /* The copyright holder's institutional names and contributor's names may not */
27 /* be used to endorse or promote products derived from this software without */
28 /* specific prior written permission of the institution or contributor. */
29 /******************************************************************************/
30 
31 #include <cctype>
32 #include <cstdio>
33 #include <cstring>
34 
35 #include "XrdOuc/XrdOucERoute.hh"
36 #include "XrdOuc/XrdOucStream.hh"
37 #include "XrdSys/XrdSysError.hh"
38 #include "XrdSys/XrdSysPlatform.hh"
39 
40 /******************************************************************************/
41 /* F o r m a t */
42 /******************************************************************************/
43 
44 int XrdOucERoute::Format(char *buff, int blen, int ecode, const char *etxt1,
45  const char *etxt2,
46  const char *xtra)
47 {
48  const char *esep = " ", *estr = XrdSysError::ec2text(ecode);
49  const char *xsep = "";
50  char ebuff[256];
51  int n;
52 
53 // Substitute something of no ecode translation exists
54 //
55  if (!estr) estr = "reason unknown";
56  else if (isupper(static_cast<int>(*estr)))
57  {strlcpy(ebuff, estr, sizeof(ebuff));
58  *ebuff = static_cast<char>(tolower(static_cast<int>(*estr)));
59  estr = ebuff;
60  }
61 
62 // Set format elements
63 //
64  if (!etxt2) etxt2 = esep = "";
65  if (xtra) xsep = "\nAdditional context: ";
66  else xtra = "";
67 
68 // Format the message
69 //
70  n = snprintf(buff, blen, "Unable to %s%s%s; %s%s%s",etxt1,esep,etxt2,estr,
71  xsep, xtra);
72  return (n < blen ? n : blen-1);
73 }
74 
75 /******************************************************************************/
76 /* R o u t e */
77 /******************************************************************************/
78 
80  const char *esfx, int ecode,
81  const char *etxt1, const char *etxt2)
82 {
83  char ebuff[2048];
84  int elen;
85 
86 // Format the error message
87 //
88  elen = Format(ebuff, sizeof(ebuff), ecode, etxt1, etxt2);
89 
90 // Route appropriately
91 //
92  if (elog) elog->Emsg(esfx, ebuff);
93  if (estrm) estrm->Put(ebuff, elen);
94 
95 // Return the error number
96 //
97  if (ecode) return (ecode < 0 ? ecode : -ecode);
98  return -1;
99 }
size_t strlcpy(char *dst, const char *src, size_t sz)
static int Format(char *buff, int blen, int ecode, const char *etxt1, const char *etxt2=0, const char *xtra=0)
Definition: XrdOucERoute.cc:44
static int Route(XrdSysError *elog, XrdOucStream *estrm, const char *esfx, int ecode, const char *etxt1, const char *etxt2=0)
Definition: XrdOucERoute.cc:79
int Put(const char *data, const int dlen)
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:116
static const char * ec2text(int ecode)
Definition: XrdSysError.cc:80