diff options
author | Antony Woods <acron1@gmail.com> | 2014-05-20 17:17:56 +0100 |
---|---|---|
committer | Antony Woods <acron1@gmail.com> | 2014-05-20 17:17:56 +0100 |
commit | dccb246cf8ba7b5480320088a817c29a4a7c8da3 (patch) | |
tree | c251f3ab13aaf3b8a5f76afe0d3ddd100db0c3b6 | |
parent | 62ace917d325040bbf3015536d2ad3725b711c6d (diff) | |
download | crow-dccb246cf8ba7b5480320088a817c29a4a7c8da3.tar.gz crow-dccb246cf8ba7b5480320088a817c29a4a7c8da3.zip |
Added primitive logging
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | crow.h | 3 | ||||
-rw-r--r-- | http_connection.h | 9 | ||||
-rw-r--r-- | http_server.h | 10 | ||||
-rw-r--r-- | logging.h | 68 | ||||
-rw-r--r-- | routing.h | 6 | ||||
-rw-r--r-- | settings.h | 7 |
7 files changed, 92 insertions, 17 deletions
@@ -1,5 +1,5 @@ all: covtest example -example: example.cpp crow.h http_server.h http_connection.h parser.h http_response.h routing.h common.h utility.h json.h datetime.h +example: example.cpp settings.h crow.h http_server.h http_connection.h parser.h http_response.h routing.h common.h utility.h json.h datetime.h logging.h g++ -Wall -g -O3 -std=c++11 -o example example.cpp http-parser/http_parser.c -pthread -lboost_system -lboost_thread -ltcmalloc_minimal -I http-parser/ test: covtest @@ -10,11 +10,11 @@ runtest: example python test.py || exit 0 pkill example -unittest: unittest.cpp routing.h utility.h crow.h http_server.h http_connection.h parser.h http_response.h common.h json.h datetime.h +unittest: unittest.cpp routing.h utility.h crow.h http_server.h http_connection.h parser.h http_response.h common.h json.h datetime.h logging.h g++ -Wall -g -std=c++11 -o unittest unittest.cpp http-parser/http_parser.c -pthread -lboost_system -lboost_thread -I http-parser/ ./unittest -covtest: unittest.cpp routing.h utility.h crow.h http_server.h http_connection.h parser.h http_response.h common.h json.h datetime.h +covtest: unittest.cpp routing.h utility.h crow.h http_server.h http_connection.h parser.h http_response.h common.h json.h datetime.h logging.h g++ -Wall -g -std=c++11 -o covtest --coverage unittest.cpp http-parser/http_parser.c -pthread -lboost_system -lboost_thread -I http-parser/ ./covtest gcov -r unittest.cpp @@ -7,8 +7,7 @@ #include <type_traits> #include <thread> -//#define CROW_ENABLE_LOGGING - +#include "settings.h" #include "http_server.h" #include "utility.h" #include "routing.h" diff --git a/http_connection.h b/http_connection.h index b995523..7dc32e1 100644 --- a/http_connection.h +++ b/http_connection.h @@ -11,6 +11,7 @@ #include "datetime.h" #include "parser.h" #include "http_response.h" +#include "logging.h" namespace crow { @@ -73,11 +74,9 @@ namespace crow res = handler_->handle(req); -#ifdef CROW_ENABLE_LOGGING - std::cerr << "HTTP/" << parser_.http_major << "." << parser_.http_minor << ' '; - std::cerr << method_name(req.method); - std::cerr << " " << res.code << ' ' <<close_connection_<<std::endl; -#endif + CROW_LOG_INFO << "HTTP/" << parser_.http_major << "." << parser_.http_minor << ' ' + << method_name(req.method) + << " " << res.code << ' ' << close_connection_; static std::string seperator = ": "; static std::string crlf = "\r\n"; diff --git a/http_server.h b/http_server.h index 387f2e5..7d76c84 100644 --- a/http_server.h +++ b/http_server.h @@ -6,9 +6,7 @@ #include "http_connection.h" #include "datetime.h" - -// TEST -#include <iostream> +#include "logging.h" namespace crow { @@ -23,7 +21,8 @@ namespace crow socket_(io_service_), signals_(io_service_, SIGINT, SIGTERM), handler_(handler), - concurrency_(concurrency) + concurrency_(concurrency), + port_(port) { do_accept(); } @@ -36,6 +35,8 @@ namespace crow std::async(std::launch::async, [this]{io_service_.run();}) ); + CROW_LOG_INFO << "Server is running, local port " << port_; + signals_.async_wait( [&](const boost::system::error_code& error, int signal_number){ io_service_.stop(); @@ -70,5 +71,6 @@ namespace crow uint16_t concurrency_ = {1}; std::string server_name_ = "Crow/0.1"; + uint16_t port_; }; } diff --git a/logging.h b/logging.h new file mode 100644 index 0000000..5a23303 --- /dev/null +++ b/logging.h @@ -0,0 +1,68 @@ +#pragma once + +#include <string> +#include <chrono> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> + +using namespace std; + +string timeStamp() +{ + char date[32]; + time_t t = time(0); + strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", gmtime(&t)); + return string(date); +} + +class logger { + + public: + + // + enum class Level + { + CRITICAL, + ERROR, + WARNING, + INFO, + DEBUG + }; + + // + 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; +#endif + } + + // + template <typename T> + logger& operator<<(T const &value) { + +#ifdef CROW_ENABLE_LOGGING + m_stringStream << value; +#endif + return *this; + } + + private: + + // + ostringstream m_stringStream; + string m_prefix; + Level m_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) +#define CROW_LOG_INFO logger("INFO ", logger::Level::INFO) +#define CROW_LOG_DEBUG logger("DEBUG ", logger::Level::DEBUG) + + + @@ -584,9 +584,9 @@ public: if (rule_index >= rules_.size()) throw std::runtime_error("Trie internal structure corrupted!"); -#ifdef CROW_ENABLE_LOGGING - std::cerr << req.url << ' ' << ((TaggedRule<>*)rules_[rule_index].get())->rule_ << std::endl; -#endif + + CROW_LOG_INFO << req.url << ' ' << ((TaggedRule<>*)rules_[rule_index].get())->rule_; + return rules_[rule_index]->handle(req, found.second); } diff --git a/settings.h b/settings.h new file mode 100644 index 0000000..251df69 --- /dev/null +++ b/settings.h @@ -0,0 +1,7 @@ +// settings for crow + +/* #ifdef - enables debug mode */ +#define CROW_ENABLE_DEBUG + +/* #ifdef - enables logging */ +#define CROW_ENABLE_LOGGING
\ No newline at end of file |