E-MailRelay
glogoutput_unix.cpp
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 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
27namespace G
28{
29 namespace LogOutputImp
30 {
31 int decode( LogOutput::SyslogFacility facility )
32 {
33 if( facility == LogOutput::SyslogFacility::User ) return LOG_USER ; // NOLINT
34 if( facility == LogOutput::SyslogFacility::Daemon ) return LOG_DAEMON ; // NOLINT
35 if( facility == LogOutput::SyslogFacility::Mail ) return LOG_MAIL ; // NOLINT
36 if( facility == LogOutput::SyslogFacility::Cron ) return LOG_CRON ; // NOLINT
37 if( facility == LogOutput::SyslogFacility::Local0 ) return LOG_LOCAL0 ; // NOLINT
38 if( facility == LogOutput::SyslogFacility::Local1 ) return LOG_LOCAL1 ; // NOLINT
39 if( facility == LogOutput::SyslogFacility::Local2 ) return LOG_LOCAL2 ; // NOLINT
40 if( facility == LogOutput::SyslogFacility::Local3 ) return LOG_LOCAL3 ; // NOLINT
41 if( facility == LogOutput::SyslogFacility::Local4 ) return LOG_LOCAL4 ; // NOLINT
42 if( facility == LogOutput::SyslogFacility::Local5 ) return LOG_LOCAL5 ; // NOLINT
43 if( facility == LogOutput::SyslogFacility::Local6 ) return LOG_LOCAL6 ; // NOLINT
44 if( facility == LogOutput::SyslogFacility::Local7 ) return LOG_LOCAL7 ; // NOLINT
45 return LOG_USER ; // NOLINT
46 }
47 int decode( LogOutput::Severity severity )
48 {
49 if( severity == LogOutput::Severity::Warning ) return LOG_WARNING ;
50 if( severity == LogOutput::Severity::Error ) return LOG_ERR ;
51 if( severity == LogOutput::Severity::InfoSummary ) return LOG_INFO ;
52 if( severity == LogOutput::Severity::InfoVerbose ) return LOG_INFO ;
53 return LOG_CRIT ;
54 }
55 int mode( LogOutput::SyslogFacility facility , LogOutput::Severity severity )
56 {
57 return decode(facility) | decode(severity) ; // NOLINT
58 }
59 }
60}
61
62void G::LogOutput::osoutput( int fd , Severity severity , char * message , std::size_t n )
63{
64 if( m_config.m_use_syslog && severity != Severity::Debug )
65 {
66 message[n] = '\0' ; // sic
67 ::syslog( LogOutputImp::mode(m_config.m_facility,severity) , "%s" , message ) ; // NOLINT
68 }
69
70 if( m_config.m_quiet_stderr && (
71 severity == Severity::Debug ||
72 severity == Severity::InfoVerbose ||
73 severity == Severity::InfoSummary ) )
74 {
75 ;
76 }
77 else
78 {
79 message[n] = '\n' ; // sic
80 GDEF_IGNORE_RETURN ::write( fd , message , n+1U ) ;
81 }
82}
83
84void 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#ifndef G_LIB_SMALL
93{
94}
95#endif
96
97void G::LogOutput::oscleanup() const noexcept
98{
99 if( m_config.m_use_syslog )
100 ::closelog() ;
101}
102
static void register_(const Path &exe)
Registers the given executable as a source of logging.
int fd() const noexcept
Returns the output file descriptor.
Definition: glogoutput.cpp:127
A Path object represents a file system path.
Definition: gpath.h:82
Low-level classes.
Definition: garg.h:36