Skip to content

Commit

Permalink
#157 add log level config
Browse files Browse the repository at this point in the history
  • Loading branch information
wdyoschina committed Oct 18, 2017
1 parent 666a257 commit 87b4f3f
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 56 deletions.
2 changes: 1 addition & 1 deletion include/metaverse/bitcoin/utility/logging.hpp
Expand Up @@ -30,7 +30,7 @@ namespace libbitcoin {

/// Set up global logging.
BCT_API void initialize_logging(bc::ofstream& debug, bc::ofstream& error,
std::ostream& output_stream, std::ostream& error_stream);
std::ostream& output_stream, std::ostream& error_stream, std::string level = "DEBUG");

/// Class Logger
class Logger{
Expand Down
2 changes: 1 addition & 1 deletion include/metaverse/server/settings.hpp
Expand Up @@ -45,7 +45,7 @@ class BCS_API settings
uint32_t subscription_expiration_minutes;
uint32_t subscription_limit;
std::string mongoose_listen;
bool log_requests{true}; //FIXME.chenhao
std::string log_level;
bool secure_only;

bool query_service_enabled;
Expand Down
75 changes: 41 additions & 34 deletions src/lib/bitcoin/utility/logging.cpp
Expand Up @@ -39,6 +39,7 @@ namespace libbitcoin {
static std::mutex file_mutex;

template<class T>
// TODO:limit template type of instance
// class = typename std::enable_if<std::is_base_of<std::basic_ostream&, T>::value>::type>
static inline void do_logging(T& ofs, log::level level, const std::string& domain,
const std::string& body)
Expand All @@ -48,10 +49,11 @@ namespace libbitcoin {
return;
}

static uint8_t flush_times = 0;
namespace ptime = boost::posix_time;

static const auto form = "%1% %2% [%3%] %4%\n";
const auto message = boost::format(form) %
boost::posix_time::microsec_clock::local_time().time_of_day() %
ptime::to_iso_string(ptime::second_clock::local_time()) %
log::to_text(level) %
domain %
body;
Expand All @@ -61,10 +63,11 @@ namespace libbitcoin {
///////////////////////////////////////////////////////////////////////
std::unique_lock<std::mutex> lock_file(file_mutex);
ofs << message;

// Cut up log file if over max_size
if (std::is_same<T, bc::ofstream>::value)
{
bc::ofstream& bo = dynamic_cast<bc::ofstream&>(ofs);
//bo.increment(message.size());
auto& current_size = bo.current_size();
current_size += message.size();
if (bo.current_size() > bo.max_size())
Expand All @@ -81,20 +84,7 @@ namespace libbitcoin {
}

}
#if 0
static void log_to_file(bc::ofstream& file, log::level level,
const std::string& domain, const std::string& body)
{
do_logging(file, level, domain, body);
}

static void log_to_both(std::ostream& device, bc::ofstream& file,
log::level level, const std::string& domain, const std::string& body)
{
do_logging(file, level, domain, body);
do_logging(device, level, domain, body);
}
#endif
static void output_ignore(bc::ofstream& file, log::level level,
const std::string& domain, const std::string& body)
{
Expand All @@ -105,52 +95,69 @@ static void output_file(bc::ofstream& file, log::level level,
const std::string& domain, const std::string& body)
{
do_logging(file, level, domain, body);
//log_to_file(file, level, domain, body);
}

static void output_both(bc::ofstream& file, std::ostream& output,
log::level level, const std::string& domain, const std::string& body)
{
//log_to_both(output, file, level, domain, body);
do_logging(file, level, domain, body);
do_logging(output, level, domain, body);
}

static void error_file(bc::ofstream& file, log::level level,
const std::string& domain, const std::string& body)
{
//log_to_file(file, level, domain, body);
do_logging(file, level, domain, body);
}

static void error_both(bc::ofstream& file, std::ostream& error,
log::level level, const std::string& domain, const std::string& body)
{
//log_to_both(error, file, level, domain, body);
do_logging(file, level, domain, body);
do_logging(error, level, domain, body);
}

void initialize_logging(bc::ofstream& debug, bc::ofstream& error,
std::ostream& output_stream, std::ostream& error_stream)
std::ostream& output_stream, std::ostream& error_stream,
std::string level)
{
using namespace std::placeholders;

// trace => debug_log if define MVS_DEBUG
#ifdef MVS_DEBUG
log::trace("").set_output_function(std::bind(output_file,
std::ref(debug), _1, _2, _3));
#else
log::trace("").set_output_function(std::bind(output_ignore,
std::ref(debug), _1, _2, _3));
#endif

// debug|info => debug_log
log::debug("").set_output_function(std::bind(output_file,
std::ref(debug), _1, _2, _3));
auto debug_log_level = log::level::debug;
if (level == "INFO" || level == "info")
debug_log_level = log::level::info;
else if (level == "TRACE" || level == "trace")
debug_log_level = log::level::trace;

// setup log level for debug_log
if (debug_log_level < log::level::debug)
{
// trace|debug|info => debug_log
log::trace("").set_output_function(std::bind(output_file,
std::ref(debug), _1, _2, _3));
log::debug("").set_output_function(std::bind(output_file,
std::ref(debug), _1, _2, _3));
}
else if (debug_log_level <log::level::info)
{
log::trace("").set_output_function(std::bind(output_ignore,
std::ref(debug), _1, _2, _3));
// debug|info => debug_log
log::debug("").set_output_function(std::bind(output_file,
std::ref(debug), _1, _2, _3));
}
else if (debug_log_level <log::level::warning)
{
// info => debug_log
log::trace("").set_output_function(std::bind(output_ignore,
std::ref(debug), _1, _2, _3));
log::debug("").set_output_function(std::bind(output_ignore,
std::ref(debug), _1, _2, _3));
}

// info => debug_log + console
log::info("").set_output_function(std::bind(output_both,
std::ref(debug), std::ref(output_stream), _1, _2, _3));

// warning|error|fatal => error_log + console
log::warning("").set_output_function(std::bind(error_file,
std::ref(error), _1, _2, _3));
Expand Down
2 changes: 1 addition & 1 deletion src/mvsd/executor.cpp
Expand Up @@ -58,7 +58,7 @@ executor::executor(parser& metadata, std::istream& input,
debug_file_((default_data_path() / metadata_.configured.network.debug_file).string(), append),
error_file_((default_data_path() / metadata_.configured.network.error_file).string(), append)
{
initialize_logging(debug_file_, error_file_, output, error);
initialize_logging(debug_file_, error_file_, output, error, metadata_.configured.server.log_level);
handle_stop(initialize_stop);
}

Expand Down
6 changes: 3 additions & 3 deletions src/mvsd/server/parser.cpp
Expand Up @@ -350,9 +350,9 @@ options_metadata parser::load_settings()
"The maximum number of subscriptions, defaults to 100000000."
)
(
"server.log_requests",
value<bool>(&configured.server.log_requests),
"Write service requests to the log, defaults to false."
"server.log_level",
value<std::string>(&configured.server.log_level),
"Setup log level of debug log in level [TRACE,DEBUG,INFO], defaults to DEBUG."
)
(
"server.secure_only",
Expand Down
7 changes: 3 additions & 4 deletions src/mvsd/server/services/block_service.cpp
Expand Up @@ -231,10 +231,9 @@ void block_service::publish_block(zmq::socket& publisher, uint32_t height,
}

// This isn't actually a request, should probably update settings.
if (settings_.log_requests)
log::debug(LOG_SERVER)
<< "Published " << security << " block ["
<< encode_hash(block->header.hash()) << "]";
log::debug(LOG_SERVER)
<< "Published " << security << " block ["
<< encode_hash(block->header.hash()) << "]";
}

} // namespace server
Expand Down
5 changes: 2 additions & 3 deletions src/mvsd/server/services/heartbeat_service.cpp
Expand Up @@ -144,9 +144,8 @@ void heartbeat_service::publish(uint32_t count, zmq::socket& publisher)
}

// This isn't actually a request, should probably update settings.
if (settings_.log_requests)
log::debug(LOG_SERVER)
<< "Published " << security << " heartbeat [" << count << "].";
log::debug(LOG_SERVER)
<< "Published " << security << " heartbeat [" << count << "].";
}

} // namespace server
Expand Down
7 changes: 3 additions & 4 deletions src/mvsd/server/services/transaction_service.cpp
Expand Up @@ -216,10 +216,9 @@ void transaction_service::publish_transaction(const transaction& tx)
}

// This isn't actually a request, should probably update settings.
if (settings_.log_requests)
log::debug(LOG_SERVER)
<< "Published " << security << " transaction ["
<< encode_hash(tx_msg.hash()) << "]";
log::debug(LOG_SERVER)
<< "Published " << security << " transaction ["
<< encode_hash(tx_msg.hash()) << "]";
}

} // namespace server
Expand Down
2 changes: 1 addition & 1 deletion src/mvsd/server/settings.cpp
Expand Up @@ -32,7 +32,7 @@ settings::settings()
subscription_expiration_minutes(10),
subscription_limit(100000000),
mongoose_listen("127.0.0.1:8820"),
log_requests(false),
log_level("DEBUG"),
secure_only(false),
query_service_enabled(true),
heartbeat_service_enabled(false),
Expand Down
7 changes: 3 additions & 4 deletions src/mvsd/server/workers/query_worker.cpp
Expand Up @@ -162,10 +162,9 @@ void query_worker::query(zmq::socket& router)
return;
}

if (settings_.log_requests)
log::info(LOG_SERVER)
<< "Query " << request.command() << " from "
<< request.route().display();
log::info(LOG_SERVER)
<< "Query " << request.command() << " from "
<< request.route().display();

// The query executor is the delegate bound by the attach method.
const auto& query_execute = handler->second;
Expand Down

0 comments on commit 87b4f3f

Please sign in to comment.