blob: 3fb9940187a7ed8a258b9db160bf823bfaf18c68 [file] [log] [blame]
/* See Project CHIP LICENSE file for licensing information. */
#include <platform/logging/LogV.h>
#include <lib/core/CHIPConfig.h>
#include <platform/CHIPDeviceConfig.h>
#include <lib/support/SafeString.h>
#include <lib/support/logging/CHIPLogging.h>
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
#include <openthread/platform/logging.h>
#endif
#include <FreeRTOS.h>
#include <queue.h>
#include <stdio.h>
#include <string.h>
#include <task.h>
#define MT793X_LOG_ENABLED (1)
#ifdef PW_RPC_ENABLED
#include "PigweedLogger.h"
#endif
#define LOG_ERROR "<error > "
#define LOG_WARN "<warn > "
#define LOG_INFO "<info > "
#define LOG_DETAIL "<detail> "
#define LOG_LWIP "<lwip > "
#define LOG_MT793X "<mt793x > "
// If a new category string LOG_* is created, add it in the MaxStringLength arguments below
static constexpr size_t kMaxCategoryStrLen = chip::MaxStringLength(LOG_ERROR, LOG_WARN, LOG_INFO, LOG_DETAIL, LOG_LWIP, LOG_MT793X);
#if MT793X_LOG_ENABLED
static bool sLogInitialized = false;
#endif
#if MT793X_LOG_ENABLED
static void PrintLog(const char * msg)
{
/*if (sLogInitialized)
{
size_t sz;
sz = strlen(msg);
#ifdef PW_RPC_ENABLED
PigweedLogger::putString(msg, sz);
#endif
const char * newline = "\r\n";
sz = strlen(newline);
#ifdef PW_RPC_ENABLED
PigweedLogger::putString(newline, sz);
#endif
}*/
printf("%s\n", msg);
}
#endif // MT793X_LOG_ENABLED
namespace chip {
namespace DeviceLayer {
/**
* Called whenever a log message is emitted by Chip or LwIP.
*
* This function is intended be overridden by the application to, e.g.,
* schedule output of queued log entries.
*/
void __attribute__((weak)) OnLogOutput(void) {}
} // namespace DeviceLayer
} // namespace chip
#include "stdio.h"
namespace chip {
namespace Logging {
namespace Platform {
/**
* CHIP log output functions.
*/
void LogV(const char * module, uint8_t category, const char * aFormat, va_list v)
{
#if MT793X_LOG_ENABLED && _CHIP_USE_LOGGING
if (IsCategoryEnabled(category))
{
char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
size_t formattedMsgLen;
// len for Category string + "[" + Module name + "] " (Brackets and space =3)
constexpr size_t maxPrefixLen = kMaxCategoryStrLen + chip::Logging::kMaxModuleNameLen + 3;
static_assert(sizeof(formattedMsg) > maxPrefixLen); // Greater than to at least accommodate a ending Null Character
switch (category)
{
case kLogCategory_Error:
strcpy(formattedMsg, LOG_ERROR);
break;
case kLogCategory_Progress:
default:
strcpy(formattedMsg, LOG_INFO);
break;
case kLogCategory_Detail:
strcpy(formattedMsg, LOG_DETAIL);
break;
}
formattedMsgLen = strlen(formattedMsg);
// Form the log prefix, e.g. "[DL] "
snprintf(formattedMsg + formattedMsgLen, sizeof(formattedMsg) - formattedMsgLen, "[%s] ", module);
formattedMsg[sizeof(formattedMsg) - 1] = 0;
formattedMsgLen = strlen(formattedMsg);
size_t len = vsnprintf(formattedMsg + formattedMsgLen, sizeof formattedMsg - formattedMsgLen, aFormat, v);
if (len >= sizeof formattedMsg - formattedMsgLen)
{
formattedMsg[sizeof formattedMsg - 1] = '\0';
}
PrintLog(formattedMsg);
}
// Let the application know that a log message has been emitted.
chip::DeviceLayer::OnLogOutput();
#endif // MT793X_LOG_ENABLED && _CHIP_USE_LOGGING
}
} // namespace Platform
} // namespace Logging
} // namespace chip
/**
* LwIP log output function.
*/
extern "C" void LwIPLog(const char * aFormat, ...)
{
va_list v;
va_start(v, aFormat);
#if MT793X_LOG_ENABLED
char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
strcpy(formattedMsg, LOG_LWIP);
size_t prefixLen = strlen(formattedMsg);
size_t len = vsnprintf(formattedMsg + prefixLen, sizeof formattedMsg - prefixLen, aFormat, v);
if (len >= sizeof formattedMsg - prefixLen)
{
formattedMsg[sizeof formattedMsg - 1] = '\0';
}
PrintLog(formattedMsg);
#if configCHECK_FOR_STACK_OVERFLOW
// Force a stack overflow check.
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
taskYIELD();
#endif
// Let the application know that a log message has been emitted.
chip::DeviceLayer::OnLogOutput();
#endif // MT793X_LOG_ENABLED
va_end(v);
}