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