Skip to content

Commit 7949a69

Browse files
committed
Add format args to dbg_assert
1 parent be0e694 commit 7949a69

File tree

9 files changed

+62
-81
lines changed

9 files changed

+62
-81
lines changed

src/base/system.cpp

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -127,24 +127,26 @@ bool dbg_assert_has_failed()
127127
return dbg_assert_failing.load(std::memory_order_acquire);
128128
}
129129

130-
void dbg_assert_imp(const char *filename, int line, bool test, const char *msg)
130+
void dbg_assert_imp(const char *filename, int line, const char *fmt, ...)
131131
{
132-
if(!test)
132+
const bool already_failing = dbg_assert_has_failed();
133+
dbg_assert_failing.store(true, std::memory_order_release);
134+
char msg[512];
135+
va_list args;
136+
va_start(args, fmt);
137+
str_format_v(msg, sizeof(msg), fmt, args);
138+
char error[512];
139+
str_format(error, sizeof(error), "%s(%d): %s", filename, line, msg);
140+
va_end(args);
141+
log_error("assert", "%s", error);
142+
if(!already_failing)
133143
{
134-
const bool already_failing = dbg_assert_has_failed();
135-
dbg_assert_failing.store(true, std::memory_order_release);
136-
char error[512];
137-
str_format(error, sizeof(error), "%s(%d): %s", filename, line, msg);
138-
dbg_msg("assert", "%s", error);
139-
if(!already_failing)
140-
{
141-
DBG_ASSERT_HANDLER handler = dbg_assert_handler;
142-
if(handler)
143-
handler(error);
144-
}
145-
log_global_logger_finish();
146-
dbg_break();
144+
DBG_ASSERT_HANDLER handler = dbg_assert_handler;
145+
if(handler)
146+
handler(error);
147147
}
148+
log_global_logger_finish();
149+
dbg_break();
148150
}
149151

150152
void dbg_break()
@@ -911,12 +913,7 @@ void sphore_init(SEMAPHORE *sem)
911913
char aBuf[64];
912914
str_format(aBuf, sizeof(aBuf), "/%d.%p", pid(), (void *)sem);
913915
*sem = sem_open(aBuf, O_CREAT | O_EXCL, S_IRWXU | S_IRWXG, 0);
914-
if(*sem == SEM_FAILED)
915-
{
916-
char aError[128];
917-
str_format(aError, sizeof(aError), "sem_open failure, errno=%d, name='%s'", errno, aBuf);
918-
dbg_assert(false, aError);
919-
}
916+
dbg_assert(*sem != SEM_FAILED, "sem_open failure, errno=%d, name='%s'", errno, aBuf);
920917
}
921918
void sphore_wait(SEMAPHORE *sem)
922919
{

src/base/system.h

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@
5555
#define MAYBE_UNUSED
5656
#endif
5757

58+
#ifdef __GNUC__
59+
#define GNUC_ATTRIBUTE(x) __attribute__(x)
60+
#else
61+
#define GNUC_ATTRIBUTE(x)
62+
#endif
63+
5864
/**
5965
* @defgroup Debug
6066
*
@@ -73,19 +79,31 @@
7379
*
7480
* @see dbg_break
7581
*/
76-
#define dbg_assert(test, msg) dbg_assert_imp(__FILE__, __LINE__, test, msg)
77-
void dbg_assert_imp(const char *filename, int line, bool test, const char *msg);
82+
#define dbg_assert(test, fmt, ...) \
83+
do \
84+
{ \
85+
if(!(test)) \
86+
{ \
87+
dbg_assert_imp(__FILE__, __LINE__, fmt, ##__VA_ARGS__); \
88+
} \
89+
} while(false)
90+
91+
/**
92+
* Use dbg_assert instead!
93+
*
94+
* @ingroup Debug
95+
*/
96+
#if defined(__cplusplus)
97+
[[noreturn]]
98+
#endif
99+
void
100+
dbg_assert_imp(const char *filename, int line, const char *fmt, ...)
101+
GNUC_ATTRIBUTE((format(printf, 3, 4)));
78102

79103
#ifdef __clang_analyzer__
80104
#include <cassert>
81105
#undef dbg_assert
82-
#define dbg_assert(test, msg) assert(test)
83-
#endif
84-
85-
#ifdef __GNUC__
86-
#define GNUC_ATTRIBUTE(x) __attribute__(x)
87-
#else
88-
#define GNUC_ATTRIBUTE(x)
106+
#define dbg_assert(test, fmt, ...) assert(test)
89107
#endif
90108

91109
/**

src/engine/client/graphics_threaded.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,12 +1893,7 @@ void *CGraphics_Threaded::AllocCommandBufferData(size_t AllocSize)
18931893
KickCommandBuffer();
18941894

18951895
pData = m_pCommandBuffer->AllocData(AllocSize);
1896-
if(pData == nullptr)
1897-
{
1898-
char aError[256];
1899-
str_format(aError, sizeof(aError), "graphics: failed to allocate data (size %" PRIzu ") for command buffer", AllocSize);
1900-
dbg_assert(false, aError);
1901-
}
1896+
dbg_assert(pData, "graphics: failed to allocate data (size %" PRIzu ") for command buffer", AllocSize);
19021897
}
19031898
return pData;
19041899
}

src/engine/client/graphics_threaded.h

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -890,19 +890,8 @@ class CGraphics_Threaded : public IEngineGraphics
890890
// kick command buffer and try again
891891
KickCommandBuffer();
892892

893-
if(!FailFunc())
894-
{
895-
char aError[256];
896-
str_format(aError, sizeof(aError), "graphics: failed to run fail handler for command '%s'", typeid(TName).name());
897-
dbg_assert(false, aError);
898-
}
899-
900-
if(!m_pCommandBuffer->AddCommandUnsafe(Cmd))
901-
{
902-
char aError[256];
903-
str_format(aError, sizeof(aError), "graphics: failed to add command '%s' to command buffer", typeid(TName).name());
904-
dbg_assert(false, aError);
905-
}
893+
dbg_assert(FailFunc(), "graphics: failed to run fail handler for command '%s'", typeid(TName).name());
894+
dbg_assert(m_pCommandBuffer->AddCommandUnsafe(Cmd), "graphics: failed to add command '%s' to command buffer", typeid(TName).name());
906895
}
907896

908897
void KickCommandBuffer();

src/engine/client/input.cpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,9 @@
3232
#undef KeyPress // Undo pollution from X11/Xlib.h included by SDL_syswm.h on Linux
3333
#endif
3434

35-
static void AssertKeyValid(int Key)
36-
{
37-
if(Key < KEY_FIRST || Key >= KEY_LAST)
38-
{
39-
char aError[32];
40-
str_format(aError, sizeof(aError), "Key invalid: %d", Key);
41-
dbg_assert(false, aError);
42-
}
43-
}
44-
4535
void CInput::AddKeyEvent(int Key, int Flags)
4636
{
47-
AssertKeyValid(Key);
37+
dbg_assert(Key >= KEY_FIRST && Key < KEY_LAST, "Key invalid: %d", Key);
4838
dbg_assert((Flags & (FLAG_PRESS | FLAG_RELEASE)) != 0 && (Flags & ~(FLAG_PRESS | FLAG_RELEASE)) == 0, "Flags invalid");
4939

5040
CEvent Event;
@@ -386,19 +376,19 @@ float CInput::GetUpdateTime() const
386376

387377
bool CInput::KeyIsPressed(int Key) const
388378
{
389-
AssertKeyValid(Key);
379+
dbg_assert(Key >= KEY_FIRST && Key < KEY_LAST, "Key invalid: %d", Key);
390380
return m_aCurrentKeyStates[Key];
391381
}
392382

393383
bool CInput::KeyPress(int Key) const
394384
{
395-
AssertKeyValid(Key);
385+
dbg_assert(Key >= KEY_FIRST && Key < KEY_LAST, "Key invalid: %d", Key);
396386
return m_aFrameKeyStates[Key];
397387
}
398388

399389
const char *CInput::KeyName(int Key) const
400390
{
401-
AssertKeyValid(Key);
391+
dbg_assert(Key >= KEY_FIRST && Key < KEY_LAST, "Key invalid: %d", Key);
402392
return g_aaKeyStrings[Key];
403393
}
404394

src/engine/client/sound.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,10 @@ CSample *CSound::AllocSample()
285285
return nullptr;
286286

287287
CSample *pSample = &m_aSamples[m_FirstFreeSampleIndex];
288-
if(pSample->m_pData != nullptr || pSample->m_NextFreeSampleIndex == SAMPLE_INDEX_USED)
289-
{
290-
char aError[128];
291-
str_format(aError, sizeof(aError), "Sample was not unloaded (index=%d, next=%d, duration=%f, data=%p)",
292-
pSample->m_Index, pSample->m_NextFreeSampleIndex, pSample->TotalTime(), pSample->m_pData);
293-
dbg_assert(false, aError);
294-
}
288+
dbg_assert(
289+
pSample->m_pData && pSample->m_NextFreeSampleIndex != SAMPLE_INDEX_USED,
290+
"Sample was not unloaded (index=%d, next=%d, duration=%f, data=%p)",
291+
pSample->m_Index, pSample->m_NextFreeSampleIndex, pSample->TotalTime(), pSample->m_pData);
295292
m_FirstFreeSampleIndex = pSample->m_NextFreeSampleIndex;
296293
pSample->m_NextFreeSampleIndex = SAMPLE_INDEX_USED;
297294
return pSample;

src/engine/shared/config.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,7 @@ void CConfigManager::SetReadOnly(const char *pScriptName, bool ReadOnly)
361361
return;
362362
}
363363
}
364-
char aBuf[IConsole::CMDLINE_LENGTH + 32];
365-
str_format(aBuf, sizeof(aBuf), "Invalid command for SetReadOnly: '%s'", pScriptName);
366-
dbg_assert(false, aBuf);
364+
dbg_assert(false, "Invalid command for SetReadOnly: '%s'", pScriptName);
367365
}
368366

369367
bool CConfigManager::Save()

src/engine/shared/datafile.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -781,12 +781,7 @@ void CDataFileWriter::Finish()
781781
DataInfo.m_CompressedSize = CompressedSize;
782782
free(DataInfo.m_pUncompressedData);
783783
DataInfo.m_pUncompressedData = nullptr;
784-
if(Result != Z_OK)
785-
{
786-
char aError[32];
787-
str_format(aError, sizeof(aError), "zlib compression error %d", Result);
788-
dbg_assert(false, aError);
789-
}
784+
dbg_assert(Result == Z_OK, "zlib compression error %d", Result);
790785
}
791786

792787
// Calculate total size of items

src/engine/shared/network_server.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,8 +732,10 @@ int CNetServer::Send(CNetChunk *pChunk)
732732
else
733733
{
734734
int Flags = 0;
735-
dbg_assert(pChunk->m_ClientId >= 0, "erroneous client id");
736-
dbg_assert(pChunk->m_ClientId < MaxClients(), "erroneous client id");
735+
dbg_assert(
736+
pChunk->m_ClientId >= 0 && pChunk->m_ClientId < MaxClients(),
737+
"erroneous client id %d",
738+
pChunk->m_ClientId);
737739

738740
if(pChunk->m_Flags & NETSENDFLAG_VITAL)
739741
Flags = NET_CHUNKFLAG_VITAL;

0 commit comments

Comments
 (0)