-
Notifications
You must be signed in to change notification settings - Fork 532
Expand file tree
/
Copy pathLogging.cpp
More file actions
164 lines (138 loc) · 5.58 KB
/
Logging.cpp
File metadata and controls
164 lines (138 loc) · 5.58 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) Meta Platforms, Inc. and its affiliates.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
#include "Logging.h"
#include "Check.h"
#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <Corrade/Containers/Array.h>
#include <Corrade/Containers/Pair.h>
#include <Corrade/Containers/StaticArray.h>
#include <Corrade/Containers/String.h>
#include <Corrade/Containers/StringStl.h>
#include <Corrade/Utility/Format.h>
#include <Corrade/Utility/FormatStl.h>
#include <Corrade/Utility/Path.h>
namespace Cr = Corrade;
using Cr::Containers::Literals::operator""_s;
namespace esp {
namespace logging {
constexpr const char* LoggingContext::LOGGING_ENV_VAR_NAME;
constexpr LoggingLevel LoggingContext::DEFAULT_LEVEL;
static_assert(uint8_t(Subsystem::NumSubsystems) ==
(sizeof(subsystemNames) / sizeof(subsystemNames[0])),
"Missing a subsystem name.");
Subsystem subsystemFromName(const Corrade::Containers::StringView name) {
const Cr::Containers::String lowerCaseName =
Cr::Utility::String::lowercase(name);
for (uint8_t i = 0; i < uint8_t(Subsystem::NumSubsystems); ++i)
if (lowerCaseName == Cr::Utility::String::lowercase(
Cr::Containers::StringView{subsystemNames[i]}))
return Subsystem(i);
CORRADE_ASSERT_UNREACHABLE("Unknown subsystem '"
<< Cr::Utility::Debug::nospace << name
<< Cr::Utility::Debug::nospace << "'",
{});
}
LoggingLevel levelFromName(const Corrade::Containers::StringView name) {
const Cr::Containers::String lowerCaseName =
Cr::Utility::String::lowercase(name);
#define CASE(level, name) \
if (lowerCaseName == #name##_s) \
return LoggingLevel::level
CASE(VeryVerbose, veryverbose);
CASE(Verbose, verbose);
CASE(Debug, debug);
CASE(Default, default);
CASE(Warning, warning);
CASE(Quiet, quiet);
CASE(Error, error);
#undef CASE
CORRADE_ASSERT_UNREACHABLE("Unknown logging level name '"
<< Cr::Utility::Debug::nospace << name
<< Cr::Utility::Debug::nospace << "'",
{});
}
#if !defined(MAGNUM_BUILD_STATIC_UNIQUE_GLOBALS) || \
defined(CORRADE_TARGET_WINDOWS)
/* (Of course) can't be in an unnamed namespace in order to export it below */
namespace {
#endif
#if defined(MAGNUM_BUILD_STATIC_UNIQUE_GLOBALS) && \
!defined(CORRADE_TARGET_WINDOWS)
/* On static builds that get linked to multiple shared libraries and then used
in a single app we want to ensure there's just one global symbol. On Linux
it's apparently enough to just export, macOS needs the weak attribute.
Windows handled differently below. */
CORRADE_VISIBILITY_EXPORT
#ifdef __GNUC__
__attribute__((weak))
#else
/* uh oh? the test will fail, probably */
#endif
#endif
const LoggingContext* currentLoggingContext = nullptr;
#if !defined(MAGNUM_BUILD_STATIC_UNIQUE_GLOBALS) || \
defined(CORRADE_TARGET_WINDOWS)
} // namespace
#endif
bool LoggingContext::hasCurrent() {
return currentLoggingContext != nullptr;
}
const LoggingContext& LoggingContext::current() {
ESP_CHECK(hasCurrent(),
"esp::logging::LoggingContext: No current logging context.");
return *currentLoggingContext;
}
LoggingContext::LoggingContext(Corrade::Containers::StringView envString)
: loggingLevels_{Cr::DirectInit, uint8_t(Subsystem::NumSubsystems),
DEFAULT_LEVEL},
prevContext_{currentLoggingContext} {
currentLoggingContext = this;
for (const Cr::Containers::StringView setLevelCommand :
envString.split(':')) {
if (setLevelCommand.contains("=")) {
const auto parts = setLevelCommand.partition('=');
LoggingLevel lvl = levelFromName(parts[2]);
for (const Cr::Containers::StringView subsystemName : parts[0].split(','))
loggingLevels_[uint8_t(subsystemFromName(subsystemName))] = lvl;
} else {
std::fill(loggingLevels_.begin(), loggingLevels_.end(),
levelFromName(setLevelCommand));
}
}
}
LoggingContext::LoggingContext()
: LoggingContext{std::getenv(LOGGING_ENV_VAR_NAME)} {}
LoggingContext::~LoggingContext() {
currentLoggingContext = prevContext_;
}
LoggingLevel LoggingContext::levelFor(Subsystem subsystem) const {
return loggingLevels_[uint8_t(subsystem)];
}
bool isLevelEnabled(Subsystem subsystem, LoggingLevel level) {
return level >= LoggingContext::current().levelFor(subsystem);
}
Cr::Containers::String buildMessagePrefix(Subsystem subsystem,
const std::string& filename,
const std::string& function,
int line) {
auto baseFileName = Cr::Utility::Path::split(filename).second();
const auto timePassed =
std::chrono::high_resolution_clock::now().time_since_epoch();
const auto timePassedSeconds =
std::chrono::duration_cast<std::chrono::seconds>(timePassed);
// for micro time
const auto nowMicros = std::chrono::duration_cast<std::chrono::microseconds>(
timePassed - timePassedSeconds);
std::time_t t = std::time_t(timePassedSeconds.count());
// format hour,min, second
std::tm timeNow = *std::localtime(&t);
return Cr::Utility::formatString(
"[{:.02d}:{:.02d}:{:.02d}:{:.06d}]:[{}] {}({})::{} : ", timeNow.tm_hour,
timeNow.tm_min, timeNow.tm_sec, nowMicros.count(),
subsystemNames[uint8_t(subsystem)], baseFileName, line, function);
}
} // namespace logging
} // namespace esp