17ec681f3Smrg/*
27ec681f3Smrg * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * on the rights to use, copy, modify, merge, publish, distribute, sub
87ec681f3Smrg * license, and/or sell copies of the Software, and to permit persons to whom
97ec681f3Smrg * the Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
197ec681f3Smrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
207ec681f3Smrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
217ec681f3Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE. */
227ec681f3Smrg
237ec681f3Smrg#include "nine_debug.h"
247ec681f3Smrg
257ec681f3Smrg#include <ctype.h>
267ec681f3Smrg#include "c11/threads.h"
277ec681f3Smrg
287ec681f3Smrgstatic const struct debug_named_value nine_debug_flags[] = {
297ec681f3Smrg    { "unknown", DBG_UNKNOWN,              "IUnknown implementation." },
307ec681f3Smrg    { "adapter", DBG_ADAPTER,              "ID3D9Adapter implementation." },
317ec681f3Smrg    { "overlay", DBG_OVERLAYEXTENSION,     "IDirect3D9ExOverlayExtension implementation." },
327ec681f3Smrg    { "auth",    DBG_AUTHENTICATEDCHANNEL, "IDirect3DAuthenticatedChannel9 implementation." },
337ec681f3Smrg    { "basetex", DBG_BASETEXTURE,          "IDirect3DBaseTexture9 implementation." },
347ec681f3Smrg    { "crypto",  DBG_CRYPTOSESSION,        "IDirect3DCryptoSession9 implementation." },
357ec681f3Smrg    { "cubetex", DBG_CUBETEXTURE,          "IDirect3DCubeTexture9 implementation." },
367ec681f3Smrg    { "device",  DBG_DEVICE,               "IDirect3DDevice9(Ex) implementation." },
377ec681f3Smrg    { "video",   DBG_DEVICEVIDEO,          "IDirect3DDeviceVideo9 implementation." },
387ec681f3Smrg    { "ibuf",    DBG_INDEXBUFFER,          "IDirect3DIndexBuffer9 implementation." },
397ec681f3Smrg    { "ps",      DBG_PIXELSHADER,          "IDirect3DPixelShader9 implementation." },
407ec681f3Smrg    { "query",   DBG_QUERY,                "IDirect3DQuery9 implementation." },
417ec681f3Smrg    { "res",     DBG_RESOURCE,             "IDirect3DResource9 implementation." },
427ec681f3Smrg    { "state",   DBG_STATEBLOCK,           "IDirect3DStateBlock9 implementation." },
437ec681f3Smrg    { "surf",    DBG_SURFACE,              "IDirect3DSurface9 implementation." },
447ec681f3Smrg    { "swap",    DBG_SWAPCHAIN,            "IDirect3DSwapChain9(Ex) implementation." },
457ec681f3Smrg    { "tex",     DBG_TEXTURE,              "IDirect3DTexture9 implementation." },
467ec681f3Smrg    { "vbuf",    DBG_VERTEXBUFFER,         "IDirect3DVertexBuffer9 implementation." },
477ec681f3Smrg    { "vdecl",   DBG_VERTEXDECLARATION,    "IDirect3DVertexDeclaration9 implementation." },
487ec681f3Smrg    { "vs",      DBG_VERTEXSHADER,         "IDirect3DVertexShader9 implementation." },
497ec681f3Smrg    { "3dsurf",  DBG_VOLUME,               "IDirect3DVolume9 implementation." },
507ec681f3Smrg    { "3dtex",   DBG_VOLUMETEXTURE,        "IDirect3DVolumeTexture9 implementation." },
517ec681f3Smrg    { "shader",  DBG_SHADER,               "Shader token stream translator." },
527ec681f3Smrg    { "ff",      DBG_FF,                   "Fixed function emulation." },
537ec681f3Smrg    { "user",    DBG_USER,                 "User errors, both fixable and unfixable." },
547ec681f3Smrg    { "error",   DBG_ERROR,                "Driver errors, always visible." },
557ec681f3Smrg    { "warn",    DBG_WARN,                 "Driver warnings, always visible in debug builds." },
567ec681f3Smrg    { "tid",     DBG_TID,                  "Display thread-ids." },
577ec681f3Smrg    DEBUG_NAMED_VALUE_END
587ec681f3Smrg};
597ec681f3Smrg
607ec681f3Smrgvoid
617ec681f3Smrg_nine_debug_printf( unsigned long flag,
627ec681f3Smrg                    const char *func,
637ec681f3Smrg                    const char *fmt,
647ec681f3Smrg                    ... )
657ec681f3Smrg{
667ec681f3Smrg    static boolean first = TRUE;
677ec681f3Smrg    static unsigned long dbg_flags = DBG_ERROR | DBG_WARN;
687ec681f3Smrg    unsigned long tid = 0;
697ec681f3Smrg
707ec681f3Smrg    if (first) {
717ec681f3Smrg        first = FALSE;
727ec681f3Smrg        dbg_flags |= debug_get_flags_option("NINE_DEBUG", nine_debug_flags, 0);
737ec681f3Smrg    }
747ec681f3Smrg
757ec681f3Smrg#if defined(HAVE_PTHREAD)
767ec681f3Smrg    if (dbg_flags & DBG_TID)
777ec681f3Smrg        tid = (unsigned long)pthread_self();
787ec681f3Smrg#endif
797ec681f3Smrg
807ec681f3Smrg    if (dbg_flags & flag) {
817ec681f3Smrg        const char *f = func ? strrchr(func, '_') : NULL;
827ec681f3Smrg        va_list ap;
837ec681f3Smrg        /* inside a class this will print nine:tid:classinlowercase:func: while
847ec681f3Smrg         * outside a class (rarely used) it will just print nine:tid:func
857ec681f3Smrg         * the reason for lower case is simply to match the filenames, as it
867ec681f3Smrg         * will also strip off the "Nine" */
877ec681f3Smrg        if (f && strncmp(func, "Nine", 4) == 0) {
887ec681f3Smrg            char klass[96]; /* no class name is this long */
897ec681f3Smrg            char *ptr = klass;
907ec681f3Smrg            for (func += 4; func != f; ++func) { *ptr++ = tolower(*func); }
917ec681f3Smrg            *ptr = '\0';
927ec681f3Smrg            if (tid)
937ec681f3Smrg                _debug_printf("nine:0x%08lx:%s:%s: ", tid, klass, ++f);
947ec681f3Smrg            else
957ec681f3Smrg                _debug_printf("nine:%s:%s: ", klass, ++f);
967ec681f3Smrg        } else if (func) {
977ec681f3Smrg            if (tid)
987ec681f3Smrg                _debug_printf("nine:0x%08lx:%s ", tid, func);
997ec681f3Smrg            else
1007ec681f3Smrg                _debug_printf("nine:%s ", func);
1017ec681f3Smrg        }
1027ec681f3Smrg
1037ec681f3Smrg        va_start(ap, fmt);
1047ec681f3Smrg        _debug_vprintf(fmt, ap);
1057ec681f3Smrg        va_end(ap);
1067ec681f3Smrg    }
1077ec681f3Smrg}
1087ec681f3Smrg
1097ec681f3Smrgvoid
1107ec681f3Smrg_nine_stub( const char *file,
1117ec681f3Smrg            const char *func,
1127ec681f3Smrg            unsigned line )
1137ec681f3Smrg{
1147ec681f3Smrg    const char *r = strrchr(file, '/');
1157ec681f3Smrg    if (r == NULL) { r = strrchr(file, '\\'); }
1167ec681f3Smrg    _debug_printf("nine:%s:%d: %s STUB!\n", r ? ++r : file, line, func);
1177ec681f3Smrg}
118