-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathLogging.cpp
112 lines (94 loc) · 3.2 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* See Project CHIP LICENSE file for licensing information. */
#include <lib/core/CHIPConfig.h>
#include <lib/support/EnforceFormat.h>
#include <lib/support/logging/Constants.h>
#include <platform/logging/LogV.h>
#include <cinttypes>
#include <cstdio>
#include <cstring>
#include <sys/syscall.h>
#include <sys/time.h>
#include <unistd.h>
#if CHIP_USE_PW_LOGGING
#include <pw_log/log.h>
#endif // CHIP_USE_PW_LOGGING
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() {}
} // namespace DeviceLayer
namespace Logging {
namespace Platform {
// File pointer for the log file
FILE * logFile = nullptr;
void OpenLogFile(const char * filePath)
{
logFile = fopen(filePath, "a");
if (logFile == nullptr)
{
perror("Failed to open log file");
}
}
void CloseLogFile()
{
if (logFile != nullptr)
{
fclose(logFile);
logFile = nullptr;
}
}
/**
* CHIP log output functions.
*/
void ENFORCE_FORMAT(3, 0) LogV(const char * module, uint8_t category, const char * msg, va_list v)
{
struct timeval tv;
// Should not fail per man page of gettimeofday(), but failed to get time is not a fatal error in log. The bad time value will
// indicate the error occurred during getting time.
gettimeofday(&tv, nullptr);
#if !CHIP_USE_PW_LOGGING
FILE * outputStream = (logFile == nullptr) ? stdout : logFile;
// Lock outputStream, so a single log line will not be corrupted in case
// where multiple threads are using logging subsystem at the same time.
flockfile(outputStream);
fprintf(outputStream, "[%" PRIu64 ".%06" PRIu64 "][%lld:%lld] CHIP:%s: ", static_cast<uint64_t>(tv.tv_sec),
static_cast<uint64_t>(tv.tv_usec), static_cast<long long>(syscall(SYS_getpid)),
static_cast<long long>(syscall(SYS_gettid)), module);
vfprintf(outputStream, msg, v);
fprintf(outputStream, "\n");
fflush(outputStream);
funlockfile(outputStream);
#else // !CHIP_USE_PW_LOGGING
char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
snprintf(formattedMsg, sizeof(formattedMsg),
"[%" PRIu64 ".%06" PRIu64 "][%lld:%lld] CHIP:%s: ", static_cast<uint64_t>(tv.tv_sec),
static_cast<uint64_t>(tv.tv_usec), static_cast<long long>(syscall(SYS_getpid)),
static_cast<long long>(syscall(SYS_gettid)), module);
size_t len = strnlen(formattedMsg, sizeof(formattedMsg));
vsnprintf(formattedMsg + len, sizeof(formattedMsg) - len, msg, v);
switch (static_cast<LogCategory>(category))
{
case kLogCategory_Error:
PW_LOG_ERROR("%s", formattedMsg);
break;
case kLogCategory_Progress:
PW_LOG_INFO("%s", formattedMsg);
break;
case kLogCategory_Detail:
case kLogCategory_None:
case kLogCategory_Automation:
PW_LOG_DEBUG("%s", formattedMsg);
break;
}
#endif // !CHIP_USE_PW_LOGGING
// Let the application know that a log message has been emitted.
DeviceLayer::OnLogOutput();
}
} // namespace Platform
} // namespace Logging
} // namespace chip