Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added thread pid to console log for multithread environment #54

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/fc/log/console_appender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace fc
struct config
{
config()
:format( "${timestamp} ${thread_name} ${context} ${file}:${line} ${method} ${level}] ${message}" ),
:format( "${timestamp} ${thread_name} ${thread_pid} ${context} ${file}:${line} ${method} ${level}] ${message}" ),
stream(console_appender::stream::std_error),flush(true){}

fc::string format;
Expand Down
1 change: 1 addition & 0 deletions include/fc/log/log_message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ namespace fc
uint64_t get_line_number()const;
string get_method()const;
string get_thread_name()const;
pid_t get_thread_pid()const;
string get_task_name()const;
string get_host_name()const;
time_point get_timestamp()const;
Expand Down
3 changes: 3 additions & 0 deletions include/fc/log/logger_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ namespace fc {

void set_thread_name( const string& name );
const string& get_thread_name();

void set_thread_pid( const pid_t& pid );
const pid_t& get_thread_pid();
}

#include <fc/reflect/reflect.hpp>
Expand Down
3 changes: 3 additions & 0 deletions src/log/console_appender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ namespace fc {
// use now() instead of context.get_timestamp() because log_message construction can include user provided long running calls
line += string( time_point::now() ); line += ' ';
line += fixed_size( 9, context.get_thread_name() ); line += ' ';
#ifdef __linux__
line += fixed_size( 9, fc::to_string( context.get_thread_pid() ) ); line += ' ';
#endif
line += fixed_size( 29, file_line ); line += ' ';

auto me = context.get_method();
Expand Down
3 changes: 3 additions & 0 deletions src/log/gelf_appender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ namespace fc
gelf_message["_file"] = context.get_file();
gelf_message["_method_name"] = context.get_method();
gelf_message["_thread_name"] = context.get_thread_name();
#ifdef __linux__
gelf_message["_thread_pid"] = context.get_thread_pid();
#endif
if (!context.get_task_name().empty())
gelf_message["_task_name"] = context.get_task_name();

Expand Down
10 changes: 10 additions & 0 deletions src/log/log_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace fc
{
const string& get_thread_name();
const pid_t& get_thread_pid();
namespace detail
{
class log_context_impl
Expand All @@ -18,6 +19,7 @@ namespace fc
uint64_t line;
string method;
string thread_name;
pid_t thread_pid;
string task_name;
string hostname;
string context;
Expand Down Expand Up @@ -52,6 +54,7 @@ namespace fc
my->method = method;
my->timestamp = time_point::now();
my->thread_name = fc::get_thread_name();
my->thread_pid = fc::get_thread_pid();
}

log_context::log_context( const variant& v )
Expand All @@ -64,6 +67,7 @@ namespace fc
my->method = obj["method"].as_string();
my->hostname = obj["hostname"].as_string();
my->thread_name = obj["thread_name"].as_string();
my->thread_pid = obj["thread_pid"].as_int64();
if (obj.contains("task_name"))
my->task_name = obj["task_name"].as_string();
my->timestamp = obj["timestamp"].as<time_point>();
Expand All @@ -73,7 +77,11 @@ namespace fc

fc::string log_context::to_string()const
{
#ifdef __linux__
return my->thread_name + " " + fc::to_string(my->thread_pid) + " " + my->file + ":" + fc::to_string(my->line) + " " + my->method;
#else
return my->thread_name + " " + my->file + ":" + fc::to_string(my->line) + " " + my->method;
#endif

}

Expand Down Expand Up @@ -168,6 +176,7 @@ namespace fc
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; }
pid_t log_context::get_thread_pid()const { return my->thread_pid; }
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 @@ -184,6 +193,7 @@ namespace fc
( "method", my->method )
( "hostname", my->hostname )
( "thread_name", my->thread_name )
( "thread_pid", my->thread_pid )
( "timestamp", variant(my->timestamp) );

if( my->context.size() )
Expand Down
18 changes: 18 additions & 0 deletions src/log/logger_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include <fc/log/gelf_appender.hpp>
#include <fc/reflect/variant.hpp>
#include <fc/exception/exception.hpp>
#ifdef __linux__
#include <sys/syscall.h>
#endif

namespace fc {
extern std::unordered_map<std::string,logger>& get_logger_map();
Expand Down Expand Up @@ -95,4 +98,19 @@ namespace fc {
thread_name = string("thread-")+fc::to_string(thread_count++);
return thread_name;
}

static thread_local pid_t thread_pid = 0;
void set_thread_pid( const pid_t& pid ) {
thread_pid = pid;
}
const pid_t& get_thread_pid() {
if( thread_pid == 0 ) {
#ifdef __linux__
thread_pid = syscall(SYS_gettid);
#else
thread_pid = -ENOSYS;
#endif
}
return thread_pid;
}
}