aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntony Woods <acron1@gmail.com>2014-05-20 23:30:51 +0100
committerAntony Woods <acron1@gmail.com>2014-05-20 23:30:51 +0100
commit1ae0387a255c7ec891179e3880579368b6cc588d (patch)
tree806b8b20e50b1ac3fa3204e3be30b5f87c8fd7cf
parentdccb246cf8ba7b5480320088a817c29a4a7c8da3 (diff)
downloadcrow-1ae0387a255c7ec891179e3880579368b6cc588d.tar.gz
crow-1ae0387a255c7ec891179e3880579368b6cc588d.zip
Added log level support
-rw-r--r--crow.h3
-rw-r--r--http_connection.h2
-rw-r--r--http_server.h2
-rw-r--r--logging.h20
-rw-r--r--routing.h20
-rw-r--r--settings.h13
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<self_t> 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, ' ') /*<< "("<<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);
}
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