Splitting logging core into poly.
This commit is contained in:
122
src/poly/logging.cc
Normal file
122
src/poly/logging.cc
Normal file
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2013 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include <poly/logging.h>
|
||||
|
||||
#include <mutex>
|
||||
|
||||
#include <gflags/gflags.h>
|
||||
#include <poly/main.h>
|
||||
#include <poly/math.h>
|
||||
|
||||
DEFINE_bool(fast_stdout, false,
|
||||
"Don't lock around stdout/stderr. May introduce weirdness.");
|
||||
DEFINE_bool(log_filenames, false,
|
||||
"Log filenames/line numbers in log statements.");
|
||||
|
||||
namespace poly {
|
||||
|
||||
std::mutex log_lock;
|
||||
|
||||
void format_log_line(char* buffer, size_t buffer_count, const char* file_path,
|
||||
const uint32_t line_number, const char level_char,
|
||||
const char* fmt, va_list args) {
|
||||
char* buffer_ptr;
|
||||
if (FLAGS_log_filenames) {
|
||||
// Strip out just the filename from the path.
|
||||
const char* filename = strrchr(file_path, poly::path_separator);
|
||||
if (filename) {
|
||||
// Slash - skip over it.
|
||||
filename++;
|
||||
} else {
|
||||
// No slash, entire thing is filename.
|
||||
filename = file_path;
|
||||
}
|
||||
|
||||
// Format string - add a trailing newline if required.
|
||||
const char* outfmt = "%c> %s:%d: ";
|
||||
buffer_ptr = buffer + snprintf(buffer, buffer_count - 1, outfmt, level_char,
|
||||
filename, line_number);
|
||||
} else {
|
||||
buffer_ptr = buffer;
|
||||
*(buffer_ptr++) = level_char;
|
||||
*(buffer_ptr++) = '>';
|
||||
*(buffer_ptr++) = ' ';
|
||||
}
|
||||
|
||||
// Scribble args into the print buffer.
|
||||
buffer_ptr = buffer_ptr + vsnprintf(buffer_ptr,
|
||||
buffer_count - (buffer_ptr - buffer) - 1,
|
||||
fmt, args);
|
||||
|
||||
// Add a trailing newline.
|
||||
if (buffer_ptr[-1] != '\n') {
|
||||
buffer_ptr[0] = '\n';
|
||||
buffer_ptr[1] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void log_line(const char* file_path, const uint32_t line_number,
|
||||
const char level_char, const char* fmt, ...) {
|
||||
// SCOPE_profile_cpu_i("emu", "log_line");
|
||||
|
||||
char buffer[2048];
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
format_log_line(buffer, poly::countof(buffer), file_path, line_number,
|
||||
level_char, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
if (!FLAGS_fast_stdout) {
|
||||
log_lock.lock();
|
||||
}
|
||||
#if 0 // defined(OutputDebugString)
|
||||
OutputDebugStringA(buffer);
|
||||
#else
|
||||
fprintf(stdout, buffer);
|
||||
fflush(stdout);
|
||||
#endif // OutputDebugString
|
||||
if (!FLAGS_fast_stdout) {
|
||||
log_lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void handle_fatal(const char* file_path, const uint32_t line_number,
|
||||
const char* fmt, ...) {
|
||||
char buffer[2048];
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
format_log_line(buffer, poly::countof(buffer), file_path, line_number, 'X',
|
||||
fmt, args);
|
||||
va_end(args);
|
||||
|
||||
if (!FLAGS_fast_stdout) {
|
||||
log_lock.lock();
|
||||
}
|
||||
#if defined(OutputDebugString)
|
||||
OutputDebugStringA(buffer);
|
||||
#else
|
||||
fprintf(stderr, buffer);
|
||||
fflush(stderr);
|
||||
#endif // OutputDebugString
|
||||
if (!FLAGS_fast_stdout) {
|
||||
log_lock.unlock();
|
||||
}
|
||||
|
||||
#if XE_LIKE_WIN32
|
||||
if (!poly::has_console_attached()) {
|
||||
MessageBoxA(NULL, buffer, "Xenia Error",
|
||||
MB_OK | MB_ICONERROR | MB_APPLMODAL | MB_SETFOREGROUND);
|
||||
}
|
||||
#endif // WIN32
|
||||
|
||||
exit(1);
|
||||
}
|
||||
|
||||
} // namespace poly
|
||||
77
src/poly/logging.h
Normal file
77
src/poly/logging.h
Normal file
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2014 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef POLY_LOGGING_H_
|
||||
#define POLY_LOGGING_H_
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <poly/string.h>
|
||||
|
||||
namespace poly {
|
||||
|
||||
#define POLY_OPTION_ENABLE_LOGGING 1
|
||||
#define POLY_OPTION_LOG_ERROR 1
|
||||
#define POLY_OPTION_LOG_WARNING 1
|
||||
#define POLY_OPTION_LOG_INFO 1
|
||||
#define POLY_OPTION_LOG_DEBUG 1
|
||||
|
||||
#define POLY_EMPTY_MACRO \
|
||||
do { \
|
||||
} while (false)
|
||||
|
||||
#if XE_COMPILER_GNUC
|
||||
#define POLY_LOG_LINE_ATTRIBUTE __attribute__((format(printf, 5, 6)))
|
||||
#else
|
||||
#define POLY_LOG_LINE_ATTRIBUTE
|
||||
#endif // GNUC
|
||||
void log_line(const char* file_path, const uint32_t line_number,
|
||||
const char level_char, const char* fmt,
|
||||
...) POLY_LOG_LINE_ATTRIBUTE;
|
||||
#undef POLY_LOG_LINE_ATTRIBUTE
|
||||
|
||||
void handle_fatal(const char* file_path, const uint32_t line_number,
|
||||
const char* fmt, ...);
|
||||
|
||||
#if POLY_OPTION_ENABLE_LOGGING
|
||||
#define PLOGCORE(level, fmt, ...) \
|
||||
poly::log_line(__FILE__, __LINE__, level, fmt, ##__VA_ARGS__)
|
||||
#else
|
||||
#define PLOGCORE(level, fmt, ...) POLY_EMPTY_MACRO
|
||||
#endif // ENABLE_LOGGING
|
||||
|
||||
#define PFATAL(fmt, ...) \
|
||||
do { \
|
||||
poly::handle_fatal(__FILE__, __LINE__, fmt, ##__VA_ARGS__); \
|
||||
} while (false)
|
||||
|
||||
#if POLY_OPTION_LOG_ERROR
|
||||
#define PLOGE(fmt, ...) PLOGCORE('!', fmt, ##__VA_ARGS__)
|
||||
#else
|
||||
#define PLOGE(fmt, ...) POLY_EMPTY_MACRO
|
||||
#endif
|
||||
#if POLY_OPTION_LOG_WARNING
|
||||
#define PLOGW(fmt, ...) PLOGCORE('w', fmt, ##__VA_ARGS__)
|
||||
#else
|
||||
#define PLOGW(fmt, ...) POLY_EMPTY_MACRO
|
||||
#endif
|
||||
#if POLY_OPTION_LOG_INFO
|
||||
#define PLOGI(fmt, ...) PLOGCORE('i', fmt, ##__VA_ARGS__)
|
||||
#else
|
||||
#define PLOGI(fmt, ...) POLY_EMPTY_MACRO
|
||||
#endif
|
||||
#if POLY_OPTION_LOG_DEBUG
|
||||
#define PLOGD(fmt, ...) PLOGCORE('d', fmt, ##__VA_ARGS__)
|
||||
#else
|
||||
#define PLOGD(fmt, ...) POLY_EMPTY_MACRO
|
||||
#endif
|
||||
|
||||
} // namespace poly
|
||||
|
||||
#endif // POLY_LOGGING_H_
|
||||
@@ -16,6 +16,7 @@
|
||||
#include <poly/config.h>
|
||||
#include <poly/cxx_compat.h>
|
||||
#include <poly/debugging.h>
|
||||
#include <poly/logging.h>
|
||||
#include <poly/mapped_memory.h>
|
||||
#include <poly/math.h>
|
||||
#include <poly/memory.h>
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
'debugging.h',
|
||||
'config.h',
|
||||
'cxx_compat.h',
|
||||
'logging.cc',
|
||||
'logging.h',
|
||||
'main.h',
|
||||
'mapped_memory.h',
|
||||
'math.cc',
|
||||
|
||||
Reference in New Issue
Block a user