Added log level support

This commit is contained in:
Antony Woods 2014-05-20 23:30:51 +01:00
parent dccb246cf8
commit 1ae0387a25
6 changed files with 43 additions and 17 deletions

3
crow.h
View File

@ -69,9 +69,10 @@ namespace crow
Server<self_t> server(this, port_, concurrency_);
server.run();
}
void debug_print()
{
std::cerr << "Routing:" << std::endl;
CROW_LOG_DEBUG << "Routing:";
router_.debug_print();
}

View File

@ -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 = ": ";

View File

@ -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){

View File

@ -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)

View File

@ -490,35 +490,35 @@ public:
{
if (n->param_childrens[i])
{
std::cerr << std::string(2*level, ' ') /*<< "("<<n->param_childrens[i]<<") "*/;
CROW_LOG_DEBUG << std::string(2*level, ' ') /*<< "("<<n->param_childrens[i]<<") "*/;
switch((ParamType)i)
{
case ParamType::INT:
std::cerr << "<int>";
CROW_LOG_DEBUG << "<int>";
break;
case ParamType::UINT:
std::cerr << "<uint>";
CROW_LOG_DEBUG << "<uint>";
break;
case ParamType::DOUBLE:
std::cerr << "<float>";
CROW_LOG_DEBUG << "<float>";
break;
case ParamType::STRING:
std::cerr << "<str>";
CROW_LOG_DEBUG << "<str>";
break;
case ParamType::PATH:
std::cerr << "<path>";
CROW_LOG_DEBUG << "<path>";
break;
default:
std::cerr << "<ERROR>";
CROW_LOG_DEBUG << "<ERROR>";
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);
}

View File

@ -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
#define CROW_ENABLE_LOGGING
/* #define - specifies log level */
/*
CRITICAL = 0
ERROR = 1
WARNING = 2
INFO = 3
DEBUG = 4
*/
#define CROW_LOG_LEVEL 4