1 /*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9 /**
10 * @file
11 * ExecuTorch logging API.
12 */
13
14 #pragma once
15
16 #include <cstdarg>
17 #include <cstddef>
18 #include <cstdlib>
19
20 #include <executorch/runtime/platform/compiler.h>
21 #include <executorch/runtime/platform/types.h>
22
23 // Set minimum log severity if compiler option is not provided.
24 #ifndef ET_MIN_LOG_LEVEL
25 #define ET_MIN_LOG_LEVEL Info
26 #endif // !defined(ET_MIN_LOG_LEVEL)
27
28 /*
29 * Enable logging by default if compiler option is not provided.
30 * This should facilitate less confusion for those developing ExecuTorch.
31 */
32 #ifndef ET_LOG_ENABLED
33 #define ET_LOG_ENABLED 1
34 #endif // !defined(ET_LOG_ENABLED)
35
36 namespace executorch {
37 namespace runtime {
38
39 /**
40 * Severity level of a log message. Must be ordered from lowest to highest
41 * severity.
42 */
43 enum class LogLevel : uint8_t {
44 /**
45 * Log messages provided for highly granular debuggability.
46 *
47 * Log messages using this severity are unlikely to be compiled by default
48 * into most debug builds.
49 */
50 Debug,
51
52 /**
53 * Log messages providing information about the state of the system
54 * for debuggability.
55 */
56 Info,
57
58 /**
59 * Log messages about errors within ExecuTorch during runtime.
60 */
61 Error,
62
63 /**
64 * Log messages that precede a fatal error. However, logging at this level
65 * does not perform the actual abort, something else needs to.
66 */
67 Fatal,
68
69 /**
70 * Number of supported log levels, with values in [0, NumLevels).
71 */
72 NumLevels,
73 };
74
75 namespace internal {
76
77 /**
78 * Get the current timestamp to construct a log event.
79 *
80 * @retval Monotonically non-decreasing timestamp in system ticks.
81 */
82 et_timestamp_t get_log_timestamp();
83
84 /**
85 * Log a string message.
86 *
87 * Note: This is an internal function. Use the `ET_LOG` macro instead.
88 *
89 * @param[in] level Log severity level.
90 * @param[in] timestamp Timestamp (in system ticks) of the log event.
91 * @param[in] filename Name of the source file creating the log event.
92 * @param[in] function Name of the function creating the log event.
93 * @param[in] line Source file line of the caller.
94 * @param[in] format Format string.
95 * @param[in] args Variable argument list.
96 */
97 ET_PRINTFLIKE(6, 0)
98 void vlogf(
99 LogLevel level,
100 et_timestamp_t timestamp,
101 const char* filename,
102 const char* function,
103 size_t line,
104 const char* format,
105 va_list args);
106
107 /**
108 * Log a string message.
109 *
110 * Note: This is an internal function. Use the `ET_LOG` macro instead.
111 *
112 * @param[in] level Log severity level.
113 * @param[in] timestamp Timestamp (in system ticks) of the log event.
114 * @param[in] filename Name of the source file creating the log event.
115 * @param[in] function Name of the function creating the log event.
116 * @param[in] line Source file line of the caller.
117 * @param[in] format Format string.
118 */
119 ET_PRINTFLIKE(6, 7)
logf(LogLevel level,et_timestamp_t timestamp,const char * filename,const char * function,size_t line,const char * format,...)120 inline void logf(
121 LogLevel level,
122 et_timestamp_t timestamp,
123 const char* filename,
124 const char* function,
125 size_t line,
126 const char* format,
127 ...) {
128 #if ET_LOG_ENABLED
129 va_list args;
130 va_start(args, format);
131 internal::vlogf(level, timestamp, filename, function, line, format, args);
132 va_end(args);
133 #endif // ET_LOG_ENABLED
134 }
135
136 } // namespace internal
137
138 } // namespace runtime
139 } // namespace executorch
140
141 namespace torch {
142 namespace executor {
143 // TODO(T197294990): Remove these deprecated aliases once all users have moved
144 // to the new `::executorch` namespaces.
145 using ::executorch::runtime::LogLevel;
146 } // namespace executor
147 } // namespace torch
148
149 #if ET_LOG_ENABLED
150
151 /**
152 * Log a message at the given log severity level.
153 *
154 * @param[in] _level Log severity level.
155 * @param[in] _format Log message format string.
156 */
157 #define ET_LOG(_level, _format, ...) \
158 do { \
159 const auto _log_level = ::executorch::runtime::LogLevel::_level; \
160 if (static_cast<uint32_t>(_log_level) >= \
161 static_cast<uint32_t>( \
162 ::executorch::runtime::LogLevel::ET_MIN_LOG_LEVEL)) { \
163 const auto _timestamp = \
164 ::executorch::runtime::internal::get_log_timestamp(); \
165 ::executorch::runtime::internal::logf( \
166 _log_level, \
167 _timestamp, \
168 ET_SHORT_FILENAME, \
169 ET_FUNCTION, \
170 ET_LINE, \
171 _format, \
172 ##__VA_ARGS__); \
173 } \
174 } while (0)
175 #else // ET_LOG_ENABLED
176
177 /**
178 * Log a message at the given log severity level.
179 *
180 * @param[in] _level Log severity level.
181 * @param[in] _format Log message format string.
182 */
183 #define ET_LOG(_level, _format, ...) ((void)0)
184
185 #endif // ET_LOG_ENABLED
186