libs/vkd3d: Implement simple debug logs.

This commit is contained in:
Józef Kucia 2016-09-21 12:57:24 +02:00
parent db41f92040
commit 52164aa79b
5 changed files with 245 additions and 0 deletions

View File

@ -17,6 +17,7 @@ CLEANFILES = $(widl_headers)
lib_LTLIBRARIES = libvkd3d.la
libvkd3d_la_SOURCES = \
libs/vkd3d/debug.c \
libs/vkd3d/vkd3d_main.c
pkgconfigdir = $(libdir)/pkgconfig

167
libs/vkd3d/debug.c Normal file
View File

@ -0,0 +1,167 @@
/*
* Copyright 2016 Józef Kucia for CodeWeavers
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "vkd3d_debug.h"
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const char *debug_level_names[] =
{
/* VKD3D_DBG_LEVEL_NONE */ "none",
/* VKD3D_DBG_LEVEL_ERR */ "err",
/* VKD3D_DBG_LEVEL_FIXME */ "fixme",
/* VKD3D_DBG_LEVEL_WARN */ "warn",
/* VKD3D_DBG_LEVEL_TRACE */ "trace",
};
static enum vkd3d_dbg_level vkd3d_dbg_get_level(void)
{
static unsigned int level = ~0u;
const char *vkd3d_debug;
unsigned int i;
if (level != ~0u)
return level;
if (!(vkd3d_debug = getenv("VKD3DDEBUG")))
vkd3d_debug = "";
for (i = 0; i < sizeof(debug_level_names) / sizeof(*debug_level_names); ++i)
{
if (!strcmp(debug_level_names[i], vkd3d_debug))
{
level = i;
return level;
}
}
/* Default debug level. */
level = VKD3D_DBG_LEVEL_FIXME;
return level;
}
void vkd3d_dbg_printf(enum vkd3d_dbg_level level, const char *function, const char *fmt, ...)
{
va_list args;
if (vkd3d_dbg_get_level() < level)
return;
assert(level <= sizeof(debug_level_names) / sizeof(*debug_level_names));
printf("%s:%s: ", debug_level_names[level], function);
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}
#define VKD3D_DEBUG_BUFFER_SIZE 512
static char *get_buffer(void)
{
static char buffers[64][VKD3D_DEBUG_BUFFER_SIZE];
static unsigned int buffer_index;
unsigned int current_index;
current_index = InterlockedIncrement(&buffer_index) % ARRAY_SIZE(buffers);
return buffers[current_index];
}
const char *vkd3d_dbg_sprintf(const char *fmt, ...)
{
char *buffer;
va_list args;
int size;
buffer = get_buffer();
va_start(args, fmt);
size = vsnprintf(buffer, VKD3D_DEBUG_BUFFER_SIZE, fmt, args);
va_end(args);
if (size == -1 || size >= VKD3D_DEBUG_BUFFER_SIZE)
buffer[VKD3D_DEBUG_BUFFER_SIZE - 1] = '\0';
return buffer;
}
const char *debugstr_w(const WCHAR *wstr)
{
char *buffer, *ptr;
WCHAR c;
if (!wstr)
return "(null)";
ptr = buffer = get_buffer();
*ptr++ = '"';
while ((c = *wstr++) && ptr <= buffer + VKD3D_DEBUG_BUFFER_SIZE - 10)
{
int escape_char;
switch (c)
{
case '"':
case '\\':
case '\n':
case '\r':
case '\t':
escape_char = c;
break;
default:
escape_char = 0;
break;
}
if (escape_char)
{
*ptr++ = '\\';
*ptr++ = escape_char;
continue;
}
if (isprint(c))
{
*ptr++ = c;
}
else
{
*ptr++ = '\\';
sprintf(ptr, "%04x", c);
ptr += 4;
}
}
*ptr++ = '"';
if (c)
{
*ptr++ = '.';
*ptr++ = '.';
*ptr++ = '.';
}
*ptr = '\0';
return buffer;
}

73
libs/vkd3d/vkd3d_debug.h Normal file
View File

@ -0,0 +1,73 @@
/*
* Copyright 2016 Józef Kucia for CodeWeavers
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef __VKD3D_DEBUG_H
#define __VKD3D_DEBUG_H
#include "vkd3d_common.h"
#ifdef __GNUC__
# define VKD3D_PRINTF_FUNC(fmt, args) __attribute__((format(printf, fmt, args)))
#else
# define VKD3D_PRINTF_FUNC(fmt, args)
#endif /* __GNUC__ */
enum vkd3d_dbg_level
{
VKD3D_DBG_LEVEL_NONE,
VKD3D_DBG_LEVEL_ERR,
VKD3D_DBG_LEVEL_FIXME,
VKD3D_DBG_LEVEL_WARN,
VKD3D_DBG_LEVEL_TRACE,
};
void vkd3d_dbg_printf(enum vkd3d_dbg_level level, const char *function,
const char *fmt, ...) VKD3D_PRINTF_FUNC(3, 4) DECLSPEC_HIDDEN;
const char *vkd3d_dbg_sprintf(const char *fmt, ...) VKD3D_PRINTF_FUNC(1, 2) DECLSPEC_HIDDEN;
const char *debugstr_w(const WCHAR *wstr) DECLSPEC_HIDDEN;
#define VKD3D_DBG_LOG(level) \
do { \
const enum vkd3d_dbg_level __level = VKD3D_DBG_LEVEL_##level; \
VKD3D_DBG_PRINTF
#define VKD3D_DBG_PRINTF(args...) \
vkd3d_dbg_printf(__level, __FUNCTION__, args); } while (0)
#define TRACE VKD3D_DBG_LOG(TRACE)
#define WARN VKD3D_DBG_LOG(WARN)
#define FIXME VKD3D_DBG_LOG(FIXME)
#define ERR VKD3D_DBG_LOG(ERR)
static inline const char *debugstr_guid(const GUID *guid)
{
if (!guid)
return "(null)";
return vkd3d_dbg_sprintf("{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
guid->Data1, guid->Data2, guid->Data3, guid->Data4[0], guid->Data4[1],
guid->Data4[2], guid->Data4[3], guid->Data4[4], guid->Data4[5],
guid->Data4[6], guid->Data4[7]);
}
#endif /* __VKD3D_DEBUG_H */

View File

@ -26,5 +26,8 @@
HRESULT WINAPI D3D12CreateDevice(IUnknown *adapter, D3D_FEATURE_LEVEL minimum_feature_level,
REFIID riid, void **device)
{
FIXME("adapter %p, minimum_feature_level %#x, riid %s, device %p stub!\n",
adapter, minimum_feature_level, debugstr_guid(riid), device);
return E_NOTIMPL;
}

View File

@ -24,6 +24,7 @@
#define __VKD3D_PRIVATE_H
#include "vkd3d_common.h"
#include "vkd3d_debug.h"
#include "d3d12.h"