Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dead (uncompilable) code from python script bindings #4690

Merged
merged 2 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 7 additions & 73 deletions src/controller/python/ChipDeviceController-ScriptBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,79 +303,13 @@ CHIP_ERROR pychip_GetDeviceByNodeId(chip::DeviceController::ChipDeviceController
return devCtrl->GetDeviceController()->GetDevice(nodeId, device);
}

#if _CHIP_USE_LOGGING && CHIP_LOG_ENABLE_DYNAMIC_LOGING_FUNCTION

// A pointer to the python logging function.
static LogMessageFunct sLogMessageFunct = NULL;

// This function is called by the Chip logging code whenever a developer message
// is logged. It serves as glue to adapt the logging arguments to what is expected
// by the python code.
// NOTE that this function MUST be thread-safe.
static void LogMessageToPython(uint8_t module, uint8_t category, const char * msg, va_list ap)
{
if (IsCategoryEnabled(category))
{
// Capture the timestamp of the log message.
struct timeval tv;
gettimeofday(&tv, NULL);

// Get the module name
char moduleName[chip::Logging::kMaxModuleNameLen + 1];
::chip:: ::Logging::GetModuleName(moduleName, sizeof(moduleName), module);

// Format the log message into a dynamic memory buffer, growing the
// buffer as needed to fit the message.
char * msgBuf = NULL;
size_t msgBufSize = 0;
size_t msgSize = 0;
constexpr size_t kInitialBufSize = 120;
do
{
va_list apCopy;
va_copy(apCopy, ap);

msgBufSize = max(msgSize + 1, kInitialBufSize);
msgBuf = (char *) realloc(msgBuf, msgBufSize);
if (msgBuf == NULL)
{
return;
}

int res = vsnprintf(msgBuf, msgBufSize, msg, apCopy);
if (res < 0)
{
return;
}
msgSize = (size_t) res;

va_end(apCopy);
} while (msgSize >= msgBufSize);

// Call the configured python logging function.
sLogMessageFunct((int64_t) tv.tv_sec, (int64_t) tv.tv_usec, moduleName, category, msgBuf);

// Release the message buffer.
free(msgBuf);
}
}

void pychip_Stack_SetLogFunct(LogMessageFunct logFunct)
{
if (logFunct != NULL)
{
sLogMessageFunct = logFunct;
::chip::Logging::SetLogFunct(LogMessageToPython);
}
else
{
sLogMessageFunct = NULL;
::chip::Logging::SetLogFunct(NULL);
}
// TODO: determine if log redirection is supposed to be functioning in CHIP
//
// Background: original log baseline supported 'redirect logs to this
// function' however CHIP does not currently provide this.
//
// Ideally log redirection should work so that python code can do things
// like using the log module.
}

#else // CHIP_LOG_ENABLE_DYNAMIC_LOGING_FUNCTION

void pychip_Stack_SetLogFunct(LogMessageFunct logFunct) {}

#endif // CHIP_LOG_ENABLE_DYNAMIC_LOGING_FUNCTION
4 changes: 2 additions & 2 deletions src/controller/python/chip/ChipStack.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def __init__(
):
fmt = "%(message)s"
if logModulePrefix:
fmt = "WEAVE:%(chip-module)s: " + fmt
fmt = "CHIP:%(chip-module)s: " + fmt
if logLevel:
fmt = "%(levelname)s:" + fmt
if datefmt is not None or logTimestamp:
Expand Down Expand Up @@ -212,7 +212,7 @@ def logFunct(timestamp, timestampUSec, moduleName, logCat, message):
moduleName = ChipUtility.CStringToString(moduleName)
message = ChipUtility.CStringToString(message)
if self.addModulePrefixToLogMessage:
message = "WEAVE:%s: %s" % (moduleName, message)
message = "CHIP:%s: %s" % (moduleName, message)
logLevel = LogCategory.categoryToLogLevel(logCat)
msgAttrs = {
"chip-module": moduleName,
Expand Down