XRootD
XrdOucECMsg.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d O u c E C M s g . c c */
4 /* */
5 /* (c) 2023 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* Produced by Andrew Hanushevsky for Stanford University under contract */
7 /* DE-AC02-76-SFO0515 with the Department of Energy */
8 /* */
9 /* This file is part of the XRootD software suite. */
10 /* */
11 /* XRootD is free software: you can redistribute it and/or modify it under */
12 /* the terms of the GNU Lesser General Public License as published by the */
13 /* Free Software Foundation, either version 3 of the License, or (at your */
14 /* option) any later version. */
15 /* */
16 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
17 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
18 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
19 /* License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public License */
22 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
23 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
24 /* */
25 /* The copyright holder's institutional names and contributor's names may not */
26 /* be used to endorse or promote products derived from this software without */
27 /* specific prior written permission of the institution or contributor. */
28 /* */
29 /******************************************************************************/
30 
31 #include <cstdio>
32 #include <string.h>
33 
34 #include "XrdOuc/XrdOucECMsg.hh"
35 #include "XrdSys/XrdSysE2T.hh"
36 
37 /******************************************************************************/
38 /* G e t */
39 /******************************************************************************/
40 
41 int XrdOucECMsg::Get(std::string& ecm, bool rst)
42 {
43  XrdSysMutexHelper mScope(ecMTX);
44 
45  if (!rst)
46  {ecm = ecMsg;
47  return eCode;
48  }
49 
50  int ec = eCode;
51  eCode = 0;
52  ecm = std::move(ecMsg);
53  ecMsg.erase();
54  return ec;
55 }
56 
57 /******************************************************************************/
58 /* M s g */
59 /******************************************************************************/
60 
61 void XrdOucECMsg::Msg(const char *pfx, const char *txt1,
62  const char *txt2, const char *txt3,
63  const char *txt4, const char *txt5)
64 {
65 
66  const char *vecP[10];
67  int n = 0;
68  bool xSpace = false;
69 
70  if (txt1 && *txt1) {vecP[n++] = txt1; xSpace = true;}
71  if (txt2 && *txt2) {if (xSpace) vecP[n++] = " ";
72  vecP[n++] = txt2; xSpace = true;
73  }
74  if (txt3 && *txt3) {if (xSpace) vecP[n++] = " ";
75  vecP[n++] = txt3; xSpace = true;
76  }
77  if (txt4 && *txt4) {if (xSpace) vecP[n++] = " ";
78  vecP[n++] = txt4; xSpace = true;
79  }
80  if (txt5 && *txt5) {if (xSpace) vecP[n++] = " ";
81  vecP[n++] = txt5;
82  }
83 
84 // Route the message appropriately
85 //
86  MsgVec(pfx, vecP, n);
87 }
88 
89 /******************************************************************************/
90 /* M s g f */
91 /******************************************************************************/
92 
93 void XrdOucECMsg::Msgf(const char *pfx, const char *fmt, ...)
94 {
95  char buffer[2048];
96  va_list args;
97  va_start (args, fmt);
98 
99 // Format the message
100 //
101  int n = vsnprintf(buffer, sizeof(buffer), fmt, args);
102 
103 // Append as needed
104 //
105  if (n > (int)sizeof(buffer)) n = sizeof(buffer);
106  ecMTX.Lock();
107  Setup(pfx, n);
108  ecMsg.append(buffer);
109  ecMTX.UnLock();
110 }
111 
112 /******************************************************************************/
113 /* M s g V A */
114 /******************************************************************************/
115 
116 void XrdOucECMsg::MsgVA(const char *pfx, const char *fmt, va_list aP)
117 {
118  char buffer[2048];
119 
120 // Format the message
121 //
122  int n = vsnprintf(buffer, sizeof(buffer), fmt, aP);
123 
124 // Append as needed
125 //
126  if (n > (int)sizeof(buffer)) n = sizeof(buffer);
127  ecMTX.Lock();
128  Setup(pfx, n);
129  ecMsg.append(buffer);
130  ecMTX.UnLock();
131 }
132 
133 /******************************************************************************/
134 /* M s g V e c */
135 /******************************************************************************/
136 
137 void XrdOucECMsg::MsgVec(const char* pfx, char const* const* vecP, int vecN)
138 {
139  int n = 0;
140 
141  for (int i = 0; i < vecN; i++) n += strlen(vecP[i]);
142  ecMTX.Lock();
143  Setup(pfx, n);
144  for (int i = 0; i < vecN; i++) ecMsg.append(vecP[i]);
145  ecMTX.UnLock();
146 }
147 
148 /******************************************************************************/
149 /* S e t E r r n o */
150 /******************************************************************************/
151 
152 int XrdOucECMsg::SetErrno(int ecc, int ret, const char *alt)
153 {
154  XrdSysMutexHelper mScope(ecMTX);
155 
156  if (!alt || *alt != '*')
157  {if (!alt) alt = XrdSysE2T(ecc);
158  Setup(msgID, strlen(alt));
159  ecMsg.append(alt);
160  }
161  errno = eCode = ecc;
162  return ret;
163 }
164 
165 /******************************************************************************/
166 /* S e t u p */
167 /******************************************************************************/
168 
169 void XrdOucECMsg::Setup(const char* pfx, int n) // Called w/ ecMTX locked!
170 {
171  int k = (pfx && *pfx ? strlen(pfx)+2 : 0);
172  char dlm;
173 
174  if ((dlm = Delim))
175  {ecMsg.reserve(ecMsg.length() + n + k + 2);
176  ecMsg.append(&dlm, 1);
177  Delim = 0;
178  } else {
179  ecMsg.reserve(n + k + 1);
180  ecMsg = "";
181  }
182 
183  if (k)
184  {ecMsg.append(pfx);
185  ecMsg.append(": ");
186  }
187 }
const char * XrdSysE2T(int errcode)
Definition: XrdSysE2T.cc:104
void MsgVA(const char *pfx, const char *fmt, std::va_list aP)
Definition: XrdOucECMsg.cc:116
std::string Msg()
Definition: XrdOucECMsg.hh:82
int SetErrno(int ecc, int ret=-1, const char *alt=0)
Definition: XrdOucECMsg.cc:152
int Get(std::string &ecm, bool rst=true)
Definition: XrdOucECMsg.cc:41
void MsgVec(const char *pfx, char const *const *vecP, int vecN)
Definition: XrdOucECMsg.cc:137
void Msgf(const char *pfx, const char *fmt,...)
Definition: XrdOucECMsg.cc:93