ONE - On-device Neural Engine
Loading...
Searching...
No Matches
logging.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
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#ifndef __ONERT_UTIL_LOGGING_H__
18#define __ONERT_UTIL_LOGGING_H__
19
20#include <iostream>
21#include <cstring>
22
23#include "util/ConfigSource.h"
24
25namespace onert
26{
27namespace util
28{
29namespace logging
30{
31
33{
34public:
35 Context() noexcept : _enabled{false}
36 {
37 const auto env = util::getConfigBool(util::config::ONERT_LOG_ENABLE);
38
39 if (env)
40 {
41 _enabled = true;
42 }
43 }
44
45 static Context &get() noexcept;
46
47public:
48 bool enabled(void) const { return _enabled; }
49
50private:
51 bool _enabled;
52};
53
54static Context &ctx = Context::get();
55
56inline std::string decorated_name(const char *input)
57{
58 const int min_prefix = 16;
59 std::string prefix(input);
60 auto len_prefix = prefix.size();
61 if (len_prefix > min_prefix)
62 return "[" + prefix + "] ";
63 std::string spaces((min_prefix - len_prefix) / 2, ' ');
64 return (len_prefix % 2 ? "[ " : "[") + spaces + prefix + spaces + "] ";
65}
66
67} // namespace logging
68} // namespace util
69} // namespace onert
70
71#define VERBOSE(name) \
72 if (::onert::util::logging::ctx.enabled()) \
73 std::cout << ::onert::util::logging::decorated_name(#name)
74
75#define VERBOSE_F() \
76 if (::onert::util::logging::ctx.enabled()) \
77 std::cout << ::onert::util::logging::decorated_name(__func__)
78
79#define WHEN_LOG_ENABLED(METHOD) \
80 if (::onert::util::logging::ctx.enabled()) \
81 do \
82 { \
83 METHOD; \
84 } while (0)
85
86#define MEASURE_TIME_START(name) \
87 do \
88 { \
89 auto beg_##name = std::chrono::steady_clock::now()
90
91#define MEASURE_TIME_END(name) \
92 auto end_##name = std::chrono::steady_clock::now(); \
93 auto dur_##name = \
94 std::chrono::duration_cast<std::chrono::microseconds>(end_##name - beg_##name); \
95 if (::onert::util::logging::ctx.enabled()) \
96 std::cout << ::onert::util::logging::decorated_name(__func__) << #name \
97 << " time = " << dur_##name.count() << std::endl; \
98 } \
99 while (0)
100
101#endif // __ONERT_UTIL_LOGGING_H__
static Context & get() noexcept
Definition logging.cc:19
bool enabled(void) const
Definition logging.h:48
std::string decorated_name(const char *input)
Definition logging.h:56
bool getConfigBool(const std::string &key)