Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add map between severity and overdue minutes #1144

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

HNOONa-0
Copy link

I have added an unordered_map that can store a severity and it's corresponding overdue time to the LogCleaner object. The modifications should be backwards compatible. If this idea is correct, we can preform some optimizations, like keeping a cache of filepaths whose severities have been computed.

Fixes: #882

Copy link
Collaborator

@sergiud sergiud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR and apologies for the late reply.

The changes are going into the right direction. Please add some unit tests. For this, you can extend the existing log cleaner tests.

@@ -337,6 +338,16 @@ const char* GetLogSeverityName(LogSeverity severity) {
return LogSeverityNames[severity];
}

LogSeverity FindFilepathLogSeverity(const std::string& filepath) {
for (int i = GLOG_INFO; i < NUM_SEVERITIES; i++) {
if (filepath.find(GetLogSeverityName(static_cast<LogSeverity>(i))) != std::string::npos) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (filepath.find(GetLogSeverityName(static_cast<LogSeverity>(i))) != std::string::npos) {
if (filepath.rfind(GetLogSeverityName(static_cast<LogSeverity>(i))) != std::string::npos) {

Search from right since the severity is used as a suffix.

return static_cast<LogSeverity>(i);
}
}
// assume that the file has the INFO severity
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not assume anything and simply return a not found sentinel.

@@ -1286,7 +1299,14 @@ LogCleaner::LogCleaner() = default;

void LogCleaner::Enable(const std::chrono::minutes& overdue) {
enabled_ = true;
overdue_ = overdue;
for (int i = GLOG_INFO; i < NUM_SEVERITIES; i++) {
overdue_per_severity_[static_cast<LogSeverity>(i)] = overdue;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fill the map only with the severity set by the user.

@@ -2627,6 +2654,10 @@ void EnableLogCleaner(const std::chrono::minutes& overdue) {
log_cleaner.Enable(overdue);
}

void EnableLogCleaner(std::unordered_map<LogSeverity, std::chrono::minutes>& overdue_per_severity) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exposing the map as a parameter makes the calls verbose and the API usage unnecessarily complicated. Instead use the following signature:

Suggested change
void EnableLogCleaner(std::unordered_map<LogSeverity, std::chrono::minutes>& overdue_per_severity) {
void EnableLogCleaner(LogSeverity severity, const std::chrono::minutes& overdue) {

void EnableLogCleaner(std::unordered_map<LogSeverity, std::chrono::minutes>& overdue_per_severity) {
log_cleaner.Enable(overdue_per_severity);
}

void DisableLogCleaner() { log_cleaner.Disable(); }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For symmetry, there should be an API call for disabling the log cleaner for specific severity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Set different overduedays for different levels of logs
2 participants