E-MailRelay
geventloggingcontext.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 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
28// use a static string here for run-time efficiency -- however, it does
29// make the effects of an inner nested object persist beyond its scope --
30// in practice that is not a problem because the event-loop's outer object
31// and any inner object are both destroyed in quick succession
32std::string GNet::EventLoggingContext::m_s ;
33
34#ifndef G_LIB_SMALL
36 m_outer(m_inner)
37{
38 m_s.assign( s.data() , s.size() ) ;
39
40 G::LogOutput::Instance::context( EventLoggingContext::fn , this ) ;
41 m_inner = this ;
42}
43#endif
44
46 m_outer(m_inner)
47{
48 set( m_s , es ) ;
49 m_s.append( s ) ;
50
51 G::LogOutput::Instance::context( EventLoggingContext::fn , this ) ;
52 m_inner = this ;
53}
54
56 m_outer(m_inner)
57{
58 set( m_s , es ) ;
59
60 G::LogOutput::Instance::context( EventLoggingContext::fn , this ) ;
61 m_inner = this ;
62}
63
64void GNet::EventLoggingContext::set( std::string & s , EventState es )
65{
66 s.clear() ; // (static instance for run-time efficiency)
67 for( const EventLogging * p = es.logging() ; p ; p = p->next() )
68 {
69 if( !p->eventLoggingString().empty() )
70 {
71 std::string_view sv = p->eventLoggingString() ;
72 s.insert( 0U , sv.data() , sv.size() ) ;
73 }
74 }
75}
76
78{
79 if( m_outer )
80 G::LogOutput::Instance::context( EventLoggingContext::fn , m_outer ) ;
81 else
82 G::LogOutput::Instance::context() ;
83 m_inner = m_outer ;
84}
85
86std::string_view GNet::EventLoggingContext::fn( void * )
87{
88 if( m_inner == nullptr ) return {} ;
89 return m_s ; // m_inner->m_s if non-static
90}
91
A class that sets the G::LogOuput::context() while in scope.
~EventLoggingContext()
Destructor. Restores the logging context.
EventLoggingContext(EventState)
Constructor that sets the G::LogOutput logging context to the accumulation of EventLogging::eventLogg...
An interface for GNet classes that define a logging context string.
Definition: geventlogging.h:47
A lightweight object containing an ExceptionHandler pointer, optional ExceptionSource pointer and opt...
Definition: geventstate.h:131
EventState logging(EventLogging *) const noexcept
Returns a copy of this object with the ExceptionLogging pointer set to the given value.
Definition: geventstate.cpp:63