diff --git a/Log.cpp b/Log.cpp index 80c7aa9..2d30e11 100644 --- a/Log.cpp +++ b/Log.cpp @@ -2,9 +2,8 @@ #include // date/time #include // ofstream (logging to file) -#include // std::put_time #include // std::ostream, std::cout, std::cin -#include // list of outputs +#include // std::unique_ptr #if LOG_USEMUTEX == 1 #include @@ -128,10 +127,10 @@ private: } }; -static std::vector outputs; +static std::vector> outputs; void log(Level lvl, std::stringbuf* strb) { - for (Output* out : outputs) { + for (auto&& out : outputs) { out->log(lvl, strb); // reset stringbuffer read pointer to the beginning strb->pubseekpos(0); @@ -187,7 +186,7 @@ std::ostream& defaultEntryMetaLevel(std::ostream& os, const Entry& e) { void init() { // add default console logger if (outputs.empty()) - outputs.push_back(new ConsoleOutput()); + outputs.push_back(std::unique_ptr(new ConsoleOutput())); // set default entry metadata printing functions auto space = [](std::ostream& os, const Entry& e) -> std::ostream& { @@ -202,17 +201,15 @@ void init() { } void stop() { - for (auto output : outputs) - delete output; outputs.clear(); } void addLogfile(const std::string& filename, Level max, bool truncate) { - outputs.push_back(new FileOutput(filename, max, truncate)); + outputs.push_back(std::unique_ptr(new FileOutput(filename, max, truncate))); } void addLogfile(const std::string& filename, Level min, Level max, bool truncate) { - outputs.push_back(new FileOutput(filename, min, max, truncate)); + outputs.push_back(std::unique_ptr(new FileOutput(filename, min, max, truncate))); } void setConsoleLogLevel(Level lvl) { @@ -220,7 +217,7 @@ void setConsoleLogLevel(Level lvl) { } void setColoredOutput(bool enabled) { - ((ConsoleOutput*) outputs.at(0))->setColoredOutput(enabled); // has to exist + dynamic_cast(*outputs.at(0)).setColoredOutput(enabled); // has to exist } } // namespace Log \ No newline at end of file diff --git a/Log.h b/Log.h index ffd2be9..c196587 100644 --- a/Log.h +++ b/Log.h @@ -1,7 +1,7 @@ #pragma once -#include -#include // std::stringstream (buffer for log entries) +#include // std::function +#include // std::stringstream (buffer for log entries) #include #include