ArmNN
 22.05.01
Logging.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include <armnn/Utils.hpp>
9 #include <iostream>
10 #include <algorithm>
11 
12 namespace armnn
13 {
14 
15 inline std::string LevelToString(LogSeverity level)
16 {
17  switch(level)
18  {
19  case LogSeverity::Trace:
20  return "Trace";
21  case LogSeverity::Debug:
22  return "Debug";
23  case LogSeverity::Info:
24  return "Info";
26  return "Warning";
27  case LogSeverity::Error:
28  return "Error";
29  case LogSeverity::Fatal:
30  return "Fatal";
31  default:
32  return "Log";
33  }
34 }
35 
36 inline LogSeverity StringToLogLevel(std::string level)
37 {
38  // Transfer to lower case
39  std::transform(level.begin(), level.end(), level.begin(),
40  [](unsigned char c){ return std::tolower(c); }
41  );
42 
43  if (level == "trace")
44  {
45  return LogSeverity::Trace;
46  }
47  else if (level == "debug")
48  {
49  return LogSeverity::Debug;
50  }
51  else if (level == "info")
52  {
53  return LogSeverity::Info;
54  }
55  else if (level == "warning")
56  {
57  return LogSeverity::Warning;
58  }
59  else if (level == "error")
60  {
61  return LogSeverity::Error;
62  }
63  else if (level == "fatal")
64  {
65  return LogSeverity::Fatal;
66  }
67  else
68  {
69  throw armnn::Exception("Unknown severity level for logging: '" + level +
70  "'. Valid options: trace, debug, info, warning, error, fatal");
71  }
72 }
73 
74 class LogSink
75 {
76 public:
77  virtual ~LogSink(){};
78 
79  virtual void Consume(const std::string&) = 0;
80 private:
81 
82 };
83 
85 {
86 public:
87  void Consume(const std::string& s) override
88  {
89  std::cout << s << std::endl;
90  }
91 };
92 
94 {
95  ScopedRecord(const std::vector<std::shared_ptr<LogSink>>& sinks, LogSeverity level, bool enabled)
96  : m_LogSinks(sinks)
97  , m_Enabled(enabled)
98  {
99  if (enabled)
100  {
101  m_Os << LevelToString(level) << ": ";
102  }
103  }
104 
106  {
107  if (m_Enabled)
108  {
109  for (auto sink : m_LogSinks)
110  {
111  if (sink)
112  {
113  sink->Consume(m_Os.str());
114  }
115  }
116  }
117  }
118 
119  ScopedRecord(const ScopedRecord&) = delete;
120  ScopedRecord& operator=(const ScopedRecord&) = delete;
121  ScopedRecord& operator=(ScopedRecord&&) = delete;
122 
124  : m_LogSinks(other.m_LogSinks)
125  , m_Os(std::move(other.m_Os))
126  , m_Enabled(other.m_Enabled)
127  {
128  // Disable the moved-from ScopedRecord, to prevent it from sending its (now empty) message to
129  // its sinks.
130  other.m_Enabled = false;
131  }
132 
133  template<typename Streamable>
134  ScopedRecord& operator<<(const Streamable& s)
135  {
136  if (m_Enabled)
137  {
138  m_Os << s;
139  }
140  return (*this);
141  }
142 
143 private:
144  const std::vector<std::shared_ptr<LogSink>>& m_LogSinks;
145  std::ostringstream m_Os;
146  bool m_Enabled;
147 };
148 
149 template<LogSeverity Level>
151 {
152 public:
154  : m_Sinks{std::make_shared<StandardOutputSink>()}
155  , m_Enable(true)
156  {
157  }
158 
159  static SimpleLogger& Get();
160 
161  void Enable(bool enable = true)
162  {
163  m_Enable = enable;
164  }
165 
167  {
168  return ScopedRecord(m_Sinks, Level, m_Enable);
169  }
170 
172  {
173  m_Sinks.clear();
174  }
175 
176  void AddSink(std::shared_ptr<LogSink> sink)
177  {
178  m_Sinks.push_back(sink);
179  }
180 private:
181  std::vector<std::shared_ptr<LogSink>> m_Sinks;
182  bool m_Enable;
183 };
184 
185 void SetLogFilter(LogSeverity level);
186 
187 void SetAllLoggingSinks(bool standardOut, bool debugOut, bool coloured);
188 
190 {
191  trace,
192  debug,
193  info,
194  warning,
195  error,
196  fatal
197 };
198 
200 {
201  return static_cast<LogSeverity>(severity);
202 }
203 
204 
205 #define ARMNN_LOG(severity) \
206  armnn::SimpleLogger<ConvertLogSeverity(armnn::BoostLogSeverityMapping::severity)>::Get().StartNewRecord()
207 
208 } //namespace armnn
ScopedRecord StartNewRecord()
Definition: Logging.hpp:166
ScopedRecord(const std::vector< std::shared_ptr< LogSink >> &sinks, LogSeverity level, bool enabled)
Definition: Logging.hpp:95
void SetAllLoggingSinks(bool standardOut, bool debugOut, bool coloured)
Definition: Logging.cpp:191
ScopedRecord(ScopedRecord &&other)
Definition: Logging.hpp:123
void Enable(bool enable=true)
Definition: Logging.hpp:161
std::string LevelToString(LogSeverity level)
Definition: Logging.hpp:15
BoostLogSeverityMapping
Definition: Logging.hpp:189
LogSeverity StringToLogLevel(std::string level)
Definition: Logging.hpp:36
Copyright (c) 2021 ARM Limited and Contributors.
void SetLogFilter(LogSeverity level)
Definition: Logging.cpp:73
virtual ~LogSink()
Definition: Logging.hpp:77
constexpr LogSeverity ConvertLogSeverity(BoostLogSeverityMapping severity)
Definition: Logging.hpp:199
ScopedRecord & operator<<(const Streamable &s)
Definition: Logging.hpp:134
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
void AddSink(std::shared_ptr< LogSink > sink)
Definition: Logging.hpp:176
virtual void Consume(const std::string &)=0
void Consume(const std::string &s) override
Definition: Logging.hpp:87
LogSeverity
Definition: Utils.hpp:14