1/*
2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23#include "nine_debug.h"
24
25#include <ctype.h>
26#include "c11/threads.h"
27
28static const struct debug_named_value nine_debug_flags[] = {
29    { "unknown", DBG_UNKNOWN,              "IUnknown implementation." },
30    { "adapter", DBG_ADAPTER,              "ID3D9Adapter implementation." },
31    { "overlay", DBG_OVERLAYEXTENSION,     "IDirect3D9ExOverlayExtension implementation." },
32    { "auth",    DBG_AUTHENTICATEDCHANNEL, "IDirect3DAuthenticatedChannel9 implementation." },
33    { "basetex", DBG_BASETEXTURE,          "IDirect3DBaseTexture9 implementation." },
34    { "crypto",  DBG_CRYPTOSESSION,        "IDirect3DCryptoSession9 implementation." },
35    { "cubetex", DBG_CUBETEXTURE,          "IDirect3DCubeTexture9 implementation." },
36    { "device",  DBG_DEVICE,               "IDirect3DDevice9(Ex) implementation." },
37    { "video",   DBG_DEVICEVIDEO,          "IDirect3DDeviceVideo9 implementation." },
38    { "ibuf",    DBG_INDEXBUFFER,          "IDirect3DIndexBuffer9 implementation." },
39    { "ps",      DBG_PIXELSHADER,          "IDirect3DPixelShader9 implementation." },
40    { "query",   DBG_QUERY,                "IDirect3DQuery9 implementation." },
41    { "res",     DBG_RESOURCE,             "IDirect3DResource9 implementation." },
42    { "state",   DBG_STATEBLOCK,           "IDirect3DStateBlock9 implementation." },
43    { "surf",    DBG_SURFACE,              "IDirect3DSurface9 implementation." },
44    { "swap",    DBG_SWAPCHAIN,            "IDirect3DSwapChain9(Ex) implementation." },
45    { "tex",     DBG_TEXTURE,              "IDirect3DTexture9 implementation." },
46    { "vbuf",    DBG_VERTEXBUFFER,         "IDirect3DVertexBuffer9 implementation." },
47    { "vdecl",   DBG_VERTEXDECLARATION,    "IDirect3DVertexDeclaration9 implementation." },
48    { "vs",      DBG_VERTEXSHADER,         "IDirect3DVertexShader9 implementation." },
49    { "3dsurf",  DBG_VOLUME,               "IDirect3DVolume9 implementation." },
50    { "3dtex",   DBG_VOLUMETEXTURE,        "IDirect3DVolumeTexture9 implementation." },
51    { "shader",  DBG_SHADER,               "Shader token stream translator." },
52    { "ff",      DBG_FF,                   "Fixed function emulation." },
53    { "user",    DBG_USER,                 "User errors, both fixable and unfixable." },
54    { "error",   DBG_ERROR,                "Driver errors, always visible." },
55    { "warn",    DBG_WARN,                 "Driver warnings, always visible in debug builds." },
56    { "tid",     DBG_TID,                  "Display thread-ids." },
57    DEBUG_NAMED_VALUE_END
58};
59
60void
61_nine_debug_printf( unsigned long flag,
62                    const char *func,
63                    const char *fmt,
64                    ... )
65{
66    static boolean first = TRUE;
67    static unsigned long dbg_flags = DBG_ERROR | DBG_WARN;
68    unsigned long tid = 0;
69
70    if (first) {
71        first = FALSE;
72        dbg_flags |= debug_get_flags_option("NINE_DEBUG", nine_debug_flags, 0);
73    }
74
75#if defined(HAVE_PTHREAD)
76    if (dbg_flags & DBG_TID)
77        tid = (unsigned long)pthread_self();
78#endif
79
80    if (dbg_flags & flag) {
81        const char *f = func ? strrchr(func, '_') : NULL;
82        va_list ap;
83        /* inside a class this will print nine:tid:classinlowercase:func: while
84         * outside a class (rarely used) it will just print nine:tid:func
85         * the reason for lower case is simply to match the filenames, as it
86         * will also strip off the "Nine" */
87        if (f && strncmp(func, "Nine", 4) == 0) {
88            char klass[96]; /* no class name is this long */
89            char *ptr = klass;
90            for (func += 4; func != f; ++func) { *ptr++ = tolower(*func); }
91            *ptr = '\0';
92            if (tid)
93                _debug_printf("nine:0x%08lx:%s:%s: ", tid, klass, ++f);
94            else
95                _debug_printf("nine:%s:%s: ", klass, ++f);
96        } else if (func) {
97            if (tid)
98                _debug_printf("nine:0x%08lx:%s ", tid, func);
99            else
100                _debug_printf("nine:%s ", func);
101        }
102
103        va_start(ap, fmt);
104        _debug_vprintf(fmt, ap);
105        va_end(ap);
106    }
107}
108
109void
110_nine_stub( const char *file,
111            const char *func,
112            unsigned line )
113{
114    const char *r = strrchr(file, '/');
115    if (r == NULL) { r = strrchr(file, '\\'); }
116    _debug_printf("nine:%s:%d: %s STUB!\n", r ? ++r : file, line, func);
117}
118