log.hpp
1 //*****************************************************************************
2 // Copyright 2017-2021 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //*****************************************************************************
16 
17 #pragma once
18 
19 #include <chrono>
20 #include <cstdarg>
21 #include <deque>
22 #include <functional>
23 #include <iomanip>
24 #include <locale>
25 #include <sstream>
26 #include <stdexcept>
27 #if defined(__linux) || defined(__APPLE__)
28 #include <sys/time.h>
29 #include <unistd.h>
30 #endif
31 #include <vector>
32 
33 #include <ngraph/ngraph_visibility.hpp>
34 
35 namespace ngraph
36 {
38  {
39  public:
40  template <size_t SIZE>
41  constexpr ConstString(const char (&p)[SIZE])
42  : m_string(p)
43  , m_size(SIZE)
44  {
45  }
46 
47  constexpr char operator[](size_t i) const
48  {
49  return i < m_size ? m_string[i] : throw std::out_of_range("");
50  }
51  constexpr const char* get_ptr(size_t offset) const { return &m_string[offset]; }
52  constexpr size_t size() const { return m_size; }
53  private:
54  const char* m_string;
55  size_t m_size;
56  };
57 
58  constexpr const char* find_last(ConstString s, size_t offset, char ch)
59  {
60  return offset == 0 ? s.get_ptr(0) : (s[offset] == ch ? s.get_ptr(offset + 1)
61  : find_last(s, offset - 1, ch));
62  }
63 
64  constexpr const char* find_last(ConstString s, char ch)
65  {
66  return find_last(s, s.size() - 1, ch);
67  }
68 
69  constexpr const char* get_file_name(ConstString s) { return find_last(s, '/'); }
70  constexpr const char* trim_file_name(ConstString root, ConstString s)
71  {
72  return s.get_ptr(root.size());
73  }
74  enum class LOG_TYPE
75  {
76  _LOG_TYPE_ERROR,
77  _LOG_TYPE_WARNING,
78  _LOG_TYPE_INFO,
79  _LOG_TYPE_DEBUG,
80  };
81 
82  class NGRAPH_API LogHelper
83  {
84  public:
85  LogHelper(LOG_TYPE,
86  const char* file,
87  int line,
88  std::function<void(const std::string&)> m_handler_func);
89  ~LogHelper();
90 
91  std::ostream& stream() { return m_stream; }
92  private:
93  std::function<void(const std::string&)> m_handler_func;
94  std::stringstream m_stream;
95  };
96 
97  class Logger
98  {
99  friend class LogHelper;
100 
101  public:
102  static void set_log_path(const std::string& path);
103  static void start();
104  static void stop();
105 
106  private:
107  static void log_item(const std::string& s);
108  static void process_event(const std::string& s);
109  static void thread_entry(void* param);
110  static std::string m_log_path;
111  static std::deque<std::string> m_queue;
112  };
113 
114  NGRAPH_API
115  void default_logger_handler_func(const std::string& s);
116 
117 #define NGRAPH_ERR \
118  ngraph::LogHelper(ngraph::LOG_TYPE::_LOG_TYPE_ERROR, \
119  ngraph::trim_file_name(PROJECT_ROOT_DIR, __FILE__), \
120  __LINE__, \
121  ngraph::default_logger_handler_func) \
122  .stream()
123 
124 #define NGRAPH_WARN \
125  ngraph::LogHelper(ngraph::LOG_TYPE::_LOG_TYPE_WARNING, \
126  ngraph::trim_file_name(PROJECT_ROOT_DIR, __FILE__), \
127  __LINE__, \
128  ngraph::default_logger_handler_func) \
129  .stream()
130 
131 #define NGRAPH_INFO \
132  ngraph::LogHelper(ngraph::LOG_TYPE::_LOG_TYPE_INFO, \
133  ngraph::trim_file_name(PROJECT_ROOT_DIR, __FILE__), \
134  __LINE__, \
135  ngraph::default_logger_handler_func) \
136  .stream()
137 
138 #ifdef NGRAPH_DEBUG_ENABLE
139 #define NGRAPH_DEBUG \
140  ngraph::LogHelper(ngraph::LOG_TYPE::_LOG_TYPE_DEBUG, \
141  ngraph::trim_file_name(PROJECT_ROOT_DIR, __FILE__), \
142  __LINE__, \
143  ngraph::default_logger_handler_func) \
144  .stream()
145 #else
146 
147  struct NullLogger
148  {
149  };
150 
151  template <typename T>
152  NullLogger&& operator<<(NullLogger&& logger, T&&)
153  {
154  return std::move(logger);
155  }
156 
157  template <typename T>
158  NullLogger&& operator<<(NullLogger&& logger, const T&)
159  {
160  return std::move(logger);
161  }
162 
163  inline NullLogger&&
164  operator<<(NullLogger&& logger,
165  std::basic_ostream<char, std::char_traits<char>>& (&)(std::basic_ostream<
166  char,
167  std::char_traits<char>>&))
168  {
169  return std::move(logger);
170  }
171 
172 #define NGRAPH_DEBUG \
173  ::ngraph::NullLogger {}
174 #endif
175 }
Definition: log.hpp:38
Definition: log.hpp:83
Definition: log.hpp:98
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
Definition: log.hpp:148