E-MailRelay
glogstream.h
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 glogstream.h
19///
20
21#ifndef G_LOG_STREAM_H
22#define G_LOG_STREAM_H
23
24#include "gdef.h"
25#include <ios>
26
27namespace G
28{
29 //| \class G::LogStream
30 /// A non-throwing copyable wrapper for std::ostream, used by
31 /// G::LogOutput and associated logging macros. This class allows
32 /// streaming to G::LogOutput to be inherently non-throwing without
33 /// needing a try/catch block at every call site. The most common
34 /// streaming operators are implemented out-of-line as a modest
35 /// code-size optimisation.
36 ///
37 struct LogStream
38 {
39 explicit LogStream( std::ostream * s ) noexcept :
40 m_ostream(s)
41 {
42 }
43 std::ostream * m_ostream ;
44 } ;
45}
46
47namespace G
48{
49 LogStream & operator<<( LogStream & s , const std::string & ) noexcept ;
50 LogStream & operator<<( LogStream & s , const char * ) noexcept ;
51 LogStream & operator<<( LogStream & s , char ) noexcept ;
52 LogStream & operator<<( LogStream & s , unsigned char ) noexcept ;
53 LogStream & operator<<( LogStream & s , int ) noexcept ;
54 LogStream & operator<<( LogStream & s , unsigned int ) noexcept ;
55 LogStream & operator<<( LogStream & s , long ) noexcept ;
56 LogStream & operator<<( LogStream & s , unsigned long ) noexcept ;
57 LogStream & operator<<( LogStream & s , void * /*handle*/ ) noexcept ;
58
59 template <typename T> LogStream & operator<<( LogStream & s , const T & t ) noexcept
60 {
61 if( s.m_ostream )
62 {
63 try
64 {
65 *(s.m_ostream) << t ;
66 }
67 catch(...)
68 {
69 }
70 }
71 return s ;
72 }
73}
74
75#endif
Low-level classes.
Definition: garg.h:36
A non-throwing copyable wrapper for std::ostream, used by G::LogOutput and associated logging macros.
Definition: glogstream.h:38