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