ArmNN  NotReleased
Logging.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include <armnn/Logging.hpp>
6 
7 
8 #include <armnn/Utils.hpp>
9 
10 #if defined(_MSC_VER)
11 #ifndef NOMINMAX
12 #define NOMINMAX // Prevent definition of min/max macros that interfere with std::min/max
13 #endif
14 #include <Windows.h>
15 #undef TIME_MS // Windows.h defines this but we don't need it and it interferes with our definition in Instrument.hpp
16 #endif
17 
18 #if defined(__ANDROID__)
19 #include <android/log.h>
20 #endif
21 
22 #include <boost/assert.hpp>
23 #include <boost/core/ignore_unused.hpp>
24 #include <iostream>
25 
26 namespace armnn
27 {
28 
30 {
37  switch (level)
38  {
39  case LogSeverity::Trace:
42  case LogSeverity::Debug:
45  case LogSeverity::Info:
51  case LogSeverity::Error:
54  case LogSeverity::Fatal:
56  break;
57  default:
58  BOOST_ASSERT(false);
59  }
60 }
61 
62 class StandardOutputColourSink : public LogSink
63 {
64 public:
65  StandardOutputColourSink(LogSeverity level = LogSeverity::Info)
66  : m_Level(level)
67  {
68  }
69 
70  void Consume(const std::string& s) override
71  {
72  std::cout << GetColour(m_Level) << s << ResetColour() << std::endl;
73  }
74 
75 private:
76  std::string ResetColour()
77  {
78  return "\033[0m";
79  }
80 
81  std::string GetColour(LogSeverity level)
82  {
83  switch(level)
84  {
85  case LogSeverity::Trace:
86  return "\033[35m";
87  case LogSeverity::Debug:
88  return "\033[32m";
89  case LogSeverity::Info:
90  return "\033[0m";
92  return "\033[33m";
93  case LogSeverity::Error:
94  return "\033[31m";
95  case LogSeverity::Fatal:
96  return "\033[41;30m";
97 
98  default:
99  return "\033[0m";
100  }
101  }
102  LogSeverity m_Level;
103 };
104 
105 class DebugOutputSink : public LogSink
106 {
107 public:
108  void Consume(const std::string& s) override
109  {
110  boost::ignore_unused(s);
111 #if defined(_MSC_VER)
112  OutputDebugString(s.c_str());
113  OutputDebugString("\n");
114 #elif defined(__ANDROID__)
115  __android_log_write(ANDROID_LOG_DEBUG, "armnn", s.c_str());
116 #else
117  boost::ignore_unused(s);
118 #endif
119  }
120 };
121 
122 template<LogSeverity Level>
123 inline void SetLoggingSinks(bool standardOut, bool debugOut, bool coloured)
124 {
126 
127  if (standardOut)
128  {
129  if (coloured)
130  {
132  std::make_shared<StandardOutputColourSink>(Level));
133  } else
134  {
136  std::make_shared<StandardOutputSink>());
137  }
138  }
139 
140  if (debugOut)
141  {
143  std::make_shared<DebugOutputSink>());
144  }
145 }
146 
147 void SetAllLoggingSinks(bool standardOut, bool debugOut, bool coloured)
148 {
149  SetLoggingSinks<LogSeverity::Trace>(standardOut, debugOut, coloured);
150  SetLoggingSinks<LogSeverity::Debug>(standardOut, debugOut, coloured);
151  SetLoggingSinks<LogSeverity::Info>(standardOut, debugOut, coloured);
152  SetLoggingSinks<LogSeverity::Warning>(standardOut, debugOut, coloured);
153  SetLoggingSinks<LogSeverity::Error>(standardOut, debugOut, coloured);
154  SetLoggingSinks<LogSeverity::Fatal>(standardOut, debugOut, coloured);
155 }
156 
157 
158 } //namespace armnn
void Enable(bool enable=true)
Definition: Logging.hpp:118
void AddSink(std::shared_ptr< LogSink > sink)
Definition: Logging.hpp:134
#define ARMNN_FALLTHROUGH
Definition: Utils.hpp:35
void SetLoggingSinks(bool standardOut, bool debugOut, bool coloured)
Definition: Logging.cpp:123
void SetLogFilter(LogSeverity level)
Definition: Logging.cpp:29
LogSeverity
Definition: Utils.hpp:12
void SetAllLoggingSinks(bool standardOut, bool debugOut, bool coloured)
Definition: Logging.cpp:147
static SimpleLogger & Get()
Definition: Logging.hpp:112