xref: /aosp_15_r20/external/mesa3d/src/util/log.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright 2017 Google
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #ifndef MESA_LOG_H
25 #define MESA_LOG_H
26 
27 #include <stdarg.h>
28 #include <stdio.h>
29 
30 #include "util/macros.h"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #ifndef MESA_LOG_TAG
37 #define MESA_LOG_TAG "MESA"
38 #endif
39 
40 enum mesa_log_level {
41    MESA_LOG_ERROR,
42    MESA_LOG_WARN,
43    MESA_LOG_INFO,
44    MESA_LOG_DEBUG,
45 };
46 
47 #define MAX_LOG_MESSAGE_LENGTH 4096
48 
49 FILE *
50 mesa_log_get_file(void);
51 
52 void PRINTFLIKE(3, 4)
53 mesa_log(enum mesa_log_level, const char *tag, const char *format, ...);
54 
55 void
56 mesa_log_v(enum mesa_log_level, const char *tag, const char *format,
57             va_list va);
58 
59 void
60 _mesa_log(const char *fmtString, ...) PRINTFLIKE(1, 2);
61 
62 void
63 _mesa_log_direct(const char *string);
64 
65 void
66 mesa_log_if_debug(enum mesa_log_level level, const char *outputString);
67 
68 #define mesa_loge(fmt, ...) mesa_log(MESA_LOG_ERROR, (MESA_LOG_TAG), (fmt), ##__VA_ARGS__)
69 #define mesa_logw(fmt, ...) mesa_log(MESA_LOG_WARN, (MESA_LOG_TAG), (fmt), ##__VA_ARGS__)
70 #define mesa_logi(fmt, ...) mesa_log(MESA_LOG_INFO, (MESA_LOG_TAG), (fmt), ##__VA_ARGS__)
71 #if MESA_DEBUG
72 #define mesa_logd(fmt, ...) mesa_log(MESA_LOG_DEBUG, (MESA_LOG_TAG), (fmt), ##__VA_ARGS__)
73 #else
74 #define mesa_logd(fmt, ...) __mesa_log_use_args((fmt), ##__VA_ARGS__)
75 #endif
76 
77 #define mesa_loge_v(fmt, va) mesa_log_v(MESA_LOG_ERROR, (MESA_LOG_TAG), (fmt), (va))
78 #define mesa_logw_v(fmt, va) mesa_log_v(MESA_LOG_WARN, (MESA_LOG_TAG), (fmt), (va))
79 #define mesa_logi_v(fmt, va) mesa_log_v(MESA_LOG_INFO, (MESA_LOG_TAG), (fmt), (va))
80 #if MESA_DEBUG
81 #define mesa_logd_v(fmt, va) mesa_log_v(MESA_LOG_DEBUG, (MESA_LOG_TAG), (fmt), (va))
82 #else
83 #define mesa_logd_v(fmt, va) __mesa_log_use_args((fmt), (va))
84 #endif
85 
86 #define mesa_log_once(level, fmt, ...)        \
87    do                                         \
88    {                                          \
89       static bool once;                       \
90       if (!once) {                            \
91          once = true;                         \
92          mesa_log(level, (MESA_LOG_TAG), fmt, ##__VA_ARGS__); \
93       }                                       \
94    } while (0)
95 
96 #define mesa_loge_once(fmt, ...) mesa_log_once(MESA_LOG_ERROR, fmt, ##__VA_ARGS__)
97 #define mesa_logw_once(fmt, ...) mesa_log_once(MESA_LOG_WARN, fmt, ##__VA_ARGS__)
98 #define mesa_logi_once(fmt, ...) mesa_log_once(MESA_LOG_INFO, fmt, ##__VA_ARGS__)
99 #define mesa_logd_once(fmt, ...) mesa_log_once(MESA_LOG_DEBUG, fmt, ##__VA_ARGS__)
100 
101 struct log_stream {
102    char *msg;
103    const char *tag;
104    size_t pos;
105    enum mesa_log_level level;
106 };
107 
108 struct log_stream *_mesa_log_stream_create(enum mesa_log_level level, const char *tag);
109 #define mesa_log_streame() _mesa_log_stream_create(MESA_LOG_ERROR, (MESA_LOG_TAG))
110 #define mesa_log_streamw() _mesa_log_stream_create(MESA_LOG_WARN, (MESA_LOG_TAG))
111 #define mesa_log_streami() _mesa_log_stream_create(MESA_LOG_INFO, (MESA_LOG_TAG))
112 void mesa_log_stream_destroy(struct log_stream *stream);
113 void mesa_log_stream_printf(struct log_stream *stream, const char *format, ...) PRINTFLIKE(2, 3);
114 
115 void _mesa_log_multiline(enum mesa_log_level level, const char *tag, const char *lines);
116 #define mesa_log_multiline(level, lines) _mesa_log_multiline(level, (MESA_LOG_TAG), lines)
117 
118 #if !MESA_DEBUG
119 /* Suppres -Wunused */
120 static inline void PRINTFLIKE(1, 2)
__mesa_log_use_args(UNUSED const char * format,...)121 __mesa_log_use_args(UNUSED const char *format, ...) { }
122 #endif
123 
124 #ifdef __cplusplus
125 }
126 #endif
127 
128 #endif /* MESA_LOG_H */
129