Skip to content

Commit 4a9f0e9

Browse files
committed
Move dbg to own file
1 parent 4b9b6af commit 4a9f0e9

9 files changed

Lines changed: 119 additions & 65 deletions

File tree

src/bots/base.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <twbl/hotreload.h>
77
#include <twbl/teeworlds/base/color.h>
8+
#include <twbl/teeworlds/base/dbg.h>
89
#include <twbl/teeworlds/base/system.h>
910
#include <twbl/teeworlds/base/vmath.h>
1011
#include <twbl/teeworlds/character.h>

src/test/mock_callback_ctx.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <twbl/callback_ctx.h>
2+
#include <twbl/teeworlds/base/dbg.h>
23
#include <twbl/teeworlds/base/system.h>
34

45
#include "mock_callback_ctx.h"

src/twbl/teeworlds/base/dbg.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <cstdarg>
2+
#include <twbl/teeworlds/base/system.h>
3+
4+
void dbg_break()
5+
{
6+
#ifdef __GNUC__
7+
__builtin_trap();
8+
#else
9+
abort();
10+
#endif
11+
}
12+
13+
void dbg_msg(const char *sys, const char *fmt, ...)
14+
{
15+
va_list args;
16+
char str[1024 * 4];
17+
char *msg;
18+
int i, len;
19+
20+
char timestr[80];
21+
str_timestamp_format(timestr, sizeof(timestr), FORMAT_SPACE);
22+
23+
str_format(str, sizeof(str), "[%s][%s]: ", timestr, sys);
24+
25+
len = str_length(str);
26+
msg = (char *)str + len;
27+
28+
va_start(args, fmt);
29+
#if defined(CONF_FAMILY_WINDOWS) && !defined(__GNUC__)
30+
_vsprintf_p(msg, sizeof(str) - len, fmt, args);
31+
#else
32+
vsnprintf(msg, sizeof(str) - len, fmt, args);
33+
#endif
34+
va_end(args);
35+
36+
puts(str);
37+
}
38+
39+
void dbg_assert_imp(const char *filename, int line, bool test, const char *msg)
40+
{
41+
if(!test)
42+
{
43+
fprintf(stderr, "assert: %s(%d): %s\n", filename, line, msg);
44+
dbg_break();
45+
}
46+
}
47+
48+
#define dbg_assert(test, msg) dbg_assert_imp(__FILE__, __LINE__, test, msg)
49+
void dbg_assert_imp(const char *filename, int line, bool test, const char *msg);

src/twbl/teeworlds/base/dbg.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef TWBL_SRC_TWBL_TEEWORLDS_BASE_DBG_H
2+
#define TWBL_SRC_TWBL_TEEWORLDS_BASE_DBG_H
3+
4+
#if __has_include(<base/dbg.h>)
5+
#include <base/dbg.h>
6+
#elif __has_include(<base/system.h>)
7+
#include <base/system.h>
8+
#else
9+
10+
#include <cstdio>
11+
#include <cstring>
12+
#include <ctime>
13+
14+
#include <twbl/teeworlds/base/detect.h>
15+
16+
#ifdef CONF_FAMILY_UNIX
17+
#include <sys/stat.h>
18+
#endif
19+
20+
#ifdef CONF_FAMILY_WINDOWS
21+
#define NOMINMAX
22+
#include <windows.h>
23+
24+
#include <string>
25+
#endif
26+
27+
#ifdef __GNUC__
28+
#define GNUC_ATTRIBUTE(x) __attribute__(x)
29+
#else
30+
#define GNUC_ATTRIBUTE(x)
31+
#endif
32+
33+
void dbg_msg(const char *sys, const char *fmt, ...)
34+
GNUC_ATTRIBUTE((format(printf, 2, 3)));
35+
36+
void dbg_break();
37+
void dbg_assert_imp(const char *filename, int line, bool test, const char *msg);
38+
39+
void dbg_assert(int test, const char *msg);
40+
#define dbg_assert(test, msg) dbg_assert_imp(__FILE__, __LINE__, test, msg)
41+
42+
#define dbg_assert(test, msg) dbg_assert_imp(__FILE__, __LINE__, test, msg)
43+
void dbg_assert_imp(const char *filename, int line, bool test, const char *msg);
44+
45+
#if defined(CONF_FAMILY_WINDOWS)
46+
std::wstring windows_utf8_to_wide(const char *str);
47+
#endif
48+
49+
#endif // __has_include(<base/system.h>)
50+
#endif // TWBL_SRC_TWBL_TEEWORLDS_BASE_DBG_H

src/twbl/teeworlds/base/system.cpp

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <cstdarg>
99
#include <cstdio>
1010
#include <cstring>
11+
#include <twbl/teeworlds/base/dbg.h>
1112

1213
#include "system.h"
1314

@@ -96,53 +97,6 @@ void str_truncate(char *dst, int dst_size, const char *src, int truncation_len)
9697
str_copy(dst, src, size);
9798
}
9899

99-
void dbg_break()
100-
{
101-
#ifdef __GNUC__
102-
__builtin_trap();
103-
#else
104-
abort();
105-
#endif
106-
}
107-
108-
void dbg_msg(const char *sys, const char *fmt, ...)
109-
{
110-
va_list args;
111-
char str[1024 * 4];
112-
char *msg;
113-
int i, len;
114-
115-
char timestr[80];
116-
str_timestamp_format(timestr, sizeof(timestr), FORMAT_SPACE);
117-
118-
str_format(str, sizeof(str), "[%s][%s]: ", timestr, sys);
119-
120-
len = str_length(str);
121-
msg = (char *)str + len;
122-
123-
va_start(args, fmt);
124-
#if defined(CONF_FAMILY_WINDOWS) && !defined(__GNUC__)
125-
_vsprintf_p(msg, sizeof(str) - len, fmt, args);
126-
#else
127-
vsnprintf(msg, sizeof(str) - len, fmt, args);
128-
#endif
129-
va_end(args);
130-
131-
puts(str);
132-
}
133-
134-
void dbg_assert_imp(const char *filename, int line, bool test, const char *msg)
135-
{
136-
if(!test)
137-
{
138-
fprintf(stderr, "assert: %s(%d): %s\n", filename, line, msg);
139-
dbg_break();
140-
}
141-
}
142-
143-
#define dbg_assert(test, msg) dbg_assert_imp(__FILE__, __LINE__, test, msg)
144-
void dbg_assert_imp(const char *filename, int line, bool test, const char *msg);
145-
146100
#if defined(CONF_FAMILY_WINDOWS)
147101
static inline time_t filetime_to_unixtime(LPFILETIME filetime)
148102
{

src/twbl/teeworlds/base/system.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,6 @@ void str_copy(char (&dst)[N], const char *src)
6262

6363
void str_truncate(char *dst, int dst_size, const char *src, int truncation_len);
6464

65-
void dbg_msg(const char *sys, const char *fmt, ...)
66-
GNUC_ATTRIBUTE((format(printf, 2, 3)));
67-
68-
void dbg_break();
69-
void dbg_assert_imp(const char *filename, int line, bool test, const char *msg);
70-
71-
void dbg_assert(int test, const char *msg);
72-
#define dbg_assert(test, msg) dbg_assert_imp(__FILE__, __LINE__, test, msg)
73-
74-
#define dbg_assert(test, msg) dbg_assert_imp(__FILE__, __LINE__, test, msg)
75-
void dbg_assert_imp(const char *filename, int line, bool test, const char *msg);
76-
7765
#if defined(CONF_FAMILY_WINDOWS)
7866
std::wstring windows_utf8_to_wide(const char *str);
7967
#endif

src/twbl/teeworlds/character.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#if __has_include(<game/server/entities/character.h>)
2+
#include <engine/shared/protocol.h>
3+
#include <game/server/entities/character.h>
4+
#include <game/server/player.h>
5+
#else
6+
7+
#include <twbl/teeworlds/base/dbg.h>
8+
#include <twbl/teeworlds/character.h>
9+
10+
int CCharacter::GetCid()
11+
{
12+
dbg_assert(m_ClientId != -1, "client id is not set");
13+
return m_ClientId;
14+
}
15+
#endif

src/twbl/teeworlds/character.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,7 @@ class CCharacter
2828
CCharacterCore m_Core;
2929

3030
int m_ClientId = -1;
31-
int GetCid()
32-
{
33-
dbg_assert(m_ClientId != -1, "client id is not set");
34-
return m_ClientId;
35-
}
36-
31+
int GetCid();
3732
vec2 GetPos() const { return m_Pos; }
3833
bool IsGrounded() const { return m_IsGrounded; }
3934

src/twbl/tracer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <twbl/teeworlds/base/dbg.h>
12
#include <twbl/teeworlds/base/system.h>
23

34
#include "tracer.h"

0 commit comments

Comments
 (0)