Added primitive logging

This commit is contained in:
Antony Woods 2014-05-20 17:17:56 +01:00
parent 62ace917d3
commit dccb246cf8
7 changed files with 92 additions and 17 deletions

View File

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

3
crow.h
View File

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

View File

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

View File

@ -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_;
};
}

68
logging.h Normal file
View File

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

View File

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

7
settings.h Normal file
View File

@ -0,0 +1,7 @@
// settings for crow
/* #ifdef - enables debug mode */
#define CROW_ENABLE_DEBUG
/* #ifdef - enables logging */
#define CROW_ENABLE_LOGGING