Inject logger instead of using a singleton

This commit is contained in:
2019-06-16 11:33:23 +02:00
parent 14740e4a64
commit 9ba225cbde
14 changed files with 76 additions and 56 deletions

View File

@@ -15,7 +15,7 @@ void ConnectionOperator::SendResponse(Connection const & connection, Http::Respo
}
catch(std::runtime_error & e)
{
Logger::GetInstance().Error("Error writing data to connection");
logger.Error("Error writing data to connection");
}
}
@@ -34,7 +34,7 @@ void ConnectionOperator::HandleNewConnection(Connection const & newConnection)
ss << "Error during parsing of request <";
ss << e.what();
ss << '>';
Logger::GetInstance().Error(ss.str());
logger.Error(ss.str());
response.code = HttpResponse::Code::BAD_REQUEST;
SendResponse(newConnection, response);
@@ -54,7 +54,7 @@ void ConnectionOperator::HandleNewConnection(Connection const & newConnection)
ss << " request for file <";
ss << request.url.GetPath();
ss << '>';
Logger::GetInstance().Error(ss.str());
logger.Error(ss.str());
response.code = HttpResponse::Code::NOT_IMPLEMENTED;
SendResponse(newConnection, response);
@@ -65,15 +65,16 @@ void ConnectionOperator::HandleNewConnection(Connection const & newConnection)
newConnection.WriteBytes(bytesToSend);
}
ConnectionOperator::ConnectionOperator()
ConnectionOperator::ConnectionOperator(Logger & _logger)
: logger(_logger)
{
// Base static file server
auto const & staticFileRoot = ServerConfiguration::GetInstance().GetWwwRoot();
if (staticFileRoot.size() > 0)
{
middlewares.emplace_back(std::make_unique<Middleware::StaticContent>(staticFileRoot));
middlewares.emplace_back(std::make_unique<Middleware::StaticContent>(_logger, staticFileRoot));
}
// ALWAYS LAST!
middlewares.emplace_back(std::make_unique<Middleware::NotFound>());
middlewares.emplace_back(std::make_unique<Middleware::NotFound>(_logger));
}