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"
22#include "geventloggingcontext.h"
23#include "gexceptionsource.h"
24#include "glogoutput.h"
25
26 GNet::EventLoggingContext * GNet::EventLoggingContext::m_inner = nullptr ;
27
28 GNet::EventLoggingContext::EventLoggingContext( ExceptionSource * esrc ) :
29 m_outer(m_inner) ,
30 m_esrc(esrc)
31{
32 m_inner = this ;
33 G::LogOutput::context( EventLoggingContext::fn , this ) ;
34}
35
36 GNet::EventLoggingContext::EventLoggingContext( const std::string & s ) :
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
45 GNet::EventLoggingContext::~EventLoggingContext() noexcept
46{
47 if( m_outer )
48 G::LogOutput::context( EventLoggingContext::fn , m_outer ) ;
49 else
50 G::LogOutput::context() ;
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
GNet::EventLoggingContext
A class that sets the G::LogOuput::context() while in scope.
Definition: geventloggingcontext.h:36
GNet::EventLoggingContext::EventLoggingContext
EventLoggingContext(ExceptionSource *esrc)
Constructor that sets the logging context to whatever ExceptionSource::exceptionSourceId() returns.
Definition: geventloggingcontext.cpp:28
GNet::EventLoggingContext::~EventLoggingContext
~EventLoggingContext() noexcept
Destructor. Restores the logging context.
Definition: geventloggingcontext.cpp:45
GNet::ExceptionSource
A mixin base class that identifies the source of an exception when delivered to GNet::ExceptionHandle...
Definition: gexceptionsource.h:40
G::LogOutput::context
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
geventloggingcontext.h
gexceptionsource.h
glogoutput.h