1/*
2 * Copyright © 2021 Intel Corporation
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#include "vk_instance.h"
25
26/* __VK_ARG_N(...) returns the number of arguments provided to it  */
27#define __VK_ARG_SEQ(_1,_2,_3,_4,_5,_6,_7,_8,N,...) N
28#define __VK_ARG_N(...) __VK_ARG_SEQ(__VA_ARGS__,8,7,6,5,4,3,2,1,0)
29
30#define VK_LOG_OBJS(...)                                        \
31   __VK_ARG_N(__VA_ARGS__), (const void*[]){__VA_ARGS__}
32
33#define VK_LOG_NO_OBJS(instance) 0, (const void**)instance
34
35#define vk_logd(objects_macro, format, ...)                             \
36   __vk_log(VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT,            \
37            VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT,                \
38            objects_macro, __FILE__, __LINE__, format, ## __VA_ARGS__)
39
40#define vk_logi(objects_macro, format, ...)                             \
41   __vk_log(VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT,               \
42            VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT,                \
43            objects_macro, __FILE__, __LINE__, format, ## __VA_ARGS__)
44
45#define vk_logw(objects_macro, format, ...)                             \
46   __vk_log(VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT,            \
47            VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT,                \
48            objects_macro, __FILE__, __LINE__, format, ## __VA_ARGS__)
49
50#define vk_loge(objects_macro, format, ...)                             \
51   __vk_log(VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT,              \
52            VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT,                \
53            objects_macro, __FILE__, __LINE__, format, ## __VA_ARGS__)
54
55#define vk_perf(objects_macro, format, ...)                             \
56   __vk_log(VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT,            \
57            VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT,            \
58            objects_macro, __FILE__, __LINE__, format, ## __VA_ARGS__)
59
60#define __vk_log(severity, type, object_count,                          \
61                 objects_or_instance, file, line, format, ...)          \
62   __vk_log_impl(severity, type, object_count, objects_or_instance,     \
63                 file, line, format, ## __VA_ARGS__)
64
65void PRINTFLIKE(7, 8)
66__vk_log_impl(VkDebugUtilsMessageSeverityFlagBitsEXT severity,
67              VkDebugUtilsMessageTypeFlagsEXT types,
68              int object_count,
69              const void **objects_or_instance,
70              const char *file,
71              int line,
72              const char *format,
73              ...);
74
75#define vk_error(obj, error) \
76   __vk_errorf(obj, error, __FILE__, __LINE__, NULL)
77
78#define vk_errorf(obj, error, ...) \
79   __vk_errorf(obj, error, __FILE__, __LINE__, __VA_ARGS__)
80
81VkResult
82__vk_errorv(const void *_obj, VkResult error,
83            const char *file, int line,
84            const char *format, va_list va);
85
86VkResult PRINTFLIKE(5, 6)
87__vk_errorf(const void *_obj, VkResult error,
88            const char *file, int line,
89            const char *format, ...);
90