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