blob: c6b580a124b53a706cac332e65f366d222ad63ce [file] [log] [blame]
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <lib/syslog/cpp/macros.h>
#include <Weave/DeviceLayer/internal/WeaveDeviceLayerInternal.h>
#include <Weave/Support/logging/WeaveLogging.h>
#if WEAVE_LOGGING_STYLE_EXTERNAL
using namespace ::nl::Weave;
using namespace ::nl::Weave::DeviceLayer::Internal;
namespace {
void GetModuleName(char * buf, uint8_t module)
{
if (!buf)
{
return;
}
if (module == ::nl::Weave::Logging::kLogModule_DeviceLayer)
{
memcpy(buf, "DL", 3);
}
else
{
::nl::Weave::Logging::GetModuleName(buf, module);
}
}
} // unnamed namespace
namespace nl {
namespace Weave {
namespace Logging {
void Log(uint8_t module, uint8_t category, const char * file, uint32_t line, const char * msg, va_list v)
{
if (IsCategoryEnabled(category))
{
char formattedMsg[WEAVE_DEVICE_CONFIG_LOG_MESSAGE_MAX_SIZE];
vsnprintf(formattedMsg, sizeof(formattedMsg), msg, v);
char module_name[nlWeaveLoggingModuleNameLen + 1];
::GetModuleName(module_name, module);
syslog::LogSeverity severity = syslog::LOG_INFO;
switch (category)
{
case kLogCategory_Error:
severity = syslog::LOG_ERROR;
break;
case kLogCategory_Progress:
case kLogCategory_Retain:
severity = syslog::LOG_INFO;
case kLogCategory_Detail:
#if NDEBUG
// Use negative values when not in debug mode to make detail logs
// fall into the verbose severity.
severity = syslog::LOG_LEVEL(-1);
#else
// On debug builds, elevate detail logs to INFO to make them more
// visible during testing and operation.
severity = syslog::LOG_INFO;
#endif // NDEBUG
break;
}
const char * file_basename = strrchr(file, '/');
file_basename = file_basename ? file_basename + 1 : file;
syslog::LogMessage(severity, file_basename, line, nullptr, nullptr).stream()
<< "[" << module_name << "] " << formattedMsg;
}
}
void Log(uint8_t module, uint8_t category, const char * msg, ...)
{
va_list vargs;
va_start(vargs, msg);
Log(module, category, "", 0, msg, vargs);
va_end(vargs);
}
void Log(uint8_t module, uint8_t category, const char * file, uint32_t line, const char * msg, ...)
{
va_list vargs;
va_start(vargs, msg);
Log(module, category, file, line, msg, vargs);
va_end(vargs);
}
} // namespace Logging
} // namespace Weave
} // namespace nl
#endif //#if WEAVE_LOGGING_STYLE_EXTERNAL