store Outputs in unique_ptrs

This commit is contained in:
Oliver 2022-09-05 00:01:31 +02:00
parent b4755b07e0
commit 29047e23d9
Signed by untrusted user: okaestne
GPG Key ID: 06A81B143EA9588F
2 changed files with 9 additions and 12 deletions

17
Log.cpp
View File

@ -2,9 +2,8 @@
#include <chrono> // date/time #include <chrono> // date/time
#include <fstream> // ofstream (logging to file) #include <fstream> // ofstream (logging to file)
#include <iomanip> // std::put_time
#include <iostream> // std::ostream, std::cout, std::cin #include <iostream> // std::ostream, std::cout, std::cin
#include <vector> // list of outputs #include <memory> // std::unique_ptr
#if LOG_USEMUTEX == 1 #if LOG_USEMUTEX == 1
#include <mutex> #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) { void log(Level lvl, std::stringbuf* strb) {
for (Output* out : outputs) { for (auto&& out : outputs) {
out->log(lvl, strb); out->log(lvl, strb);
// reset stringbuffer read pointer to the beginning // reset stringbuffer read pointer to the beginning
strb->pubseekpos(0); strb->pubseekpos(0);
@ -187,7 +186,7 @@ std::ostream& defaultEntryMetaLevel(std::ostream& os, const Entry& e) {
void init() { void init() {
// add default console logger // add default console logger
if (outputs.empty()) if (outputs.empty())
outputs.push_back(new ConsoleOutput()); outputs.push_back(std::unique_ptr<Output>(new ConsoleOutput()));
// set default entry metadata printing functions // set default entry metadata printing functions
auto space = [](std::ostream& os, const Entry& e) -> std::ostream& { auto space = [](std::ostream& os, const Entry& e) -> std::ostream& {
@ -202,17 +201,15 @@ void init() {
} }
void stop() { void stop() {
for (auto output : outputs)
delete output;
outputs.clear(); outputs.clear();
} }
void addLogfile(const std::string& filename, Level max, bool truncate) { 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) { 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) { void setConsoleLogLevel(Level lvl) {
@ -220,7 +217,7 @@ void setConsoleLogLevel(Level lvl) {
} }
void setColoredOutput(bool enabled) { 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 } // namespace Log

4
Log.h
View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <functional> #include <functional> // std::function
#include <sstream> // std::stringstream (buffer for log entries) #include <sstream> // std::stringstream (buffer for log entries)
#include <string> #include <string>
#include <vector> #include <vector>