ArmNN
 24.02
Logging.cpp
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 #include <armnn/Logging.hpp>
8 #include <armnn/Utils.hpp>
10 
11 #if defined(_MSC_VER)
12 #include <common/include/WindowsWrapper.hpp>
13 #endif
14 
15 #if defined(__ANDROID__)
16 #include <android/log.h>
17 #endif
18 
19 #include <iostream>
20 
21 namespace armnn
22 {
23 
24 template<LogSeverity Level>
26 {
27  static SimpleLogger<Level> logger;
28  return logger;
29 }
30 
31 template<>
33 {
35  return logger;
36 }
37 
38 template<>
40 {
42  return logger;
43 }
44 
45 template<>
47 {
48  static SimpleLogger<LogSeverity::Info> logger;
49  return logger;
50 }
51 
52 template<>
54 {
56  return logger;
57 }
58 
59 template<>
61 {
63  return logger;
64 }
65 
66 template<>
68 {
70  return logger;
71 }
72 
74 {
81  switch (level)
82  {
83  case LogSeverity::Trace:
86  case LogSeverity::Debug:
89  case LogSeverity::Info:
95  case LogSeverity::Error:
98  case LogSeverity::Fatal:
100  break;
101  default:
102  ARMNN_ASSERT(false);
103  }
104 }
105 
106 class StandardOutputColourSink : public LogSink
107 {
108 public:
109  StandardOutputColourSink(LogSeverity level = LogSeverity::Info)
110  : m_Level(level)
111  {
112  }
113 
114  void Consume(const std::string& s) override
115  {
116  std::cout << GetColour(m_Level) << s << ResetColour() << std::endl;
117  }
118 
119 private:
120  std::string ResetColour()
121  {
122  return "\033[0m";
123  }
124 
125  std::string GetColour(LogSeverity level)
126  {
127  switch(level)
128  {
129  case LogSeverity::Trace:
130  return "\033[35m";
131  case LogSeverity::Debug:
132  return "\033[32m";
133  case LogSeverity::Info:
134  return "\033[0m";
136  return "\033[33m";
137  case LogSeverity::Error:
138  return "\033[31m";
139  case LogSeverity::Fatal:
140  return "\033[41;30m";
141 
142  default:
143  return "\033[0m";
144  }
145  }
146  LogSeverity m_Level;
147 };
148 
149 class DebugOutputSink : public LogSink
150 {
151 public:
152  void Consume(const std::string& s) override
153  {
154  IgnoreUnused(s);
155 #if defined(_MSC_VER)
156  OutputDebugString(s.c_str());
157  OutputDebugString("\n");
158 #elif defined(__ANDROID__)
159  __android_log_write(ANDROID_LOG_DEBUG, "armnn", s.c_str());
160 #else
161  IgnoreUnused(s);
162 #endif
163  }
164 };
165 
166 template<LogSeverity Level>
167 inline void SetLoggingSinks(bool standardOut, bool debugOut, bool coloured)
168 {
170 
171  if (standardOut)
172  {
173  if (coloured)
174  {
176  std::make_shared<StandardOutputColourSink>(Level));
177  } else
178  {
180  std::make_shared<StandardOutputSink>());
181  }
182  }
183 
184  if (debugOut)
185  {
187  std::make_shared<DebugOutputSink>());
188  }
189 }
190 
191 void SetAllLoggingSinks(bool standardOut, bool debugOut, bool coloured)
192 {
193  SetLoggingSinks<LogSeverity::Trace>(standardOut, debugOut, coloured);
194  SetLoggingSinks<LogSeverity::Debug>(standardOut, debugOut, coloured);
195  SetLoggingSinks<LogSeverity::Info>(standardOut, debugOut, coloured);
196  SetLoggingSinks<LogSeverity::Warning>(standardOut, debugOut, coloured);
197  SetLoggingSinks<LogSeverity::Error>(standardOut, debugOut, coloured);
198  SetLoggingSinks<LogSeverity::Fatal>(standardOut, debugOut, coloured);
199 }
200 
201 } //namespace armnn
ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
armnn::SimpleLogger::Get
static SimpleLogger & Get()
Definition: Logging.cpp:25
armnn::LogSeverity::Trace
@ Trace
armnn::SetLoggingSinks
void SetLoggingSinks(bool standardOut, bool debugOut, bool coloured)
Definition: Logging.cpp:167
armnn::SimpleLogger::Enable
void Enable(bool enable=true)
Definition: Logging.hpp:168
armnn::SetLogFilter
void SetLogFilter(LogSeverity level)
Definition: Logging.cpp:73
armnn::SimpleLogger::AddSink
void AddSink(std::shared_ptr< LogSink > sink)
Definition: Logging.hpp:183
armnn::LogSeverity::Info
@ Info
IgnoreUnused.hpp
armnn::LogSeverity::Error
@ Error
armnn::SimpleLogger
Definition: Logging.hpp:157
Assert.hpp
Logging.hpp
Utils.hpp
armnn::SetAllLoggingSinks
void SetAllLoggingSinks(bool standardOut, bool debugOut, bool coloured)
Definition: Logging.cpp:191
armnn::LogSeverity::Fatal
@ Fatal
ARMNN_FALLTHROUGH
#define ARMNN_FALLTHROUGH
Definition: Utils.hpp:36
armnn::SimpleLogger::RemoveAllSinks
void RemoveAllSinks()
Definition: Logging.hpp:178
armnn::IgnoreUnused
void IgnoreUnused(Ts &&...)
Definition: IgnoreUnused.hpp:14
armnn::LogSeverity::Warning
@ Warning
armnn::LogSeverity::Debug
@ Debug
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::LogSeverity
LogSeverity
Definition: Utils.hpp:13