From c41e7ce65ff3b758f72b396e98345f87a6fddbd8 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 4 May 2017 10:00:49 +0100 Subject: [PATCH] Add more logging --- src/CMakeLists.txt | 2 ++ src/main.cpp | 3 ++- src/response.cpp | 23 ++++++++++++++++++++++- src/response_factory.cpp | 3 ++- src/settings_bag.cpp | 17 ++++++++++++++++- src/settings_bag.hpp | 4 ++++ 6 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2aec511..31931c5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -57,6 +57,8 @@ target_link_libraries(${PROJECT_NAME} ) target_compile_definitions(${PROJECT_NAME} PRIVATE BOOST_SPIRIT_USE_PHOENIX_V3=1 + PRIVATE $<$:SPDLOG_DEBUG_ON> + PRIVATE $<$:SPDLOG_TRACE_ON> ) target_compile_options(${PROJECT_NAME} PRIVATE -fdiagnostics-color=always diff --git a/src/main.cpp b/src/main.cpp index 8c30f26..e062613 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -85,7 +85,7 @@ int main() { spdlog::set_level(spdlog::level::trace); //set to maximum possible here auto statuslog = spdlog::stderr_logger_st("statuslog"); - statuslog->debug("Loading config: \"{}\"\n", config_file_path()); + statuslog->info("Loading config: \"{}\"", config_file_path()); std::ifstream conf(config_file_path()); conf >> std::noskipws; @@ -102,6 +102,7 @@ int main() { tawashi::cgi::Env cgi_env; tawashi::ResponseFactory resp_factory(settings); + SPDLOG_TRACE(statuslog, "Registering makers in the response factory"); resp_factory.register_maker("index.cgi", &make_response); resp_factory.register_maker("", &make_response); resp_factory.register_maker("paste.cgi", &make_response); diff --git a/src/response.cpp b/src/response.cpp index 9fcf947..368f838 100644 --- a/src/response.cpp +++ b/src/response.cpp @@ -29,6 +29,7 @@ #include #include #include +#include namespace tawashi { namespace { @@ -74,7 +75,7 @@ namespace tawashi { boost::optional load_whole_file (const std::string& parWebsiteRoot, const char* parSuffix, const std::string& parName, bool parThrow) { std::ostringstream oss; oss << parWebsiteRoot << parName << parSuffix; - std::cerr << "Trying to load \"" << oss.str() << "\"\n"; + spdlog::get("statuslog")->debug("Trying to load \"{}\"", oss.str()); std::ifstream if_mstch(oss.str(), std::ios::binary | std::ios::in); if (not if_mstch) { @@ -118,6 +119,14 @@ namespace tawashi { } mstch::config::escape = &disable_mstch_escaping; + + auto statuslog = spdlog::get("statuslog"); + assert(statuslog); + statuslog->info("Preparing Response for {} request to {}; size: {}", + cgi_env().request_method(), + cgi_env().query_string(), + cgi_env().content_length() + ); } Response::~Response() noexcept = default; @@ -129,6 +138,11 @@ namespace tawashi { } void Response::send() { + auto statuslog = spdlog::get("statuslog"); + assert(statuslog); + + statuslog->info("Sending response"); + SPDLOG_TRACE(statuslog, "Preparing mustache dictionary"); mstch::map mustache_context { {"version", std::string{STRINGIZE(VERSION_MAJOR) "." STRINGIZE(VERSION_MINOR) "." STRINGIZE(VERSION_PATCH)}}, {"base_uri", m_settings->as("base_uri")}, @@ -136,6 +150,7 @@ namespace tawashi { }; if (m_redis) { + SPDLOG_TRACE(statuslog, "Finalizing redis connection"); m_redis->wait_for_connect(); auto batch = m_redis->make_batch(); batch.select(m_settings->as("redis_db")); @@ -143,19 +158,24 @@ namespace tawashi { batch.throw_if_failed(); } + SPDLOG_TRACE(statuslog, "Raising event on_process"); this->on_process(); + SPDLOG_TRACE(statuslog, "Raising event on_mustache_prepare"); this->on_mustache_prepare(mustache_context); m_header_sent = true; switch (m_resp_type) { case ContentType: + SPDLOG_TRACE(statuslog, "Response is a Content-type (data)"); std::cout << "Content-type: " << m_resp_value << "\n\n"; break; case Location: + SPDLOG_TRACE(statuslog, "Response is a Location (redirect)"); std::cout << "Location: " << m_resp_value << "\n\n"; break; } + SPDLOG_TRACE(statuslog, "Rendering in mustache"); std::cout << mstch::render( on_mustache_retrieve(), mustache_context, @@ -167,6 +187,7 @@ namespace tawashi { false ) ); + SPDLOG_TRACE(statuslog, "Flushing output"); std::cout.flush(); } diff --git a/src/response_factory.cpp b/src/response_factory.cpp index 9d734e0..f1bd7b7 100644 --- a/src/response_factory.cpp +++ b/src/response_factory.cpp @@ -34,12 +34,13 @@ namespace tawashi { m_local_data(std::make_unique()) { m_local_data->settings = parSettings; - } ResponseFactory::~ResponseFactory() noexcept = default; std::unique_ptr ResponseFactory::make_response (const boost::string_ref& parName) { + //spdlog::get("statuslog")->info("making response object for \"{}\"", parName); + auto maker_it = m_local_data->makers.find(std::string(parName.data(), parName.size())); if (m_local_data->makers.end() != maker_it) { return maker_it->second(m_local_data->settings); diff --git a/src/settings_bag.cpp b/src/settings_bag.cpp index 221a8a2..bbb8277 100644 --- a/src/settings_bag.cpp +++ b/src/settings_bag.cpp @@ -21,11 +21,26 @@ #include #include #include +#include namespace tawashi { + namespace { + const IniFile::KeyValueMapType* get_tawashi_node (const IniFile& parIni) { + auto it_found = parIni.parsed().find("tawashi"); + if (parIni.parsed().end() != it_found) { + return &it_found->second; + } + else { + spdlog::get("statuslog")->warn("Couldn't find section [tawashi] in the settings file"); + static const IniFile::KeyValueMapType empty_key_values; + return &empty_key_values; + } + } + } //unnamed namespace + SettingsBag::SettingsBag (const Kakoune::SafePtr& parIni) : m_ini(parIni), - m_values(&parIni->parsed().at("tawashi")) + m_values(get_tawashi_node(*parIni)) { assert(m_values); } diff --git a/src/settings_bag.hpp b/src/settings_bag.hpp index c7f0e66..3610699 100644 --- a/src/settings_bag.hpp +++ b/src/settings_bag.hpp @@ -23,6 +23,9 @@ #include #include #include +#if defined(SPDLOG_DEBUG_ON) +# include +#endif namespace tawashi { class SettingsBag : public Kakoune::SafeCountable { @@ -48,6 +51,7 @@ namespace tawashi { } inline const boost::string_ref& SettingsBag::at (boost::string_ref parIndex) const { + SPDLOG_DEBUG(spdlog::get("statuslog"), "Retrieving setting \"{}\"", std::string(parIndex.data(), parIndex.size())); return (*this)[parIndex]; } } //namespace tawashi