ArmNN
 21.02
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 
123  ScopedRecord(ScopedRecord&& other) = default;
124 
125  template<typename Streamable>
126  ScopedRecord& operator<<(const Streamable& s)
127  {
128  if (m_Enabled)
129  {
130  m_Os << s;
131  }
132  return (*this);
133  }
134 
135 private:
136  const std::vector<std::shared_ptr<LogSink>>& m_LogSinks;
137  std::ostringstream m_Os;
138  bool m_Enabled;
139 };
140 
141 template<LogSeverity Level>
143 {
144 public:
146  : m_Sinks{std::make_shared<StandardOutputSink>()}
147  , m_Enable(true)
148  {
149  }
150 
151  static SimpleLogger& Get()
152  {
153  static SimpleLogger<Level> logger;
154  return logger;
155  }
156 
157  void Enable(bool enable = true)
158  {
159  m_Enable = enable;
160  }
161 
163  {
164  ScopedRecord record(m_Sinks, Level, m_Enable);
165  return record;
166  }
167 
169  {
170  m_Sinks.clear();
171  }
172 
173  void AddSink(std::shared_ptr<LogSink> sink)
174  {
175  m_Sinks.push_back(sink);
176  }
177 private:
178  std::vector<std::shared_ptr<LogSink>> m_Sinks;
179  bool m_Enable;
180 };
181 
182 void SetLogFilter(LogSeverity level);
183 
184 void SetAllLoggingSinks(bool standardOut, bool debugOut, bool coloured);
185 
187 {
188  trace,
189  debug,
190  info,
191  warning,
192  error,
193  fatal
194 };
195 
197 {
198  return static_cast<LogSeverity>(severity);
199 }
200 
201 
202 #define ARMNN_LOG(severity) \
203  armnn::SimpleLogger<ConvertLogSeverity(armnn::BoostLogSeverityMapping::severity)>::Get().StartNewRecord()
204 
205 } //namespace armnn
static SimpleLogger & Get()
Definition: Logging.hpp:151
ScopedRecord StartNewRecord()
Definition: Logging.hpp:162
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:142
void Enable(bool enable=true)
Definition: Logging.hpp:157
std::string LevelToString(LogSeverity level)
Definition: Logging.hpp:15
BoostLogSeverityMapping
Definition: Logging.hpp:186
LogSeverity StringToLogLevel(std::string level)
Definition: Logging.hpp:36
Copyright (c) 2021 ARM Limited and Contributors.
void SetLogFilter(LogSeverity level)
Definition: Logging.cpp:24
virtual ~LogSink()
Definition: Logging.hpp:77
constexpr LogSeverity ConvertLogSeverity(BoostLogSeverityMapping severity)
Definition: Logging.hpp:196
ScopedRecord & operator<<(const Streamable &s)
Definition: Logging.hpp:126
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:173
virtual void Consume(const std::string &)=0
void Consume(const std::string &s) override
Definition: Logging.hpp:87
LogSeverity
Definition: Utils.hpp:13