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

Make LogCleaner support relative paths #654

Merged
merged 1 commit into from
Oct 29, 2021
Merged

Conversation

aesophor
Copy link
Contributor

Closes #653

In #653, the user reports that the following usage of glog API:

google::SetLogDestination(google::INFO, ".");
google::EnableLogCleaner(1);

will lead to an uncaught out_of_range exception.

terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::at: __n (which is 18446744073709551615) >= this->size() (which is 0)
[1]    835009 abort (core dumped)  ./out

This is caused by a bug in LogCleaner::Run() where base_filename doesn't end with a "/".

glog/src/logging.cc

Lines 1334 to 1335 in 0b3d4cb

string dir = base_filename.substr(0, base_filename.find_last_of(dir_delim_) + 1);
dirs.push_back(dir);

This PR:

  1. checks if base_filename in LogCleaner::Run() ends with a "/", and if not, then we simply pass the relative path to LogCleaner::GetOverdueLogNames().
  2. In LogCleaner::GetOverdueLogNames, we only concatenate log_directory and ent->d_name if log_directory ends with a "/". Otherwise, just pass the relative path to LogCleaner::IsLogFromCurrentProject.
  3. Some trivial changes to improve the readability of source code

@google-cla google-cla bot added the cla: yes label May 18, 2021
@aesophor
Copy link
Contributor Author

aesophor commented May 18, 2021

@sergiud @jixianliu1234

I've also discovered this:

google::SetLogDestination(google::INFO, "./");  // creates logs under process's cwd
                                                // example: 20210518-122553.954758

google::SetLogDestination(google::INFO, ".");   // also creates logs under process's cwd, but filenames starts with "."
                                                // example: .20210518-122537.954584

Another example:

google::SetLogDestination(google::INFO, "/tmp/");  // creates logs under /tmp/
google::SetLogDestination(google::INFO, "/tmp");   // creates logs under / with filenames starting with "tmp"

Therefore, I think the correct way of setting log destination to "." is google::SetLogDestination(google::INFO, "./");.
Maybe this should be documented somewhere, so that users won't accidentally write google::SetLogDestination(google::INFO, "."); and expect the logs to all go to the current working directory.

Please tell me if I've missed anything in the documentation. Thanks.

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.

I believe there should be no difference between

google::SetLogDestination(google::INFO, "./");

and

google::SetLogDestination(google::INFO, ".");

since . and ./ are both directories. However, this will require decomposing the path to determine the directory and the base filename.

src/logging.cc Outdated

string filepath = ent->d_name;

if (log_directory.at(log_directory.size() - 1) == dir_delim_) {
Copy link
Collaborator

@sergiud sergiud May 23, 2021

Choose a reason for hiding this comment

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

at throws an out of range access. This should be avoided.

src/logging.cc Outdated
@@ -1435,19 +1439,19 @@ bool LogCleaner::IsLogFromCurrentProject(const string& filepath,
for (size_t i = cleaned_base_filename.size(); i < real_filepath_size; i++) {
const char& c = filepath[i];

if (i <= cleaned_base_filename.size() + 7) { // 0 ~ 7 : YYYYMMDD
if (i <= cleaned_base_filename.size() + 7) { // 0 ~ 7 : YYYYMMDD
Copy link
Collaborator

Choose a reason for hiding this comment

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

These formatting changes seem to be unrelated. I would undo them.

@sergiud
Copy link
Collaborator

sergiud commented Jul 14, 2021

Any progress?

@sergiud
Copy link
Collaborator

sergiud commented Oct 11, 2021

@aesophor There are some conflicts now. Could you please rebase?

@coveralls
Copy link
Collaborator

coveralls commented Oct 12, 2021

Coverage Status

Coverage increased (+0.2%) to 76.611% when pulling c27c3a8 on aesophor:fix-path into e8e40f7 on google:master.

@aesophor
Copy link
Contributor Author

@aesophor There are some conflicts now. Could you please rebase?

@sergiud No problem! I'll get back to this ASAP (probably at the beginning of November). Thanks!

@google-cla
Copy link

google-cla bot commented Oct 13, 2021

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added cla: no and removed cla: yes labels Oct 13, 2021
@sergiud
Copy link
Collaborator

sergiud commented Oct 13, 2021

@googlebot I consent.

@google-cla google-cla bot added cla: yes and removed cla: no labels Oct 13, 2021
@sergiud sergiud force-pushed the fix-path branch 6 times, most recently from 0c0e592 to 40f461c Compare October 13, 2021 16:22
@sergiud
Copy link
Collaborator

sergiud commented Oct 13, 2021

@aesophor Thanks. That's not necessary anymore; I already addressed all the open points.

After careful consideration, the basenames . and ./ must not be treated equally to refer to the current directory. To set a directory, the log_dir flag should be used instead. If glog were to treat the aforementioned basenames the same, it would not be possible to use . as a prefix (e.g., for hidden log files).

Although not directly related to this PR, I encountered some problems writing unit tests for the log cleaner:

  • Why can't the number of (overdue) days be 0? Removing all but the latest log seems to be a perfectly reasonable use case to me.
  • Successively starting the same process which immediately exits does not trigger the log cleaner (overdue days = 0). However, if I delay the process start e.g., by a second the log cleaner kicks in as expected. Why is that?

@sergiud sergiud merged commit 17e7679 into google:master Oct 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash if enable google::EnableLogCleaner(1);
3 participants