Skip to content

Commit

Permalink
Changed log file to flush after each line
Browse files Browse the repository at this point in the history
* Log file buffering makes it more difficult to monitor scan
  progress from another session because it may take a while
  for a file buffer to fill up before it is flushed. Now one
  of the threads printing to a log file will flush the buffer
  • Loading branch information
StoneStepsInc committed Nov 5, 2023
1 parent 7ffd78b commit 3f93501
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/fit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,7 @@ int main(int argc, char *argv[])
std::unique_ptr<FILE, fit::file_handle_deleter_t> log_file;

if(!options.log_file.empty()) {
// open the log file in binary mode, where supported, so character encoding is preserved
log_file.reset(fopen(reinterpret_cast<const char*>(options.log_file.c_str()), "ab"));

if(!log_file)
Expand Down
8 changes: 7 additions & 1 deletion src/print_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ print_stream_t::~print_stream_t(void)

void print_stream_t::print(FILE *stream, const char *prefix, const char *fmt, va_list valist)
{
std::lock_guard<std::mutex> lock(print_mtx);
std::unique_lock<std::mutex> lock(print_mtx);

va_list ap;

Expand All @@ -47,6 +47,12 @@ void print_stream_t::print(FILE *stream, const char *prefix, const char *fmt, va

fputc('\n', print_stream);
}

lock.unlock();

// flush log file on one thread to allow tailing it from another session
if(print_stream && lock.try_lock())
fflush(print_stream);
}

void print_stream_t::info(const char *fmt, ...)
Expand Down

0 comments on commit 3f93501

Please sign in to comment.