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>
2122namespace 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
4449Message::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
6566Message::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
120128void 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+
240282long 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
0 commit comments