Skip to content

Commit a9808d0

Browse files
author
L'In20Cible
committed
Patches for issue #211.
1 parent 84a5ea5 commit a9808d0

File tree

8 files changed

+33
-48
lines changed

8 files changed

+33
-48
lines changed

addons/source-python/packages/source-python/loggers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,17 @@ def _log(self, level, msg, *args, **kwargs):
252252
# Print to the main SP log file?
253253
if SP_LOG & areas:
254254

255+
# Patch for issue #211.
256+
# Save/Overwrite the logger's name
257+
name = _sp_logger.logger.name
258+
_sp_logger.logger.name = self.logger.name
259+
255260
# Print to the SP log file
256261
_sp_logger.logger.log(level, msg, *args, **kwargs)
257262

263+
# Restore the name
264+
_sp_logger.logger.name = name
265+
258266
@staticmethod
259267
def _get_level_value(level):
260268
"""Return a level value used by the logging package.

src/core/modules/commands/commands_say.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ void SayConCommand::Dispatch( const CCommand& command )
239239
}
240240

241241
if (!stripped_command.Tokenize(szCommand)) {
242-
PythonLog(0, "Failed to tokenize '%s'.", command.GetCommandString());
242+
PythonLog(0, "commands.say", "Failed to tokenize '%s'.", command.GetCommandString());
243243
return;
244244
}
245245

src/core/modules/memory/memory_function.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ void CFunction::AddHook(HookType_t eType, PyObject* pCallable)
342342

343343
PythonLog(
344344
4,
345+
"memory",
345346
"Hooking function: type=%s, addr=%u, conv=%s, args=%s, rtype=%s, callback=%s",
346347
szType,
347348
m_ulAddr,

src/core/modules/memory/memory_pointer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ void CPointer::__del__(PyObject* self)
342342
CPointer* ptr = extract<CPointer *>(self);
343343
if (ptr->m_bAutoDealloc)
344344
{
345-
PythonLog(4, "Automatically deallocating pointer at %u.", ptr->m_ulAddr);
345+
PythonLog(4, "memory", "Automatically deallocating pointer at %u.", ptr->m_ulAddr);
346346
PreDealloc(self);
347347
}
348348
}

src/core/modules/memory/memory_rtti.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class CBaseType : public IBaseType
103103
for (int i=0; i < level; ++i)
104104
x += "-";
105105

106-
PythonLog(-1, "%s%s", x.c_str(), GetName());
106+
PythonLog(-1, "memory", "%s%s", x.c_str(), GetName());
107107
for (unsigned int i=0; i < GetNumBaseClasses(); ++i) {
108108
GetBaseClass(i)->Dump(level + 1);
109109
}

src/core/modules/memory/memory_scanner.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,34 +104,34 @@ void CBinaryFile::AddSignatureToCache(unsigned char* sigstr, int iLength, unsign
104104

105105
bool CBinaryFile::SearchSigInCache(unsigned char* sigstr, CPointer*& result)
106106
{
107-
PythonLog(4, "Searching for a cached signature...");
107+
PythonLog(4, "memory", "Searching for a cached signature...");
108108
for (std::list<Signature_t>::iterator iter=m_Signatures.begin(); iter != m_Signatures.end(); ++iter)
109109
{
110110
Signature_t sig = *iter;
111111
if (strcmp((const char *) sig.m_szSignature, (const char *) sigstr) == 0)
112112
{
113-
PythonLog(4, "Found a cached signature!");
113+
PythonLog(4, "memory", "Found a cached signature!");
114114
result = new CPointer(sig.m_ulAddr);
115115
return true;
116116
}
117117
}
118-
PythonLog(4, "Could not find a cached signature.");
118+
PythonLog(4, "memory", "Could not find a cached signature.");
119119
return false;
120120
}
121121

122122
bool CBinaryFile::SearchSigInBinary(object oSignature, int iLength, unsigned char* sigstr, CPointer*& result)
123123
{
124-
PythonLog(4, "Searching in the binary...");
124+
PythonLog(4, "memory", "Searching in the binary...");
125125
CPointer* pPtr = FindSignatureRaw(oSignature);
126126
if (pPtr->IsValid())
127127
{
128-
PythonLog(4, "Found a signature in the binary!");
128+
PythonLog(4, "memory", "Found a signature in the binary!");
129129
AddSignatureToCache(sigstr, iLength, pPtr->m_ulAddr);
130130
result = pPtr;
131131
return true;
132132
}
133133
delete pPtr;
134-
PythonLog(4, "Could not find the signature in the binary.");
134+
PythonLog(4, "memory", "Could not find the signature in the binary.");
135135
return false;
136136
}
137137

@@ -140,13 +140,13 @@ bool CBinaryFile::SearchSigHooked(object oSignature, int iLength, unsigned char*
140140
CPointer* pPtr = FindSignatureRaw(oSignature);
141141
if (!pPtr->IsValid())
142142
{
143-
PythonLog(4, "Could not find a hooked signature.");
143+
PythonLog(4, "memory", "Could not find a hooked signature.");
144144
delete pPtr;
145145
return false;
146146
}
147147

148-
PythonLog(4, "Found a hooked signature!");
149-
PythonLog(4, "Checking if it's unique...");
148+
PythonLog(4, "memory", "Found a hooked signature!");
149+
PythonLog(4, "memory", "Checking if it's unique...");
150150

151151
// Add iLength, so we start searching after the match
152152
CPointer new_ptr = CPointer(pPtr->m_ulAddr + len(oSignature));
@@ -159,7 +159,7 @@ bool CBinaryFile::SearchSigHooked(object oSignature, int iLength, unsigned char*
159159
if (bIsValid)
160160
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Found more than one hooked signatures. Please pass more bytes.");
161161

162-
PythonLog(4, "Signature is unique!");
162+
PythonLog(4, "memory", "Signature is unique!");
163163
AddSignatureToCache(sigstr, iLength, pPtr->m_ulAddr);
164164
result = pPtr;
165165
return true;
@@ -182,7 +182,7 @@ CPointer* CBinaryFile::FindSignature(object oSignature)
182182
object oHexSig = oSignature.attr("hex")();
183183
const char* szHexSig = extract<const char*>(oHexSig);
184184

185-
PythonLog(4, "Searching for a hooked signature (relative jump)...");
185+
PythonLog(4, "memory", "Searching for a hooked signature (relative jump)...");
186186
if (iLength <= 6)
187187
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Signature is too short to search for a hooked signature (relative jump): %s", szHexSig);
188188

@@ -191,7 +191,7 @@ CPointer* CBinaryFile::FindSignature(object oSignature)
191191
if (SearchSigHooked(oSignature, iLength, sigstr, result))
192192
return result;
193193

194-
PythonLog(4, "Searching for a hooked signature (absolute jump)...");
194+
PythonLog(4, "memory", "Searching for a hooked signature (absolute jump)...");
195195
if (iLength <= 7)
196196
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Signature is too short to search for a hooked signature (absolute jump): %s", szHexSig);
197197

src/core/sp_main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ PLUGIN_RESULT CSourcePython::NetworkIDValidated( const char *pszUserName, const
548548
void CSourcePython::OnQueryCvarValueFinished( QueryCvarCookie_t iCookie, edict_t *pPlayerEntity,
549549
EQueryCvarValueStatus eStatus, const char *pCvarName, const char *pCvarValue )
550550
{
551-
PythonLog(4, "Cvar query (cookie: %d, status: %d) - name: %s, value: %s", iCookie, eStatus, pCvarName, pCvarValue );
551+
PythonLog(4, "listeners", "Cvar query (cookie: %d, status: %d) - name: %s, value: %s", iCookie, eStatus, pCvarName, pCvarValue );
552552
unsigned int iEntityIndex;
553553
if (!IndexFromEdict(pPlayerEntity, iEntityIndex))
554554
return;

src/core/utilities/call_python.h

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -75,39 +75,8 @@ T xdecref_and_return(T value)
7575
(3) info
7676
(4) debug
7777
*/
78-
inline void PythonLog( const char* szLevel, const char* szFormat, ... )
79-
{
80-
// Format the message
81-
char szMessage[2048];
82-
va_list args;
83-
va_start(args, szFormat);
84-
vsprintf(szMessage, szFormat, args);
85-
va_end(args);
86-
87-
// Get the std::string instance of szMessage
88-
std::string szMethod = szLevel;
89-
90-
// Get the method's name
91-
std::string szMethodName = "log_" + szMethod;
92-
93-
BEGIN_BOOST_PY()
94-
95-
// Import the loggers module
96-
object oLogModule = import("loggers");
97-
98-
// Get the main SP Logger instance
99-
object oLogger = oLogModule.attr("_sp_logger");
100-
101-
// Get the PyObject instance of the logger
102-
PyObject* poLogger = oLogger.ptr();
103-
104-
// Call the given method
105-
call_method<void>( poLogger, szMethodName.c_str(), szMessage );
106-
107-
END_BOOST_PY()
108-
}
10978

110-
inline void PythonLog( int iLevel, const char* szFormat, ... )
79+
inline void PythonLog( int iLevel, const char *szLogger, const char* szFormat, ... )
11180
{
11281
// Format the message
11382
char szMessage[2048];
@@ -124,6 +93,13 @@ inline void PythonLog( int iLevel, const char* szFormat, ... )
12493
// Get the main SP Logger instance
12594
object oLogger = oLogModule.attr("_sp_logger");
12695

96+
// Patch for issue #211.
97+
if (szLogger != NULL) {
98+
list childs = str(szLogger).split(".");
99+
for (int iCurrentIndex=0; iCurrentIndex < len(childs); iCurrentIndex++)
100+
oLogger = oLogger[childs[iCurrentIndex]];
101+
}
102+
127103
// Get the PyObject instance logger
128104
PyObject* poLogger = oLogger.ptr();
129105

0 commit comments

Comments
 (0)