Moved DEBUGSTR to a separated file

This make more easy enable and disable debugging across
ansicon
This commit is contained in:
Luis Lavena 2010-12-10 12:21:13 -03:00
parent d239a1c717
commit a3a594001b
5 changed files with 105 additions and 85 deletions

82
ANSI.c
View File

@ -69,94 +69,14 @@
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <ImageHlp.h>
#include <tlhelp32.h>
#include "injdll.h"
#include "debugstr.h"
#define lenof(array) (sizeof(array)/sizeof(*(array)))
#define isdigit(c) ('0' <= (c) && (c) <= '9')
// ========== Auxiliary debug function
#define MYDEBUG 0 // no debugging
//#define MYDEBUG 1 // use OutputDebugString
//#define MYDEBUG 2 // use %temp%\ansicon.log
#if (MYDEBUG > 0)
#if (MYDEBUG > 1)
char tempfile[MAX_PATH];
#endif
void DEBUGSTR( LPTSTR szFormat, ... ) // sort of OutputDebugStringf
{
TCHAR szBuffer[1024], szEscape[1024];
va_list pArgList;
va_start( pArgList, szFormat );
_vsnwprintf( szBuffer, lenof(szBuffer), szFormat, pArgList );
va_end( pArgList );
szFormat = szBuffer;
if (*szFormat == '\\')
{
BOOL first = TRUE;
LPTSTR pos = szEscape;
while (*++szFormat != '\0' && pos < szEscape + lenof(szEscape) - 4)
{
if (*szFormat < 32)
{
*pos++ = '\\';
switch (*szFormat)
{
case '\b': *pos++ = 'b'; break;
case '\t': *pos++ = 't'; break;
case '\r': *pos++ = 'r'; break;
case '\n': *pos++ = 'n'; break;
case 27 : *pos++ = 'e'; break;
default:
pos += wprintf( pos, L"%.*o",
(szFormat[1] >= '0' && szFormat[1] <= '7') ? 3 : 1,
*szFormat );
}
}
else if (*szFormat == '"')
{
if (first)
first = FALSE;
else if (szFormat[1] == '\0')
;
else
*pos++ = '\\';
*pos++ = '"';
}
else
{
*pos++ = *szFormat;
}
}
*pos = '\0';
szFormat = szEscape;
}
#if (MYDEBUG > 1)
{
FILE* file = fopen( tempfile, "at" ); // _fmode might be binary
if (file != NULL)
{
fwprintf( file, L"%s\n", szFormat );
fclose( file );
}
}
#else
OutputDebugString( szFormat );
#endif
}
#else
#if defined(_MSC_VER) && _MSC_VER <= 1400
void DEBUGSTR() { }
#else
#define DEBUGSTR(...)
#endif
#endif
// ========== Global variables and constants
// Macro for adding pointers/DWORDs together without C arithmetic interfering

View File

@ -64,6 +64,7 @@
#include <ctype.h>
#include <io.h>
#include "injdll.h"
#include "debugstr.h"
#define lenof(array) (sizeof(array)/sizeof(*(array)))

81
debugstr.c Normal file
View File

@ -0,0 +1,81 @@
/*
debugstr.c - Auxiliary debug functionality.
*/
#ifndef UNICODE
# define UNICODE
#endif
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <ImageHlp.h>
#include "debugstr.h"
#define lenof(array) (sizeof(array)/sizeof(*(array)))
#if (MYDEBUG > 0)
void DEBUGSTR( LPTSTR szFormat, ... ) // sort of OutputDebugStringf
{
TCHAR szBuffer[1024], szEscape[1024];
va_list pArgList;
va_start( pArgList, szFormat );
_vsnwprintf( szBuffer, lenof(szBuffer), szFormat, pArgList );
va_end( pArgList );
szFormat = szBuffer;
if (*szFormat == '\\')
{
BOOL first = TRUE;
LPTSTR pos = szEscape;
while (*++szFormat != '\0' && pos < szEscape + lenof(szEscape) - 4)
{
if (*szFormat < 32)
{
*pos++ = '\\';
switch (*szFormat)
{
case '\b': *pos++ = 'b'; break;
case '\t': *pos++ = 't'; break;
case '\r': *pos++ = 'r'; break;
case '\n': *pos++ = 'n'; break;
case 27 : *pos++ = 'e'; break;
default:
pos += wprintf( pos, L"%.*o",
(szFormat[1] >= '0' && szFormat[1] <= '7') ? 3 : 1,
*szFormat );
}
}
else if (*szFormat == '"')
{
if (first)
first = FALSE;
else if (szFormat[1] == '\0')
;
else
*pos++ = '\\';
*pos++ = '"';
}
else
{
*pos++ = *szFormat;
}
}
*pos = '\0';
szFormat = szEscape;
}
#if (MYDEBUG > 1)
{
FILE* file = fopen( tempfile, "at" ); // _fmode might be binary
if (file != NULL)
{
fwprintf( file, L"%s\n", szFormat );
fclose( file );
}
}
#else
OutputDebugString( szFormat );
#endif
}
#endif

18
debugstr.h Normal file
View File

@ -0,0 +1,18 @@
// ========== Auxiliary debug function
#define MYDEBUG 0 // no debugging
//#define MYDEBUG 1 // use OutputDebugString
//#define MYDEBUG 2 // use %temp%\ansicon.log
#if (MYDEBUG > 0)
# if (MYDEBUG > 1)
char tempfile[MAX_PATH];
# endif
void DEBUGSTR( LPTSTR szFormat, ... );
#else
# if defined(_MSC_VER) && _MSC_VER <= 1400
void DEBUGSTR() { }
# else
# define DEBUGSTR(...)
# endif
#endif

View File

@ -32,19 +32,19 @@ ansicon64: x64 x64/ansicon.exe x64/ANSI64.dll x64/ANSI32.dll x64/ANSI-LLW.exe
x86:
mkdir x86
x86/ansicon.exe: x86/ansicon.o x86/proctype.o x86/injdll32.o x86/ansiconv.o
x86/ansicon.exe: x86/ansicon.o x86/debugstr.o x86/proctype.o x86/injdll32.o x86/ansiconv.o
$(CC) -m32 $+ -s -o $@
x86/ANSI32.dll: x86/ANSI.o x86/proctype.o x86/injdll32.o x86/ansiv.o
x86/ANSI32.dll: x86/ANSI.o x86/debugstr.o x86/proctype.o x86/injdll32.o x86/ansiv.o
$(CC) -m32 $+ -s -o $@ -mdll -Wl,-shared
x64:
mkdir x64
x64/ansicon.exe: x64/ansicon.o x64/proctype.o x64/injdll64.o x64/injdll32.o x64/ansiconv.o
x64/ansicon.exe: x64/ansicon.o x64/debugstr.o x64/proctype.o x64/injdll64.o x64/injdll32.o x64/ansiconv.o
$(CC) -m64 $+ -s -o $@
x64/ANSI64.dll: x64/ANSI.o x64/proctype.o x64/injdll64.o x64/injdll32.o x64/ansiv.o
x64/ANSI64.dll: x64/ANSI.o x64/debugstr.o x64/proctype.o x64/injdll64.o x64/injdll32.o x64/ansiv.o
$(CC) -m64 $+ -s -o $@ -mdll -Wl,-shared
x64/ANSI32.dll: x86/ANSI32.dll