port: EterBase/Debug.cpp log functions verbatim (log.txt, syserr.txt)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
f9b1044a4a
commit
e0cdcee2e6
+193
-29
@@ -1,11 +1,12 @@
|
||||
// Platform skeleton for EterBase/Debug.h (40250 EterBase/Debug.cpp), generated by platform_stub.py.
|
||||
// Every MT_PLATFORM_STUB() body is unimplemented: replace it with the platform implementation. Implemented
|
||||
// bodies follow the 40250 non-_DEBUG build (batch 2D: Trace*, TraceError to stderr).
|
||||
// Platform implementation of EterBase/Debug.h (40250 EterBase/Debug.cpp), the non-_DEBUG build: Log*/Trace*
|
||||
// reach log.txt only after OpenLogFile(true), TraceError goes to stderr (syserr.txt after OpenLogFile). The
|
||||
// MT_PLATFORM_STUB() bodies left are the _DEBUG console and the Debug.h declarations 40250 never defines.
|
||||
#include "EterBase/StdAfx.h"
|
||||
#include "EterBase/Debug.h"
|
||||
|
||||
#include "../PlatformStub.h"
|
||||
#include "EterBase/Timer.h"
|
||||
#include "EterBase/Singleton.h"
|
||||
#include "LogBox.h"
|
||||
#include "TraceErrorObserver.h"
|
||||
|
||||
@@ -26,51 +27,166 @@ void SetTraceErrorObserver(TraceErrorObserver observer)
|
||||
g_trace_error_observer = observer;
|
||||
}
|
||||
|
||||
auto SetLogLevel(UINT) -> void
|
||||
// 40250 Debug.cpp:11
|
||||
static int isLogFile = false;
|
||||
HWND g_PopupHwnd = NULL;
|
||||
|
||||
// 40250 Debug.cpp:14
|
||||
class CLogFile : public CSingleton<CLogFile>
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
public:
|
||||
CLogFile() : m_fp(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~CLogFile()
|
||||
{
|
||||
if (m_fp)
|
||||
fclose(m_fp);
|
||||
|
||||
m_fp = NULL;
|
||||
}
|
||||
|
||||
void Initialize()
|
||||
{
|
||||
m_fp = fopen("log.txt", "w");
|
||||
}
|
||||
|
||||
void Write(const char * c_pszMsg)
|
||||
{
|
||||
if (!m_fp)
|
||||
return;
|
||||
|
||||
time_t ct = time(0);
|
||||
struct tm ctm = *localtime(&ct);
|
||||
|
||||
fprintf(m_fp, "%02d%02d %02d:%02d:%05d :: %s",
|
||||
ctm.tm_mon + 1,
|
||||
ctm.tm_mday,
|
||||
ctm.tm_hour,
|
||||
ctm.tm_min,
|
||||
ELTimer_GetMSec() % 60000,
|
||||
c_pszMsg);
|
||||
|
||||
fflush(m_fp);
|
||||
}
|
||||
|
||||
protected:
|
||||
FILE * m_fp;
|
||||
};
|
||||
|
||||
static CLogFile gs_logfile;
|
||||
|
||||
static UINT gs_uLevel=0;
|
||||
|
||||
// 40250 Debug.cpp:61. The Log*/Trace* bodies are the non-_DEBUG build: they only reach log.txt, which
|
||||
// OpenLogFile(true) opens (40250 does that in _DEBUG builds only).
|
||||
void SetLogLevel(UINT uLevel)
|
||||
{
|
||||
gs_uLevel=uLevel;
|
||||
}
|
||||
|
||||
auto Log(UINT, const char *) -> void
|
||||
void Log(UINT uLevel, const char* c_szMsg)
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
if (uLevel>=gs_uLevel)
|
||||
Trace(c_szMsg);
|
||||
}
|
||||
|
||||
auto Logn(UINT, const char *) -> void
|
||||
void Logn(UINT uLevel, const char* c_szMsg)
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
if (uLevel>=gs_uLevel)
|
||||
Tracen(c_szMsg);
|
||||
}
|
||||
|
||||
auto Logf(UINT, const char *, ...) -> void
|
||||
void Logf(UINT uLevel, const char* c_szFormat, ...)
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
if (uLevel<gs_uLevel)
|
||||
return;
|
||||
|
||||
char szBuf[DEBUG_STRING_MAX_LEN+1];
|
||||
|
||||
va_list args;
|
||||
va_start(args, c_szFormat);
|
||||
_vsnprintf(szBuf, sizeof(szBuf), c_szFormat, args);
|
||||
va_end(args);
|
||||
szBuf[DEBUG_STRING_MAX_LEN] = '\0'; // PORT: _vsnprintf does not terminate on truncation
|
||||
|
||||
if (isLogFile)
|
||||
LogFile(szBuf);
|
||||
}
|
||||
|
||||
auto Lognf(UINT, const char *, ...) -> void
|
||||
void Lognf(UINT uLevel, const char* c_szFormat, ...)
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
if (uLevel<gs_uLevel)
|
||||
return;
|
||||
|
||||
va_list args;
|
||||
va_start(args, c_szFormat);
|
||||
|
||||
char szBuf[DEBUG_STRING_MAX_LEN+2];
|
||||
int len = _vsnprintf(szBuf, sizeof(szBuf)-1, c_szFormat, args);
|
||||
|
||||
if (len > 0)
|
||||
{
|
||||
szBuf[len] = '\n';
|
||||
szBuf[len + 1] = '\0';
|
||||
}
|
||||
va_end(args);
|
||||
|
||||
if (isLogFile)
|
||||
LogFile(szBuf);
|
||||
}
|
||||
|
||||
void Trace(const char *)
|
||||
|
||||
void Trace(const char * c_szMsg)
|
||||
{
|
||||
// Non-_DEBUG 40250: written only to the log file (OpenLogFile, not implemented yet).
|
||||
if (isLogFile)
|
||||
LogFile(c_szMsg);
|
||||
}
|
||||
|
||||
void Tracen(const char *)
|
||||
void Tracen(const char* c_szMsg)
|
||||
{
|
||||
// Non-_DEBUG 40250: written only to the log file (OpenLogFile, not implemented yet).
|
||||
if (isLogFile)
|
||||
{
|
||||
LogFile(c_szMsg);
|
||||
LogFile("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void Tracenf(const char *, ...)
|
||||
void Tracenf(const char* c_szFormat, ...)
|
||||
{
|
||||
// Non-_DEBUG 40250: written only to the log file (OpenLogFile, not implemented yet).
|
||||
va_list args;
|
||||
va_start(args, c_szFormat);
|
||||
|
||||
char szBuf[DEBUG_STRING_MAX_LEN+2];
|
||||
int len = _vsnprintf(szBuf, sizeof(szBuf)-1, c_szFormat, args);
|
||||
|
||||
if (len > 0)
|
||||
{
|
||||
szBuf[len] = '\n';
|
||||
szBuf[len + 1] = '\0';
|
||||
}
|
||||
va_end(args);
|
||||
|
||||
if (isLogFile)
|
||||
LogFile(szBuf);
|
||||
}
|
||||
|
||||
void Tracef(const char *, ...)
|
||||
void Tracef(const char* c_szFormat, ...)
|
||||
{
|
||||
// Non-_DEBUG 40250: written only to the log file (OpenLogFile, not implemented yet).
|
||||
char szBuf[DEBUG_STRING_MAX_LEN+1];
|
||||
|
||||
va_list args;
|
||||
va_start(args, c_szFormat);
|
||||
_vsnprintf(szBuf, sizeof(szBuf), c_szFormat, args);
|
||||
va_end(args);
|
||||
szBuf[DEBUG_STRING_MAX_LEN] = '\0'; // PORT: _vsnprintf does not terminate on truncation
|
||||
|
||||
if (isLogFile)
|
||||
LogFile(szBuf);
|
||||
}
|
||||
|
||||
// 40250 Debug.cpp:199
|
||||
void TraceError(const char* c_szFormat, ...)
|
||||
{
|
||||
#ifndef _DISTRIBUTE
|
||||
@@ -104,12 +220,41 @@ void TraceError(const char* c_szFormat, ...)
|
||||
fflush(stderr);
|
||||
if (g_trace_error_observer)
|
||||
g_trace_error_observer(szBuf + 8);
|
||||
|
||||
if (isLogFile)
|
||||
LogFile(szBuf);
|
||||
#endif
|
||||
}
|
||||
|
||||
auto TraceErrorWithoutEnter(const char *, ...) -> void
|
||||
// 40250 Debug.cpp:239. It prints from szBuf + 8 although nothing is prepended (TraceError's "SYSERR: "
|
||||
// offset); there is no caller in 40250.
|
||||
void TraceErrorWithoutEnter(const char* c_szFormat, ...)
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
#ifndef _DISTRIBUTE
|
||||
|
||||
char szBuf[DEBUG_STRING_MAX_LEN];
|
||||
|
||||
va_list args;
|
||||
va_start(args, c_szFormat);
|
||||
_vsnprintf(szBuf, sizeof(szBuf), c_szFormat, args);
|
||||
va_end(args);
|
||||
szBuf[DEBUG_STRING_MAX_LEN - 1] = '\0'; // PORT: _vsnprintf does not terminate on truncation
|
||||
|
||||
time_t ct = time(0);
|
||||
struct tm ctm = *localtime(&ct);
|
||||
|
||||
fprintf(stderr, "%02d%02d %02d:%02d:%05d :: %s",
|
||||
ctm.tm_mon + 1,
|
||||
ctm.tm_mday,
|
||||
ctm.tm_hour,
|
||||
ctm.tm_min,
|
||||
ELTimer_GetMSec() % 60000,
|
||||
szBuf + 8);
|
||||
fflush(stderr);
|
||||
|
||||
if (isLogFile)
|
||||
LogFile(szBuf);
|
||||
#endif
|
||||
}
|
||||
|
||||
namespace
|
||||
@@ -160,16 +305,26 @@ void LogBoxf(const char * c_szFormat, ...)
|
||||
LogBox(szBuf);
|
||||
}
|
||||
|
||||
auto LogFile(const char *) -> void
|
||||
// 40250 Debug.cpp:292
|
||||
void LogFile(const char * c_szMsg)
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
CLogFile::Instance().Write(c_szMsg);
|
||||
}
|
||||
|
||||
auto LogFilef(const char *, ...) -> void
|
||||
void LogFilef(const char * c_szMessage, ...)
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
va_list args;
|
||||
va_start(args, c_szMessage);
|
||||
char szBuf[DEBUG_STRING_MAX_LEN+1];
|
||||
_vsnprintf(szBuf, sizeof(szBuf), c_szMessage, args);
|
||||
va_end(args); // PORT: 40250 leaks the va_list
|
||||
szBuf[DEBUG_STRING_MAX_LEN] = '\0'; // PORT: _vsnprintf does not terminate on truncation
|
||||
|
||||
CLogFile::Instance().Write(szBuf);
|
||||
}
|
||||
|
||||
// 40250 Debug.cpp:320: AllocConsole + CONOUT$/CONIN$, called by the _DEBUG WinMain only. The host
|
||||
// process already has its terminal (or logcat).
|
||||
auto OpenConsoleWindow() -> void
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
@@ -185,9 +340,18 @@ auto SetupLog() -> void
|
||||
MT_PLATFORM_STUB();
|
||||
}
|
||||
|
||||
auto OpenLogFile(bool) -> void
|
||||
// 40250 Debug.cpp:307
|
||||
void OpenLogFile(bool bUseLogFIle)
|
||||
{
|
||||
MT_PLATFORM_STUB();
|
||||
#ifndef _DISTRIBUTE
|
||||
freopen("syserr.txt", "w", stderr);
|
||||
|
||||
if (bUseLogFIle)
|
||||
{
|
||||
isLogFile = true;
|
||||
CLogFile::Instance().Initialize();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
auto CloseLogFile() -> void
|
||||
|
||||
@@ -214,6 +214,11 @@ if(BUILD_TESTING AND CMAKE_SYSTEM_NAME STREQUAL CMAKE_HOST_SYSTEM_NAME)
|
||||
target_link_libraries(port_screenshot_test PRIVATE port_platform)
|
||||
add_test(NAME port.screenshot COMMAND $<TARGET_FILE:port_screenshot_test>)
|
||||
|
||||
# EterBase/Debug.cpp: syserr.txt / log.txt (redirects its own stderr).
|
||||
add_executable(port_debug_log_test ${PROJECT_SOURCE_DIR}/tests/port/port_debug_log_test.cpp)
|
||||
target_link_libraries(port_debug_log_test PRIVATE port_platform)
|
||||
add_test(NAME port.debug_log COMMAND $<TARGET_FILE:port_debug_log_test>)
|
||||
|
||||
# 批次 2V1: Packet.h struct sizes against the classic wire layouts.
|
||||
add_executable(port_packet_test ${PROJECT_SOURCE_DIR}/tests/port/port_packet_test.cpp
|
||||
${PROJECT_SOURCE_DIR}/tests/port/port_packet_test_wire.cpp)
|
||||
|
||||
Reference in New Issue
Block a user