MySQL 9.1.0
Source Code Documentation
syslog_plugin.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 2024, Oracle and/or its affiliates.
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, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_SYSLOG_PLUGIN_INCLUDED
27#define MYSQL_HARNESS_SYSLOG_PLUGIN_INCLUDED
28
31#include "mysql/harness/plugin.h"
32
33#include <syslog.h>
34
35#include <cstring> // strncpy
36
37constexpr const char *kSyslogPluginName = "syslog";
38extern "C" {
39extern mysql_harness::Plugin harness_plugin_syslog;
40}
41
44
45/**
46 * Unix-based systems specific logging handler(sink) that writes the logs to the
47 * syslog.
48 */
50 public:
51 static constexpr unsigned kMaxIdentSize = 100;
52
53 SyslogHandler(bool format_messages = true, LogLevel level = LogLevel::kNotSet)
54 : mysql_harness::logging::Handler(format_messages, level,
55 LogTimestampPrecision::kSec) {}
56 ~SyslogHandler() override { close(); }
57
58 void open(const std::string &ident) noexcept {
59 std::strncpy(ident_, ident.c_str(), kMaxIdentSize);
60 ident_[kMaxIdentSize - 1] = '\0';
61 openlog(ident_, LOG_CONS | LOG_NDELAY, LOG_DAEMON);
62 }
63
64 void close() const noexcept { closelog(); }
65
66 // does nothing for the syslog handler
67 void reopen(const std::string /*dst*/) override {}
68
69 private:
70 void do_log(const mysql_harness::logging::Record &record) noexcept override {
71 syslog(log_level_to_syslog(record.level), "%s", record.message.c_str());
72 }
73
74 static int log_level_to_syslog(const LogLevel level) noexcept {
75 switch (level) {
76 case LogLevel::kFatal:
77 return LOG_EMERG;
78 case LogLevel::kError:
79 return LOG_ERR;
80 case LogLevel::kWarning:
81 return LOG_WARNING;
82 case LogLevel::kInfo:
83 case LogLevel::kNote:
84 case LogLevel::kSystem:
85 // Let loglevels NOTE and SYSTEM map to LOG_INFO
86 return LOG_INFO;
87 default: // kDebug
88 assert(level == LogLevel::kDebug);
89 return LOG_DEBUG;
90 }
91 }
92
94};
95
96#endif // MYSQL_HARNESS_SYSLOG_PLUGIN_INCLUDED
Unix-based systems specific logging handler(sink) that writes the logs to the syslog.
Definition: syslog_plugin.h:49
SyslogHandler(bool format_messages=true, LogLevel level=LogLevel::kNotSet)
Definition: syslog_plugin.h:53
static constexpr unsigned kMaxIdentSize
Definition: syslog_plugin.h:51
void open(const std::string &ident) noexcept
Definition: syslog_plugin.h:58
void close() const noexcept
Definition: syslog_plugin.h:64
static int log_level_to_syslog(const LogLevel level) noexcept
Definition: syslog_plugin.h:74
~SyslogHandler() override
Definition: syslog_plugin.h:56
char ident_[kMaxIdentSize]
Definition: syslog_plugin.h:93
void reopen(const std::string) override
Request to reopen underlying log sink.
Definition: syslog_plugin.h:67
void do_log(const mysql_harness::logging::Record &record) noexcept override
Log message handler primitive.
Definition: syslog_plugin.h:70
Base class for log message handler.
Definition: handler.h:50
Logging interface for using and extending the logging subsystem.
static int record
Definition: mysqltest.cc:193
LogLevel
Log level values.
Definition: logging.h:90
LogTimestampPrecision
Log timestamp precision values.
Definition: logging.h:160
Definition: common.h:42
Log record containing information collected by the logging system.
Definition: logging.h:182
mysql_harness::Plugin harness_plugin_syslog
Definition: syslog_plugin.cc:33
constexpr const char * kSyslogPluginName
Definition: syslog_plugin.h:37