Skip to content

Commit

Permalink
split http log from main
Browse files Browse the repository at this point in the history
  • Loading branch information
liut committed Nov 16, 2021
1 parent 7f1fe90 commit 4a95cf0
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ handleTorrents(http::request<string_body> const& req) // get or post

if (req.method() == verb::post)
{
LOG_DEBUG << " post clen " << req.find(http::field::content_length)->value() << " bytes\n";
PLOGD_(WebLog) << " post clen " << req.find(http::field::content_length)->value() << " bytes\n";
std::string dir(shth_->dir_store.string());
auto savePath = req.find("x-save-path");
if (savePath != req.end())
Expand All @@ -106,7 +106,7 @@ handleTorrents(http::request<string_body> const& req) // get or post
}
const char* buf = req.body().data();
const int size = req.body().size();
LOG_DEBUG << "post metainfo " << size << " bytes with save-path: '" << dir << "'\n";
PLOGD_(WebLog) << "post metainfo " << size << " bytes with save-path: '" << dir << "'\n";
if (shth_->add_torrent(buf, size, dir))
{
return make_resp_204(req);
Expand Down Expand Up @@ -190,7 +190,7 @@ join(websocket_session* wss)
,{"id", qid}
,{"body", curr_stats}
});
LOG_DEBUG << "ws joinning " << sync_ver.load();
PLOGD_(WebLog) << "ws joinning " << sync_ver.load();
wss->send(std::make_shared<std::string const>(json::serialize(jv)));
}

Expand All @@ -200,7 +200,7 @@ leave(websocket_session* wss)
{
std::lock_guard<std::mutex> lock(mutex_);
sessions_.erase(wss);
LOG_DEBUG << "ws leaved";
PLOGD_(WebLog) << "ws leaved";
}

// Broadcast a message to all websocket client sessions
Expand Down
2 changes: 1 addition & 1 deletion src/http_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fail(beast::error_code ec, char const* what)
if(ec == net::error::operation_aborted)
return;

std::cerr << what << ": " << ec.message() << "\n";
PLOGI_(WebLog) << what << ": " << ec.message() << "\n";
}

void
Expand Down
5 changes: 3 additions & 2 deletions src/http_websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ websocket_session::
~websocket_session()
{
closed = true;
LOG_DEBUG << "ws destroying";
PLOGD_(WebLog) << "ws destroying";
// Remove this session from the list of active sessions
caller_->leave(this);
}
Expand Down Expand Up @@ -143,12 +143,13 @@ on_write(beast::error_code ec, std::size_t)
queue_.erase(queue_.begin());

// Send the next message if any
if(! queue_.empty())
if(! queue_.empty()) {
ws_.async_write(
net::buffer(*queue_.front()),
beast::bind_front_handler(
&websocket_session::on_write,
shared_from_this()));
}
}

void
Expand Down
4 changes: 2 additions & 2 deletions src/http_websocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ run(http::request<Body, http::basic_fields<Allocator>> const& req)
{
uri_ = {req.target().data(), req.target().size()};
qid_ = url_decode(query_arg_one(uri_, "id="));
LOG_DEBUG << "ws run: '" << uri_ << "' qid: " << qid_;
PLOGD_(WebLog) << "ws run: '" << uri_ << "' qid: " << qid_;
// Set suggested timeout settings for the websocket
ws_.set_option(
websocket::stream_base::timeout::suggested(
Expand All @@ -88,7 +88,7 @@ run(http::request<Body, http::basic_fields<Allocator>> const& req)
}));

ws_.control_callback([](auto kind, auto payload){
LOG_DEBUG << "ws ctl kind " << asValue(kind) << " payload " << payload;
PLOGD_(WebLog) << "ws ctl kind " << asValue(kind) << " payload " << payload;
});

// Accept the websocket handshake
Expand Down
7 changes: 4 additions & 3 deletions src/listener.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

#include "listener.hpp"
#include "http_session.hpp"
#include <iostream>

#include "listener.hpp"
#include "http_session.hpp"
#include "log.hpp"

namespace btd {

Expand Down Expand Up @@ -71,7 +72,7 @@ fail(beast::error_code ec, char const* what)
// Don't report on canceled operations
if(ec == net::error::operation_aborted)
return;
std::cerr << what << ": " << ec.message() << "\n";
PLOGW_(WebLog) << what << ": " << ec.message() << "\n";
}

// Handle a connection
Expand Down
4 changes: 3 additions & 1 deletion src/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@

enum // Define log instanceIds. Default is 0 and is omitted from this enum.
{
AlertLog = 1
MainLog = 0, // default
AlertLog = 1,
WebLog = 2
};
13 changes: 11 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@ int main(int argc, char* argv[])
{
const std::string logMain(getLogsDir()+"/kedge-main.log");
const std::string logAlert(getLogsDir()+"/kedge-alert.log");
plog::init(plog::debug, logMain.c_str(), 1024*1024*32, 2); // Initialize the default logger instance.
plog::init<AlertLog>(plog::debug, logAlert.c_str(), 1024*1024*64, 2); // Initialize the 2nd logger instance.
const std::string logWeb(getLogsDir()+"/kedge-web.log");
plog::init(plog::debug, logMain.c_str(), 1024*1024*32, 2);
plog::init<AlertLog>(plog::debug, logAlert.c_str(), 1024*1024*64, 2);
plog::init<WebLog>(plog::debug, logWeb.c_str(), 1024*1024*64, 2);

LOG_DEBUG << "init log " << logMain;
LOG_DEBUG << "init log " << logAlert;
LOG_DEBUG << "init log " << logWeb;

PLOGD_(AlertLog) << "log start";
PLOGD_(WebLog) << "log start";

option opt;
if (!opt.init_from(argc, argv)) { return EXIT_FAILURE; }
Expand Down

0 comments on commit 4a95cf0

Please sign in to comment.