VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
glog.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
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 // glog.cpp
19 //
20 
21 #include "gdef.h"
22 #include "glog.h"
23 #include "glogoutput.h"
24 
25 G::Log::Log( Severity severity , const char * file , int line ) :
26  m_severity(severity) ,
27  m_file(file) ,
28  m_line(line)
29 {
30 }
31 
33 {
34  try
35  {
36  flush() ;
37  }
38  catch(...)
39  {
40  }
41 }
42 
43 bool G::Log::at( Severity s )
44 {
45  LogOutput * output = G::LogOutput::instance() ;
46  return output != nullptr && output->at(s) ;
47 }
48 
49 bool G::Log::active()
50 {
51  LogOutput * output = G::LogOutput::instance() ;
52  if( output == nullptr )
53  {
54  return false ;
55  }
56  else
57  {
58  // (enable it just to get the original state, then restore it)
59  bool a = output->enable(true) ;
60  output->enable(a) ;
61  return a ;
62  }
63 }
64 
65 void G::Log::flush()
66 {
67  if( active() )
68  {
69  G::LogOutput::output( m_severity , m_file , m_line , m_ss.str() ) ;
70  }
71 }
72 
73 std::ostream & G::Log::operator<<( const char * s )
74 {
75  s = s ? s : "" ;
76  return m_ss << s ;
77 }
78 
79 std::ostream & G::Log::operator<<( const std::string & s )
80 {
81  return m_ss << s ;
82 }
83 
84 /// \file glog.cpp
bool enable(bool enabled=true)
Enables or disables output. Returns the previous setting.
Definition: glogoutput.cpp:114
static bool at(Severity)
Returns true if G::LogOutput::output() would log at the given level.
Definition: glog.cpp:43
static LogOutput * instance()
Returns a pointer to the controlling LogOutput object.
Definition: glogoutput.cpp:109
Log(Severity, const char *file, int line)
Constructor.
Definition: glog.cpp:25
static void output(G::Log::Severity, const char *file, int line, const std::string &)
Generates output if there is an existing LogOutput object which is enabled.
Definition: glogoutput.cpp:126
bool at(G::Log::Severity) const
Returns true if output() generates output at the given severity level.
Definition: glogoutput.cpp:132
Controls and implements low-level logging output, as used by the Log interface.
Definition: glogoutput.h:41
std::ostream & operator<<(const char *s)
Streams 's' and then returns a stream for streaming more stuff into.
Definition: glog.cpp:73
~Log()
Destructor. Writes the accumulated string to the log output.
Definition: glog.cpp:32