util/dprintf.c: Add debug printf

This commit is contained in:
Tau 2018-11-07 16:38:24 -05:00
parent d6eeec637c
commit 91a08b6abb
3 changed files with 87 additions and 0 deletions

63
util/dprintf.c Normal file
View File

@ -0,0 +1,63 @@
#ifndef NDEBUG
#include <windows.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include "util/dprintf.h"
static char dbg_buf[16384];
static size_t dbg_buf_pos;
void dprintf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
dprintfv(fmt, ap);
va_end(ap);
}
void dprintfv(const char *fmt, va_list ap)
{
dbg_buf_pos += vsnprintf_s(
dbg_buf + dbg_buf_pos,
sizeof(dbg_buf) - dbg_buf_pos,
sizeof(dbg_buf) - dbg_buf_pos - 1,
fmt,
ap);
if (dbg_buf_pos + 1 > sizeof(dbg_buf)) {
abort();
}
if (strchr(dbg_buf, '\n') == NULL) {
return;
}
OutputDebugStringA(dbg_buf);
dbg_buf_pos = 0;
dbg_buf[0] = '\0';
}
void dwprintf(const wchar_t *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
dwprintfv(fmt, ap);
va_end(ap);
}
void dwprintfv(const wchar_t *fmt, va_list ap)
{
wchar_t msg[512];
_vsnwprintf_s(msg, _countof(msg), _countof(msg) - 1, fmt, ap);
OutputDebugStringW(msg);
}
#endif

22
util/dprintf.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include <stdarg.h>
#include <stddef.h>
#ifdef __GNUC__
#define DPRINTF_CHK __attribute__(( format(printf, 1, 2) ))
#else
#define DPRINTF_CHK
#endif
#ifndef NDEBUG
void dprintf(const char *fmt, ...) DPRINTF_CHK;
void dprintfv(const char *fmt, va_list ap);
void dwprintf(const wchar_t *fmt, ...);
void dwprintfv(const wchar_t *fmt, va_list ap);
#else
#define dprintf(...)
#define dprintfv(fmt, ap)
#define dwprintf(...)
#define dwprintfv(fmt, ap)
#endif

View File

@ -7,5 +7,7 @@ util_lib = static_library(
capnhook.get_variable('hook_dep'), capnhook.get_variable('hook_dep'),
], ],
sources : [ sources : [
'dprintf.c',
'dprintf.h',
], ],
) )