forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogging.cpp
83 lines (67 loc) · 2.36 KB
/
Logging.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* See Project CHIP LICENSE file for licensing information. */
#include <platform/logging/LogV.h>
#include <lib/core/CHIPConfig.h>
#include <lib/support/logging/Constants.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/cbprintf.h>
// If CONFIG_LOG_MODE_MINIMAL the timestamp is NOT added automatically by the Zephyr logger
#ifdef CONFIG_LOG_MODE_MINIMAL
#define LOG_FORMAT "%u %s"
#define LOG_MESSAGE(msg) k_uptime_get_32(), (msg)
#else
#define LOG_FORMAT "%s"
#define LOG_MESSAGE(msg) (msg)
#endif
/* Assume using always debug level and rely on Matter logger layer filtering */
LOG_MODULE_REGISTER(chip, LOG_LEVEL_DBG);
namespace chip {
namespace DeviceLayer {
/**
* Called whenever a log message is emitted by chip.
*
* 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 Logging {
namespace Platform {
/**
* CHIP log output function.
*/
void LogV(const char * module, uint8_t category, const char * msg, va_list v)
{
char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
snprintfcb(formattedMsg, sizeof(formattedMsg), "[%s]", module);
const size_t prefixLen = strlen(formattedMsg);
vsnprintfcb(formattedMsg + prefixLen, sizeof(formattedMsg) - prefixLen, msg, v);
const char * allocatedMsg = formattedMsg;
// Invoke the Zephyr logging library to log the message.
//
// Unfortunately the Zephyr logging macros end up assigning uint16_t
// variables to uint16_t:10 fields, which triggers integer conversion
// warnings. And treating the Zephyr headers as system headers does not
// help, apparently. Just turn off that warning around this switch.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
switch (category)
{
case kLogCategory_Error:
LOG_ERR(LOG_FORMAT, LOG_MESSAGE(allocatedMsg));
break;
case kLogCategory_Progress:
default:
LOG_INF(LOG_FORMAT, LOG_MESSAGE(allocatedMsg));
break;
case kLogCategory_Detail:
LOG_DBG(LOG_FORMAT, LOG_MESSAGE(allocatedMsg));
break;
}
#pragma GCC diagnostic pop
// Let the application know that a log message has been emitted.
DeviceLayer::OnLogOutput();
}
} // namespace Platform
} // namespace Logging
} // namespace chip