E-MailRelay
glogstream.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2023 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 wrapper for std::ostream, used by G::Log.
31 /// This allows streaming to a G::Log instance to be inherently
32 /// non-throwing without needing a try/catch block at every
33 /// call site. The most common streaming operators are
34 /// implemented out-of-line as a modest code size optimisation.
35 ///
36 struct LogStream
37 {
38 explicit LogStream( std::ostream * s ) noexcept :
39 m_ostream(s)
40 {
41 }
42 std::ostream * m_ostream ;
43 } ;
44}
45
46namespace G
47{
48 LogStream & operator<<( LogStream & s , const std::string & ) noexcept ;
49 LogStream & operator<<( LogStream & s , const char * ) noexcept ;
50 LogStream & operator<<( LogStream & s , char ) noexcept ;
51 LogStream & operator<<( LogStream & s , unsigned char ) noexcept ;
52 LogStream & operator<<( LogStream & s , int ) noexcept ;
53 LogStream & operator<<( LogStream & s , unsigned int ) noexcept ;
54 LogStream & operator<<( LogStream & s , long ) noexcept ;
55 LogStream & operator<<( LogStream & s , unsigned long ) noexcept ;
56
57 template <typename T> LogStream & operator<<( LogStream & s , const T & t ) noexcept
58 {
59 if( s.m_ostream )
60 {
61 try
62 {
63 *(s.m_ostream) << t ;
64 }
65 catch(...)
66 {
67 }
68 }
69 return s ;
70 }
71}
72
73#endif
Low-level classes.
Definition: garg.h:30
A non-throwing wrapper for std::ostream, used by G::Log.
Definition: glogstream.h:37