Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / luci / log / src / Log.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
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
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "luci/Log.h"
18
19 #include <luci/UserSettings.h>
20
21 #include <cassert>
22 #include <cstdlib>
23 #include <iostream>
24
25 // TODO Extract these lexical conversion routines as a library
26 namespace
27 {
28
29 /**
30  * @brief Convert C-string as a value of type T
31  *
32  * safecast(s, v) returns v if s is nullptr.
33  */
34 template <typename T> T safecast(const char *, const T &);
35
36 template <> bool safecast<bool>(const char *s, const bool &value)
37 {
38   return (s == nullptr) ? value : (std::stoi(s) != 0);
39 }
40
41 template <> int safecast<int>(const char *s, const int &value)
42 {
43   return (s == nullptr) ? value : std::stoi(s);
44 }
45
46 } // namespace
47
48 //
49 // Logger
50 //
51 namespace luci
52 {
53
54 Logger::Logger(hermes::Context *ctx) { activate(ctx->sources(), ctx->bus()); }
55 Logger::~Logger() { deactivate(); }
56
57 } // namespace luci
58
59 //
60 // LoggerConfig
61 //
62 namespace luci
63 {
64
65 LoggerConfig::LoggerConfig()
66 {
67   auto settings = luci::UserSettings::settings();
68
69   _show_warn = !settings->get(luci::UserSettings::Key::MuteWarnings);
70
71   // Turn on info logging if LUCI_LOG is set as non-zero value
72   _show_info = safecast<bool>(std::getenv("LUCI_LOG"), false);
73
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);
77 }
78
79 void LoggerConfig::configure(const hermes::Source *source, hermes::Source::Setting &setting) const
80 {
81   // Let's ignore hermes::Sources if that is not a moco logger
82   if (auto logger = dynamic_cast<const Logger *>(source))
83   {
84     configure(logger, setting);
85   }
86 }
87
88 void LoggerConfig::configure(const Logger *, hermes::Source::Setting &setting) const
89 {
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();
95
96   // TODO enable FATAL and ERROR
97   if (_show_warn)
98   {
99     setting.filter(hermes::SeverityCategory::WARN).accept_all();
100   }
101   if (_show_info)
102   {
103     setting.filter(hermes::SeverityCategory::INFO).accept_all();
104   }
105   if (_show_verbose)
106   {
107     setting.filter(hermes::SeverityCategory::VERBOSE).accept_upto(_show_verbose);
108   }
109 }
110
111 } // namespace luci