E-MailRelay
geventloggingcontext.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file geventloggingcontext.cpp
19///
20
21#include "gdef.h"
23#include "gexceptionsource.h"
24#include "glogoutput.h"
25
26GNet::EventLoggingContext * GNet::EventLoggingContext::m_inner = nullptr ;
27
29 m_outer(m_inner) ,
30 m_esrc(esrc)
31{
32 m_inner = this ;
33 G::LogOutput::context( EventLoggingContext::fn , this ) ;
34}
35
37 m_outer(m_inner) ,
38 m_esrc(nullptr) ,
39 m_s(s)
40{
41 m_inner = this ;
42 G::LogOutput::context( EventLoggingContext::fn , this ) ;
43}
44
46{
47 if( m_outer )
48 G::LogOutput::context( EventLoggingContext::fn , m_outer ) ;
49 else
51 m_inner = m_outer ;
52}
53
54std::string GNet::EventLoggingContext::fn( void * vp )
55{
56 std::string s = static_cast<EventLoggingContext*>(vp)->str() ;
57 if( !s.empty() )
58 s.append( "; " , 2U ) ; // semi-colon for simpler fail2ban regexpes
59 return s ;
60}
61
62std::string GNet::EventLoggingContext::str()
63{
64 return m_esrc ? m_esrc->exceptionSourceId() : m_s ;
65}
66
A class that sets the G::LogOuput::context() while in scope.
EventLoggingContext(ExceptionSource *esrc)
Constructor that sets the logging context to whatever ExceptionSource::exceptionSourceId() returns.
~EventLoggingContext() noexcept
Destructor. Restores the logging context.
A mixin base class that identifies the source of an exception when delivered to GNet::ExceptionHandle...
static void context(std::string(*fn)(void *)=nullptr, void *fn_arg=nullptr) noexcept
Sets a functor that is used to provide a context string for every log line, if configured.
Definition: glogoutput.cpp:133