libDwm-0.9.45
DwmSysLogger.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2007, 2016, 2021, 2024
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
10//
11// 1. Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// 2. Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16// 3. The names of the authors and copyright holders may not be used to
17// endorse or promote products derived from this software without
18// specific prior written permission.
19//
20// IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
21// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
22// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
23// EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
24// DAMAGE.
25//
26// THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
27// DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
28// UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
29// REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
30// IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31// WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
32// OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
33// TRADEMARK OR OTHER RIGHTS.
34//===========================================================================
35
36//---------------------------------------------------------------------------
39//---------------------------------------------------------------------------
40
41#ifndef _DWMSYSLOGGER_HH_
42#define _DWMSYSLOGGER_HH_
43
44extern "C" {
45 #include <errno.h>
46 #include <stdarg.h>
47 #include <syslog.h>
48}
49
50#include <cstdio>
51#include <version>
52#include <mutex>
53#include <source_location>
54#include <string>
55
56#include "DwmPortability.hh"
57
58#if (defined DWM_HAVE_STD_FORMAT)
59 #include <format>
60#else
61 #if (defined DWM_HAVE_LIBFMT)
62 #include <fmt/format.h>
63 #endif
64#endif
65
66namespace Dwm {
67
68 //--------------------------------------------------------------------------
71 //--------------------------------------------------------------------------
73 {
74 public:
75 //------------------------------------------------------------------------
91 //------------------------------------------------------------------------
92 static bool Open(const char *ident, int logopt, int facility);
93
94 //------------------------------------------------------------------------
99 //------------------------------------------------------------------------
100 static bool Open(const char *ident, int logopt,
101 const std::string & facility);
102
103 //------------------------------------------------------------------------
107 //------------------------------------------------------------------------
108 static bool Close();
109
110 //------------------------------------------------------------------------
114 //------------------------------------------------------------------------
115 static bool Log(int priority, const char *message, ...);
116
117 //------------------------------------------------------------------------
121 //------------------------------------------------------------------------
122 static bool Log(const std::string & filename, int lineno,
123 const std::string & function, int priority,
124 const char *message, ...);
125
126 //------------------------------------------------------------------------
129 //------------------------------------------------------------------------
130 static bool Log(std::source_location loc, int priority,
131 const char *message, ...);
132
133#if (defined DWM_HAVE_STD_FORMAT)
134 //------------------------------------------------------------------------
136 //------------------------------------------------------------------------
137 template <typename ...Args>
138 static bool FmtLog(std::source_location loc, int priority,
139 std::format_string<Args...> fm,
140 Args&&... args)
141 {
142 // The reason I put the formatted string into the format string
143 // of the downstream Log() call instead of separately using a
144 // "%s": I want to be able to still use the convenient "%m" in
145 // a log message, and it must be in the format string for that
146 // to happen downstream. This is likely to change at a later date.
147 return Log(loc, priority,
148 std::format(fm, std::forward<Args>(args)...).c_str());
149 }
150
151#elif (defined DWM_HAVE_LIBFMT)
152 //------------------------------------------------------------------------
154 //------------------------------------------------------------------------
155 template <typename ...Args>
156 static bool FmtLog(std::source_location loc, int priority,
157 fmt::format_string<Args...> fm,
158 Args&&... args)
159 {
160 // The reason I put the formatted string into the format string
161 // of the downstream Log() call instead of separately using a
162 // "%s": I want to be able to still use the convenient "%m" in
163 // a log message, and it must be in the format string for that
164 // to happen downstream. This is likely to change at a later date.
165 return Log(loc, priority,
166 fmt::format(fm, std::forward<Args>(args)...).c_str());
167 }
168#endif
169
170 //------------------------------------------------------------------------
173 //------------------------------------------------------------------------
174 static bool VaLog(int priority, const std::string & message,
175 va_list & vaList);
176
177 //------------------------------------------------------------------------
182 //------------------------------------------------------------------------
183 static bool VaLog(const std::string & filename, int lineno,
184 const std::string & function, int priority,
185 const std::string & message, va_list & vaList);
186
187 //------------------------------------------------------------------------
190 //------------------------------------------------------------------------
191 static bool ShowPriorities();
192
193 //------------------------------------------------------------------------
201 //------------------------------------------------------------------------
202 static bool ShowPriorities(bool showPriorities);
203
204 //------------------------------------------------------------------------
207 //------------------------------------------------------------------------
208 static bool ShowFileLocation();
209
210 //------------------------------------------------------------------------
216 //------------------------------------------------------------------------
217 static bool ShowFileLocation(bool showFileLocation);
218
219 //------------------------------------------------------------------------
223 //------------------------------------------------------------------------
224 static bool ShowFunction();
225
226 //------------------------------------------------------------------------
232 //------------------------------------------------------------------------
233 static bool ShowFunction(bool showFunction);
234
235 //------------------------------------------------------------------------
237 //------------------------------------------------------------------------
238 static int MinimumPriority();
239
240 //------------------------------------------------------------------------
244 //------------------------------------------------------------------------
245 static int MinimumPriority(int minimumPriority);
246
247 //------------------------------------------------------------------------
251 //------------------------------------------------------------------------
252 static int MinimumPriority(const std::string & minimumPriority);
253
254 //------------------------------------------------------------------------
257 //------------------------------------------------------------------------
258 static std::string PriorityTag(int priority);
259
260 //------------------------------------------------------------------------
262 //------------------------------------------------------------------------
263 static int PriorityValue(const std::string & name);
264
265 private:
266 static std::mutex _mutex;
267 static bool _isOpen;
268 static int _logOptions;
269 static std::string _ident;
270 static int _logFacility;
271 static bool _showPriorities;
272 static bool _showFunction;
273 static bool _showFileLocation;
274 static int _minimumPriority;
275 };
276
277} // namespace Dwm
278
279//----------------------------------------------------------------------------
284//----------------------------------------------------------------------------
285#define Syslog(...) \
286 Dwm::SysLogger::Log(std::source_location::current(),__VA_ARGS__)
287
288//----------------------------------------------------------------------------
294//----------------------------------------------------------------------------
295#if (defined DWM_HAVE_STD_FORMAT || defined DWM_HAVE_LIBFMT)
296 #define FSyslog(...) \
297 Dwm::SysLogger::FmtLog(std::source_location::current(),__VA_ARGS__)
298#endif
299
300#endif // _DWMSYSLOGGER_HH_
301
302//---------------------------- emacs settings -----------------------------
303// Local Variables:
304// mode: C++
305// tab-width: 2
306// indent-tabs-mode: nil
307// c-basic-offset: 2
308// End:
309//-------------------------------------------------------------------------
A configure target for dealing with OS differences.
This class encapsulates syslog logging in global scope (all members are static).
Definition DwmSysLogger.hh:73
static bool VaLog(int priority, const std::string &message, va_list &vaList)
Just like vsyslog(), takes a priority and a format string and a va_list.
static bool VaLog(const std::string &filename, int lineno, const std::string &function, int priority, const std::string &message, va_list &vaList)
Just like vsyslog(), but takes filename, lineno and function arguments which are usually FILE,...
static bool Log(const std::string &filename, int lineno, const std::string &function, int priority, const char *message,...)
Like syslog(), but takes filename, lineno and function arguments which are usually FILE,...
static bool Open(const char *ident, int logopt, int facility)
Opens the logger for syslog logging.
static std::string PriorityTag(int priority)
Returns the priority tag ("[F]", "[W]", et.
static bool ShowPriorities(bool showPriorities)
If showPriorities is true, the logger will tag each log message with an indication of the message's p...
static bool Log(std::source_location loc, int priority, const char *message,...)
Like syslog(), but takes a source_location argument that can be used to log the location of the calle...
static bool ShowPriorities()
If the logger is set to show priorities in each log message, returns true.
static bool ShowFileLocation()
If the logger is set to show file location information in Log(const std::string &,...
static bool Open(const char *ident, int logopt, const std::string &facility)
The same as Open() above except the log facility is passed as a const reference to a string which mus...
static int MinimumPriority(const std::string &minimumPriority)
Sets and returns the lowest message priority that the logger will log.
static int MinimumPriority()
Returns the lowest message priority that the logger will log.
static bool Log(int priority, const char *message,...)
Just like syslog(), takes a priority and a format string and variable list of arguments.
static bool ShowFunction(bool showFunction)
If showFunction is true, the logger will show function information in each log message logged with Lo...
static int PriorityValue(const std::string &name)
Returns the priority value for the given priority name.
static bool ShowFileLocation(bool showFileLocation)
If showFileLocation is true, the logger will show the file and line number information in each log me...
static bool ShowFunction()
If the logger is set to show function information in Log(const string &, int, const string &,...
static int MinimumPriority(int minimumPriority)
Sets and returns the lowest message priority that the logger will log.
static bool Close()
Closes the logger.