Skip to content

Commit a0ecd32

Browse files
committed
Dry up CScriptDebugging a little bit
1 parent f56ea70 commit a0ecd32

File tree

3 files changed

+274
-548
lines changed

3 files changed

+274
-548
lines changed

Client/mods/deathmatch/logic/CScriptDebugging.cpp

Lines changed: 0 additions & 277 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
#include <StdInc.h>
1818

19-
#define MAX_STRING_LENGTH 2048
20-
2119
FILE* CScriptDebugging::m_pLogFile;
2220

2321
CScriptDebugging::CScriptDebugging ( CLuaManager* pLuaManager )
@@ -51,165 +49,12 @@ CScriptDebugging::~CScriptDebugging ( void )
5149
}
5250
}
5351

54-
55-
void CScriptDebugging::LogCustom ( lua_State* luaVM, unsigned char ucRed, unsigned char ucGreen, unsigned char ucBlue, const char* szFormat, ... )
56-
{
57-
assert ( szFormat );
58-
59-
// Compose the formatted message
60-
char szBuffer [MAX_STRING_LENGTH];
61-
va_list marker;
62-
va_start ( marker, szFormat );
63-
VSNPRINTF ( szBuffer, MAX_STRING_LENGTH, szFormat, marker );
64-
va_end ( marker );
65-
66-
LogString ( "", GetLuaDebugInfo( luaVM ), szBuffer, 0, ucRed, ucGreen, ucBlue );
67-
}
68-
69-
void CScriptDebugging::LogInformation ( lua_State* luaVM, const char* szFormat, ... )
70-
{
71-
assert ( szFormat );
72-
73-
// Compose the formatted message
74-
char szBuffer [MAX_STRING_LENGTH];
75-
va_list marker;
76-
va_start ( marker, szFormat );
77-
VSNPRINTF ( szBuffer, MAX_STRING_LENGTH, szFormat, marker );
78-
va_end ( marker );
79-
80-
// Log it
81-
LogString ( "INFO: ", GetLuaDebugInfo( luaVM ), szBuffer, 3 );
82-
}
83-
84-
85-
void CScriptDebugging::LogWarning ( lua_State* luaVM, const char* szFormat, ... )
86-
{
87-
assert ( szFormat );
88-
89-
// Compose the formatted message
90-
char szBuffer [MAX_STRING_LENGTH];
91-
va_list marker;
92-
va_start ( marker, szFormat );
93-
VSNPRINTF ( szBuffer, MAX_STRING_LENGTH, szFormat, marker );
94-
va_end ( marker );
95-
96-
// Log it
97-
LogString ( "WARNING: ", GetLuaDebugInfo( luaVM ), szBuffer, 2 );
98-
}
99-
100-
101-
void CScriptDebugging::LogError ( lua_State* luaVM, const char* szFormat, ... )
102-
{
103-
assert ( szFormat );
104-
105-
// Compose the formatted message
106-
char szBuffer [MAX_STRING_LENGTH];
107-
va_list marker;
108-
va_start ( marker, szFormat );
109-
VSNPRINTF ( szBuffer, MAX_STRING_LENGTH, szFormat, marker );
110-
va_end ( marker );
111-
112-
// Log it
113-
LogString ( "ERROR: ", GetLuaDebugInfo( luaVM ), szBuffer, 1 );
114-
}
115-
116-
117-
void CScriptDebugging::LogWarning ( const SLuaDebugInfo& luaDebugInfo, const char* szFormat, ... )
118-
{
119-
assert ( szFormat );
120-
121-
// Compose the formatted message
122-
char szBuffer [MAX_STRING_LENGTH];
123-
va_list marker;
124-
va_start ( marker, szFormat );
125-
VSNPRINTF ( szBuffer, MAX_STRING_LENGTH, szFormat, marker );
126-
va_end ( marker );
127-
128-
// Log it
129-
LogString ( "WARNING: ", luaDebugInfo, szBuffer, 2 );
130-
}
131-
132-
133-
void CScriptDebugging::LogError ( const SLuaDebugInfo& luaDebugInfo, const char* szFormat, ... )
134-
{
135-
assert ( szFormat );
136-
137-
// Compose the formatted message
138-
char szBuffer [MAX_STRING_LENGTH];
139-
va_list marker;
140-
va_start ( marker, szFormat );
141-
VSNPRINTF ( szBuffer, MAX_STRING_LENGTH, szFormat, marker );
142-
va_end ( marker );
143-
144-
// Log it
145-
LogString ( "ERROR: ", luaDebugInfo, szBuffer, 1 );
146-
}
147-
148-
149-
void CScriptDebugging::LogBadPointer ( lua_State* luaVM, const char* szArgumentType, unsigned int uiArgument )
150-
{
151-
assert ( szArgumentType );
152-
153-
// Populate a message to print/send
154-
LogWarning ( luaVM, "Bad '%s' pointer @ '%s'(%u)", szArgumentType, lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ), uiArgument );
155-
}
156-
157-
158-
void CScriptDebugging::LogBadType ( lua_State* luaVM )
159-
{
160-
// Populate a message to print/send
161-
LogWarning ( luaVM, "Bad argument @ '%s'", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ) );
162-
}
163-
164-
165-
void CScriptDebugging::LogCustom ( lua_State* luaVM, const char* szMessage )
166-
{
167-
assert ( szMessage );
168-
169-
// Populate a message to print/send
170-
LogWarning ( luaVM, "%s", szMessage );
171-
}
172-
173-
17452
void CScriptDebugging::LogBadLevel ( lua_State* luaVM, unsigned int uiRequiredLevel )
17553
{
17654
// Populate a message to print/send
17755
LogWarning ( luaVM, "Requires level '%d' @ '%s", uiRequiredLevel, lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ) );
17856
}
17957

180-
181-
// Handle filename/line number in string
182-
void CScriptDebugging::LogPCallError( lua_State* luaVM, const SString& strRes, bool bInitialCall )
183-
{
184-
std::vector < SString > vecSplit;
185-
strRes.Split( ":", vecSplit );
186-
187-
if ( vecSplit.size() >= 3 )
188-
{
189-
// File+line info present
190-
SString strFile = vecSplit[ 0 ];
191-
int iLine = atoi( vecSplit[ 1 ] );
192-
193-
// Get the message string (we cannot use vecSplit here as the message itself could contain ':')
194-
auto pos = strRes.find ( ':', vecSplit[0].length () + vecSplit[1].length () ) ;
195-
SString strMsg = strRes.SubStr ( pos+2 );
196-
197-
if ( iLine == 0 && bInitialCall )
198-
{
199-
// Location hint for compiled scripts
200-
LogError ( SLuaDebugInfo( strFile, iLine ), "(global scope) %s", *strMsg );
201-
}
202-
else
203-
LogError ( SLuaDebugInfo( strFile, iLine ), "%s", *strMsg );
204-
}
205-
else
206-
{
207-
// File+line info not present
208-
LogError( luaVM, "%s", strRes.c_str () );
209-
}
210-
}
211-
212-
21358
void CALLBACK TimerProc( void* lpParametar, BOOLEAN TimerOrWaitFired )
21459
{
21560
// Got a logfile?
@@ -280,98 +125,6 @@ bool CScriptDebugging::SetLogfile ( const char* szFilename, unsigned int uiLevel
280125
return false;
281126
}
282127

283-
284-
//
285-
// Get best debug info we possibly can from the relevent lua state
286-
//
287-
const SLuaDebugInfo& CScriptDebugging::GetLuaDebugInfo( lua_State * luaVM )
288-
{
289-
static SLuaDebugInfo scriptDebugInfo;
290-
scriptDebugInfo = SLuaDebugInfo();
291-
292-
// Get a VM from somewhere
293-
if ( !luaVM && !m_LuaMainStack.empty () )
294-
luaVM = m_LuaMainStack.back ()->GetVM ();
295-
296-
// Lua oop found at level 4 added one just in case it somehow ends up deeper due to nested calls
297-
for ( int level = 1; level <= 5; level++ )
298-
{
299-
lua_Debug debugInfo;
300-
if ( luaVM && lua_getstack ( luaVM, level, &debugInfo ) )
301-
{
302-
lua_getinfo ( luaVM, "nlS", &debugInfo );
303-
// Lua oop handlers get marked as "C", ignore these as the information we want is further up the stack (typically level 4+)
304-
if ( strcmp(debugInfo.source, "=[C]") != 0 )
305-
{
306-
// Make sure this function isn't defined in a string (eg: from runcode)
307-
if ( debugInfo.source[0] == '@' )
308-
{
309-
// Get and store the location of the debug message
310-
scriptDebugInfo.strFile = debugInfo.source + 1;
311-
scriptDebugInfo.iLine = debugInfo.currentline;
312-
scriptDebugInfo.infoType = DEBUG_INFO_FILE_AND_LINE;
313-
// Stop here as we now have the best info
314-
break;
315-
}
316-
else
317-
{
318-
scriptDebugInfo.strShortSrc = debugInfo.short_src;
319-
scriptDebugInfo.infoType = DEBUG_INFO_SHORT_SRC;
320-
// Try other levels to see if we can get the calling file
321-
}
322-
}
323-
}
324-
else
325-
{
326-
// Use saved info if set
327-
if ( m_SavedLuaDebugInfo.infoType != DEBUG_INFO_NONE )
328-
{
329-
scriptDebugInfo = m_SavedLuaDebugInfo;
330-
}
331-
// No point in trying other levels as lua_getstack will fail
332-
break;
333-
}
334-
}
335-
336-
return scriptDebugInfo;
337-
}
338-
339-
340-
//
341-
// Make full log message from components
342-
//
343-
SString CScriptDebugging::ComposeErrorMessage( const char* szPrePend, const SLuaDebugInfo& luaDebugInfo, const char* szMessage )
344-
{
345-
SString strText;
346-
347-
// 1st part - if not empty, it should have a space at the end
348-
strText = szPrePend;
349-
350-
// 2nd part if set
351-
if ( luaDebugInfo.infoType == DEBUG_INFO_FILE_AND_LINE )
352-
{
353-
if ( luaDebugInfo.iLine == INVALID_LINE_NUMBER )
354-
strText += SString( "%s: ", *luaDebugInfo.strFile );
355-
else
356-
strText += SString( "%s:%d: ", *luaDebugInfo.strFile, luaDebugInfo.iLine );
357-
}
358-
359-
// 3rd part
360-
strText += szMessage;
361-
362-
// 5th part - just kidding. This is actually the 4th part!
363-
if ( !luaDebugInfo.strShortSrc.empty() )
364-
{
365-
if ( luaDebugInfo.strShortSrc.BeginsWith( "[" ) )
366-
strText += SString( " %s", *luaDebugInfo.strShortSrc );
367-
else
368-
strText += SString( " [%s]", *luaDebugInfo.strShortSrc );
369-
}
370-
371-
return strText;
372-
}
373-
374-
375128
void CScriptDebugging::LogString ( const char* szPrePend, const SLuaDebugInfo& luaDebugInfo, const char* szMessage, unsigned int uiMinimumDebugLevel, unsigned char ucRed, unsigned char ucGreen, unsigned char ucBlue )
376129
{
377130
SString strText = ComposeErrorMessage( szPrePend, luaDebugInfo, szMessage );
@@ -469,33 +222,3 @@ void CScriptDebugging::PrintLog ( const char* szText )
469222
fwrite( strInput.c_str(), strInput.length(), 1, m_pLogFile );
470223
}
471224
}
472-
473-
474-
// Keep a stack of called VMs to give global warnings/errors a context
475-
void CScriptDebugging::PushLuaMain ( CLuaMain* pLuaMain )
476-
{
477-
m_LuaMainStack.push_back ( pLuaMain );
478-
}
479-
480-
void CScriptDebugging::PopLuaMain ( CLuaMain* pLuaMain )
481-
{
482-
dassert ( !m_LuaMainStack.empty () );
483-
if ( !m_LuaMainStack.empty () )
484-
{
485-
dassert ( m_LuaMainStack.back () == pLuaMain );
486-
m_LuaMainStack.pop_back ();
487-
}
488-
}
489-
490-
void CScriptDebugging::OnLuaMainDestroy ( CLuaMain* pLuaMain )
491-
{
492-
dassert ( !ListContains ( m_LuaMainStack, pLuaMain ) );
493-
ListRemove ( m_LuaMainStack, pLuaMain );
494-
}
495-
496-
CLuaMain* CScriptDebugging::GetTopLuaMain ( void )
497-
{
498-
if ( !m_LuaMainStack.empty () )
499-
return m_LuaMainStack.back();
500-
return NULL;
501-
}

0 commit comments

Comments
 (0)