Skip to content

Commit 9f647c0

Browse files
Merge pull request #16 from ynput/develop
Logging Enhancements
2 parents cc952de + 555d24f commit 9f647c0

3 files changed

Lines changed: 65 additions & 7 deletions

File tree

src/lib/ynput/core/iostd/envVarHelpers.hpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define YNPUT_ENV_VAR_HELPER
33

44
#include "../../../../NameSpaceDef/namespaces.hpp"
5+
#include "../../lib/logging/AyonLogger.hpp"
56
#include <cstdlib>
67
#include <map>
78
#include <string>
@@ -17,11 +18,19 @@
1718
YNPUT_CORE_IOSTD_NAMESPACE_OPEN
1819
std::string
1920
getEnvKey(const std::string &envKey) {
21+
auto & logger = AyonLogger::getInstance();
22+
static const std::string kLogKeyName = "getEnvKey";
23+
static const bool kRegistered = logger.registerLoggingKey(kLogKeyName);
24+
(void)kRegistered;
25+
26+
auto logKey = logger.key(kLogKeyName);
2027
const char* charEnvKey = std::getenv(envKey.c_str());
2128
if (charEnvKey != nullptr) {
2229
std::string strEnvKey(charEnvKey);
30+
logger.info(logKey, "Loaded environment key '{}'", envKey);
2331
return strEnvKey;
2432
}
33+
logger.warn(logKey, "Environment key '{}' was not found", envKey);
2534
return "";
2635
};
2736

@@ -33,12 +42,20 @@ getEnvKey(const std::string &envKey) {
3342
*/
3443
std::string
3544
cleanEnvKey(std::string &dirtyKey) {
45+
auto & logger = AyonLogger::getInstance();
46+
static const std::string kLogKeyName = "cleanEnvKey";
47+
static const bool kRegistered = logger.registerLoggingKey(kLogKeyName);
48+
(void)kRegistered;
49+
50+
auto logKey = logger.key(kLogKeyName);
3651
auto start = dirtyKey.find_first_not_of(" \t\n\r\f\v");
3752
if (start == std::string::npos) {
53+
logger.warn(logKey, "Environment key value was empty after cleanup");
3854
return "";
3955
}
4056
auto end = dirtyKey.find_last_not_of(" \t\n\r\f\v");
4157

58+
logger.info(logKey, "Cleaned environment key value");
4259
return dirtyKey.substr(start, end - start + 1);
4360
};
4461

@@ -52,6 +69,12 @@ cleanEnvKey(std::string &dirtyKey) {
5269
*/
5370
std::vector<std::string>
5471
split(const std::string &str, char delimiter) {
72+
auto & logger = AyonLogger::getInstance();
73+
static const std::string kLogKeyName = "splitEnvValue";
74+
static const bool kRegistered = logger.registerLoggingKey(kLogKeyName);
75+
(void)kRegistered;
76+
77+
auto logKey = logger.key(kLogKeyName);
5578
std::vector<std::string> tokens;
5679
std::stringstream ss(str);
5780
std::string token;
@@ -60,6 +83,7 @@ split(const std::string &str, char delimiter) {
6083
tokens.push_back(token);
6184
}
6285

86+
logger.info(logKey, "Split environment value into {} token(s)", tokens.size());
6387
return tokens;
6488
}
6589

@@ -71,16 +95,24 @@ split(const std::string &str, char delimiter) {
7195
*/
7296
std::vector<std::string>
7397
getEnvArray(const std::string &envKey) {
98+
auto & logger = AyonLogger::getInstance();
99+
static const std::string kLogKeyName = "getEnvArray";
100+
static const bool kRegistered = logger.registerLoggingKey(kLogKeyName);
101+
(void)kRegistered;
102+
103+
auto logKey = logger.key(kLogKeyName);
74104
std::string envKeyVal = getEnvKey(envKey);
75105
std::vector<std::string> arrayItems;
76106
if (envKeyVal.empty()) {
107+
logger.warn(logKey, "Environment array key '{}' was empty", envKey);
77108
return arrayItems;
78109
}
79110
arrayItems = split(envKeyVal, ',');
80111

81112
for (std::string &dirtyItem: arrayItems) {
82113
dirtyItem = cleanEnvKey(dirtyItem);
83114
}
115+
logger.info(logKey, "Loaded environment array '{}' with {} item(s)", envKey, arrayItems.size());
84116
return arrayItems;
85117
};
86118

@@ -93,9 +125,16 @@ getEnvArray(const std::string &envKey) {
93125
*/
94126
std::map<std::string, std::string>
95127
getEnvMap(const std::string &envKey) {
128+
auto & logger = AyonLogger::getInstance();
129+
static const std::string kLogKeyName = "getEnvMap";
130+
static const bool kRegistered = logger.registerLoggingKey(kLogKeyName);
131+
(void)kRegistered;
132+
133+
auto logKey = logger.key(kLogKeyName);
96134
std::string envKeyVal = getEnvKey(envKey);
97135
std::map<std::string, std::string> envMap;
98136
if (envKeyVal.empty()) {
137+
logger.warn(logKey, "Environment map key '{}' was empty", envKey);
99138
return envMap;
100139
}
101140
std::vector<std::string> dirtyArrayItems = split(envKeyVal, ',');
@@ -109,6 +148,7 @@ getEnvMap(const std::string &envKey) {
109148
envMap.emplace(std::make_pair(cleanEnvKey(key), cleanEnvKey(val)));
110149
}
111150
}
151+
logger.info(logKey, "Loaded environment map '{}' with {} item(s)", envKey, envMap.size());
112152
return envMap;
113153
};
114154
YNPUT_CORE_IOSTD_NAMESPACE_CLOSE

src/lib/ynput/lib/logging/AyonLogger.hpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class AyonLogger {
5353
if (!spdlog::thread_pool()) {
5454
spdlog::init_thread_pool(8192, 1);
5555
}
56+
m_fileLoggerThreadPool = spdlog::thread_pool();
5657

5758
auto abs_path = std::filesystem::absolute(filepath).string();
5859
std::string logger_name = std::string("AyonLogger_file_logger_") + abs_path;
@@ -175,7 +176,7 @@ class AyonLogger {
175176
// Explicit flush for async loggers
176177
void flush() {
177178
if (m_consoleLogger) m_consoleLogger->flush();
178-
if (m_enableFileLogging && m_fileLogger) m_fileLogger->flush();
179+
if (canUseAsyncFileLogger()) m_fileLogger->flush();
179180
}
180181

181182
private:
@@ -193,14 +194,18 @@ class AyonLogger {
193194
}
194195

195196
~AyonLogger() {
196-
flush();
197197
if (m_consoleLogger) {
198-
spdlog::drop(m_consoleLogger->name());
199-
m_consoleLogger.reset();
198+
m_consoleLogger->flush();
200199
}
201200
if (m_enableFileLogging && m_fileLogger) {
202201
spdlog::drop(m_fileLogger->name());
203202
m_fileLogger.reset();
203+
m_fileLoggerThreadPool.reset();
204+
m_enableFileLogging = false;
205+
}
206+
if (m_consoleLogger) {
207+
spdlog::drop(m_consoleLogger->name());
208+
m_consoleLogger.reset();
204209
}
205210
}
206211

@@ -212,10 +217,14 @@ class AyonLogger {
212217
if (m_consoleLogger)
213218
m_consoleLogger->log(lvl, fmt, std::forward<Args>(args)...);
214219

215-
if (m_enableFileLogging && m_fileLogger)
220+
if (canUseAsyncFileLogger())
216221
m_fileLogger->log(lvl, fmt, std::forward<Args>(args)...);
217222
}
218223

224+
bool canUseAsyncFileLogger() const {
225+
return m_enableFileLogging && m_fileLogger && !m_fileLoggerThreadPool.expired();
226+
}
227+
219228
void setLevel(spdlog::level::level_enum lvl, bool applyToFile) {
220229
if (m_consoleLogger)
221230
m_consoleLogger->set_level(lvl);
@@ -225,6 +234,7 @@ class AyonLogger {
225234

226235
std::shared_ptr<spdlog::logger> m_consoleLogger;
227236
std::shared_ptr<spdlog::logger> m_fileLogger;
237+
std::weak_ptr<spdlog::details::thread_pool> m_fileLoggerThreadPool;
228238

229239
bool m_enableFileLogging{false};
230240
std::mutex m_initMutex;

src/lib/ynput/tool/ayon/rootHelpers.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define TOOL_AYON_ROT_HELPERS_DEF
33

44
#include "../../../../NameSpaceDef/namespaces.hpp"
5+
#include "../../lib/logging/AyonLogger.hpp"
56

67
#include <algorithm>
78
#include <regex>
@@ -17,9 +18,15 @@
1718
*/
1819
YNPUT_TOOL_AYON_NAMESPACE_OPEN
1920

20-
// TODO after we implemented a singleton logger we will Re implement logging into this function
2121
std::string
2222
rootReplace(const std::string &rootLessPath, const std::unordered_map<std::string, std::string> &siteRoots) {
23+
auto & logger = AyonLogger::getInstance();
24+
static const std::string kLogKeyName = "rootReplace";
25+
static const bool kRegistered = logger.registerLoggingKey(kLogKeyName);
26+
(void)kRegistered;
27+
28+
auto logKey = logger.key(kLogKeyName);
29+
2330
std::string rootedPath;
2431

2532
std::smatch matchea;
@@ -38,6 +45,7 @@ rootReplace(const std::string &rootLessPath, const std::unordered_map<std::strin
3845
return rootedPath;
3946
}
4047
catch (std::out_of_range &e) {
48+
logger.warn(logKey, "Could not find site root '{}' for path '{}'", key, rootLessPath);
4149
return rootLessPath;
4250
}
4351
}
@@ -47,4 +55,4 @@ rootReplace(const std::string &rootLessPath, const std::unordered_map<std::strin
4755
};
4856

4957
YNPUT_TOOL_AYON_NAMESPACE_CLOSE
50-
#endif // !TOOL_AYON_ROT_HELPERS_DEF
58+
#endif // !TOOL_AYON_ROT_HELPERS_DEF

0 commit comments

Comments
 (0)