From 38e562dd307ff178d08d2aad132706488a31a45a Mon Sep 17 00:00:00 2001 From: Jon Siwek Date: Thu, 25 Aug 2011 10:00:40 -0500 Subject: [PATCH] Change ASCII writer to delay creation of log after rotation until next write. When a given log was rotated, a new log was opened immediately. If that log was never written to again, those empty logs perpetually rotate, too. This change makes it so a log won't be created for a given rotation interval if there was nothing written to it in that interval. --- src/LogWriterAscii.cc | 13 ++++++++++--- .../policy.frameworks.logging.rotate-custom/out | 2 -- testing/btest/policy/frameworks/logging/rotate.bro | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/LogWriterAscii.cc b/src/LogWriterAscii.cc index c537587d6a..fc70730d86 100644 --- a/src/LogWriterAscii.cc +++ b/src/LogWriterAscii.cc @@ -216,6 +216,9 @@ bool LogWriterAscii::DoWriteOne(ODesc* desc, LogVal* val, const LogField* field) bool LogWriterAscii::DoWrite(int num_fields, const LogField* const * fields, LogVal** vals) { + if ( ! file ) + DoInit(Path(), NumFields(), Fields()); + ODesc desc(DESC_READABLE); desc.SetEscape(separator, separator_len); @@ -245,19 +248,23 @@ bool LogWriterAscii::DoWrite(int num_fields, const LogField* const * fields, bool LogWriterAscii::DoRotate(string rotated_path, double open, double close, bool terminating) { - if ( IsSpecial(Path()) ) - // Don't rotate special files. + // Don't rotate special files or if there's not one currently open. + if ( ! file || IsSpecial(Path()) ) return true; fclose(file); + file = 0; string nname = rotated_path + ".log"; rename(fname.c_str(), nname.c_str()); if ( ! FinishedRotation(nname, fname, open, close, terminating) ) + { Error(Fmt("error rotating %s to %s", fname.c_str(), nname.c_str())); + return false; + } - return DoInit(Path(), NumFields(), Fields()); + return true; } bool LogWriterAscii::DoSetBuf(bool enabled) diff --git a/testing/btest/Baseline/policy.frameworks.logging.rotate-custom/out b/testing/btest/Baseline/policy.frameworks.logging.rotate-custom/out index 18bd12d88f..5fd6486543 100644 --- a/testing/btest/Baseline/policy.frameworks.logging.rotate-custom/out +++ b/testing/btest/Baseline/policy.frameworks.logging.rotate-custom/out @@ -59,7 +59,6 @@ custom rotate, [writer=WRITER_ASCII, fname=test2-11-03-07_12.59.55.log, path=tes > test.2011-03-07-10-00-05.log > test.2011-03-07-11-00-05.log > test.2011-03-07-12-00-05.log -> test.log > test2-11-03-07_03.00.05.log > test2-11-03-07_03.59.55.log > test2-11-03-07_04.00.05.log @@ -80,4 +79,3 @@ custom rotate, [writer=WRITER_ASCII, fname=test2-11-03-07_12.59.55.log, path=tes > test2-11-03-07_11.59.55.log > test2-11-03-07_12.00.05.log > test2-11-03-07_12.59.55.log -> test2.log diff --git a/testing/btest/policy/frameworks/logging/rotate.bro b/testing/btest/policy/frameworks/logging/rotate.bro index d53b92f169..4b496689f5 100644 --- a/testing/btest/policy/frameworks/logging/rotate.bro +++ b/testing/btest/policy/frameworks/logging/rotate.bro @@ -1,5 +1,5 @@ # -# @TEST-EXEC: bro -r %DIR/rotation.trace %INPUT 2>&1 | grep "test" >out +# @TEST-EXEC: bro -b -r %DIR/rotation.trace %INPUT 2>&1 | grep "test" >out # @TEST-EXEC: for i in test.*.log; do printf '> %s\n' $i; cat $i; done >>out # @TEST-EXEC: btest-diff out