VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
glogoutput_unix.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 // glogoutput_unix.cpp
19 //
20 
21 #include "gdef.h"
22 #include "glogoutput.h"
23 #include <syslog.h>
24 #include <iostream>
25 
26 namespace
27 {
28  int decode( G::LogOutput::SyslogFacility facility )
29  {
30  if( facility == G::LogOutput::User ) return LOG_USER ;
31  if( facility == G::LogOutput::Daemon ) return LOG_DAEMON ;
32  if( facility == G::LogOutput::Mail ) return LOG_MAIL ;
33  if( facility == G::LogOutput::Cron ) return LOG_CRON ;
34  // etc...
35  return LOG_USER ;
36  }
37  int decode( G::Log::Severity severity )
38  {
39  if( severity == G::Log::s_Warning ) return LOG_WARNING ;
40  if( severity == G::Log::s_Error ) return LOG_ERR ;
41  if( severity == G::Log::s_LogSummary ) return LOG_INFO ;
42  if( severity == G::Log::s_LogVerbose ) return LOG_INFO ;
43  return LOG_CRIT ;
44  }
45  int mode( G::LogOutput::SyslogFacility facility , G::Log::Severity severity )
46  {
47  return decode(facility) | decode(severity) ;
48  }
49 }
50 
51 void G::LogOutput::rawOutput( std::ostream & std_err , G::Log::Severity severity , const std::string & message )
52 {
53  if( severity != G::Log::s_Debug && m_syslog )
54  {
55  ::syslog( mode(m_facility,severity) , "%s" , message.c_str() ) ;
56  }
57  std_err << message << std::endl ;
58 }
59 
60 void G::LogOutput::init()
61 {
62  if( m_syslog )
63  ::openlog( nullptr , LOG_PID , decode(m_facility) ) ;
64 }
65 
66 void G::LogOutput::cleanup()
67 {
68  if( m_syslog )
69  ::closelog() ;
70 }
71 
72 void G::LogOutput::getLocalTime( time_t epoch_time , struct std::tm * broken_down_time_p )
73 {
74  localtime_r( &epoch_time , broken_down_time_p ) ; // see gdef.h
75 }
76 
77 /// \file glogoutput_unix.cpp
bool syslog() const
Returns true if syslog output is enabled.
Definition: glogoutput.cpp:302
virtual void rawOutput(std::ostream &, G::Log::Severity, const std::string &)
Overridable.