Skip to content

Commit 81962dc

Browse files
Add removing censor API to Log
There are secrets that can be updated with time and it make sense not to censor them anymore not to let performance suffer. Relates-To: NLAM-140 Signed-off-by: Rustam Gamidov <[email protected]>
1 parent b09408c commit 81962dc

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

olp-cpp-sdk-core/include/olp/core/logging/Log.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,17 @@ class CORE_API Log {
571571
*
572572
* @param message The line to be censored out from the log.
573573
*/
574-
static void censor(const std::string& message);
574+
static void addCensor(const std::string& message);
575+
576+
/**
577+
* @brief Removes a line from censoring out from the log.
578+
*
579+
* Censoring out is a replacement with a predefiend mask with length not
580+
* correlated with the original line length.
581+
*
582+
* @param message The line to be excluded from censoring out from the log.
583+
*/
584+
static void removeCensor(const std::string& message);
575585
};
576586
} // namespace logging
577587
} // namespace olp

olp-cpp-sdk-core/src/logging/Log.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ namespace logging {
3434

3535
namespace {
3636
constexpr auto kSecretMask = "*****";
37-
}
37+
const auto kSecretMaskLength = std::strlen(kSecretMask);
38+
} // namespace
3839

3940
struct LogMessageExt : public LogMessage {
4041
olp::porting::optional<std::string> adjusted_message;
@@ -77,7 +78,8 @@ class LogImpl {
7778
unsigned int line, const char* function,
7879
const char* fullFunction);
7980

80-
void censor(std::string msg);
81+
void addCensor(std::string msg);
82+
void removeCensor(const std::string& msg);
8183

8284
private:
8385
LogImpl();
@@ -219,15 +221,22 @@ void LogImpl::censorLogItem(LogItem& log_item, const std::string& original) {
219221
log_item.adjusted_message.value().replace(found_pos, secret.length(),
220222
kSecretMask);
221223
found_pos = log_item.adjusted_message.value().find(
222-
secret, found_pos + std::strlen(kSecretMask));
224+
secret, found_pos + kSecretMaskLength);
223225
}
224226
}
225227
}
226228

227-
void LogImpl::censor(std::string msg) {
229+
void LogImpl::addCensor(std::string msg) {
228230
m_toCensor.emplace_back(std::move(msg));
229231
}
230232

233+
void LogImpl::removeCensor(const std::string& msg) {
234+
auto it = std::find(m_toCensor.begin(), m_toCensor.end(), msg);
235+
if (it != m_toCensor.end()) {
236+
m_toCensor.erase(it);
237+
}
238+
}
239+
231240
// implementation of public static Log API
232241
//--------------------------------------------------------
233242

@@ -348,11 +357,19 @@ void Log::logMessage(Level level, const std::string& tag,
348357
});
349358
}
350359

351-
void Log::censor(const std::string& message) {
360+
void Log::addCensor(const std::string& message) {
361+
if (!LogImpl::aliveStatus())
362+
return;
363+
364+
LogImpl::getInstance().locked([&](LogImpl& log) { log.addCensor(message); });
365+
}
366+
367+
void Log::removeCensor(const std::string& message) {
352368
if (!LogImpl::aliveStatus())
353369
return;
354370

355-
LogImpl::getInstance().locked([&](LogImpl& log) { log.censor(message); });
371+
LogImpl::getInstance().locked(
372+
[&](LogImpl& log) { log.removeCensor(message); });
356373
}
357374

358375
} // namespace logging

olp-cpp-sdk-core/tests/logging/LogTest.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,9 @@ TEST(LogTest, LogCensor) {
429429
constexpr auto secret_02 = "to hide";
430430
constexpr auto secret_mask = "******";
431431

432-
olp::logging::Log::censor(secret_01);
433-
olp::logging::Log::censor(secret_02);
434-
olp::logging::Log::censor(secret_mask);
432+
olp::logging::Log::addCensor(secret_01);
433+
olp::logging::Log::addCensor(secret_02);
434+
olp::logging::Log::addCensor(secret_mask);
435435

436436
OLP_SDK_LOG_INFO_F("", "Nothing to censor");
437437
OLP_SDK_LOG_INFO_F("", "Inlined1st secretin message");
@@ -452,6 +452,16 @@ TEST(LogTest, LogCensor) {
452452
EXPECT_EQ("***** twice *****", appender->messages_[5].message_);
453453
EXPECT_EQ("no loop **********", appender->messages_[6].message_);
454454
EXPECT_EQ("Fatal *****", appender->messages_[7].message_);
455+
456+
olp::logging::Log::removeCensor("not exisiting secret");
457+
OLP_SDK_LOG_TRACE_F("trace", "%s %s", "plain", secret_01);
458+
olp::logging::Log::removeCensor(secret_01);
459+
OLP_SDK_LOG_TRACE_F("trace", "%s %s", "plain", secret_01);
460+
461+
ASSERT_EQ(10U, appender->messages_.size());
462+
463+
EXPECT_EQ("plain *****", appender->messages_[8].message_);
464+
EXPECT_EQ("plain 1st secret", appender->messages_[9].message_);
455465
}
456466

457467
TEST(LogTest, LogLimits) {

0 commit comments

Comments
 (0)