• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 #include "util/macros.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 #ifndef MESA_LOG_TAG
36 #define MESA_LOG_TAG "MESA"
37 #endif
38 
39 enum mesa_log_level {
40    MESA_LOG_ERROR,
41    MESA_LOG_WARN,
42    MESA_LOG_INFO,
43    MESA_LOG_DEBUG,
44 };
45 
46 void PRINTFLIKE(3, 4)
47 mesa_log(enum mesa_log_level, const char *tag, const char *format, ...);
48 
49 void
50 mesa_log_v(enum mesa_log_level, const char *tag, const char *format,
51             va_list va);
52 
53 #define mesa_loge(fmt, ...) mesa_log(MESA_LOG_ERROR, (MESA_LOG_TAG), (fmt), ##__VA_ARGS__)
54 #define mesa_logw(fmt, ...) mesa_log(MESA_LOG_WARN, (MESA_LOG_TAG), (fmt), ##__VA_ARGS__)
55 #define mesa_logi(fmt, ...) mesa_log(MESA_LOG_INFO, (MESA_LOG_TAG), (fmt), ##__VA_ARGS__)
56 #ifdef DEBUG
57 #define mesa_logd(fmt, ...) mesa_log(MESA_LOG_DEBUG, (MESA_LOG_TAG), (fmt), ##__VA_ARGS__)
58 #else
59 #define mesa_logd(fmt, ...) __mesa_log_use_args((fmt), ##__VA_ARGS__)
60 #endif
61 
62 #define mesa_loge_v(fmt, va) mesa_log_v(MESA_LOG_ERROR, (MESA_LOG_TAG), (fmt), (va))
63 #define mesa_logw_v(fmt, va) mesa_log_v(MESA_LOG_WARN, (MESA_LOG_TAG), (fmt), (va))
64 #define mesa_logi_v(fmt, va) mesa_log_v(MESA_LOG_INFO, (MESA_LOG_TAG), (fmt), (va))
65 #ifdef DEBUG
66 #define mesa_logd_v(fmt, va) mesa_log_v(MESA_LOG_DEBUG, (MESA_LOG_TAG), (fmt), (va))
67 #else
68 #define mesa_logd_v(fmt, va) __mesa_log_use_args((fmt), (va))
69 #endif
70 
71 #define mesa_log_once(level, fmt, ...)        \
72    do                                         \
73    {                                          \
74       static bool once;                       \
75       if (!once) {                            \
76          once = true;                         \
77          mesa_log(level, (MESA_LOG_TAG), fmt, ##__VA_ARGS__); \
78       }                                       \
79    } while (0)
80 
81 #define mesa_loge_once(fmt, ...) mesa_log_once(MESA_LOG_ERROR, fmt, ##__VA_ARGS__)
82 #define mesa_logw_once(fmt, ...) mesa_log_once(MESA_LOG_WARN, fmt, ##__VA_ARGS__)
83 #define mesa_logi_once(fmt, ...) mesa_log_once(MESA_LOG_INFO, fmt, ##__VA_ARGS__)
84 #define mesa_logd_once(fmt, ...) mesa_log_once(MESA_LOG_DEBUG, fmt, ##__VA_ARGS__)
85 
86 struct log_stream {
87    char *msg;
88    const char *tag;
89    size_t pos;
90    enum mesa_log_level level;
91 };
92 
93 struct log_stream *_mesa_log_stream_create(enum mesa_log_level level, char *tag);
94 #define mesa_log_streame() _mesa_log_stream_create(MESA_LOG_ERROR, (MESA_LOG_TAG))
95 #define mesa_log_streamw() _mesa_log_stream_create(MESA_LOG_WARN, (MESA_LOG_TAG))
96 #define mesa_log_streami() _mesa_log_stream_create(MESA_LOG_INFO, (MESA_LOG_TAG))
97 void mesa_log_stream_destroy(struct log_stream *stream);
98 void mesa_log_stream_printf(struct log_stream *stream, const char *format, ...);
99 
100 void _mesa_log_multiline(enum mesa_log_level level, const char *tag, const char *lines);
101 #define mesa_log_multiline(level, lines) _mesa_log_multiline(level, (MESA_LOG_TAG), lines)
102 
103 #ifndef DEBUG
104 /* Suppres -Wunused */
105 static inline void PRINTFLIKE(1, 2)
__mesa_log_use_args(UNUSED const char * format,...)106 __mesa_log_use_args(UNUSED const char *format, ...) { }
107 #endif
108 
109 #ifdef __cplusplus
110 }
111 #endif
112 
113 #endif /* MESA_LOG_H */
114