ArmNN
 21.08
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 
25 {
32  switch (level)
33  {
34  case LogSeverity::Trace:
37  case LogSeverity::Debug:
40  case LogSeverity::Info:
46  case LogSeverity::Error:
49  case LogSeverity::Fatal:
51  break;
52  default:
53  ARMNN_ASSERT(false);
54  }
55 }
56 
57 class StandardOutputColourSink : public LogSink
58 {
59 public:
60  StandardOutputColourSink(LogSeverity level = LogSeverity::Info)
61  : m_Level(level)
62  {
63  }
64 
65  void Consume(const std::string& s) override
66  {
67  std::cout << GetColour(m_Level) << s << ResetColour() << std::endl;
68  }
69 
70 private:
71  std::string ResetColour()
72  {
73  return "\033[0m";
74  }
75 
76  std::string GetColour(LogSeverity level)
77  {
78  switch(level)
79  {
80  case LogSeverity::Trace:
81  return "\033[35m";
82  case LogSeverity::Debug:
83  return "\033[32m";
84  case LogSeverity::Info:
85  return "\033[0m";
87  return "\033[33m";
88  case LogSeverity::Error:
89  return "\033[31m";
90  case LogSeverity::Fatal:
91  return "\033[41;30m";
92 
93  default:
94  return "\033[0m";
95  }
96  }
97  LogSeverity m_Level;
98 };
99 
100 class DebugOutputSink : public LogSink
101 {
102 public:
103  void Consume(const std::string& s) override
104  {
105  IgnoreUnused(s);
106 #if defined(_MSC_VER)
107  OutputDebugString(s.c_str());
108  OutputDebugString("\n");
109 #elif defined(__ANDROID__)
110  __android_log_write(ANDROID_LOG_DEBUG, "armnn", s.c_str());
111 #else
112  IgnoreUnused(s);
113 #endif
114  }
115 };
116 
117 template<LogSeverity Level>
118 inline void SetLoggingSinks(bool standardOut, bool debugOut, bool coloured)
119 {
121 
122  if (standardOut)
123  {
124  if (coloured)
125  {
127  std::make_shared<StandardOutputColourSink>(Level));
128  } else
129  {
131  std::make_shared<StandardOutputSink>());
132  }
133  }
134 
135  if (debugOut)
136  {
138  std::make_shared<DebugOutputSink>());
139  }
140 }
141 
142 void SetAllLoggingSinks(bool standardOut, bool debugOut, bool coloured)
143 {
144  SetLoggingSinks<LogSeverity::Trace>(standardOut, debugOut, coloured);
145  SetLoggingSinks<LogSeverity::Debug>(standardOut, debugOut, coloured);
146  SetLoggingSinks<LogSeverity::Info>(standardOut, debugOut, coloured);
147  SetLoggingSinks<LogSeverity::Warning>(standardOut, debugOut, coloured);
148  SetLoggingSinks<LogSeverity::Error>(standardOut, debugOut, coloured);
149  SetLoggingSinks<LogSeverity::Fatal>(standardOut, debugOut, coloured);
150 }
151 
152 
153 } //namespace armnn
static SimpleLogger & Get()
Definition: Logging.hpp:151
void SetAllLoggingSinks(bool standardOut, bool debugOut, bool coloured)
Definition: Logging.cpp:142
void Enable(bool enable=true)
Definition: Logging.hpp:157
Copyright (c) 2021 ARM Limited and Contributors.
void IgnoreUnused(Ts &&...)
void SetLogFilter(LogSeverity level)
Definition: Logging.cpp:24
#define ARMNN_FALLTHROUGH
Definition: Utils.hpp:36
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
void AddSink(std::shared_ptr< LogSink > sink)
Definition: Logging.hpp:173
LogSeverity
Definition: Utils.hpp:13
void SetLoggingSinks(bool standardOut, bool debugOut, bool coloured)
Definition: Logging.cpp:118