• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 #ifndef LIBWESTON_TRACE
27 #define LIBWESTON_TRACE
28 
29 #include <stdint.h>
30 
31 #define TRACE_ARGS(color) LABEL, __func__, __LINE__, "\033[" #color "m"
32 #define DEFINE_LOG_LABEL(str) static const char *LABEL = str
33 
34 #define LOG_ENTERS(str) log_printf(TRACE_ARGS(33), "%s {", str); log_level_inc()
35 #define LOG_EXITS(str) log_level_dec(); log_printf(TRACE_ARGS(33), "} %s", str)
36 #define LOG_ENTER() LOG_ENTERS("")
37 #define LOG_EXIT() LOG_EXITS("")
38 #define LOG_INFO(fmt, ...) log_printf(TRACE_ARGS(36), fmt, ##__VA_ARGS__)
39 #define LOG_IMPORTANT(fmt, ...) log_printf(TRACE_ARGS(32), fmt, ##__VA_ARGS__)
40 #define LOG_CORE(fmt, ...) log_printf(TRACE_ARGS(35), "core: " fmt, ##__VA_ARGS__)
41 #define LOG_ERROR(fmt, ...) log_printf(TRACE_ARGS(31), fmt, ##__VA_ARGS__)
42 #define LOG_PASS() log_printf(TRACE_ARGS(32), "pass")
43 
44 #define LOG_REGION(note, region) \
45         LOG_INFO(#note " " #region " (%d, %d) (%d, %d)", \
46                 (region)->extents.x1, (region)->extents.y1, \
47                 (region)->extents.x2, (region)->extents.y2)
48 
49 #define LOG_MATRIX(matrix) \
50     LOG_INFO(#matrix ": {"); \
51     LOG_INFO(#matrix "    %f, %f, %f, %f", (matrix)->d[0], (matrix)->d[4], (matrix)->d[8], (matrix)->d[12]); \
52     LOG_INFO(#matrix "    %f, %f, %f, %f", (matrix)->d[1], (matrix)->d[5], (matrix)->d[9], (matrix)->d[13]); \
53     LOG_INFO(#matrix "    %f, %f, %f, %f", (matrix)->d[2], (matrix)->d[6], (matrix)->d[10], (matrix)->d[14]); \
54     LOG_INFO(#matrix "    %f, %f, %f, %f", (matrix)->d[3], (matrix)->d[7], (matrix)->d[11], (matrix)->d[15]); \
55     LOG_INFO(#matrix "}")
56 
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
60 
61 void log_init();
62 void log_printf(const char *label, const char *func, int32_t line, const char *color, const char *fmt, ...);
63 void log_level_inc();
64 void log_level_dec();
65 
66 #ifdef __cplusplus
67 }
68 
69 #define LOG_SCOPE(...) ScopedLog log(LABEL, __func__, __LINE__, ##__VA_ARGS__)
70 class ScopedLog {
71 public:
72     ScopedLog(const char *label, const char *func, int32_t line, const char *str = "");
73     ~ScopedLog();
74 
75 private:
76     const char *label_;
77     const char *func_;
78     int32_t line_;
79     const char *str_;
80 };
81 #endif
82 
83 #endif // LIBWESTON_TRACE
84