2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <luci/UserSettings.h>
25 // TODO Extract these lexical conversion routines as a library
30 * @brief Convert C-string as a value of type T
32 * safecast(s, v) returns v if s is nullptr.
34 template <typename T> T safecast(const char *, const T &);
36 template <> bool safecast<bool>(const char *s, const bool &value)
38 return (s == nullptr) ? value : (std::stoi(s) != 0);
41 template <> int safecast<int>(const char *s, const int &value)
43 return (s == nullptr) ? value : std::stoi(s);
54 Logger::Logger(hermes::Context *ctx) { activate(ctx->sources(), ctx->bus()); }
55 Logger::~Logger() { deactivate(); }
65 LoggerConfig::LoggerConfig()
67 auto settings = luci::UserSettings::settings();
69 _show_warn = !settings->get(luci::UserSettings::Key::MuteWarnings);
71 // Turn on info logging if LUCI_LOG is set as non-zero value
72 _show_info = safecast<bool>(std::getenv("LUCI_LOG"), false);
74 // Turn on verbose logging if LUCI_LOG is set to some level
75 // VERBOSE(l, 1) will be visible with LUCI_LOG=2 and VERBOSE(l, 2) with LUCI_LOG=3 and so on
76 _show_verbose = safecast<int>(std::getenv("LUCI_LOG"), 0);
79 void LoggerConfig::configure(const hermes::Source *source, hermes::Source::Setting &setting) const
81 // Let's ignore hermes::Sources if that is not a moco logger
82 if (auto logger = dynamic_cast<const Logger *>(source))
84 configure(logger, setting);
88 void LoggerConfig::configure(const Logger *, hermes::Source::Setting &setting) const
90 setting.filter(hermes::SeverityCategory::FATAL).reject_all();
91 setting.filter(hermes::SeverityCategory::ERROR).reject_all();
92 setting.filter(hermes::SeverityCategory::WARN).reject_all();
93 setting.filter(hermes::SeverityCategory::INFO).reject_all();
94 setting.filter(hermes::SeverityCategory::VERBOSE).reject_all();
96 // TODO enable FATAL and ERROR
99 setting.filter(hermes::SeverityCategory::WARN).accept_all();
103 setting.filter(hermes::SeverityCategory::INFO).accept_all();
107 setting.filter(hermes::SeverityCategory::VERBOSE).accept_upto(_show_verbose);