Skip to content

Commit

Permalink
Remove thread logging #25
Browse files Browse the repository at this point in the history
  • Loading branch information
mvandeberg committed Sep 14, 2017
1 parent d04c3bb commit 82d54ad
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 48 deletions.
18 changes: 9 additions & 9 deletions include/fc/log/console_appender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#include <fc/log/logger.hpp>
#include <vector>

namespace fc
namespace fc
{
class console_appender : public appender
class console_appender : public appender
{
public:
struct color
struct color
{
enum type {
red,
Expand All @@ -24,20 +24,20 @@ namespace fc

struct stream { enum type { std_out, std_error }; };

struct level_color
struct level_color
{
level_color( log_level l=log_level::all,
level_color( log_level l=log_level::all,
color::type c=color::console_default )
:level(l),color(c){}

log_level level;
console_appender::color::type color;
};

struct config
struct config
{
config()
:format( "${timestamp} ${thread_name} ${context} ${file}:${line} ${method} ${level}] ${message}" ),
:format( "${timestamp} ${context} ${file}:${line} ${method} ${level}] ${message}" ),
stream(console_appender::stream::std_error),flush(true){}

fc::string format;
Expand All @@ -53,8 +53,8 @@ namespace fc

~console_appender();
virtual void log( const log_message& m );
void print( const std::string& text_to_print,

void print( const std::string& text_to_print,
color::type text_color = color::console_default );

void configure( const config& cfg );
Expand Down
8 changes: 4 additions & 4 deletions src/log/console_appender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace fc {
#endif
};

console_appender::console_appender( const variant& args )
console_appender::console_appender( const variant& args )
:my(new impl)
{
configure( args.as<config>() );
Expand Down Expand Up @@ -66,7 +66,7 @@ namespace fc {
#ifdef WIN32
static WORD
#else
static const char*
static const char*
#endif
get_console_color(console_appender::color::type t ) {
switch( t ) {
Expand Down Expand Up @@ -100,7 +100,7 @@ namespace fc {
///////////////
std::stringstream line;
line << (m.get_context().get_timestamp().time_since_epoch().count() % (1000ll*1000ll*60ll*60))/1000 <<"ms ";
line << std::setw( 10 ) << std::left << m.get_context().get_thread_name().substr(0,9).c_str() <<" "<<std::setw(30)<< std::left <<file_line.str();
line << std::setw(30)<< std::left <<file_line.str();

auto me = m.get_context().get_method();
// strip all leading scopes...
Expand Down Expand Up @@ -140,7 +140,7 @@ namespace fc {
#endif

if( text.size() )
fprintf( out, "%s", text.c_str() ); //fmt_str.c_str() );
fprintf( out, "%s", text.c_str() ); //fmt_str.c_str() );

#ifdef WIN32
if (my->console_handle != INVALID_HANDLE_VALUE)
Expand Down
4 changes: 2 additions & 2 deletions src/log/file_appender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ namespace fc {
};

file_appender::config::config(const fc::path& p) :
format( "${timestamp} ${thread_name} ${context} ${file}:${line} ${method} ${level}] ${message}" ),
format( "${timestamp} ${context} ${file}:${line} ${method} ${level}] ${message}" ),
filename(p),
flush(true),
rotate(false)
Expand Down Expand Up @@ -162,7 +162,7 @@ namespace fc {
std::stringstream line;
//line << (m.get_context().get_timestamp().time_since_epoch().count() % (1000ll*1000ll*60ll*60))/1000 <<"ms ";
line << string(m.get_context().get_timestamp()) << " ";
line << std::setw( 21 ) << (m.get_context().get_thread_name().substr(0,9) + string(":") + m.get_context().get_task_name()).c_str() << " ";
line << std::setw( 21 ) << (m.get_context().get_task_name()).c_str() << " ";

string method_name = m.get_context().get_method();
// strip all leading scopes...
Expand Down
31 changes: 15 additions & 16 deletions src/log/gelf_appender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <sstream>
#include <iostream>

namespace fc
namespace fc
{

class gelf_appender::impl : public retainable
Expand All @@ -27,7 +27,7 @@ namespace fc
optional<ip::endpoint> gelf_endpoint;
udp_socket gelf_socket;

impl(const config& c) :
impl(const config& c) :
cfg(c)
{
}
Expand All @@ -52,7 +52,7 @@ namespace fc
}
if (!my->gelf_endpoint)
{
// couldn't parse as a numeric ip address, try resolving as a DNS name.
// couldn't parse as a numeric ip address, try resolving as a DNS name.
// This can yield, so don't do it in the catch block above
string::size_type colon_pos = my->cfg.endpoint.find(':');
try
Expand All @@ -62,7 +62,7 @@ namespace fc
string hostname = my->cfg.endpoint.substr( 0, colon_pos );
std::vector<ip::endpoint> endpoints = resolve(hostname, port);
if (endpoints.empty())
FC_THROW_EXCEPTION(unknown_host_exception, "The host name can not be resolved: ${hostname}",
FC_THROW_EXCEPTION(unknown_host_exception, "The host name can not be resolved: ${hostname}",
("hostname", hostname));
my->gelf_endpoint = endpoints.back();
}
Expand Down Expand Up @@ -95,7 +95,7 @@ namespace fc
gelf_message["version"] = "1.1";
gelf_message["host"] = my->cfg.host;
gelf_message["short_message"] = format_string(message.get_format(), message.get_data());

gelf_message["timestamp"] = context.get_timestamp().time_since_epoch().count() / 1000000.;

switch (context.get_log_level())
Expand Down Expand Up @@ -124,16 +124,15 @@ namespace fc
gelf_message["_line"] = context.get_line_number();
gelf_message["_file"] = context.get_file();
gelf_message["_method_name"] = context.get_method();
gelf_message["_thread_name"] = context.get_thread_name();
if (!context.get_task_name().empty())
gelf_message["_task_name"] = context.get_task_name();

string gelf_message_as_string = json::to_string(gelf_message);
//unsigned uncompressed_size = gelf_message_as_string.size();
gelf_message_as_string = zlib_compress(gelf_message_as_string);

// graylog2 expects the zlib header to be 0x78 0x9c
// but miniz.c generates 0x78 0x01 (indicating
// but miniz.c generates 0x78 0x01 (indicating
// low compression instead of default compression)
// so change that here
assert(gelf_message_as_string[0] == (char)0x78);
Expand All @@ -155,16 +154,16 @@ namespace fc
// no need to split
std::shared_ptr<char> send_buffer(new char[gelf_message_as_string.size()],
[](char* p){ delete[] p; });
memcpy(send_buffer.get(), gelf_message_as_string.c_str(),
memcpy(send_buffer.get(), gelf_message_as_string.c_str(),
gelf_message_as_string.size());

my->gelf_socket.send_to(send_buffer, gelf_message_as_string.size(),
my->gelf_socket.send_to(send_buffer, gelf_message_as_string.size(),
*my->gelf_endpoint);
}
else
{
// split the message
// we need to generate an 8-byte ID for this message.
// we need to generate an 8-byte ID for this message.
// city hash should do
uint64_t message_id = city_hash64(gelf_message_as_string.c_str(), gelf_message_as_string.size());
const unsigned header_length = 2 /* magic */ + 8 /* msg id */ + 1 /* seq */ + 1 /* count */;
Expand All @@ -174,10 +173,10 @@ namespace fc
unsigned number_of_packets_sent = 0;
while (bytes_sent < gelf_message_as_string.size())
{
unsigned bytes_to_send = std::min((unsigned)gelf_message_as_string.size() - bytes_sent,
unsigned bytes_to_send = std::min((unsigned)gelf_message_as_string.size() - bytes_sent,
body_length);
std::shared_ptr<char> send_buffer(new char[max_payload_size],

std::shared_ptr<char> send_buffer(new char[max_payload_size],
[](char* p){ delete[] p; });
char* ptr = send_buffer.get();
// magic number for chunked message
Expand All @@ -190,9 +189,9 @@ namespace fc

*(unsigned char*)(ptr++) = number_of_packets_sent;
*(unsigned char*)(ptr++) = total_number_of_packets;
memcpy(ptr, gelf_message_as_string.c_str() + bytes_sent,
memcpy(ptr, gelf_message_as_string.c_str() + bytes_sent,
bytes_to_send);
my->gelf_socket.send_to(send_buffer, header_length + bytes_to_send,
my->gelf_socket.send_to(send_buffer, header_length + bytes_to_send,
*my->gelf_endpoint);
++number_of_packets_sent;
bytes_sent += bytes_to_send;
Expand Down
29 changes: 12 additions & 17 deletions src/log/log_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ namespace fc
string file;
uint64_t line;
string method;
string thread_name;
string task_name;
string hostname;
string context;
Expand All @@ -44,7 +43,7 @@ namespace fc
log_context::log_context()
:my( std::make_shared<detail::log_context_impl>() ){}

log_context::log_context( log_level ll, const char* file, uint64_t line,
log_context::log_context( log_level ll, const char* file, uint64_t line,
const char* method )
:my( std::make_shared<detail::log_context_impl>() )
{
Expand All @@ -53,7 +52,6 @@ namespace fc
my->line = line;
my->method = method;
my->timestamp = time_point::now();
my->thread_name = fc::thread::current().name();
const char* current_task_desc = fc::thread::current().current_task_desc();
my->task_name = current_task_desc ? current_task_desc : "?unnamed?";
}
Expand All @@ -67,7 +65,6 @@ namespace fc
my->line = obj["line"].as_uint64();
my->method = obj["method"].as_string();
my->hostname = obj["hostname"].as_string();
my->thread_name = obj["thread_name"].as_string();
if (obj.contains("task_name"))
my->task_name = obj["task_name"].as_string();
my->timestamp = obj["timestamp"].as<time_point>();
Expand All @@ -77,7 +74,7 @@ namespace fc

fc::string log_context::to_string()const
{
return my->thread_name + " " + my->file + ":" + fc::to_string(my->line) + " " + my->method;
return my->file + ":" + fc::to_string(my->line) + " " + my->method;

}

Expand All @@ -92,18 +89,18 @@ namespace fc


void to_variant( const log_context& l, variant& v )
{
v = l.to_variant();
{
v = l.to_variant();
}

void from_variant( const variant& l, log_context& c )
{
c = log_context(l);
{
c = log_context(l);
}

void from_variant( const variant& l, log_message& c )
{
c = log_message(l);
{
c = log_message(l);
}
void to_variant( const log_message& m, variant& v )
{
Expand All @@ -114,7 +111,7 @@ namespace fc
{
switch( e )
{
case log_level::all:
case log_level::all:
v = "all";
return;
case log_level::debug:
Expand All @@ -136,7 +133,7 @@ namespace fc
}
void from_variant( const variant& v, log_level& e )
{
try
try
{
if( v.as_string() == "all" ) e = log_level::all;
else if( v.as_string() == "debug" ) e = log_level::debug;
Expand All @@ -145,7 +142,7 @@ namespace fc
else if( v.as_string() == "error" ) e = log_level::error;
else if( v.as_string() == "off" ) e = log_level::off;
else FC_THROW_EXCEPTION( bad_cast_exception, "Failed to cast from Variant to log_level" );
} FC_RETHROW_EXCEPTIONS( error,
} FC_RETHROW_EXCEPTIONS( error,
"Expected 'all|debug|info|warn|error|off', but got '${variant}'",
("variant",v) );
}
Expand All @@ -155,7 +152,6 @@ namespace fc
string log_context::get_file()const { return my->file; }
uint64_t log_context::get_line_number()const { return my->line; }
string log_context::get_method()const { return my->method; }
string log_context::get_thread_name()const { return my->thread_name; }
string log_context::get_task_name()const { return my->task_name; }
string log_context::get_host_name()const { return my->hostname; }
time_point log_context::get_timestamp()const { return my->timestamp; }
Expand All @@ -171,10 +167,9 @@ namespace fc
( "line", my->line )
( "method", my->method )
( "hostname", my->hostname )
( "thread_name", my->thread_name )
( "timestamp", variant(my->timestamp) );

if( my->context.size() )
if( my->context.size() )
o( "context", my->context );

return o;
Expand Down

0 comments on commit 82d54ad

Please sign in to comment.