E-MailRelay

glogoutput_unix.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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 glogoutput_unix.cpp
19///
20
21#include "gdef.h"
22#include "glogoutput.h"
23#include "glimits.h"
24#include <syslog.h>
25#include <iostream>
26
27 namespace G
28{
29 namespace LogOutputImp
30 {
31 int decode( G::LogOutput::SyslogFacility facility )
32 {
33 if( facility == G::LogOutput::SyslogFacility::User ) return LOG_USER ;
34 if( facility == G::LogOutput::SyslogFacility::Daemon ) return LOG_DAEMON ;
35 if( facility == G::LogOutput::SyslogFacility::Mail ) return LOG_MAIL ;
36 if( facility == G::LogOutput::SyslogFacility::Cron ) return LOG_CRON ;
37 if( facility == G::LogOutput::SyslogFacility::Local0 ) return LOG_LOCAL0 ;
38 if( facility == G::LogOutput::SyslogFacility::Local1 ) return LOG_LOCAL1 ;
39 if( facility == G::LogOutput::SyslogFacility::Local2 ) return LOG_LOCAL2 ;
40 if( facility == G::LogOutput::SyslogFacility::Local3 ) return LOG_LOCAL3 ;
41 if( facility == G::LogOutput::SyslogFacility::Local4 ) return LOG_LOCAL4 ;
42 if( facility == G::LogOutput::SyslogFacility::Local5 ) return LOG_LOCAL5 ;
43 if( facility == G::LogOutput::SyslogFacility::Local6 ) return LOG_LOCAL6 ;
44 if( facility == G::LogOutput::SyslogFacility::Local7 ) return LOG_LOCAL7 ;
45 return LOG_USER ;
46 }
47 int decode( G::Log::Severity severity )
48 {
49 if( severity == G::Log::Severity::s_Warning ) return LOG_WARNING ;
50 if( severity == G::Log::Severity::s_Error ) return LOG_ERR ;
51 if( severity == G::Log::Severity::s_InfoSummary ) return LOG_INFO ;
52 if( severity == G::Log::Severity::s_InfoVerbose ) return LOG_INFO ;
53 return LOG_CRIT ;
54 }
55 int mode( G::LogOutput::SyslogFacility facility , G::Log::Severity severity )
56 {
57 return decode(facility) | decode(severity) ;
58 }
59 }
60}
61
62 void G::LogOutput::osoutput( int fd , G::Log::Severity severity , char * message , std::size_t n )
63{
64 if( m_config.m_use_syslog && severity != Log::Severity::s_Debug )
65 {
66 message[n] = '\0' ;
67 ::syslog( LogOutputImp::mode(m_config.m_facility,severity) , "%s" , message ) ;
68 }
69
70 if( m_config.m_quiet_stderr && (
71 severity == Log::Severity::s_Debug ||
72 severity == Log::Severity::s_InfoVerbose ||
73 severity == Log::Severity::s_InfoSummary ) )
74 {
75 ;
76 }
77 else
78 {
79 message[n] = '\n' ;
80 GDEF_IGNORE_RETURN ::write( fd , message , n+1U ) ;
81 }
82}
83
84 void G::LogOutput::osinit()
85{
86 m_handle = 1 ; // pacify -Wunused-private-field
87 if( m_config.m_use_syslog )
88 ::openlog( nullptr , LOG_PID , LogOutputImp::decode(m_config.m_facility) ) ;
89}
90
91 void G::LogOutput::register_( const std::string & )
92{
93}
94
95 void G::LogOutput::oscleanup() const noexcept
96{
97 if( m_config.m_use_syslog )
98 ::closelog() ;
99}
100
G::LogOutput::register_
static void register_(const std::string &exe)
Registers the given executable as a source of logging.
Definition: glogoutput_unix.cpp:91
glimits.h
glogoutput.h
G
Low-level classes.
Definition: galign.h:28