2013-01-13 08:25:41 +01:00
|
|
|
/**
|
|
|
|
|
******************************************************************************
|
|
|
|
|
* 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. *
|
|
|
|
|
******************************************************************************
|
|
|
|
|
*/
|
|
|
|
|
|
2014-08-22 05:26:55 +02:00
|
|
|
#include <poly/logging.h>
|
2013-01-13 08:25:41 +01:00
|
|
|
|
2014-07-10 07:28:51 +02:00
|
|
|
#include <mutex>
|
|
|
|
|
|
2014-05-21 20:24:44 +02:00
|
|
|
#include <gflags/gflags.h>
|
2014-08-17 01:34:04 +02:00
|
|
|
#include <poly/main.h>
|
2014-08-17 02:58:33 +02:00
|
|
|
#include <poly/math.h>
|
2014-05-21 20:24:44 +02:00
|
|
|
|
|
|
|
|
DEFINE_bool(fast_stdout, false,
|
2014-08-22 05:26:55 +02:00
|
|
|
"Don't lock around stdout/stderr. May introduce weirdness.");
|
|
|
|
|
DEFINE_bool(log_filenames, false,
|
|
|
|
|
"Log filenames/line numbers in log statements.");
|
2014-05-21 20:24:44 +02:00
|
|
|
|
2014-08-22 05:26:55 +02:00
|
|
|
namespace poly {
|
2014-05-21 20:24:44 +02:00
|
|
|
|
2014-07-10 07:28:51 +02:00
|
|
|
std::mutex log_lock;
|
2014-08-22 05:26:55 +02:00
|
|
|
|
|
|
|
|
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);
|
2013-01-13 08:25:41 +01:00
|
|
|
} else {
|
2014-08-22 05:26:55 +02:00
|
|
|
buffer_ptr = buffer;
|
|
|
|
|
*(buffer_ptr++) = level_char;
|
|
|
|
|
*(buffer_ptr++) = '>';
|
|
|
|
|
*(buffer_ptr++) = ' ';
|
2013-01-13 08:25:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Scribble args into the print buffer.
|
2014-08-17 20:48:29 +02:00
|
|
|
buffer_ptr = buffer_ptr + vsnprintf(buffer_ptr,
|
|
|
|
|
buffer_count - (buffer_ptr - buffer) - 1,
|
|
|
|
|
fmt, args);
|
2014-01-17 07:16:06 +01:00
|
|
|
|
|
|
|
|
// Add a trailing newline.
|
|
|
|
|
if (buffer_ptr[-1] != '\n') {
|
|
|
|
|
buffer_ptr[0] = '\n';
|
|
|
|
|
buffer_ptr[1] = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-22 05:26:55 +02:00
|
|
|
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");
|
2014-05-28 22:59:43 +02:00
|
|
|
|
2014-01-17 07:16:06 +01:00
|
|
|
char buffer[2048];
|
2013-01-13 08:25:41 +01:00
|
|
|
va_list args;
|
|
|
|
|
va_start(args, fmt);
|
2014-08-22 05:26:55 +02:00
|
|
|
format_log_line(buffer, poly::countof(buffer), file_path, line_number,
|
|
|
|
|
level_char, fmt, args);
|
2013-01-13 08:25:41 +01:00
|
|
|
va_end(args);
|
|
|
|
|
|
2014-05-21 20:24:44 +02:00
|
|
|
if (!FLAGS_fast_stdout) {
|
2014-07-10 07:28:51 +02:00
|
|
|
log_lock.lock();
|
2014-05-21 20:24:44 +02:00
|
|
|
}
|
2014-08-22 05:26:55 +02:00
|
|
|
#if 0 // defined(OutputDebugString)
|
2014-01-17 07:16:06 +01:00
|
|
|
OutputDebugStringA(buffer);
|
2013-01-13 08:25:41 +01:00
|
|
|
#else
|
2014-08-22 07:27:33 +02:00
|
|
|
fprintf(stdout, "%s", buffer);
|
2013-05-23 09:26:55 +02:00
|
|
|
fflush(stdout);
|
2013-01-13 08:25:41 +01:00
|
|
|
#endif // OutputDebugString
|
2014-05-21 20:24:44 +02:00
|
|
|
if (!FLAGS_fast_stdout) {
|
2014-07-10 07:28:51 +02:00
|
|
|
log_lock.unlock();
|
2014-05-21 20:24:44 +02:00
|
|
|
}
|
2013-01-13 08:25:41 +01:00
|
|
|
}
|
2014-01-17 07:16:06 +01:00
|
|
|
|
2014-08-22 05:26:55 +02:00
|
|
|
void handle_fatal(const char* file_path, const uint32_t line_number,
|
|
|
|
|
const char* fmt, ...) {
|
2014-01-17 07:16:06 +01:00
|
|
|
char buffer[2048];
|
|
|
|
|
va_list args;
|
|
|
|
|
va_start(args, fmt);
|
2014-08-22 05:26:55 +02:00
|
|
|
format_log_line(buffer, poly::countof(buffer), file_path, line_number, 'X',
|
|
|
|
|
fmt, args);
|
2014-01-17 07:16:06 +01:00
|
|
|
va_end(args);
|
|
|
|
|
|
2014-05-21 20:24:44 +02:00
|
|
|
if (!FLAGS_fast_stdout) {
|
2014-07-10 07:28:51 +02:00
|
|
|
log_lock.lock();
|
2014-05-21 20:24:44 +02:00
|
|
|
}
|
2014-01-17 07:16:06 +01:00
|
|
|
#if defined(OutputDebugString)
|
|
|
|
|
OutputDebugStringA(buffer);
|
2014-05-21 20:24:44 +02:00
|
|
|
#else
|
2014-08-22 07:27:33 +02:00
|
|
|
fprintf(stderr, "%s", buffer);
|
2014-01-17 07:16:06 +01:00
|
|
|
fflush(stderr);
|
2014-05-21 20:24:44 +02:00
|
|
|
#endif // OutputDebugString
|
|
|
|
|
if (!FLAGS_fast_stdout) {
|
2014-07-10 07:28:51 +02:00
|
|
|
log_lock.unlock();
|
2014-05-21 20:24:44 +02:00
|
|
|
}
|
2014-01-17 07:16:06 +01:00
|
|
|
|
|
|
|
|
#if XE_LIKE_WIN32
|
2014-08-17 01:34:04 +02:00
|
|
|
if (!poly::has_console_attached()) {
|
2014-01-17 07:16:06 +01:00
|
|
|
MessageBoxA(NULL, buffer, "Xenia Error",
|
|
|
|
|
MB_OK | MB_ICONERROR | MB_APPLMODAL | MB_SETFOREGROUND);
|
|
|
|
|
}
|
|
|
|
|
#endif // WIN32
|
|
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2014-08-22 05:26:55 +02:00
|
|
|
|
|
|
|
|
} // namespace poly
|