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>
1718YNPUT_CORE_IOSTD_NAMESPACE_OPEN
1819std::string
1920getEnvKey (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 */
3443std::string
3544cleanEnvKey (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 */
5370std::vector<std::string>
5471split (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 */
7296std::vector<std::string>
7397getEnvArray (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 */
94126std::map<std::string, std::string>
95127getEnvMap (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};
114154YNPUT_CORE_IOSTD_NAMESPACE_CLOSE
0 commit comments