1/*
2 Copyright (c) 2015, Facebook, Inc.
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; version 2 of the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16#pragma once
17
18#include <log.h>
19#include <sstream>
20#include <string>
21
22namespace myrocks {
23
24class Rdb_logger : public rocksdb::Logger {
25public:
26 explicit Rdb_logger(const rocksdb::InfoLogLevel log_level =
27 rocksdb::InfoLogLevel::ERROR_LEVEL)
28 : m_mysql_log_level(log_level) {}
29
30 void Logv(const rocksdb::InfoLogLevel log_level, const char *format,
31 va_list ap) override {
32 DBUG_ASSERT(format != nullptr);
33
34 enum loglevel mysql_log_level;
35
36 if (m_logger) {
37 m_logger->Logv(log_level, format, ap);
38 }
39
40 if (log_level < m_mysql_log_level) {
41 return;
42 }
43
44 if (log_level >= rocksdb::InfoLogLevel::ERROR_LEVEL) {
45 mysql_log_level = ERROR_LEVEL;
46 } else if (log_level >= rocksdb::InfoLogLevel::WARN_LEVEL) {
47 mysql_log_level = WARNING_LEVEL;
48 } else {
49 mysql_log_level = INFORMATION_LEVEL;
50 }
51
52 // log to MySQL
53 std::string f("LibRocksDB:");
54 f.append(format);
55 error_log_print(mysql_log_level, f.c_str(), ap);
56 }
57
58 void Logv(const char *format, va_list ap) override {
59 DBUG_ASSERT(format != nullptr);
60 // If no level is specified, it is by default at information level
61 Logv(rocksdb::InfoLogLevel::INFO_LEVEL, format, ap);
62 }
63
64 void SetRocksDBLogger(const std::shared_ptr<rocksdb::Logger> logger) {
65 m_logger = logger;
66 }
67
68 void SetInfoLogLevel(const rocksdb::InfoLogLevel log_level) override {
69 // The InfoLogLevel for the logger is used by rocksdb to filter
70 // messages, so it needs to be the lower of the two loggers
71 rocksdb::InfoLogLevel base_level = log_level;
72
73 if (m_logger && m_logger->GetInfoLogLevel() < base_level) {
74 base_level = m_logger->GetInfoLogLevel();
75 }
76 rocksdb::Logger::SetInfoLogLevel(base_level);
77 m_mysql_log_level = log_level;
78 }
79
80private:
81 std::shared_ptr<rocksdb::Logger> m_logger;
82 rocksdb::InfoLogLevel m_mysql_log_level;
83};
84
85} // namespace myrocks
86