From 1ae0387a255c7ec891179e3880579368b6cc588d Mon Sep 17 00:00:00 2001 From: Antony Woods Date: Tue, 20 May 2014 23:30:51 +0100 Subject: Added log level support --- crow.h | 3 ++- http_connection.h | 2 +- http_server.h | 2 +- logging.h | 20 +++++++++++++++++--- routing.h | 20 ++++++++++---------- settings.h | 13 ++++++++++++- 6 files changed, 43 insertions(+), 17 deletions(-) diff --git a/crow.h b/crow.h index ade49aa..d2b71af 100644 --- a/crow.h +++ b/crow.h @@ -69,9 +69,10 @@ namespace crow Server server(this, port_, concurrency_); server.run(); } + void debug_print() { - std::cerr << "Routing:" << std::endl; + CROW_LOG_DEBUG << "Routing:"; router_.debug_print(); } diff --git a/http_connection.h b/http_connection.h index 7dc32e1..b90d5ed 100644 --- a/http_connection.h +++ b/http_connection.h @@ -75,7 +75,7 @@ namespace crow res = handler_->handle(req); CROW_LOG_INFO << "HTTP/" << parser_.http_major << "." << parser_.http_minor << ' ' - << method_name(req.method) + << method_name(req.method) << " " << req.url << " " << res.code << ' ' << close_connection_; static std::string seperator = ": "; diff --git a/http_server.h b/http_server.h index 7d76c84..3ca88f2 100644 --- a/http_server.h +++ b/http_server.h @@ -35,7 +35,7 @@ namespace crow std::async(std::launch::async, [this]{io_service_.run();}) ); - CROW_LOG_INFO << "Server is running, local port " << port_; + CROW_LOG_INFO << server_name_ << " server is running, local port " << port_; signals_.async_wait( [&](const boost::system::error_code& error, int signal_number){ diff --git a/logging.h b/logging.h index 5a23303..dc610f0 100644 --- a/logging.h +++ b/logging.h @@ -31,12 +31,16 @@ class logger { }; // + static Level currentLevel; + logger(string prefix, logger::Level level) : m_prefix(prefix), m_level(level) { } ~logger() { #ifdef CROW_ENABLE_LOGGING - cerr << "(" << timeStamp() << ") [" << m_prefix << "] " << m_stringStream.str() << endl; + if(m_level <= currentLevel) { + cerr << "(" << timeStamp() << ") [" << m_prefix << "] " << m_stringStream.str() << endl; + } #endif } @@ -45,12 +49,19 @@ class logger { logger& operator<<(T const &value) { #ifdef CROW_ENABLE_LOGGING - m_stringStream << value; + if(m_level <= currentLevel) { + m_stringStream << value; + } #endif return *this; } - private: + // + static void setLogLevel(logger::Level level) { + currentLevel = level; + } + + private: // ostringstream m_stringStream; @@ -58,6 +69,9 @@ class logger { Level m_level; }; +// +logger::Level logger::currentLevel = (Level)CROW_LOG_LEVEL; + #define CROW_LOG_CRITICAL logger("CRITICAL", logger::Level::CRITICAL) #define CROW_LOG_ERROR logger("ERROR ", logger::Level::ERROR) #define CROW_LOG_WARNING logger("WARNING ", logger::Level::WARNING) diff --git a/routing.h b/routing.h index a338d4a..c3be5fd 100644 --- a/routing.h +++ b/routing.h @@ -490,35 +490,35 @@ public: { if (n->param_childrens[i]) { - std::cerr << std::string(2*level, ' ') /*<< "("<param_childrens[i]<<") "*/; + CROW_LOG_DEBUG << std::string(2*level, ' ') /*<< "("<param_childrens[i]<<") "*/; switch((ParamType)i) { case ParamType::INT: - std::cerr << ""; + CROW_LOG_DEBUG << ""; break; case ParamType::UINT: - std::cerr << ""; + CROW_LOG_DEBUG << ""; break; case ParamType::DOUBLE: - std::cerr << ""; + CROW_LOG_DEBUG << ""; break; case ParamType::STRING: - std::cerr << ""; + CROW_LOG_DEBUG << ""; break; case ParamType::PATH: - std::cerr << ""; + CROW_LOG_DEBUG << ""; break; default: - std::cerr << ""; + CROW_LOG_DEBUG << ""; break; } - std::cerr << std::endl; + debug_node_print(&nodes_[n->param_childrens[i]], level+1); } } for(auto& kv : n->children) { - std::cerr << std::string(2*level, ' ') /*<< "(" << kv.second << ") "*/ << kv.first << std::endl; + CROW_LOG_DEBUG << std::string(2*level, ' ') /*<< "(" << kv.second << ") "*/ << kv.first; debug_node_print(&nodes_[kv.second], level+1); } } @@ -585,7 +585,7 @@ public: if (rule_index >= rules_.size()) throw std::runtime_error("Trie internal structure corrupted!"); - CROW_LOG_INFO << req.url << ' ' << ((TaggedRule<>*)rules_[rule_index].get())->rule_; + CROW_LOG_DEBUG << "Matched rule '" << ((TaggedRule<>*)rules_[rule_index].get())->rule_ << "'"; return rules_[rule_index]->handle(req, found.second); } diff --git a/settings.h b/settings.h index 251df69..563fb1b 100644 --- a/settings.h +++ b/settings.h @@ -1,7 +1,18 @@ // settings for crow +// TODO - replace with runtime config. libucl? /* #ifdef - enables debug mode */ #define CROW_ENABLE_DEBUG /* #ifdef - enables logging */ -#define CROW_ENABLE_LOGGING \ No newline at end of file +#define CROW_ENABLE_LOGGING + +/* #define - specifies log level */ +/* + CRITICAL = 0 + ERROR = 1 + WARNING = 2 + INFO = 3 + DEBUG = 4 +*/ +#define CROW_LOG_LEVEL 4 \ No newline at end of file -- cgit v1.2.3-54-g00ecf