XRootD
Loading...
Searching...
No Matches
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
41int XrdOucECMsg::Get(std::string& ecm, bool rst)
42{
43 if (!rst)
44 {ecm = ecMsg;
45 return eCode;
46 }
47
48 int ec = eCode;
49 eCode = 0;
50 ecm = std::move(ecMsg);
51 ecMsg.erase();
52 return ec;
53}
54
55/******************************************************************************/
56/* M s g */
57/******************************************************************************/
58
59void XrdOucECMsg::Msg(const char *pfx, const char *txt1,
60 const char *txt2, const char *txt3,
61 const char *txt4, const char *txt5)
62{
63
64 const char *vecP[10];
65 int n = 0;
66 bool xSpace = false;
67
68 if (txt1 && *txt1) {vecP[n++] = txt1; xSpace = true;}
69 if (txt2 && *txt2) {if (xSpace) vecP[n++] = " ";
70 vecP[n++] = txt2; xSpace = true;
71 }
72 if (txt3 && *txt3) {if (xSpace) vecP[n++] = " ";
73 vecP[n++] = txt3; xSpace = true;
74 }
75 if (txt4 && *txt4) {if (xSpace) vecP[n++] = " ";
76 vecP[n++] = txt4; xSpace = true;
77 }
78 if (txt5 && *txt5) {if (xSpace) vecP[n++] = " ";
79 vecP[n++] = txt5;
80 }
81
82// Route the message appropriately
83//
84 MsgVec(pfx, vecP, n);
85}
86
87/******************************************************************************/
88/* M s g f */
89/******************************************************************************/
90
91void XrdOucECMsg::Msgf(const char *pfx, const char *fmt, ...)
92{
93 char buffer[2048];
94 va_list args;
95 va_start (args, fmt);
96
97// Format the message
98//
99 int n = vsnprintf(buffer, sizeof(buffer), fmt, args);
100
101// Append as needed
102//
103 if (n > (int)sizeof(buffer)) n = sizeof(buffer);
104 Setup(pfx, n);
105 ecMsg.append(buffer);
106}
107
108/******************************************************************************/
109/* M s g V A */
110/******************************************************************************/
111
112void XrdOucECMsg::MsgVA(const char *pfx, const char *fmt, va_list aP)
113{
114 char buffer[2048];
115
116// Format the message
117//
118 int n = vsnprintf(buffer, sizeof(buffer), fmt, aP);
119
120// Append as needed
121//
122 if (n > (int)sizeof(buffer)) n = sizeof(buffer);
123 Setup(pfx, n);
124 ecMsg.append(buffer);
125}
126
127/******************************************************************************/
128/* M s g V e c */
129/******************************************************************************/
130
131void XrdOucECMsg::MsgVec(const char* pfx, char const* const* vecP, int vecN)
132{
133 int n = 0;
134
135 for (int i = 0; i < vecN; i++) n += strlen(vecP[i]);
136 Setup(pfx, n);
137 for (int i = 0; i < vecN; i++) ecMsg.append(vecP[i]);
138}
139
140/******************************************************************************/
141/* S e t E r r n o */
142/******************************************************************************/
143
144int XrdOucECMsg::SetErrno(int ecc, int ret, const char *alt)
145{
146 if (!alt || *alt != '*')
147 {if (!msgID) ecMsg = (alt ? alt : XrdSysE2T(ecc));
148 else Msgf(msgID, XrdSysE2T(ecc));
149 }
150 errno = eCode = ecc;
151 return ret;
152}
153
154/******************************************************************************/
155/* S e t u p */
156/******************************************************************************/
157
158void XrdOucECMsg::Setup(const char* pfx, int n)
159{
160 int k = (pfx && *pfx ? strlen(pfx)+2 : 0);
161
162 if (Delim)
163 {ecMsg.reserve(ecMsg.length() + n + k + 2);
164 ecMsg.append(&Delim, 1);
165 Delim = 0;
166 } else {
167 ecMsg.reserve(n + k + 1);
168 ecMsg = "";
169 }
170
171 if (k)
172 {ecMsg.append(pfx);
173 ecMsg.append(": ");
174 }
175}
const char * XrdSysE2T(int errcode)
Definition XrdSysE2T.cc:104
void MsgVA(const char *pfx, const char *fmt, std::va_list aP)
int Get(std::string &ecm, bool rst=true)
void MsgVec(const char *pfx, char const *const *vecP, int vecN)
int SetErrno(int ecc, int retval=-1, const char *alt=0)
std::string & Msg()
void Msgf(const char *pfx, const char *fmt,...)