Adding thread handle to logging.

This commit is contained in:
Ben Vanik
2015-08-29 20:49:17 -07:00
parent c486fcfcba
commit a86b3821f2
11 changed files with 113 additions and 49 deletions

View File

@@ -12,6 +12,7 @@
#include <gflags/gflags.h>
#include <atomic>
#include <cinttypes>
#include <cstdarg>
#include <mutex>
#include <vector>
@@ -34,11 +35,24 @@ DEFINE_bool(flush_log, true, "Flush log file after each log line batch.");
namespace xe {
class Logger;
Logger* logger_ = nullptr;
thread_local std::vector<char> log_format_buffer_(64 * 1024);
class Logger {
public:
Logger() : ring_buffer_(buffer_, kBufferSize), running_(true) {
Logger(const std::wstring& app_name)
: ring_buffer_(buffer_, kBufferSize), running_(true) {
if (!FLAGS_log_file.empty()) {
auto file_path = xe::to_wstring(FLAGS_log_file.c_str());
xe::filesystem::CreateParentFolder(file_path);
file_ = xe::filesystem::OpenFile(file_path, "wt");
} else {
auto file_path = app_name + L".log";
file_ = xe::filesystem::OpenFile(file_path, "wt");
}
flush_event_ = xe::threading::Event::CreateAutoResetEvent(false);
write_thread_ =
xe::threading::Thread::Create({}, [this]() { WriteThread(); });
@@ -53,17 +67,6 @@ class Logger {
fclose(file_);
}
void Initialize(const std::wstring& app_name) {
if (!FLAGS_log_file.empty()) {
auto file_path = xe::to_wstring(FLAGS_log_file.c_str());
xe::filesystem::CreateParentFolder(file_path);
file_ = xe::filesystem::OpenFile(file_path, "wt");
} else {
auto file_path = app_name + L".log";
file_ = xe::filesystem::OpenFile(file_path, "wt");
}
}
void AppendLine(uint32_t thread_id, const char level_char, const char* buffer,
size_t buffer_length) {
LogLine line;
@@ -104,8 +107,24 @@ class Logger {
LogLine line;
ring_buffer_.Read(&line, sizeof(line));
ring_buffer_.Read(log_format_buffer_.data(), line.buffer_length);
const char prefix[3] = {line.level_char, '>', ' '};
fwrite(prefix, 1, sizeof(prefix), file_);
char prefix[] = {
line.level_char,
'>',
' ',
'0', // Thread ID gets placed here (8 chars).
'0',
'0',
'0',
'0',
'0',
'0',
'0',
' ',
0,
};
std::snprintf(prefix + 3, sizeof(prefix) - 3, "%08" PRIX32 " ",
line.thread_id);
fwrite(prefix, 1, sizeof(prefix) - 1, file_);
fwrite(log_format_buffer_.data(), 1, line.buffer_length, file_);
if (log_format_buffer_[line.buffer_length - 1] != '\n') {
const char suffix[1] = {'\n'};
@@ -131,10 +150,9 @@ class Logger {
std::unique_ptr<xe::threading::Thread> write_thread_;
};
Logger logger_;
void InitializeLogging(const std::wstring& app_name) {
logger_.Initialize(app_name);
// We leak this intentionally - lots of cleanup code needs it.
logger_ = new Logger(app_name);
}
void LogLineFormat(const char level_char, const char* fmt, ...) {
@@ -143,20 +161,26 @@ void LogLineFormat(const char level_char, const char* fmt, ...) {
size_t chars_written = vsnprintf(log_format_buffer_.data(),
log_format_buffer_.capacity(), fmt, args);
va_end(args);
logger_.AppendLine(xe::threading::current_thread_id(), level_char,
log_format_buffer_.data(), chars_written);
logger_->AppendLine(xe::threading::current_thread_id(), level_char,
log_format_buffer_.data(), chars_written);
}
void LogLineVarargs(const char level_char, const char* fmt, va_list args) {
size_t chars_written = vsnprintf(log_format_buffer_.data(),
log_format_buffer_.capacity(), fmt, args);
logger_.AppendLine(xe::threading::current_thread_id(), level_char,
log_format_buffer_.data(), chars_written);
logger_->AppendLine(xe::threading::current_thread_id(), level_char,
log_format_buffer_.data(), chars_written);
}
void LogLine(const char level_char, const char* str, size_t str_length) {
logger_->AppendLine(
xe::threading::current_thread_id(), level_char, str,
str_length == std::string::npos ? std::strlen(str) : str_length);
}
void LogLine(const char level_char, const std::string& str) {
logger_.AppendLine(xe::threading::current_thread_id(), level_char,
str.c_str(), str.length());
logger_->AppendLine(xe::threading::current_thread_id(), level_char,
str.c_str(), str.length());
}
void FatalError(const char* fmt, ...) {

View File

@@ -27,6 +27,8 @@ void InitializeLogging(const std::wstring& app_name);
void LogLineFormat(const char level_char, const char* fmt, ...);
void LogLineVarargs(const char level_char, const char* fmt, va_list args);
// Appends a line to the log.
void LogLine(const char level_char, const char* str,
size_t str_length = std::string::npos);
void LogLine(const char level_char, const std::string& str);
// Logs a fatal error with printf-style formatting and aborts the program.

View File

@@ -16,12 +16,15 @@
namespace xe {
StringBuffer::StringBuffer(size_t initial_capacity) : buffer_offset_(0) {
StringBuffer::StringBuffer(size_t initial_capacity) {
buffer_capacity_ = std::max(initial_capacity, static_cast<size_t>(16 * 1024));
buffer_ = reinterpret_cast<char*>(malloc(buffer_capacity_));
}
StringBuffer::~StringBuffer() { free(buffer_); }
StringBuffer::~StringBuffer() {
free(buffer_);
buffer_ = nullptr;
}
void StringBuffer::Reset() { buffer_offset_ = 0; }

View File

@@ -0,0 +1,25 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2015 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include "xenia/base/threading.h"
namespace xe {
namespace threading {
thread_local uint32_t current_thread_id_ = UINT_MAX;
uint32_t current_thread_id() {
return current_thread_id_ == UINT_MAX ? current_thread_system_id()
: current_thread_id_;
}
void set_current_thread_id(uint32_t id) { current_thread_id_ = id; }
} // namespace threading
} // namespace xe

View File

@@ -56,7 +56,13 @@ void EnableAffinityConfiguration();
// Gets a stable thread-specific ID, but may not be. Use for informative
// purposes only.
uint32_t current_thread_system_id();
// Gets a stable thread-specific ID that defaults to the same value as
// current_thread_system_id but may be overridden.
// Guest threads often change this to the guest thread handle.
uint32_t current_thread_id();
void set_current_thread_id(uint32_t id);
// Sets the current thread name.
void set_name(const std::string& name);
@@ -345,8 +351,8 @@ class Thread : public WaitHandle {
// threads that had been waiting for the thread to terminate.
static void Exit(int exit_code);
// Returns the ID of the thread
virtual uint32_t id() const = 0;
// Returns the ID of the thread.
virtual uint32_t system_id() const = 0;
// Returns the current name of the thread, if previously specified.
std::string name() const { return name_; }

View File

@@ -35,7 +35,7 @@ void EnableAffinityConfiguration() {
SetProcessAffinityMask(process_handle, system_affinity_mask);
}
uint32_t current_thread_id() {
uint32_t current_thread_system_id() {
return static_cast<uint32_t>(GetCurrentThreadId());
}
@@ -358,7 +358,7 @@ class Win32Thread : public Win32Handle<Thread> {
}
int32_t priority() override { return GetThreadPriority(handle_); }
uint32_t id() const override { return GetThreadId(handle_); }
uint32_t system_id() const override { return GetThreadId(handle_); }
void set_priority(int32_t new_priority) override {
SetThreadPriority(handle_, new_priority);