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 <y.g.2@gmx.de>
Reviewed-on: https://git.okaestne.de/okaestne/Log/pulls/7
Co-authored-by: MrBesen <y.g.2@gmx.de>
Co-committed-by: MrBesen <y.g.2@gmx.de>
This commit is contained in:
MrBesen 2022-07-04 21:09:52 +02:00 committed by Oliver
parent 96d10d6e72
commit adf02f00c6
2 changed files with 10 additions and 10 deletions

16
Log.cpp
View File

@ -102,13 +102,13 @@ private:
class FileOutput : public Output { class FileOutput : public Output {
public: public:
FileOutput(const std::string& filename, Level lvl_max) FileOutput(const std::string& filename, Level lvl_max, bool truncate)
: Output(lvl_max), filename(filename), ofs(filename, std::ofstream::out | std::ofstream::app) {} : 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), : Output(lvl_max),
filename(filename), filename(filename),
ofs(filename, std::ofstream::out | std::ofstream::app), ofs(filename, truncate ? std::ostream::trunc : std::ostream::app),
lvl_min(lvl_min) {} lvl_min(lvl_min) {}
private: private:
@ -182,12 +182,12 @@ void stop() {
outputs.clear(); outputs.clear();
} }
void addLogfile(const std::string& filename, Level max) { void addLogfile(const std::string& filename, Level max, bool truncate) {
outputs.push_back(new FileOutput(filename, max)); outputs.push_back(new FileOutput(filename, max, truncate));
} }
void addLogfile(const std::string& filename, Level min, Level max) { void addLogfile(const std::string& filename, Level min, Level max, bool truncate) {
outputs.push_back(new FileOutput(filename, min, max)); outputs.push_back(new FileOutput(filename, min, max, truncate));
} }
void setConsoleLogLevel(Level lvl) { void setConsoleLogLevel(Level lvl) {

4
Log.h
View File

@ -15,8 +15,8 @@ void init();
// close all output streams // close all output streams
void stop(); void stop();
void addLogfile(const std::string& filename, Level max); void addLogfile(const std::string& filename, Level max, bool truncate = false);
void addLogfile(const std::string& filename, Level min, Level max); void addLogfile(const std::string& filename, Level min, Level max, bool truncate = false);
void setConsoleLogLevel(Level lvl); void setConsoleLogLevel(Level lvl);
void setColoredOutput(bool enabled); void setColoredOutput(bool enabled);