Skip to content

Commit 659fb56

Browse files
Backport #82068 to 25.6: Revert "Move logging to a separate thread"
1 parent 2974910 commit 659fb56

32 files changed

+676
-649
lines changed

base/poco/Foundation/include/Poco/Message.h

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define Foundation_Message_INCLUDED
1919

2020

21+
#include <map>
2122
#include <vector>
2223
#include "Poco/Foundation.h"
2324
#include "Poco/Timestamp.h"
@@ -66,21 +67,11 @@ class Foundation_API Message
6667
/// The thread and process ids are set.
6768

6869
Message(
69-
const std::string & source,
70-
const std::string & text,
71-
Priority prio,
72-
std::string && file,
73-
int line,
74-
std::string_view fmt_str = {},
75-
const std::vector<std::string> & fmt_str_args = {});
70+
const std::string & source, const std::string & text, Priority prio, const char * file, int line,
71+
std::string_view fmt_str = {}, const std::vector<std::string> & fmt_str_args = {});
7672
Message(
77-
std::string && source,
78-
std::string && text,
79-
Priority prio,
80-
std::string && file,
81-
int line,
82-
std::string_view fmt_str,
83-
std::vector<std::string> && fmt_str_args);
73+
std::string && source, std::string && text, Priority prio, const char * file, int line,
74+
std::string_view fmt_str, std::vector<std::string> && fmt_str_args);
8475
/// Creates a Message with the given source, text, priority,
8576
/// source file path and line.
8677
///
@@ -159,9 +150,9 @@ class Foundation_API Message
159150
/// the __FILE__ macro. The string is not copied
160151
/// internally for performance reasons.
161152

162-
const std::string & getSourceFile() const;
153+
const char * getSourceFile() const;
163154
/// Returns the source file path of the code creating
164-
/// the message. May be "" if not set.
155+
/// the message. May be 0 if not set.
165156

166157
void setSourceLine(int line);
167158
/// Sets the source file line of the statement
@@ -181,8 +172,37 @@ class Foundation_API Message
181172
/// generating the log message. May be 0
182173
/// if not set.
183174

175+
bool has(const std::string & param) const;
176+
/// Returns true if a parameter with the given name exists.
177+
178+
const std::string & get(const std::string & param) const;
179+
/// Returns a const reference to the value of the parameter
180+
/// with the given name. Throws a NotFoundException if the
181+
/// parameter does not exist.
182+
183+
const std::string & get(const std::string & param, const std::string & defaultValue) const;
184+
/// Returns a const reference to the value of the parameter
185+
/// with the given name. If the parameter with the given name
186+
/// does not exist, then defaultValue is returned.
187+
188+
void set(const std::string & param, const std::string & value);
189+
/// Sets the value for a parameter. If the parameter does
190+
/// not exist, then it is created.
191+
192+
const std::string & operator[](const std::string & param) const;
193+
/// Returns a const reference to the value of the parameter
194+
/// with the given name. Throws a NotFoundException if the
195+
/// parameter does not exist.
196+
197+
std::string & operator[](const std::string & param);
198+
/// Returns a reference to the value of the parameter with the
199+
/// given name. This can be used to set the parameter's value.
200+
/// If the parameter does not exist, it is created with an
201+
/// empty string value.
202+
184203
protected:
185204
void init();
205+
typedef std::map<std::string, std::string> StringMap;
186206

187207
private:
188208
std::string _source;
@@ -192,8 +212,9 @@ class Foundation_API Message
192212
long _tid;
193213
std::string _thread;
194214
mutable long _pid = -1;
195-
std::string _file;
215+
const char * _file;
196216
int _line;
217+
StringMap * _pMap;
197218
std::string_view _fmt_str;
198219
std::vector<std::string> _fmt_str_args;
199220
};
@@ -238,7 +259,7 @@ inline long Message::getTid() const
238259
}
239260

240261

241-
inline const std::string & Message::getSourceFile() const
262+
inline const char * Message::getSourceFile() const
242263
{
243264
return _file;
244265
}

base/poco/Foundation/src/Message.cpp

Lines changed: 114 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414

1515
#include "Poco/Message.h"
16+
#include "Poco/Exception.h"
1617
#include "Poco/Process.h"
1718
#include "Poco/Thread.h"
1819
#include <algorithm>
@@ -21,63 +22,59 @@
2122
namespace Poco {
2223

2324

24-
Message::Message()
25-
: _prio(PRIO_FATAL)
26-
, _tid(0)
27-
, _line(0)
25+
Message::Message():
26+
_prio(PRIO_FATAL),
27+
_tid(0),
28+
_file(0),
29+
_line(0),
30+
_pMap(0)
2831
{
2932
init();
3033
}
3134

3235

33-
Message::Message(const std::string & source, const std::string & text, Priority prio)
34-
: _source(source)
35-
, _text(text)
36-
, _prio(prio)
37-
, _tid(0)
38-
, _line(0)
36+
Message::Message(const std::string& source, const std::string& text, Priority prio):
37+
_source(source),
38+
_text(text),
39+
_prio(prio),
40+
_tid(0),
41+
_file(0),
42+
_line(0),
43+
_pMap(0)
3944
{
4045
init();
4146
}
4247

4348

4449
Message::Message(
45-
const std::string & source,
46-
const std::string & text,
47-
Priority prio,
48-
std::string && file,
49-
int line,
50-
std::string_view fmt_str,
51-
const std::vector<std::string> & fmt_str_args)
52-
: _source(source)
53-
, _text(text)
54-
, _prio(prio)
55-
, _tid(0)
56-
, _file(file)
57-
, _line(line)
58-
, _fmt_str(fmt_str)
59-
, _fmt_str_args(fmt_str_args)
50+
const std::string& source, const std::string& text, Priority prio, const char* file, int line,
51+
std::string_view fmt_str, const std::vector<std::string>& fmt_str_args):
52+
_source(source),
53+
_text(text),
54+
_prio(prio),
55+
_tid(0),
56+
_file(file),
57+
_line(line),
58+
_pMap(0),
59+
_fmt_str(fmt_str),
60+
_fmt_str_args(fmt_str_args)
6061
{
6162
init();
6263
}
6364

6465

6566
Message::Message(
66-
std::string && source,
67-
std::string && text,
68-
Priority prio,
69-
std::string && file,
70-
int line,
71-
std::string_view fmt_str,
72-
std::vector<std::string> && fmt_str_args)
73-
: _source(std::move(source))
74-
, _text(std::move(text))
75-
, _prio(prio)
76-
, _tid(0)
77-
, _file(file)
78-
, _line(line)
79-
, _fmt_str(fmt_str)
80-
, _fmt_str_args(std::move(fmt_str_args))
67+
std::string && source, std::string && text, Priority prio, const char * file, int line,
68+
std::string_view fmt_str, std::vector<std::string> && fmt_str_args):
69+
_source(std::move(source)),
70+
_text(std::move(text)),
71+
_prio(prio),
72+
_tid(0),
73+
_file(file),
74+
_line(line),
75+
_pMap(0),
76+
_fmt_str(fmt_str),
77+
_fmt_str_args(std::move(fmt_str_args))
8178
{
8279
init();
8380
}
@@ -95,6 +92,10 @@ Message::Message(const Message& msg):
9592
_fmt_str(msg._fmt_str),
9693
_fmt_str_args(msg._fmt_str_args)
9794
{
95+
if (msg._pMap)
96+
_pMap = new StringMap(*msg._pMap);
97+
else
98+
_pMap = 0;
9899
}
99100

100101

@@ -111,10 +112,17 @@ Message::Message(const Message& msg, const std::string& text):
111112
_fmt_str(msg._fmt_str),
112113
_fmt_str_args(msg._fmt_str_args)
113114
{
115+
if (msg._pMap)
116+
_pMap = new StringMap(*msg._pMap);
117+
else
118+
_pMap = 0;
114119
}
115120

116121

117-
Message::~Message() = default;
122+
Message::~Message()
123+
{
124+
delete _pMap;
125+
}
118126

119127

120128
void Message::init()
@@ -152,6 +160,7 @@ void Message::swap(Message& msg)
152160
swap(_pid, msg._pid);
153161
swap(_file, msg._file);
154162
swap(_line, msg._line);
163+
swap(_pMap, msg._pMap);
155164
swap(_fmt_str, msg._fmt_str);
156165
swap(_fmt_str_args, msg._fmt_str_args);
157166
}
@@ -237,6 +246,39 @@ void Message::setFormatStringArgs(const std::vector<std::string>& fmt_str_args)
237246
_fmt_str_args = fmt_str_args;
238247
}
239248

249+
250+
bool Message::has(const std::string& param) const
251+
{
252+
return _pMap && (_pMap->find(param) != _pMap->end());
253+
}
254+
255+
256+
const std::string& Message::get(const std::string& param) const
257+
{
258+
if (_pMap)
259+
{
260+
StringMap::const_iterator it = _pMap->find(param);
261+
if (it != _pMap->end())
262+
return it->second;
263+
}
264+
265+
throw NotFoundException();
266+
}
267+
268+
269+
const std::string& Message::get(const std::string& param, const std::string& defaultValue) const
270+
{
271+
if (_pMap)
272+
{
273+
StringMap::const_iterator it = _pMap->find(param);
274+
if (it != _pMap->end())
275+
return it->second;
276+
}
277+
278+
return defaultValue;
279+
}
280+
281+
240282
long Message::getPid() const
241283
{
242284
if (_pid < 0)
@@ -245,4 +287,35 @@ long Message::getPid() const
245287
}
246288

247289

290+
void Message::set(const std::string& param, const std::string& value)
291+
{
292+
if (!_pMap)
293+
_pMap = new StringMap;
294+
295+
std::pair<StringMap::iterator, bool> result =
296+
_pMap->insert(std::make_pair(param, value));
297+
if (!result.second)
298+
{
299+
result.first->second = value;
300+
}
301+
}
302+
303+
304+
const std::string& Message::operator [] (const std::string& param) const
305+
{
306+
if (_pMap)
307+
return (*_pMap)[param];
308+
else
309+
throw NotFoundException();
310+
}
311+
312+
313+
std::string& Message::operator [] (const std::string& param)
314+
{
315+
if (!_pMap)
316+
_pMap = new StringMap;
317+
return (*_pMap)[param];
318+
}
319+
320+
248321
} // namespace Poco

base/poco/Foundation/src/PatternFormatter.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void PatternFormatter::format(const Message& msg, std::string& text)
7474
case 'T': text.append(msg.getThread()); break;
7575
case 'I': NumberFormatter::append(text, msg.getTid()); break;
7676
case 'N': text.append(Environment::nodeName()); break;
77-
case 'U': text.append(msg.getSourceFile()); break;
77+
case 'U': text.append(msg.getSourceFile() ? msg.getSourceFile() : ""); break;
7878
case 'u': NumberFormatter::append(text, msg.getSourceLine()); break;
7979
case 'w': text.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()], 0, 3); break;
8080
case 'W': text.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()]); break;
@@ -109,6 +109,13 @@ void PatternFormatter::format(const Message& msg, std::string& text)
109109
text.append(msg.getSource());
110110
break;
111111
case 'x':
112+
try
113+
{
114+
text.append(msg[ip->property]);
115+
}
116+
catch (...)
117+
{
118+
}
112119
break;
113120
case 'L':
114121
if (!localTime)

base/poco/Net/include/Poco/Net/RemoteSyslogListener.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ namespace Net
101101

102102
static const std::string LOG_PROP_APP;
103103
static const std::string LOG_PROP_HOST;
104+
static const std::string LOG_PROP_STRUCTURED_DATA;
104105

105106
protected:
106107
~RemoteSyslogListener();

base/poco/Net/src/RemoteSyslogChannel.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const std::string RemoteSyslogChannel::PROP_FACILITY("facility");
3434
const std::string RemoteSyslogChannel::PROP_FORMAT("format");
3535
const std::string RemoteSyslogChannel::PROP_LOGHOST("loghost");
3636
const std::string RemoteSyslogChannel::PROP_HOST("host");
37+
const std::string RemoteSyslogChannel::STRUCTURED_DATA("structured-data");
3738

3839

3940
RemoteSyslogChannel::RemoteSyslogChannel():
@@ -138,7 +139,14 @@ void RemoteSyslogChannel::log(const Message& msg)
138139
m += ' ';
139140
m += msg.getSource();
140141
m += ' ';
141-
m += "-";
142+
if (msg.has(STRUCTURED_DATA))
143+
{
144+
m += msg.get(STRUCTURED_DATA);
145+
}
146+
else
147+
{
148+
m += "-";
149+
}
142150
}
143151
m += ' ';
144152
m += msg.getText();

0 commit comments

Comments
 (0)