PIDUINO
syslog.h
1/* Copyright © 2019 Pascal JEAN, All rights reserved.
2 * This file is part of the Piduino Library.
3 *
4 * The Piduino Library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * The Piduino Library 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 GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with the Piduino Library; if not, see <http://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include <stdarg.h>
20#include <syslog.h>
21#include <sstream>
22#include <string>
23#include <piduino/flags.h>
24#include <piduino/global.h>
25
46namespace Piduino {
47
48#ifndef __DOXYGEN__
49 class SysLogBuffer : public std::stringbuf {
50 protected:
52 int sync();
53 void setPriority (const int priority);
54 inline int priority() const {
55 return m_priority;
56 }
57 private:
59 };
60#endif
61
66 class SysLog : protected SysLogBuffer, public std::ostream {
67 public:
68
76 enum Priority {
77 Emergency = LOG_EMERG,
78 Alert = LOG_ALERT,
79 Critical = LOG_CRIT,
80 Error = LOG_ERR,
81 Warning = LOG_WARNING,
82 Notice = LOG_NOTICE,
83 Info = LOG_INFO,
84 Debug = LOG_DEBUG,
85 NoPriority = -1
86 };
87
96 enum Facility {
97 Auth = LOG_AUTH,
98 AuthPriv = LOG_AUTHPRIV,
99 Cron = LOG_CRON,
100 Daemon = LOG_DAEMON,
101 Ftp = LOG_FTP,
102 Kernel = LOG_KERN,
103 Local0 = LOG_LOCAL0,
104 Local1 = LOG_LOCAL1,
105 Local2 = LOG_LOCAL2,
106 Local3 = LOG_LOCAL3,
107 Local4 = LOG_LOCAL4,
108 Local5 = LOG_LOCAL5,
109 Local6 = LOG_LOCAL6,
110 Local7 = LOG_LOCAL7,
111 Lpr = LOG_LPR,
112 Mail = LOG_MAIL,
113 News = LOG_NEWS,
114 Syslogd = LOG_SYSLOG,
115 User = LOG_USER,
116 Uucp = LOG_UUCP,
117 NoFacility = -1
118 };
119
126
127 Console = LOG_CONS,
128 NoDelay = LOG_NDELAY,
129 NoWait = LOG_NOWAIT,
130 Delay = LOG_ODELAY,
131 Perror = LOG_PERROR,
132 Pid = LOG_PID,
133 NoOption = 0
134 };
135
141
146 SysLog (Priority initialPrio = Info);
147
152
159 void open (const std::string & ident, const SysLog::Facility & facility = User, const SysLog::Option & option = Pid);
165
169 inline bool is_open() const {
170 return m_isopen;
171 }
177 void close();
178
179
189 void log (Priority p, const char *format, ...);
190
195 void log (const char *format, ...);
196
197 inline void setMask (Priority p) {
198 (void) setlogmask (LOG_MASK (p));
199 }
200
201 inline void setMaskUpTo (Priority p) {
202 (void) setlogmask (LOG_UPTO (p));
203 }
204
205 inline int mask() const {
206 return setlogmask (0);
207 }
208
209 inline void setPriority (Priority p = Info) {
210 SysLogBuffer::setPriority (static_cast<int> (p));
211 }
212
213 inline Priority priority() const {
214 return static_cast<Priority> (SysLogBuffer::priority());
215 }
216
217 inline std::string priorityName() const {
218 return priorityName (priority());
219 }
220
228 inline Facility facility() const {
229 return m_facility;
230 }
231
236 inline std::string facilityName() const {
237 return facilityName (facility());
238 }
239
243 inline Option option() const {
244 return m_option;
245 }
246
250 static std::string priorityName (Priority p);
251
255 static std::string facilityName (const Facility & facility);
256
260 static void cerrToSyslog ();
261
265 static void coutToSyslog ();
266
267 private:
271 private:
272 void open (const char * ident, const SysLog::Facility & facility, const SysLog::Option & option);
273 };
274
275#ifndef __DOXYGEN__
278 };
279
280 inline _Setpriority
282 return { __priority };
283 }
284
285 inline std::ostream &
286 operator<< (std::ostream & __os, _Setpriority __p) {
287 SysLog * log = dynamic_cast<SysLog *> (&__os);
288
289 log->setPriority (__p.m_value);
290 // __os << "setpriority(" << log->priorityName() << ") ";
291 return __os;
292 }
293#endif
294
295 // ---------------------------------------------------------------------------
296 //
297 // Piduino SysLog Global Object
298 //
299 // ---------------------------------------------------------------------------
300 extern SysLog Syslog;
301}
int priority() const
Definition: syslog.h:54
void setPriority(const int priority)
SysLogBuffer(int priority)
Definition: syslog.h:51
System logger.
Definition: syslog.h:66
static std::string priorityName(Priority p)
Name of a priority.
Option option() const
Reads the current option flags.
Definition: syslog.h:243
static void cerrToSyslog()
redirect std::cerr to the system logger
void log(Priority p, const char *format,...)
Generates a log message.
Flags< OptionFlag > Option
Definition: syslog.h:140
void open(const char *ident, const SysLog::Facility &facility, const SysLog::Option &option)
Facility m_facility
Definition: syslog.h:269
std::string priorityName() const
Definition: syslog.h:217
Priority
importance of the message
Definition: syslog.h:76
@ Error
error conditions
Definition: syslog.h:80
@ Notice
normal, but significant, condition
Definition: syslog.h:82
@ Alert
action must be taken immediately
Definition: syslog.h:78
@ Critical
critical conditions
Definition: syslog.h:79
@ Debug
debug-level message
Definition: syslog.h:84
@ Emergency
system is unusable
Definition: syslog.h:77
@ Info
informational message
Definition: syslog.h:83
@ Warning
warning conditions
Definition: syslog.h:81
std::string facilityName() const
Name of the current facility.
Definition: syslog.h:236
SysLog(Priority initialPrio=Info)
Constructor.
bool m_isopen
Definition: syslog.h:270
Priority priority() const
Definition: syslog.h:213
OptionFlag
The option argument specifies flags which control the operation of open() and subsequent calls to log...
Definition: syslog.h:125
@ Pid
Include PID with each message.
Definition: syslog.h:132
@ Perror
(Not in POSIX.1-2001 or POSIX.1-2008.) Print to stderr as well.
Definition: syslog.h:131
@ Console
Write directly to system console if there is an error while sending to system logger.
Definition: syslog.h:127
@ Delay
The converse of LOG_NDELAY; opening of the connection is delayed until syslog() is called....
Definition: syslog.h:130
@ NoDelay
Open the connection immediately (normally, the connection is opened when the first message is logged)...
Definition: syslog.h:128
@ NoWait
Don't wait for child processes that may have been created while logging the message....
Definition: syslog.h:129
Facility facility() const
Reads the current facility.
Definition: syslog.h:228
static void coutToSyslog()
redirect std::cout to the system logger
void open(const std::string &ident, const SysLog::Facility &facility=User, const SysLog::Option &option=Pid)
Opens a connection to the system logger.
void log(const char *format,...)
Generates a log message This is an overloaded member function, provided for convenience....
int mask() const
Definition: syslog.h:205
bool is_open() const
Returns whether the object is currently associated to the system logger.
Definition: syslog.h:169
~SysLog()
Destructor.
void setPriority(Priority p=Info)
Definition: syslog.h:209
void setMask(Priority p)
Definition: syslog.h:197
void close()
closes the descriptor being used to write to the system logger.
static std::string facilityName(const Facility &facility)
Name of a facility.
void open(const SysLog::Facility &facility=User, const SysLog::Option &option=Pid)
Opens a connection to the system logger This is an overloaded member function, provided for convenien...
Facility
The facility argument is used to specify what type of program is logging the message....
Definition: syslog.h:96
@ Local7
reserved for local use
Definition: syslog.h:110
@ Local4
reserved for local use
Definition: syslog.h:107
@ Ftp
ftp daemon
Definition: syslog.h:101
@ User
(default) generic user-level messages
Definition: syslog.h:115
@ Kernel
kernel messages (these can't be generated from user processes)
Definition: syslog.h:102
@ Mail
mail subsystem
Definition: syslog.h:112
@ Local5
reserved for local use
Definition: syslog.h:108
@ Syslogd
messages generated internally by syslogd(8)
Definition: syslog.h:114
@ Daemon
system daemons without separate facility value
Definition: syslog.h:100
@ Lpr
line printer subsystem
Definition: syslog.h:111
@ Local3
reserved for local use
Definition: syslog.h:106
@ Auth
security/authorization messages
Definition: syslog.h:97
@ Local6
reserved for local use
Definition: syslog.h:109
@ News
USENET news subsystem.
Definition: syslog.h:113
@ Uucp
UUCP subsystem.
Definition: syslog.h:116
@ Local2
reserved for local use
Definition: syslog.h:105
@ AuthPriv
security/authorization messages (private)
Definition: syslog.h:98
@ Cron
clock daemon (cron and at)
Definition: syslog.h:99
@ Local1
reserved for local use
Definition: syslog.h:104
@ Local0
reserved for local use
Definition: syslog.h:103
void setMaskUpTo(Priority p)
Definition: syslog.h:201
Option m_option
Definition: syslog.h:268
Global namespace for Piduino.
Definition: board.h:28
SysLog Syslog
Piduino SysLog Global Object.
_Setpriority setpriority(SysLog::Priority __priority)
Definition: syslog.h:281
static std::ostream & operator<<(std::ostream &out, const OptionParser &op)
Definition: popl.h:1183
SysLog::Priority m_value
Definition: syslog.h:277