diff options
author | Jan Niklas Hasse <jhasse@bixense.com> | 2020-08-03 10:48:17 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-03 10:48:17 (GMT) |
commit | 00968321d6ace10b6798855cab7620dc6a5620fd (patch) | |
tree | 0eb1152dc7c28de4936a4661da15580e6e6947a1 /src/build_log.cc | |
parent | 086a9b2f8833fd45f2119596f74f90b9f7e2cdf5 (diff) | |
parent | cc79afbc0510b7320d35fb481f0f200dec92cb43 (diff) | |
download | Ninja-00968321d6ace10b6798855cab7620dc6a5620fd.zip Ninja-00968321d6ace10b6798855cab7620dc6a5620fd.tar.gz Ninja-00968321d6ace10b6798855cab7620dc6a5620fd.tar.bz2 |
Merge pull request #1780 from jhasse/delay-open
Delay actually opening log files until the first write, fix #1724
Diffstat (limited to 'src/build_log.cc')
-rw-r--r-- | src/build_log.cc | 51 |
1 files changed, 32 insertions, 19 deletions
diff --git a/src/build_log.cc b/src/build_log.cc index 98543b6..e5f179c 100644 --- a/src/build_log.cc +++ b/src/build_log.cc @@ -23,6 +23,7 @@ #include "build_log.h" #include "disk_interface.h" +#include <cassert> #include <errno.h> #include <stdlib.h> #include <string.h> @@ -132,25 +133,9 @@ bool BuildLog::OpenForWrite(const string& path, const BuildLogUser& user, return false; } - log_file_ = fopen(path.c_str(), "ab"); - if (!log_file_) { - *err = strerror(errno); - return false; - } - setvbuf(log_file_, NULL, _IOLBF, BUFSIZ); - SetCloseOnExec(fileno(log_file_)); - - // Opening a file in append mode doesn't set the file pointer to the file's - // end on Windows. Do that explicitly. - fseek(log_file_, 0, SEEK_END); - - if (ftell(log_file_) == 0) { - if (fprintf(log_file_, kFileSignature, kCurrentVersion) < 0) { - *err = strerror(errno); - return false; - } - } - + assert(!log_file_); + log_file_path_ = path; // we don't actually open the file right now, but will + // do so on the first write attempt return true; } @@ -174,6 +159,9 @@ bool BuildLog::RecordCommand(Edge* edge, int start_time, int end_time, log_entry->end_time = end_time; log_entry->mtime = mtime; + if (!OpenForWriteIfNeeded()) { + return false; + } if (log_file_) { if (!WriteEntry(log_file_, *log_entry)) return false; @@ -186,11 +174,36 @@ bool BuildLog::RecordCommand(Edge* edge, int start_time, int end_time, } void BuildLog::Close() { + OpenForWriteIfNeeded(); // create the file even if nothing has been recorded if (log_file_) fclose(log_file_); log_file_ = NULL; } +bool BuildLog::OpenForWriteIfNeeded() { + if (log_file_path_.empty()) { + return true; + } + log_file_ = fopen(log_file_path_.c_str(), "ab"); + if (!log_file_) { + return false; + } + setvbuf(log_file_, NULL, _IOLBF, BUFSIZ); + SetCloseOnExec(fileno(log_file_)); + + // Opening a file in append mode doesn't set the file pointer to the file's + // end on Windows. Do that explicitly. + fseek(log_file_, 0, SEEK_END); + + if (ftell(log_file_) == 0) { + if (fprintf(log_file_, kFileSignature, kCurrentVersion) < 0) { + return false; + } + } + log_file_path_.clear(); + return true; +} + struct LineReader { explicit LineReader(FILE* file) : file_(file), buf_end_(buf_), line_start_(buf_), line_end_(NULL) { |