store Outputs in unique_ptrs
This commit is contained in:
parent
b4755b07e0
commit
29047e23d9
17
Log.cpp
17
Log.cpp
|
@ -2,9 +2,8 @@
|
|||
|
||||
#include <chrono> // date/time
|
||||
#include <fstream> // ofstream (logging to file)
|
||||
#include <iomanip> // std::put_time
|
||||
#include <iostream> // std::ostream, std::cout, std::cin
|
||||
#include <vector> // list of outputs
|
||||
#include <memory> // std::unique_ptr
|
||||
|
||||
#if LOG_USEMUTEX == 1
|
||||
#include <mutex>
|
||||
|
@ -128,10 +127,10 @@ private:
|
|||
}
|
||||
};
|
||||
|
||||
static std::vector<Output*> outputs;
|
||||
static std::vector<std::unique_ptr<Output>> 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<Output>(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<Output>(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<Output>(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<ConsoleOutput&>(*outputs.at(0)).setColoredOutput(enabled); // has to exist
|
||||
}
|
||||
|
||||
} // namespace Log
|
Loading…
Reference in New Issue