From e103d0c9d6dbd9de7579fa5c6f6723a8c08ded1e Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Mon, 18 Nov 2013 20:37:58 -0500 Subject: [PATCH] Port PtDebugPrint to C++ This has a couple of benefits: - We should theoretically no longer run into UnicodeDecodeErrors from the plString-based implementation - By doing things engine side, we can use the logging API to colorize log messages. --- Sources/Plasma/FeatureLib/pfPython/cyMisc.cpp | 27 ++++++++++++++ Sources/Plasma/FeatureLib/pfPython/cyMisc.h | 1 + .../FeatureLib/pfPython/cyMiscGlue4.cpp | 35 +++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/Sources/Plasma/FeatureLib/pfPython/cyMisc.cpp b/Sources/Plasma/FeatureLib/pfPython/cyMisc.cpp index 6c39d4d7..ef01a509 100644 --- a/Sources/Plasma/FeatureLib/pfPython/cyMisc.cpp +++ b/Sources/Plasma/FeatureLib/pfPython/cyMisc.cpp @@ -2599,6 +2599,33 @@ void cyMisc::DebugAssert( bool cond, const char * msg ) hsAssert( cond, msg ); } +void cyMisc::DebugPrint(const plString& msg, uint32_t level) +{ + if (level < fPythonLoggingLevel) + return; + plStatusLog* log = plStatusLogMgr::GetInstance().FindLog("python.log", false); + if (!log) + return; + + switch (level) { + case kDebugDump: + log->AddLine(msg.c_str(), plStatusLog::kGreen); + break; + case kWarningLevel: + log->AddLine(msg.c_str(), plStatusLog::kYellow); + break; + case kAssertLevel: + hsAssert(false, msg.c_str()); + // ... fall thru to the actual log-print + case kErrorLevel: + log->AddLine(msg.c_str(), plStatusLog::kRed); + break; + default: + log->AddLine(msg.c_str(), plStatusLog::kWhite); + break; + } +} + ////////////////////////////////////////////////////////////////////////////// // // Function : Set a python object to be called back after a certain amount of time. diff --git a/Sources/Plasma/FeatureLib/pfPython/cyMisc.h b/Sources/Plasma/FeatureLib/pfPython/cyMisc.h index 9f2c6024..a6befc1f 100644 --- a/Sources/Plasma/FeatureLib/pfPython/cyMisc.h +++ b/Sources/Plasma/FeatureLib/pfPython/cyMisc.h @@ -853,6 +853,7 @@ public: // PURPOSE : debugging // static void DebugAssert( bool cond, const char * msg ); + static void DebugPrint(const plString& msg, uint32_t level); ////////////////////////////////////////////////////////////////////////////// diff --git a/Sources/Plasma/FeatureLib/pfPython/cyMiscGlue4.cpp b/Sources/Plasma/FeatureLib/pfPython/cyMiscGlue4.cpp index 1694765d..863cb117 100644 --- a/Sources/Plasma/FeatureLib/pfPython/cyMiscGlue4.cpp +++ b/Sources/Plasma/FeatureLib/pfPython/cyMiscGlue4.cpp @@ -416,6 +416,40 @@ PYTHON_GLOBAL_METHOD_DEFINITION(PtDebugAssert, args, "Params: cond, msg\nDebug o PYTHON_RETURN_NONE; } +PYTHON_GLOBAL_METHOD_DEFINITION_WKEY(PtDebugPrint, args, kwargs, "Params: *msgs, **kwargs\nPrints msgs to the Python log given the message's level") +{ + uint32_t level = cyMisc::kErrorLevel; + + do { + // Grabbin' levelz + if (kwargs && PyDict_Check(kwargs)) { + PyObject* value = PyDict_GetItem(kwargs, PyString_FromString("level")); + if (value) { + if (PyInt_Check(value)) + level = PyInt_AsLong(value); + else + break; + } + } + + for (size_t i = 0; i < PySequence_Fast_GET_SIZE(args); ++i) { + PyObject* theMsg = PySequence_Fast_GET_ITEM(args, i); + if (!PyString_CheckEx(theMsg)) + theMsg = PyObject_Repr(theMsg); + + if (theMsg) + cyMisc::DebugPrint(PyString_AsStringEx(theMsg), level); + else + break; + } + PYTHON_RETURN_NONE; + } while (false); + + // fell through to the type error case + PyErr_SetString(PyExc_TypeError, "PtDebugPrint expects a sequence of strings and an optional int"); + PYTHON_RETURN_ERROR; +} + PYTHON_GLOBAL_METHOD_DEFINITION(PtSetAlarm, args, "Params: secs, cbObject, cbContext\nsecs is the amount of time before your alarm goes off.\n" "cbObject is a python object with the method onAlarm(int context)\ncbContext is an integer.") { @@ -812,6 +846,7 @@ void cyMisc::AddPlasmaMethods4(std::vector &methods) PYTHON_GLOBAL_METHOD(methods, PtSetGlobalClickability); PYTHON_GLOBAL_METHOD(methods, PtDebugAssert); + PYTHON_GLOBAL_METHOD(methods, PtDebugPrint); PYTHON_GLOBAL_METHOD(methods, PtSetAlarm); PYTHON_GLOBAL_METHOD(methods, PtSaveScreenShot);