From adf02f00c61f284764021274b63e6e11e2d1db4f Mon Sep 17 00:00:00 2001 From: MrBesen Date: Mon, 4 Jul 2022 21:09:52 +0200 Subject: [PATCH] truncMode: truncate logfiles (#7) Add a flag, to truncate the logfiles. Default value is false. false = append on every write (like it is right now) true = truncate file when logfile is opened Co-authored-by: mrbesen Reviewed-on: https://git.okaestne.de/okaestne/Log/pulls/7 Co-authored-by: MrBesen Co-committed-by: MrBesen --- Log.cpp | 16 ++++++++-------- Log.h | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Log.cpp b/Log.cpp index 12f8860..408f6a6 100644 --- a/Log.cpp +++ b/Log.cpp @@ -102,13 +102,13 @@ private: class FileOutput : public Output { public: - FileOutput(const std::string& filename, Level lvl_max) - : Output(lvl_max), filename(filename), ofs(filename, std::ofstream::out | std::ofstream::app) {} + FileOutput(const std::string& filename, Level lvl_max, bool truncate) + : Output(lvl_max), filename(filename), ofs(filename, truncate ? std::ostream::trunc : std::ostream::app) {} - FileOutput(const std::string& filename, Level lvl_min, Level lvl_max) + FileOutput(const std::string& filename, Level lvl_min, Level lvl_max, bool truncate) : Output(lvl_max), filename(filename), - ofs(filename, std::ofstream::out | std::ofstream::app), + ofs(filename, truncate ? std::ostream::trunc : std::ostream::app), lvl_min(lvl_min) {} private: @@ -182,12 +182,12 @@ void stop() { outputs.clear(); } -void addLogfile(const std::string& filename, Level max) { - outputs.push_back(new FileOutput(filename, max)); +void addLogfile(const std::string& filename, Level max, bool truncate) { + outputs.push_back(new FileOutput(filename, max, truncate)); } -void addLogfile(const std::string& filename, Level min, Level max) { - outputs.push_back(new FileOutput(filename, min, max)); +void addLogfile(const std::string& filename, Level min, Level max, bool truncate) { + outputs.push_back(new FileOutput(filename, min, max, truncate)); } void setConsoleLogLevel(Level lvl) { diff --git a/Log.h b/Log.h index 0107bf4..ea7b06d 100644 --- a/Log.h +++ b/Log.h @@ -15,8 +15,8 @@ void init(); // close all output streams void stop(); -void addLogfile(const std::string& filename, Level max); -void addLogfile(const std::string& filename, Level min, Level max); +void addLogfile(const std::string& filename, Level max, bool truncate = false); +void addLogfile(const std::string& filename, Level min, Level max, bool truncate = false); void setConsoleLogLevel(Level lvl); void setColoredOutput(bool enabled);