MySQL 9.0.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#include <cstring>
35
36constexpr const char *kSyslogPluginName = "syslog";
37extern "C" {
38extern mysql_harness::Plugin harness_plugin_syslog;
39}
40
43
44/**
45 * Unix-based systems specific logging handler(sink) that writes the logs to the
46 * syslog.
47 */
49 public:
50 static constexpr unsigned kMaxIdentSize = 100;
51
52 SyslogHandler(bool format_messages = true, LogLevel level = LogLevel::kNotSet)
53 : mysql_harness::logging::Handler(format_messages, level,
54 LogTimestampPrecision::kSec) {}
55 ~SyslogHandler() override { close(); }
56
57 void open(const std::string &ident) noexcept {
58 std::strncpy(ident_, ident.c_str(), kMaxIdentSize);
59 ident_[kMaxIdentSize - 1] = '\0';
60 openlog(ident_, LOG_CONS | LOG_NDELAY, LOG_DAEMON);
61 }
62
63 void close() const noexcept { closelog(); }
64
65 // does nothing for the syslog handler
66 void reopen(const std::string /*dst*/) override {}
67
68 private:
69 void do_log(const mysql_harness::logging::Record &record) noexcept override {
70 syslog(log_level_to_syslog(record.level), "%s", record.message.c_str());
71 }
72
73 static int log_level_to_syslog(const LogLevel level) noexcept {
74 switch (level) {
75 case LogLevel::kFatal:
76 return LOG_EMERG;
77 case LogLevel::kError:
78 return LOG_ERR;
79 case LogLevel::kWarning:
80 return LOG_WARNING;
81 case LogLevel::kInfo:
82 case LogLevel::kNote:
83 case LogLevel::kSystem:
84 // Let loglevels NOTE and SYSTEM map to LOG_INFO
85 return LOG_INFO;
86 default: // kDebug
87 assert(level == LogLevel::kDebug);
88 return LOG_DEBUG;
89 }
90 }
91
93};
94
95#endif // MYSQL_HARNESS_SYSLOG_PLUGIN_INCLUDED
Unix-based systems specific logging handler(sink) that writes the logs to the syslog.
Definition: syslog_plugin.h:48
SyslogHandler(bool format_messages=true, LogLevel level=LogLevel::kNotSet)
Definition: syslog_plugin.h:52
static constexpr unsigned kMaxIdentSize
Definition: syslog_plugin.h:50
void open(const std::string &ident) noexcept
Definition: syslog_plugin.h:57
void close() const noexcept
Definition: syslog_plugin.h:63
static int log_level_to_syslog(const LogLevel level) noexcept
Definition: syslog_plugin.h:73
~SyslogHandler() override
Definition: syslog_plugin.h:55
char ident_[kMaxIdentSize]
Definition: syslog_plugin.h:92
void reopen(const std::string) override
Request to reopen underlying log sink.
Definition: syslog_plugin.h:66
void do_log(const mysql_harness::logging::Record &record) noexcept override
Log message handler primitive.
Definition: syslog_plugin.h:69
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:195
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:36