Simplify the common combination of sprintf() and OutputDebugString() (PR #1031)

- Update LogOutput() and LogFileOutput().
- Add StrFormat() to produce std::string out of snprintf() & add StrFormat.cpp to projects.
- Add PTRDIFF_T_FMT in parallel to SIZE_T_FMT to StdAfx.h, for completeness.
- In Log.cpp, changed to get timestamp using posix functions.
- Removed TCHAR usage throughout - simply use char.
This commit is contained in:
Kelvin Lee 2022-02-14 08:37:05 +11:00 committed by GitHub
parent 83e56924f7
commit 9a3832084a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 222 additions and 133 deletions

View File

@ -294,6 +294,14 @@
RelativePath=".\source\StdAfx.h"
>
</File>
<File
RelativePath=".\source\StrFormat.cpp"
>
</File>
<File
RelativePath=".\source\StrFormat.h"
>
</File>
<File
RelativePath=".\source\Utilities.cpp"
>

View File

@ -111,6 +111,7 @@
<ClInclude Include="source\SSI263.h" />
<ClInclude Include="source\SSI263Phonemes.h" />
<ClInclude Include="source\StdAfx.h" />
<ClInclude Include="source\StrFormat.h" />
<ClInclude Include="source\SynchronousEventManager.h" />
<ClInclude Include="source\Tape.h" />
<ClInclude Include="source\Tfe\Bpf.h" />
@ -214,6 +215,7 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release v141_xp|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release NoDX|Win32'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="source\StrFormat.cpp" />
<ClCompile Include="source\SynchronousEventManager.cpp" />
<ClCompile Include="source\Tape.cpp" />
<ClCompile Include="source\Tfe\tfe.cpp">

View File

@ -247,6 +247,9 @@
<ClCompile Include="source\6522.cpp">
<Filter>Source Files\Emulator</Filter>
</ClCompile>
<ClCompile Include="source\StrFormat.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="source\CommonVICE\6510core.h">
@ -567,6 +570,9 @@
<ClInclude Include="source\6522.h">
<Filter>Source Files\Emulator</Filter>
</ClInclude>
<ClInclude Include="source\StrFormat.h">
<Filter>Source Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Image Include="resource\Applewin.bmp">

View File

@ -339,9 +339,7 @@ static void DebugHddEntrypoint(const USHORT PC)
if (!bOldPCAtC7xx /*&& PC != 0xc70a*/)
{
Count++;
char szDebug[100];
sprintf(szDebug, "HDD Entrypoint: $%04X\n", PC);
OutputDebugString(szDebug);
LogOutput("HDD Entrypoint: $%04X\n", PC);
}
bOldPCAtC7xx = true;

View File

@ -2,6 +2,8 @@
#include "Card.h"
#include "Common.h"
#include "StrFormat.h"
#include "Log.h"
void LogFileTimeUntilFirstKeyReadReset(void);
void LogFileTimeUntilFirstKeyRead(void);

View File

@ -3820,11 +3820,10 @@ Update_t CmdConfigSetDebugDir (int nArgs)
while ((iPathSeparator = sNewPath.find( PATH_SEPARATOR, iPrevSeparator )) != std::string::npos)
{
#if _DEBUG
char zDebug[128];
sprintf( zDebug, "Prev: %d\n", iPrevSeparator ); OutputDebugStringA( zDebug );
sprintf( zDebug, "Next: %d\n", iPathSeparator ); OutputDebugStringA( zDebug );
sprintf( zDebug, "%s\n", sNewPath.c_str() ); OutputDebugStringA( zDebug );
sprintf( zDebug, "%*s%s\n", iPathSeparator, "", "^"); OutputDebugStringA( zDebug );
LogOutput( "Prev: %" SIZE_T_FMT "\n", iPrevSeparator );
LogOutput( "Next: %" SIZE_T_FMT "\n", iPathSeparator );
LogOutput( "%s\n", sNewPath.c_str() );
LogOutput( "%*s%s\n", int(iPathSeparator), "", "^" );
#endif
std::string sSubDir = sNewPath.substr( iPrevSeparator, iPathSeparator - iPrevSeparator + 1 );
@ -3843,9 +3842,9 @@ Update_t CmdConfigSetDebugDir (int nArgs)
if (nLastSeperator != std::string::npos)
{
#if _DEBUG
sprintf( zDebug, "Last: %d\n", nLastSeperator ); OutputDebugStringA( zDebug );
sprintf( zDebug, "%s\n", g_sCurrentDir.c_str() ); OutputDebugStringA( zDebug );
sprintf( zDebug, "%*s%s\n", nLastSeperator, "", "^"); OutputDebugStringA( zDebug );
LogOutput( "Last: %" SIZE_T_FMT "\n", nLastSeperator );
LogOutput( "%s\n", g_sCurrentDir.c_str() );
LogOutput( "%*s%s\n", int(nLastSeperator), "", "^" );
#endif
std::string sCurrentDir = g_sCurrentDir.substr( 0, nLastSeperator + 1 ); // Path always has trailing slash so include it
g_sCurrentDir = sCurrentDir;
@ -4741,7 +4740,7 @@ Update_t CmdNTSC (int nArgs)
public:
static void update( const char *pPrefixText )
{
TCHAR text[ CONSOLE_WIDTH*2 ] = TEXT("");
char text[ CONSOLE_WIDTH*2 ] = "";
size_t len1 = strlen( pPrefixText );
size_t len2 = sPaletteFilePath.size();
@ -4752,11 +4751,9 @@ Update_t CmdNTSC (int nArgs)
ConsoleBufferPush( pPrefixText ); // TODO: Add a ": " separator
#if _DEBUG
sprintf( text, "Filename.length.1: %d\n", len1 );
OutputDebugString( text );
sprintf( text, "Filename.length.2: %d\n", len2 );
OutputDebugString( text );
OutputDebugString( sPaletteFilePath.c_str() );
LogOutput( "Filename.length.1: %d\n", len1 );
LogOutput( "Filename.length.2: %d\n", len2 );
OutputDebugStringA( sPaletteFilePath.c_str() );
#endif
// File path is too long
// TODO: Need to split very long path names
@ -4892,9 +4889,7 @@ Update_t CmdNTSC (int nArgs)
if (iDstX == 15)
iSrcX = 15;
#if 0 // _DEBUG
char text[ 128 ];
sprintf( text, "[ %X ] = [ %X ]\n", iDstX, iSrcX );
OutputDebugStringA( text );
LogOutput( "[ %X ] = [ %X ]\n", iDstX, iSrcX );
#endif
pTmp[ iDstX + 16*iPhase ] = pPhase0[ iSrcX ];
}
@ -6205,11 +6200,9 @@ int FindSourceLine( WORD nAddress )
// iSourceLine = g_aSourceDebug.find( nAddress );
#if 0 // _DEBUG
{
TCHAR sText[ CONSOLE_WIDTH ];
for (int i = 0; i < g_vSourceLines.size(); i++ )
{
wsprintf( sText, "%d: %s\n", i, g_vSourceLines[ i ] );
OutputDebugString( sText );
LogOutput( "%d: %s\n", i, g_vSourceLines[ i ] );
}
}
#endif
@ -6221,9 +6214,7 @@ int FindSourceLine( WORD nAddress )
iLine = iSource->second;
#if 0 // _DEBUG
TCHAR sText[ CONSOLE_WIDTH ];
wsprintf( sText, "%04X -> %d line\n", iAddress, iLine );
OutputDebugString( sText );
LogOutput( "%04X -> %d line\n", iAddress, iLine );
#endif
if (iAddress == nAddress)

View File

@ -911,10 +911,10 @@ void AssemblerHashOpcodes ()
nMnemonicHash = AssemblerHashMnemonic( pMnemonic );
g_aOpcodesHash[ iOpcode ] = nMnemonicHash;
#if DEBUG_ASSEMBLER
//OutputDebugString( "" );
char sText[ 128 ];
ConsolePrintFormat( sText, "%s : %08X ", pMnemonic, nMnemonicHash );
// CLC: 002B864
//OutputDebugString( "" );
char sText[ 128 ];
ConsolePrintFormat( sText, "%s : %08X ", pMnemonic, nMnemonicHash );
// CLC: 002B864
#endif
}
ConsoleUpdate();

View File

@ -183,9 +183,9 @@ bool DebuggerSetColor( const int iScheme, const int iColor, const COLORREF nColo
//===========================================================================
static void _SetupColorRamp(const int iPrimary, int & iColor_)
{
TCHAR sRamp[CONSOLE_WIDTH * 2] = TEXT("");
std::string strRamp;
#if DEBUG_COLOR_RAMP
TCHAR sText[CONSOLE_WIDTH];
char sText[CONSOLE_WIDTH];
#endif
bool bR = (iPrimary & 1) ? true : false;
@ -202,16 +202,15 @@ static void _SetupColorRamp(const int iPrimary, int & iColor_)
DWORD nColor = RGB(nR, nG, nB);
g_aColorPalette[iColor_] = nColor;
#if DEBUG_COLOR_RAMP
wsprintf(sText, TEXT("RGB(%3d,%3d,%3d), "), nR, nG, nB);
strcat(sRamp, sText);
int len = snprintf_s(sText, _TRUNCATE, "RGB(%3d,%3d,%3d), ", nR, nG, nB);
strRamp.append(sText, len);
#endif
iColor_++;
}
#if DEBUG_COLOR_RAMP
wsprintf(sText, TEXT(" // %d%d%d\n"), bB, bG, bR);
strcat(sRamp, sText);
OutputDebugString(sRamp);
sRamp[0] = 0;
int len = snprintf_s(sText, _TRUNCATE, " // %d%d%d\n", bB, bG, bR);
strRamp.append(sText, len);
OutputDebugStringA(strRamp.c_str());
#endif
}
#endif // _DEBUG

View File

@ -733,9 +733,7 @@ void DisasmCalcTopFromCurAddress(bool bUpdateTop)
// .20 Fixed: DisasmCalcTopFromCurAddress()
//if ((eMode >= AM_1) && (eMode <= AM_3))
#if 0 // _DEBUG
TCHAR sText[CONSOLE_WIDTH];
wsprintf(sText, "%04X : %d bytes\n", iAddress, nOpbytes);
OutputDebugString(sText);
LogOutput("%04X : %d bytes\n", iAddress, nOpbytes);
#endif
iAddress += nOpbytes;
}

View File

@ -1449,15 +1449,13 @@ void Disk2InterfaceCard::DumpTrackWOZ(FloppyDisk floppy) // pass a copy of m_flo
floppy.m_bitMask = 1 << remainder;
bool newLine = true;
TCHAR str[20];
while (1)
{
if (newLine)
{
newLine = false;
StringCbPrintf(str, sizeof(str), "%04X:", floppy.m_bitOffset & 0xffff);
OutputDebugString(str);
LogOutput("%04X:", floppy.m_bitOffset & 0xffff);
}
BYTE n = floppy.m_trackimage[floppy.m_byte];
@ -1483,9 +1481,8 @@ void Disk2InterfaceCard::DumpTrackWOZ(FloppyDisk floppy) // pass a copy of m_flo
nibbleCount++;
char syncBits = zeroCount <= 9 ? '0'+zeroCount : '+';
if (zeroCount == 0) StringCbPrintf(str, sizeof(str), " %02X", shiftReg);
else StringCbPrintf(str, sizeof(str), "(%c)%02X", syncBits, shiftReg);
OutputDebugString(str);
if (zeroCount == 0) LogOutput(" %02X", shiftReg);
else LogOutput("(%c)%02X", syncBits, shiftReg);
formatTrack.DecodeLatchNibbleRead(shiftReg);
@ -1509,15 +1506,13 @@ void Disk2InterfaceCard::DumpTrackWOZ(FloppyDisk floppy) // pass a copy of m_flo
if (zeroCount)
{
char syncBits = zeroCount <= 9 ? '0'+zeroCount : '+';
StringCbPrintf(str, sizeof(str), "(%c)", syncBits);
OutputDebugString(str);
LogOutput("(%c)", syncBits);
}
// Output any partial nibble
if (shiftReg)
{
StringCbPrintf(str, sizeof(str), "%02X/Partial Nibble", shiftReg);
OutputDebugString(str);
LogOutput("%02X/Partial Nibble", shiftReg);
}
// Output any remaining "read D5AAxx detected"

View File

@ -29,6 +29,9 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "StdAfx.h"
#include "Log.h"
#include <time.h>
FILE* g_fh = NULL;
#ifdef _MSC_VER
@ -42,6 +45,20 @@ FILE* g_fh = NULL;
//---------------------------------------------------------------------------
inline std::string GetTimeStamp()
{
time_t ltime;
time(&ltime);
#ifdef _MSC_VER
char ct[32];
ctime_s(ct, sizeof(ct), &ltime);
#else
char ctbuf[32];
const char* ct = ctime_r(&ltime, ctbuf);
#endif
return std::string(ct, 24);
}
void LogInit(void)
{
if (g_fh)
@ -55,10 +72,8 @@ void LogInit(void)
}
setvbuf(g_fh, NULL, _IONBF, 0); // No buffering (so implicit fflush after every fprintf)
CHAR aDateStr[80], aTimeStr[80];
GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, (LPTSTR)aDateStr, sizeof(aDateStr));
GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, (LPTSTR)aTimeStr, sizeof(aTimeStr));
fprintf(g_fh, "*** Logging started: %s %s\n", aDateStr, aTimeStr);
fprintf(g_fh, "*** Logging started: %s\n", GetTimeStamp().c_str());
}
void LogDone(void)
@ -73,31 +88,27 @@ void LogDone(void)
//---------------------------------------------------------------------------
void LogOutput(LPCTSTR format, ...)
void LogOutput(const char* format, ...)
{
TCHAR output[256];
va_list args;
va_start(args, format);
_vsntprintf(output, sizeof(output) - 1, format, args);
output[sizeof(output) - 1] = 0;
OutputDebugString(output);
OutputDebugStringA(StrFormat(format, args).c_str());
va_end(args);
}
//---------------------------------------------------------------------------
void LogFileOutput(LPCTSTR format, ...)
void LogFileOutput(const char* format, ...)
{
if (!g_fh)
return;
TCHAR output[256];
va_list args;
va_start(args, format);
_vsntprintf(output, sizeof(output) - 1, format, args);
output[sizeof(output) - 1] = 0;
fprintf(g_fh, "%s", output);
vfprintf(g_fh, format, args);
va_end(args);
}

View File

@ -1,5 +1,9 @@
#pragma once
#include <cstdio>
#include "StrFormat.h"
#ifndef _VC71 // __VA_ARGS__ not supported on MSVC++ .NET 7.x
#ifdef _DEBUG
#define LOG(format, ...) LogOutput(format, __VA_ARGS__)
@ -8,15 +12,10 @@
#endif
#endif
extern FILE* g_fh; // Filehandle for log file
extern FILE* g_fh; // File handle for log file
void LogInit(void);
void LogDone(void);
#ifdef _MSC_VER
void LogOutput(LPCTSTR format, ...);
void LogFileOutput(LPCTSTR format, ...);
#else
void LogOutput(LPCTSTR format, ...) __attribute__ ((format (printf, 1, 2)));
void LogFileOutput(LPCTSTR format, ...) __attribute__ ((format (printf, 1, 2)));
#endif
void LogOutput(const char* format, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
void LogFileOutput(const char* format, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);

View File

@ -1095,7 +1095,7 @@ void SetMemMode(DWORD uNewMemMode)
psz += sprintf(psz, "CX=%d " , SW_INTCXROM ? 1 : 0);
psz += sprintf(psz, "WRAM=%d " , SW_WRITERAM ? 1 : 0);
psz += sprintf(psz, "\n");
OutputDebugString(szStr);
OutputDebugStringA(szStr);
}
#endif
memmode = uNewMemMode;
@ -1978,9 +1978,7 @@ static void DebugFlip(WORD address, ULONG nExecutedCycles)
const double fFreq = CLK_6502 / (double)uCyclesBetweenFlips;
char szStr[100];
sprintf(szStr, "Cycles between flips = %d (%f Hz)\n", uCyclesBetweenFlips, fFreq);
OutputDebugString(szStr);
LogOutput("Cycles between flips = %d (%f Hz)\n", uCyclesBetweenFlips, fFreq);
}
#endif

View File

@ -325,7 +325,6 @@ void CMouseInterface::OnCommand()
{
#ifdef _DEBUG_SPURIOUS_IRQ
static UINT uSpuriousIrqCount = 0;
char szDbg[200];
BYTE byOldState = m_byState;
#endif
@ -353,7 +352,7 @@ void CMouseInterface::OnCommand()
m_byBuff[5] = m_byState; // button 0/1 interrupt status
m_byState &= ~STAT_MOVEMENT_SINCE_READMOUSE;
#ifdef _DEBUG_SPURIOUS_IRQ
sprintf(szDbg, "[MOUSE_READ] Old=%02X New=%02X\n", byOldState, m_byState); OutputDebugString(szDbg);
LogOutput("[MOUSE_READ] Old=%02X New=%02X\n", byOldState, m_byState);
#endif
break;
case MOUSE_SERV:
@ -363,11 +362,11 @@ void CMouseInterface::OnCommand()
if ((m_byMode & MODE_INT_ALL) && (m_byBuff[1] & MODE_INT_ALL) == 0)
{
uSpuriousIrqCount++;
sprintf(szDbg, "[MOUSE_SERV] 0x%04X Buff[1]=0x%02X, ***\n", uSpuriousIrqCount, m_byBuff[1]); OutputDebugString(szDbg);
LogOutput("[MOUSE_SERV] 0x%04X Buff[1]=0x%02X, ***\n", uSpuriousIrqCount, m_byBuff[1]);
}
else
{
sprintf(szDbg, "[MOUSE_SERV] ------ Buff[1]=0x%02X\n", m_byBuff[1]); OutputDebugString(szDbg);
LogOutput("[MOUSE_SERV] ------ Buff[1]=0x%02X\n", m_byBuff[1]);
}
#endif
CpuIrqDeassert(IS_MOUSE);
@ -472,7 +471,7 @@ void CMouseInterface::OnMouseEvent(bool bEventVBL)
m_byState |= byState;
CpuIrqAssert(IS_MOUSE);
#ifdef _DEBUG_SPURIOUS_IRQ
char szDbg[200]; sprintf(szDbg, "[MOUSE EVNT] 0x%02X Mode=0x%02X\n", m_byState, m_byMode); OutputDebugString(szDbg);
LogOutput("[MOUSE EVNT] 0x%02X Mode=0x%02X\n", m_byState, m_byMode);
#endif
}
}

View File

@ -2438,9 +2438,7 @@ static bool CheckVideoTables2( eApple2Type type, uint32_t mode )
_ASSERT(addr1 == addr2);
if (addr1 != addr2)
{
char str[80];
sprintf(str, "vpos=%04X, hpos=%02X, Video_adr=$%04X, NTSC_adr=$%04X\n", g_nVideoClockVert, g_nVideoClockHorz, addr1, addr2);
OutputDebugString(str);
LogOutput("vpos=%04X, hpos=%02X, Video_adr=$%04X, NTSC_adr=$%04X\n", g_nVideoClockVert, g_nVideoClockHorz, addr1, addr2);
return false;
}

View File

@ -1112,14 +1112,12 @@ void CSuperSerialCard::CheckCommEvent(DWORD dwEvtMask)
DWORD WINAPI CSuperSerialCard::CommThread(LPVOID lpParameter)
{
CSuperSerialCard* pSSC = (CSuperSerialCard*) lpParameter;
char szDbg[100];
BOOL bRes = SetCommMask(pSSC->m_hCommHandle, EV_RLSD | EV_DSR | EV_CTS | EV_TXEMPTY | EV_RXCHAR);
if (!bRes)
{
sprintf(szDbg, "SSC: CommThread(): SetCommMask() failed\n");
LogOutput("%s", szDbg);
LogFileOutput("%s", szDbg);
LogOutput("SSC: CommThread(): SetCommMask() failed\n");
LogFileOutput("SSC: CommThread(): SetCommMask() failed\n");
return -1;
}
@ -1145,11 +1143,15 @@ DWORD WINAPI CSuperSerialCard::CommThread(LPVOID lpParameter)
COMSTAT Stat;
ClearCommError(pSSC->m_hCommHandle, &dwErrors, &Stat);
if (dwErrors & CE_RXOVER)
sprintf(szDbg, "SSC: CommThread(): LastError=0x%08X, CommError=CE_RXOVER (0x%08X): InQueue=0x%08X\n", dwRet, dwErrors, Stat.cbInQue);
{
LogOutput("SSC: CommThread(): LastError=0x%08X, CommError=CE_RXOVER (0x%08X): InQueue=0x%08X\n", dwRet, dwErrors, Stat.cbInQue);
LogFileOutput("SSC: CommThread(): LastError=0x%08X, CommError=CE_RXOVER (0x%08X): InQueue=0x%08X\n", dwRet, dwErrors, Stat.cbInQue);
}
else
sprintf(szDbg, "SSC: CommThread(): LastError=0x%08X, CommError=Other (0x%08X): InQueue=0x%08X, OutQueue=0x%08X\n", dwRet, dwErrors, Stat.cbInQue, Stat.cbOutQue);
LogOutput("%s", szDbg);
LogFileOutput("%s", szDbg);
{
LogOutput("SSC: CommThread(): LastError=0x%08X, CommError=Other (0x%08X): InQueue=0x%08X, OutQueue=0x%08X\n", dwRet, dwErrors, Stat.cbInQue, Stat.cbOutQue);
LogFileOutput("SSC: CommThread(): LastError=0x%08X, CommError=Other (0x%08X): InQueue=0x%08X, OutQueue=0x%08X\n", dwRet, dwErrors, Stat.cbInQue, Stat.cbOutQue);
}
return -1;
}
@ -1175,7 +1177,7 @@ DWORD WINAPI CSuperSerialCard::CommThread(LPVOID lpParameter)
}
dwWaitResult -= WAIT_OBJECT_0; // Determine event # that signaled
//sprintf(szDbg, "CommThread: GotEvent1: %d\n", dwWaitResult); OutputDebugString(szDbg);
//LogOutput("CommThread: GotEvent1: %d\n", dwWaitResult);
if (dwWaitResult == (nNumEvents-1))
break; // Termination event

View File

@ -488,7 +488,6 @@ static int nDbgSpkrCnt = 0;
static ULONG Spkr_SubmitWaveBuffer_FullSpeed(short* pSpeakerBuffer, ULONG nNumSamples)
{
//char szDbg[200];
nDbgSpkrCnt++;
if(!SpeakerVoice.bActive)
@ -514,7 +513,7 @@ static ULONG Spkr_SubmitWaveBuffer_FullSpeed(short* pSpeakerBuffer, ULONG nNumSa
dwByteOffset = dwCurrentPlayCursor + (g_dwDSSpkrBufferSize/8)*3; // Ideal: 0.375 is between 0.25 & 0.50 full
dwByteOffset %= g_dwDSSpkrBufferSize;
//sprintf(szDbg, "[Submit_FS] PC=%08X, WC=%08X, Diff=%08X, Off=%08X, NS=%08X [REINIT]\n", dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor, dwByteOffset, nNumSamples); OutputDebugString(szDbg);
//LogOutput("[Submit_FS] PC=%08X, WC=%08X, Diff=%08X, Off=%08X, NS=%08X [REINIT]\n", dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor, dwByteOffset, nNumSamples);
}
else
{
@ -525,7 +524,7 @@ static ULONG Spkr_SubmitWaveBuffer_FullSpeed(short* pSpeakerBuffer, ULONG nNumSa
// |-----PxxxxxW-----|
if((dwByteOffset > dwCurrentPlayCursor) && (dwByteOffset < dwCurrentWriteCursor))
{
//sprintf(szDbg, "[Submit_FS] PC=%08X, WC=%08X, Diff=%08X, Off=%08X, NS=%08X xxx\n", dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor, dwByteOffset, nNumSamples); OutputDebugString(szDbg);
//LogOutput("[Submit_FS] PC=%08X, WC=%08X, Diff=%08X, Off=%08X, NS=%08X xxx\n", dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor, dwByteOffset, nNumSamples);
dwByteOffset = dwCurrentWriteCursor;
}
}
@ -534,7 +533,7 @@ static ULONG Spkr_SubmitWaveBuffer_FullSpeed(short* pSpeakerBuffer, ULONG nNumSa
// |xxW----------Pxxx|
if((dwByteOffset > dwCurrentPlayCursor) || (dwByteOffset < dwCurrentWriteCursor))
{
//sprintf(szDbg, "[Submit_FS] PC=%08X, WC=%08X, Diff=%08X, Off=%08X, NS=%08X XXX\n", dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor, dwByteOffset, nNumSamples); OutputDebugString(szDbg);
//LogOutput("[Submit_FS] PC=%08X, WC=%08X, Diff=%08X, Off=%08X, NS=%08X XXX\n", dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor, dwByteOffset, nNumSamples);
dwByteOffset = dwCurrentWriteCursor;
}
}
@ -590,7 +589,7 @@ static ULONG Spkr_SubmitWaveBuffer_FullSpeed(short* pSpeakerBuffer, ULONG nNumSa
if(nNumSamples)
{
//sprintf(szDbg, "[Submit_FS] C=%08X, PC=%08X, WC=%08X, Diff=%08X, Off=%08X, NS=%08X ***\n", nDbgSpkrCnt, dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor, dwByteOffset, nNumSamples); OutputDebugString(szDbg);
//LogOutput("[Submit_FS] C=%08X, PC=%08X, WC=%08X, Diff=%08X, Off=%08X, NS=%08X ***\n", nDbgSpkrCnt, dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor, dwByteOffset, nNumSamples);
if(nNumSamples*sizeof(short) <= dwDSLockedBufferSize0)
{
@ -624,7 +623,7 @@ static ULONG Spkr_SubmitWaveBuffer_FullSpeed(short* pSpeakerBuffer, ULONG nNumSa
if(nNumPadSamples)
{
//sprintf(szDbg, "[Submit_FS] C=%08X, PC=%08X, WC=%08X, Diff=%08X, Off=%08X, PS=%08X, Data=%04X\n", nDbgSpkrCnt, dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor, dwByteOffset, nNumPadSamples, g_nSpeakerData); OutputDebugString(szDbg);
//LogOutput("[Submit_FS] C=%08X, PC=%08X, WC=%08X, Diff=%08X, Off=%08X, PS=%08X, Data=%04X\n", nDbgSpkrCnt, dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor, dwByteOffset, nNumPadSamples, g_nSpeakerData);
dwBufferSize0 = dwDSLockedBufferSize0 - dwBufferSize0;
dwBufferSize1 = dwDSLockedBufferSize1 - dwBufferSize1;
@ -662,7 +661,6 @@ static ULONG Spkr_SubmitWaveBuffer_FullSpeed(short* pSpeakerBuffer, ULONG nNumSa
static ULONG Spkr_SubmitWaveBuffer(short* pSpeakerBuffer, ULONG nNumSamples)
{
//char szDbg[200];
nDbgSpkrCnt++;
if(!SpeakerVoice.bActive)
@ -702,7 +700,7 @@ static ULONG Spkr_SubmitWaveBuffer(short* pSpeakerBuffer, ULONG nNumSamples)
dwByteOffset = dwCurrentPlayCursor + (g_dwDSSpkrBufferSize/8)*3; // Ideal: 0.375 is between 0.25 & 0.50 full
dwByteOffset %= g_dwDSSpkrBufferSize;
//sprintf(szDbg, "[Submit] PC=%08X, WC=%08X, Diff=%08X, Off=%08X [REINIT]\n", dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor, dwByteOffset); OutputDebugString(szDbg);
//LogOutput("[Submit] PC=%08X, WC=%08X, Diff=%08X, Off=%08X [REINIT]\n", dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor, dwByteOffset);
}
else
{
@ -714,9 +712,8 @@ static ULONG Spkr_SubmitWaveBuffer(short* pSpeakerBuffer, ULONG nNumSamples)
if((dwByteOffset > dwCurrentPlayCursor) && (dwByteOffset < dwCurrentWriteCursor))
{
double fTicksSecs = (double)GetTickCount() / 1000.0;
//sprintf(szDbg, "%010.3f: [Submit] PC=%08X, WC=%08X, Diff=%08X, Off=%08X, NS=%08X xxx\n", fTicksSecs, dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor, dwByteOffset, nNumSamples);
//OutputDebugString(szDbg);
//if (g_fh) fprintf(g_fh, szDbg);
//LogOutput("%010.3f: [Submit] PC=%08X, WC=%08X, Diff=%08X, Off=%08X, NS=%08X xxx\n", fTicksSecs, dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor, dwByteOffset, nNumSamples);
//LogFileOutput("%010.3f: [Submit] PC=%08X, WC=%08X, Diff=%08X, Off=%08X, NS=%08X xxx\n", fTicksSecs, dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor, dwByteOffset, nNumSamples);
dwByteOffset = dwCurrentWriteCursor;
nNumSamplesError = 0;
@ -729,9 +726,8 @@ static ULONG Spkr_SubmitWaveBuffer(short* pSpeakerBuffer, ULONG nNumSamples)
if((dwByteOffset > dwCurrentPlayCursor) || (dwByteOffset < dwCurrentWriteCursor))
{
double fTicksSecs = (double)GetTickCount() / 1000.0;
//sprintf(szDbg, "%010.3f: [Submit] PC=%08X, WC=%08X, Diff=%08X, Off=%08X, NS=%08X XXX\n", fTicksSecs, dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor, dwByteOffset, nNumSamples);
//OutputDebugString(szDbg);
//if (g_fh) fprintf(g_fh, "%s", szDbg);
//LogOutput("%010.3f: [Submit] PC=%08X, WC=%08X, Diff=%08X, Off=%08X, NS=%08X XXX\n", fTicksSecs, dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor, dwByteOffset, nNumSamples);
//LogFileOutput("%010.3f: [Submit] PC=%08X, WC=%08X, Diff=%08X, Off=%08X, NS=%08X XXX\n", fTicksSecs, dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor, dwByteOffset, nNumSamples);
dwByteOffset = dwCurrentWriteCursor;
nNumSamplesError = 0;
@ -776,7 +772,7 @@ static ULONG Spkr_SubmitWaveBuffer(short* pSpeakerBuffer, ULONG nNumSamples)
if(nNumSamplesToUse)
{
//sprintf(szDbg, "[Submit] C=%08X, PC=%08X, WC=%08X, Diff=%08X, Off=%08X, NS=%08X +++\n", nDbgSpkrCnt, dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor, dwByteOffset, nNumSamplesToUse); OutputDebugString(szDbg);
//LogOutput("[Submit] C=%08X, PC=%08X, WC=%08X, Diff=%08X, Off=%08X, NS=%08X +++\n", nDbgSpkrCnt, dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor, dwByteOffset, nNumSamplesToUse);
hr = DSGetLock(SpeakerVoice.lpDSBvoice,
dwByteOffset, (DWORD)nNumSamplesToUse * sizeof(short),
@ -941,8 +937,7 @@ bool Spkr_DSInit()
hr = SpeakerVoice.lpDSBvoice->GetCurrentPosition(&dwCurrentPlayCursor, &dwCurrentWriteCursor);
LogFileOutput("Spkr_DSInit: GetCurrentPosition kludge (%08X)\n", hr);
char szDbg[100];
sprintf(szDbg, "[DSInit] PC=%08X, WC=%08X, Diff=%08X\n", dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor); OutputDebugString(szDbg);
LogOutput("[DSInit] PC=%08X, WC=%08X, Diff=%08X\n", dwCurrentPlayCursor, dwCurrentWriteCursor, dwCurrentWriteCursor-dwCurrentPlayCursor);
}
return true;

View File

@ -35,6 +35,7 @@ typedef UINT64 uint64_t;
#include <vector>
#include <memory>
#include <stdexcept>
#include <cstdarg>
// SM_CXPADDEDBORDER is not supported on 2000 & XP:
// http://msdn.microsoft.com/en-nz/library/windows/desktop/ms724385(v=vs.85).aspx
@ -43,3 +44,17 @@ typedef UINT64 uint64_t;
#endif
#define USE_SPEECH_API
#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || \
(!defined(_MSC_VER) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L))
#ifdef _WIN64
#define SIZE_T_FMT "llu"
#define PTRDIFF_T_FMT "lld"
#else
#define SIZE_T_FMT "lu"
#define PTRDIFF_T_FMT "ld"
#endif
#else
#define SIZE_T_FMT "zu"
#define PTRDIFF_T_FMT "td"
#endif

67
source/StrFormat.cpp Normal file
View File

@ -0,0 +1,67 @@
#include "StdAfx.h"
#include "StrFormat.h"
#include <cassert>
std::string StrFormat(const char* format, ...)
{
va_list va;
va_start(va, format);
std::string s = StrFormatV(format, va);
va_end(va);
return s;
}
std::string StrFormatV(const char* format, va_list va)
{
size_t const bufsz_base = 2048; // Big enough for most cases.
char buf[bufsz_base];
#if defined(_MSC_VER) && _MSC_VER < 1900
#pragma warning(push)
#pragma warning(disable: 4996) // warning _vsnprintf() is unsafe.
// VS2013 or before, _vsnprintf() cannot return required buffer size in case of overflow.
// MSVC seems fine not needing va_copy(), otherwise va_copy() may need to be called twice
// to be accurate. Not calling va_copy() here to keep things simpler.
int len = _vsnprintf(buf, sizeof(buf), format, va);
if (len >= 0 && size_t(len) <= sizeof(buf))
{
// _vsnprintf() can fill up the full buffer without nul termination.
return std::string(buf, size_t(len)); // No overflow.
}
else
{
// Overflow, need bigger buffer.
len = _vsnprintf(NULL, 0, format, va); // Get required buffer size.
std::string s(size_t(len), '\0');
len = _vsnprintf(&(*s.begin()), s.length() + 1, format, va);
assert(size_t(len) == s.length());
return s;
}
#pragma warning(pop)
#else
// Need to call va_copy() so va can be used potentially for a second time.
// glibc on Linux *certainly* needs this while MSVC is fine without it though.
va_list va_copied;
va_copy(va_copied, va);
int len = vsnprintf(buf, sizeof(buf), format, va_copied);
va_end(va_copied);
if (len < 0)
{
return std::string(); // Error.
}
else if (size_t(len) < sizeof(buf))
{
return std::string(buf, size_t(len)); // No overflow.
}
else
{
// Overflow, need bigger buffer.
std::string s(size_t(len), '\0');
len = vsnprintf(&(*s.begin()), s.length() + 1, format, va);
assert(size_t(len) == s.length());
return s;
}
#endif
}

13
source/StrFormat.h Normal file
View File

@ -0,0 +1,13 @@
#pragma once
#include <string>
#include <cstdarg>
#ifdef _MSC_VER
#define ATTRIBUTE_FORMAT_PRINTF(a, b)
#else
#define ATTRIBUTE_FORMAT_PRINTF(a, b) __attribute__((format(printf, a, b)))
#endif
std::string StrFormat(const char* format, ...) ATTRIBUTE_FORMAT_PRINTF(1, 2);
std::string StrFormatV(const char* format, va_list va);

View File

@ -194,8 +194,7 @@ void debug_output( const char *text, BYTE *what, int count )
int len1 = count;
int i;
sprintf(buffer, "\n%s: length = %u\n", text, len1);
OutputDebugString(buffer);
LogOutput("\n%s: length = %u\n", text, len1);
do {
p = buffer;
for (i=0; (i<8) && len1>0; len1--, i++) {
@ -203,7 +202,7 @@ void debug_output( const char *text, BYTE *what, int count )
p += 3;
}
*(p-1) = '\n'; *p = 0;
OutputDebugString(buffer);
OutputDebugStringA(buffer);
} while (len1>0);
}
#endif // #ifdef TFE_DEBUG_PKTDUMP

View File

@ -623,9 +623,7 @@ void Win32Frame::FrameDrawDiskStatus( HDC passdc )
#if _DEBUG && 0
if (nDOS33track != nDisk1Track)
{
char text[128];
sprintf( text, "\n\n\nWARNING: DOS33Track: %d (%02X) != nDisk1Track: %d (%02X)\n\n\n", nDOS33track, nDOS33track, nDisk1Track, nDisk1Track );
OutputDebugString( text );
LogOutput( "\n\n\nWARNING: DOS33Track: %d (%02X) != nDisk1Track: %d (%02X)\n\n\n", nDOS33track, nDOS33track, nDisk1Track, nDisk1Track );
}
#endif // _DEBUG
@ -1648,9 +1646,9 @@ LRESULT Win32Frame::WndProc(
case WM_SYSCOLORCHANGE:
#if DEBUG_DD_PALETTE
if( g_bIsFullScreen )
OutputDebugString( "WM_SYSCOLORCHANGE: Full Screen\n" );
OutputDebugStringA( "WM_SYSCOLORCHANGE: Full Screen\n" );
else
OutputDebugString( "WM_SYSCOLORCHANGE: Windowed\n" );
OutputDebugStringA( "WM_SYSCOLORCHANGE: Windowed\n" );
#endif
DeleteGdiObjects();
@ -1841,9 +1839,7 @@ void Win32Frame::ProcessButtonClick(int button, bool bFromButtonUI /*=false*/)
bool bAllowFadeIn = true;
#if DEBUG_DD_PALETTE
char _text[ 80 ];
sprintf( _text, "Button: F%d Full Screen: %d\n", button+1, g_bIsFullScreen );
OutputDebugString( _text );
LogOutput( "Button: F%d Full Screen: %d\n", button+1, g_bIsFullScreen );
#endif
switch (button) {
@ -2552,14 +2548,13 @@ void Win32Frame::FrameSetCursorPosByMousePos()
SetCursorPos(Point.x+iWindowX-VIEWPORTX, Point.y+iWindowY-VIEWPORTY);
#if defined(_DEBUG) && 0 // OutputDebugString() when cursor position changes since last time
static int OldX=0, OldY=0;
char szDbg[200];
int X=Point.x+iWindowX-VIEWPORTX;
int Y=Point.y+iWindowY-VIEWPORTY;
static int OldX = 0, OldY = 0;
int X = Point.x + iWindowX - VIEWPORTX;
int Y = Point.y + iWindowY - VIEWPORTY;
if (X != OldX || Y != OldY)
{
sprintf(szDbg, "[FrameSetCursorPosByMousePos] x,y=%d,%d (MaxX,Y=%d,%d)\n", X,Y, iMaxX,iMaxY); OutputDebugString(szDbg);
OldX=X; OldY=Y;
LogOutput("[FrameSetCursorPosByMousePos] x,y=%d,%d (MaxX,Y=%d,%d)\n", X, Y, iMaxX, iMaxY);
OldX = X; OldY = Y;
}
#endif
}
@ -2573,7 +2568,6 @@ void Win32Frame::FrameSetCursorPosByMousePos(int x, int y, int dx, int dy, bool
if (!GetCardMgr().IsMouseCardInstalled())
return;
// char szDbg[200];
if (!g_hFrameWindow || (g_bShowingCursor && bLeavingAppleScreen) || (!g_bShowingCursor && !bLeavingAppleScreen))
return;
@ -2598,11 +2592,11 @@ void Win32Frame::FrameSetCursorPosByMousePos(int x, int y, int dx, int dy, bool
POINT Point = {viewportx+2, viewporty+2}; // top-left
ClientToScreen(g_hFrameWindow, &Point);
SetCursorPos(Point.x+iWindowX-VIEWPORTX, Point.y+iWindowY-VIEWPORTY);
// sprintf(szDbg, "[MOUSE_LEAVING ] x=%d, y=%d (Scale: x,y=%f,%f; iX,iY=%d,%d)\n", iWindowX, iWindowY, fScaleX, fScaleY, iX, iY); OutputDebugString(szDbg);
//LogOutput("[MOUSE_LEAVING ] x=%d, y=%d (Scale: x,y=%f,%f; iX,iY=%d,%d)\n", iWindowX, iWindowY, fScaleX, fScaleY, iX, iY);
}
else // Mouse entering Apple screen area
{
// sprintf(szDbg, "[MOUSE_ENTERING] x=%d, y=%d\n", x, y); OutputDebugString(szDbg);
//LogOutput("[MOUSE_ENTERING] x=%d, y=%d\n", x, y);
if (!g_bIsFullScreen) // GH#464
{
x -= (viewportx+2-VIEWPORTX); if (x < 0) x = 0;