Skip to content

Commit f3e3968

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 f3e3968

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-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: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <olp/core/porting/optional.h>
2727
#include <olp/core/thread/Atomic.h>
2828

29+
#include <algorithm>
2930
#include <string>
3031
#include <unordered_map>
3132

@@ -34,7 +35,8 @@ namespace logging {
3435

3536
namespace {
3637
constexpr auto kSecretMask = "*****";
37-
}
38+
const auto kSecretMaskLength = std::strlen(kSecretMask);
39+
} // namespace
3840

3941
struct LogMessageExt : public LogMessage {
4042
olp::porting::optional<std::string> adjusted_message;
@@ -77,7 +79,8 @@ class LogImpl {
7779
unsigned int line, const char* function,
7880
const char* fullFunction);
7981

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

8285
private:
8386
LogImpl();
@@ -219,15 +222,22 @@ void LogImpl::censorLogItem(LogItem& log_item, const std::string& original) {
219222
log_item.adjusted_message.value().replace(found_pos, secret.length(),
220223
kSecretMask);
221224
found_pos = log_item.adjusted_message.value().find(
222-
secret, found_pos + std::strlen(kSecretMask));
225+
secret, found_pos + kSecretMaskLength);
223226
}
224227
}
225228
}
226229

227-
void LogImpl::censor(std::string msg) {
230+
void LogImpl::addCensor(std::string msg) {
228231
m_toCensor.emplace_back(std::move(msg));
229232
}
230233

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

@@ -348,11 +358,19 @@ void Log::logMessage(Level level, const std::string& tag,
348358
});
349359
}
350360

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

355-
LogImpl::getInstance().locked([&](LogImpl& log) { log.censor(message); });
372+
LogImpl::getInstance().locked(
373+
[&](LogImpl& log) { log.removeCensor(message); });
356374
}
357375

358376
} // 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)