implemented optional log output to Visual Studio Output window if PA_ENABLE_MSVC_DEBUG_OUTPUT is defined, this simplifies debugging of external process if Visual Studio is attached to process

This commit is contained in:
dmitrykos 2010-03-10 21:47:01 +00:00
commit e4c126aab1

View file

@ -53,17 +53,21 @@
@todo Consider reentrancy and possibly corrupted strdump buffer.
*/
#include <stdio.h>
#include <stdarg.h>
#include "pa_debugprint.h"
// for OutputDebugStringA
#if defined(_MSC_VER) && defined(PA_ENABLE_MSVC_DEBUG_OUTPUT)
#define WIN32_LEAN_AND_MEAN // exclude rare headers
#include "windows.h"
#endif
// User callback
static PaUtilLogCallback userCB = NULL;
static PaUtilLogCallback userCB=0;
// Sets user callback
void PaUtil_SetDebugPrintFunction(PaUtilLogCallback cb)
{
userCB = cb;
@ -72,41 +76,51 @@ void PaUtil_SetDebugPrintFunction(PaUtilLogCallback cb)
/*
If your platform doesnt have vsnprintf, you are stuck with a
VERY dangerous alternative, vsprintf (with no n)
*/
#if _MSC_VER
/* Some Windows Mobile SDKs don't define vsnprintf but all define _vsnprintf (hopefully).
According to MSDN "vsnprintf is identical to _vsnprintf". So we use _vsnprintf with MSC.
*/
#define VSNPRINTF _vsnprintf
#if _MSC_VER
/* Some Windows Mobile SDKs don't define vsnprintf but all define _vsnprintf (hopefully).
According to MSDN "vsnprintf is identical to _vsnprintf". So we use _vsnprintf with MSC.
*/
#define VSNPRINTF _vsnprintf
#else
#define VSNPRINTF vsnprintf
#define VSNPRINTF vsnprintf
#endif
#define SIZEDUMP 1024
#define PA_LOG_BUF_SIZE 2048
void PaUtil_DebugPrint( const char *format, ... )
{
// Optional logging into Output console of Visual Studio
#if defined(_MSC_VER) && defined(PA_ENABLE_MSVC_DEBUG_OUTPUT)
{
char buf[PA_LOG_BUF_SIZE];
va_list ap;
va_start(ap, format);
VSNPRINTF(buf, sizeof(buf), format, ap);
buf[sizeof(buf)-1] = 0;
OutputDebugStringA(buf);
va_end(ap);
}
#endif
if (userCB)
// Output to User-Callback
if (userCB != NULL)
{
char strdump[SIZEDUMP];
char strdump[PA_LOG_BUF_SIZE];
va_list ap;
va_start( ap, format );
VSNPRINTF( strdump, SIZEDUMP, format, ap );
strdump[SIZEDUMP-1] = 0;
va_start(ap, format);
VSNPRINTF(strdump, sizeof(strdump), format, ap);
strdump[sizeof(strdump)-1] = 0;
userCB(strdump);
va_end( ap );
va_end(ap);
}
else
// Standard output to stderr
{
va_list ap;
va_start( ap, format );
vfprintf( stderr, format, ap );
va_end( ap );
fflush( stderr );
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
fflush(stderr);
}
}