Use the Logger class in ev_server

JSON-90
Roker 5 years ago
parent b43edf229a
commit 7f67dad613

@ -9,6 +9,7 @@
#include "json_rpc.hh"
#include "pep-utils.hh"
#include "gpg_environment.hh"
#include "logger.hh"
#include "server_version.hh"
#include <pEp/message_api.h>
@ -169,8 +170,9 @@ void ev_server::sendReplyString(evhttp_request* req, const char* contentType, co
evhttp_send_reply(req, 500, "evbuffer_add() failed.", outBuf);
}
Log() << "\n=== sendReplyString(): ret=" << ret << ", contentType=" << (contentType ? "«" + std::string(contentType)+ "»" : "NULL")
<< ", output=«" << outputText << "»." << std::endl;
Log() << Logger::Debug << "sendReplyString(): ret=" << ret
<< ", contentType=" << (contentType ? "«" + std::string(contentType)+ "»" : "NULL")
<< ", output=«" << outputText << "».";
}
@ -208,7 +210,7 @@ void ev_server::OnOtherRequest(evhttp_request* req, void*)
const evhttp_uri* uri = evhttp_request_get_evhttp_uri(req);
const char* path = evhttp_uri_get_path(uri);
const char* uri_string = evhttp_request_get_uri(req);
Log() << "** Request: [" << uri_string << "] " << (path? " Path: [" + std::string(path) + "]" : "null path") << "\n";
Log() << Logger::Debug << "** Request: [" << uri_string << "] " << (path? " Path: [" + std::string(path) + "]" : "null path") << "\n";
try{
if(path)
@ -216,7 +218,7 @@ void ev_server::OnOtherRequest(evhttp_request* req, void*)
const auto q = files.find(path);
if(q != files.end()) // found in "files" map
{
Log() << "\t found file \"" << q->second.fileName << "\", type=" << q->second.mimeType << ".\n";
Log() << Logger::Debug << "\t found file \"" << q->second.fileName.string() << "\", type=" << q->second.mimeType << ".\n";
sendFile( req, q->second.mimeType, q->second.fileName);
return;
}
@ -229,8 +231,9 @@ void ev_server::OnOtherRequest(evhttp_request* req, void*)
catch(const std::runtime_error& e)
{
const std::string error_msg = "Internal error caused by URI \"" + std::string(uri_string) + "\"";
// TODO: log e.what() to log file, but don't send it in HTTP error message
// because it might contain sensitive information, e.g. local file paths etc.!
// Log e.what() to log file, but DON'T send it in HTTP error message
// because it might contain sensitive information, e.g. local file paths etc.!
Log() << Logger::Error << "OnOtherRequest: " << error_msg << ". what:" << e.what();
evhttp_send_error(req, HTTP_INTERNAL, error_msg.c_str() );
}
};
@ -279,6 +282,7 @@ void ev_server::OnGetFunctions(evhttp_request* req, void*)
void ev_server::OnApiRequest(evhttp_request* req, void* obj)
{
Logger L( Log(), "OnApiReq");
evbuffer* inbuf = evhttp_request_get_input_buffer(req);
const size_t length = evbuffer_get_length(inbuf);
@ -296,24 +300,26 @@ void ev_server::OnApiRequest(evhttp_request* req, void* obj)
const std::string data_string(data.data(), data.data() + nr );
if(nr>0)
{
ja->Log() << "\tData: «" << data_string << "»\n";
L << Logger::Debug << "Data: «" << data_string << "»";
bool b = js::read( data_string, p);
if(p.type() == js::obj_type)
{
const js::Object& request = p.get_obj();
answer = call( functions, request, ja );
}else{
answer = make_error( JSON_RPC::PARSE_ERROR, "evbuffer_copyout does not return a JSON string. b=" + std::to_string(b), js::Value{data_string}, 42 );
const std::string error_msg = "evbuffer_copyout does not return a JSON string. b=" + std::to_string(b);
L << Logger::Error << error_msg;
answer = make_error( JSON_RPC::PARSE_ERROR, error_msg, js::Value{data_string}, 42 );
}
}else{
ja->Log() << "\tError: " << nr << ".\n";
L << Logger::Error << "Error: " << nr << ".\n";
answer = make_error( JSON_RPC::INTERNAL_ERROR, "evbuffer_copyout returns negative value", p, request_id );
}
}
catch(const std::exception& e)
{
Log() << "\tException: \"" << e.what() << "\"\n";
L << Logger::Error << "Exception: \"" << e.what() << "\"";
answer = make_error( JSON_RPC::INTERNAL_ERROR, "Got a std::exception: \"" + std::string(e.what()) + "\"", p, request_id );
}
@ -321,16 +327,10 @@ void ev_server::OnApiRequest(evhttp_request* req, void* obj)
};
std::ostream& ev_server::Log()
Logger& ev_server::Log()
{
*log_file << "evserver: ";
return *log_file;
}
void ev_server::setLogfile(std::ostream* new_logfile)
{
log_file = new_logfile ? new_logfile : &nulllogger;
static Logger L("evs");
return L;
}
@ -338,6 +338,3 @@ void ev_server::addSharks()
{
add_sharks = true;
}
std::ostream* ev_server::log_file = &nulllogger;

@ -3,12 +3,13 @@
#ifndef EV_SERVER_HH
#define EV_SERVER_HH
#include "nulllogger.hh"
#include <string>
#include <boost/filesystem/path.hpp>
struct evhttp_request;
class Logger;
class ev_server
{
public:
@ -34,22 +35,13 @@ public:
// so changing it later might be useless.
static boost::filesystem::path path_to_html;
// if new_logfile is NULL, the logfile is reset to nulllogger.
static
void setLogfile(std::ostream* new_logfile);
// add sharks to the JSON Adapter
static
void addSharks();
protected:
// prints "evserver:" to the log and returns it to allow << chaining
static
std::ostream& Log();
private:
static
std::ostream* log_file;
Logger& Log();
};
#endif

@ -733,7 +733,7 @@ bool JsonAdapter::verify_security_token(const std::string& s) const
check_guard();
if(s!=i->token)
{
Log(Logger:Notice) << "sec_token=\"" << i->token << "\" (len=" << i->token.size() << ") is unequal to \"" << s << "\" (len=" << s.size() << ")!";
Log(Logger::Notice) << "sec_token=\"" << i->token << "\" (len=" << i->token.size() << ") is unequal to \"" << s << "\" (len=" << s.size() << ")!";
}
return s == i->token;
}

Loading…
Cancel
Save