2024-01-11 08:08:16 +00:00
|
|
|
// SPDX-FileCopyrightText: 2002-2024 PCSX2 Dev Team
|
2023-12-22 11:57:49 +00:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0+
|
2009-07-03 00:49:40 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
#include "common/Console.h"
|
2022-05-18 13:27:23 +00:00
|
|
|
#include "common/Assertions.h"
|
2024-01-11 08:08:16 +00:00
|
|
|
#include "common/FileSystem.h"
|
|
|
|
#include "common/SmallString.h"
|
|
|
|
#include "common/Timer.h"
|
2010-06-06 04:17:43 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
#include "fmt/format.h"
|
2009-07-03 00:49:40 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <vector>
|
2009-12-24 22:22:34 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include "common/RedtapeWindows.h"
|
|
|
|
#else
|
2021-09-11 03:11:54 +00:00
|
|
|
#include <unistd.h>
|
2015-10-31 00:16:17 +00:00
|
|
|
#endif
|
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
using namespace std::string_view_literals;
|
2009-11-08 04:32:15 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
// Dummy objects, need to get rid of them...
|
|
|
|
ConsoleLogWriter<LOGLEVEL_INFO> Console;
|
|
|
|
ConsoleLogWriter<LOGLEVEL_DEV> DevCon;
|
2009-07-03 00:49:40 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
#ifdef _DEBUG
|
|
|
|
ConsoleLogWriter<LOGLEVEL_DEBUG> DbgConWriter;
|
|
|
|
#else
|
|
|
|
NullLogWriter DbgConWriter;
|
2010-08-06 05:46:09 +00:00
|
|
|
#endif
|
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
#define TIMESTAMP_FORMAT_STRING "[{:10.4f}] "
|
|
|
|
#define TIMESTAMP_PRINTF_STRING "[%10.4f] "
|
|
|
|
|
|
|
|
namespace Log
|
2010-08-06 05:46:09 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
static void WriteToConsole(LOGLEVEL level, ConsoleColors color, std::string_view message);
|
|
|
|
static void WriteToDebug(LOGLEVEL level, ConsoleColors color, std::string_view message);
|
|
|
|
static void WriteToFile(LOGLEVEL level, ConsoleColors color, std::string_view message);
|
2009-07-03 00:49:40 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
static void UpdateMaxLevel();
|
2015-10-31 00:16:17 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
static void ExecuteCallbacks(LOGLEVEL level, ConsoleColors color, std::string_view message);
|
2009-07-03 00:49:40 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
static Common::Timer::Value s_start_timestamp = Common::Timer::GetCurrentValue();
|
2009-07-03 00:49:40 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
static LOGLEVEL s_max_level = LOGLEVEL_NONE;
|
|
|
|
static LOGLEVEL s_console_level = LOGLEVEL_NONE;
|
|
|
|
static LOGLEVEL s_debug_level = LOGLEVEL_NONE;
|
|
|
|
static LOGLEVEL s_file_level = LOGLEVEL_NONE;
|
|
|
|
static LOGLEVEL s_host_level = LOGLEVEL_NONE;
|
|
|
|
static bool s_log_timestamps = true;
|
2009-10-05 14:40:46 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
static FileSystem::ManagedCFilePtr s_file_handle;
|
|
|
|
static std::string s_file_path;
|
|
|
|
static std::mutex s_file_mutex;
|
2009-11-18 16:53:44 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
static HostCallbackType s_host_callback;
|
2009-10-05 14:40:46 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
static HANDLE s_hConsoleStdIn = NULL;
|
|
|
|
static HANDLE s_hConsoleStdOut = NULL;
|
|
|
|
static HANDLE s_hConsoleStdErr = NULL;
|
|
|
|
#endif
|
|
|
|
} // namespace Log
|
|
|
|
|
|
|
|
float Log::GetCurrentMessageTime()
|
|
|
|
{
|
|
|
|
return static_cast<float>(Common::Timer::ConvertValueToSeconds(Common::Timer::GetCurrentValue() - s_start_timestamp));
|
|
|
|
}
|
|
|
|
|
|
|
|
__ri void Log::WriteToConsole(LOGLEVEL level, ConsoleColors color, std::string_view message)
|
|
|
|
{
|
|
|
|
static constexpr std::string_view s_ansi_color_codes[ConsoleColors_Count] = {
|
|
|
|
"\033[0m"sv, // default
|
|
|
|
"\033[30m\033[1m"sv, // black
|
|
|
|
"\033[32m"sv, // green
|
|
|
|
"\033[31m"sv, // red
|
|
|
|
"\033[34m"sv, // blue
|
|
|
|
"\033[35m"sv, // magenta
|
|
|
|
"\033[35m"sv, // orange (FIXME)
|
|
|
|
"\033[37m"sv, // gray
|
|
|
|
"\033[36m"sv, // cyan
|
|
|
|
"\033[33m"sv, // yellow
|
|
|
|
"\033[37m"sv, // white
|
|
|
|
"\033[30m\033[1m"sv, // strong black
|
|
|
|
"\033[31m\033[1m"sv, // strong red
|
|
|
|
"\033[32m\033[1m"sv, // strong green
|
|
|
|
"\033[34m\033[1m"sv, // strong blue
|
|
|
|
"\033[35m\033[1m"sv, // strong magenta
|
|
|
|
"\033[35m\033[1m"sv, // strong orange (FIXME)
|
|
|
|
"\033[37m\033[1m"sv, // strong gray
|
|
|
|
"\033[36m\033[1m"sv, // strong cyan
|
|
|
|
"\033[33m\033[1m"sv, // strong yellow
|
|
|
|
"\033[37m\033[1m"sv, // strong white
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr size_t BUFFER_SIZE = 512;
|
|
|
|
|
|
|
|
SmallStackString<BUFFER_SIZE> buffer;
|
|
|
|
buffer.reserve(32 + message.length());
|
|
|
|
buffer.append(s_ansi_color_codes[color]);
|
|
|
|
|
|
|
|
if (s_log_timestamps)
|
|
|
|
buffer.append_fmt(TIMESTAMP_FORMAT_STRING, Log::GetCurrentMessageTime());
|
|
|
|
|
|
|
|
buffer.append(message);
|
|
|
|
buffer.append('\n');
|
2009-10-05 14:40:46 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
const HANDLE hOutput = (level <= LOGLEVEL_WARNING) ? s_hConsoleStdErr : s_hConsoleStdOut;
|
|
|
|
|
|
|
|
// Convert to UTF-16 first so Unicode characters display correctly. NT is going to do it anyway...
|
|
|
|
wchar_t wbuf[BUFFER_SIZE];
|
|
|
|
wchar_t* wmessage_buf = wbuf;
|
|
|
|
int wmessage_buflen = static_cast<int>(std::size(wbuf) - 1);
|
|
|
|
if (buffer.length() >= std::size(wbuf))
|
2021-09-06 18:28:26 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
wmessage_buflen = static_cast<int>(buffer.length());
|
|
|
|
wmessage_buf = static_cast<wchar_t*>(std::malloc(static_cast<size_t>(wmessage_buflen) * sizeof(wchar_t)));
|
|
|
|
if (!wmessage_buf)
|
|
|
|
return;
|
2021-09-06 18:28:26 +00:00
|
|
|
}
|
2009-11-08 04:32:15 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
const int wmessage_size = MultiByteToWideChar(CP_UTF8, 0, buffer.data(), static_cast<int>(buffer.length()), wmessage_buf, wmessage_buflen);
|
|
|
|
if (wmessage_size <= 0)
|
|
|
|
goto cleanup;
|
2009-10-05 14:40:46 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
DWORD chars_written;
|
|
|
|
WriteConsoleW(hOutput, wmessage_buf, static_cast<DWORD>(wmessage_size), &chars_written, nullptr);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (wmessage_buf != wbuf)
|
|
|
|
std::free(wmessage_buf);
|
|
|
|
#else
|
|
|
|
const int fd = (level <= LOGLEVEL_WARNING) ? STDERR_FILENO : STDOUT_FILENO;
|
|
|
|
write(fd, buffer.data(), buffer.length());
|
|
|
|
#endif
|
2009-10-05 14:40:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
bool Log::IsConsoleOutputEnabled()
|
2009-11-08 04:32:15 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
return (s_console_level > LOGLEVEL_NONE);
|
2009-11-08 04:32:15 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
void Log::SetConsoleOutputLevel(LOGLEVEL level)
|
2009-11-08 04:32:15 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
if (s_console_level == level)
|
2021-09-11 03:11:54 +00:00
|
|
|
return;
|
2009-11-08 04:32:15 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
const bool was_enabled = (s_console_level > LOGLEVEL_NONE);
|
|
|
|
const bool now_enabled = (level > LOGLEVEL_NONE);
|
|
|
|
s_console_level = level;
|
|
|
|
UpdateMaxLevel();
|
2009-11-08 04:32:15 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
if (was_enabled == now_enabled)
|
|
|
|
return;
|
2021-09-06 18:28:26 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
// Worst that happens here is we write to a bad handle..
|
2009-10-05 14:40:46 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
static constexpr auto enable_virtual_terminal_processing = [](HANDLE hConsole) {
|
|
|
|
DWORD old_mode;
|
|
|
|
if (!GetConsoleMode(hConsole, &old_mode))
|
|
|
|
return;
|
2009-07-03 20:12:33 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
// already enabled?
|
|
|
|
if (old_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING)
|
|
|
|
return;
|
2009-07-03 00:49:40 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
SetConsoleMode(hConsole, old_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
|
|
|
|
};
|
2009-07-03 00:49:40 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
// On windows, no console is allocated by default on a windows based application
|
|
|
|
static bool console_was_allocated = false;
|
|
|
|
static HANDLE old_stdin = NULL;
|
|
|
|
static HANDLE old_stdout = NULL;
|
|
|
|
static HANDLE old_stderr = NULL;
|
2022-05-18 13:27:23 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
if (now_enabled)
|
2022-05-18 13:27:23 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
old_stdin = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
old_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
old_stderr = GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
|
|
|
|
if (!old_stdout)
|
|
|
|
{
|
|
|
|
// Attach to the parent console if we're running from a command window
|
|
|
|
if (!AttachConsole(ATTACH_PARENT_PROCESS) && !AllocConsole())
|
|
|
|
return;
|
|
|
|
|
|
|
|
s_hConsoleStdIn = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
s_hConsoleStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
s_hConsoleStdErr = GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
|
|
|
|
enable_virtual_terminal_processing(s_hConsoleStdOut);
|
|
|
|
enable_virtual_terminal_processing(s_hConsoleStdErr);
|
|
|
|
|
|
|
|
std::FILE* fp;
|
|
|
|
freopen_s(&fp, "CONIN$", "r", stdin);
|
|
|
|
freopen_s(&fp, "CONOUT$", "w", stdout);
|
|
|
|
freopen_s(&fp, "CONOUT$", "w", stderr);
|
|
|
|
|
|
|
|
console_was_allocated = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s_hConsoleStdIn = old_stdin;
|
|
|
|
s_hConsoleStdOut = old_stdout;
|
|
|
|
s_hConsoleStdErr = old_stderr;
|
|
|
|
}
|
2022-05-18 13:27:23 +00:00
|
|
|
}
|
2024-01-11 08:08:16 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (console_was_allocated)
|
|
|
|
{
|
|
|
|
console_was_allocated = false;
|
2022-05-18 13:27:23 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
std::FILE* fp;
|
|
|
|
freopen_s(&fp, "NUL:", "w", stderr);
|
|
|
|
freopen_s(&fp, "NUL:", "w", stdout);
|
|
|
|
freopen_s(&fp, "NUL:", "w", stdin);
|
2009-11-15 06:26:55 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
SetStdHandle(STD_ERROR_HANDLE, old_stderr);
|
|
|
|
SetStdHandle(STD_OUTPUT_HANDLE, old_stdout);
|
|
|
|
SetStdHandle(STD_INPUT_HANDLE, old_stdin);
|
2009-11-15 06:26:55 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
s_hConsoleStdIn = NULL;
|
|
|
|
s_hConsoleStdOut = NULL;
|
|
|
|
s_hConsoleStdErr = NULL;
|
|
|
|
|
|
|
|
FreeConsole();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2009-11-15 06:26:55 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
__ri void Log::WriteToDebug(LOGLEVEL level, ConsoleColors color, std::string_view message)
|
2009-11-15 06:26:55 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
static constexpr size_t BUFFER_SIZE = 512;
|
2010-08-06 05:46:09 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
// Convert to UTF-16 first so Unicode characters display correctly. NT is going to do it anyway...
|
|
|
|
wchar_t wbuf[BUFFER_SIZE];
|
|
|
|
wchar_t* wmessage_buf = wbuf;
|
|
|
|
int wmessage_buflen = static_cast<int>(std::size(wbuf) - 1);
|
|
|
|
if (message.length() >= std::size(wbuf))
|
|
|
|
{
|
|
|
|
wmessage_buflen = static_cast<int>(message.length());
|
|
|
|
wmessage_buf = static_cast<wchar_t*>(std::malloc((static_cast<size_t>(wmessage_buflen) + 2) * sizeof(wchar_t)));
|
|
|
|
if (!wmessage_buf)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int wmessage_size = 0;
|
|
|
|
if (!message.empty()) [[likely]]
|
|
|
|
{
|
|
|
|
wmessage_size = MultiByteToWideChar(CP_UTF8, 0, message.data(), static_cast<int>(message.length()), wmessage_buf, wmessage_buflen);
|
|
|
|
if (wmessage_size <= 0)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2009-11-18 16:53:44 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
wmessage_buf[wmessage_size++] = L'\n';
|
|
|
|
wmessage_buf[wmessage_size++] = 0;
|
|
|
|
OutputDebugStringW(wmessage_buf);
|
2009-11-18 16:53:44 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
cleanup:
|
|
|
|
if (wmessage_buf != wbuf)
|
|
|
|
std::free(wmessage_buf);
|
|
|
|
#endif
|
2009-11-15 06:26:55 +00:00
|
|
|
}
|
|
|
|
|
2024-01-12 15:19:51 +00:00
|
|
|
bool Log::IsDebugOutputAvailable()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return IsDebuggerPresent();
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
bool Log::IsDebugOutputEnabled()
|
2009-11-15 06:26:55 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
return (s_console_level > LOGLEVEL_NONE);
|
2009-11-15 06:26:55 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
void Log::SetDebugOutputLevel(LOGLEVEL level)
|
2009-11-15 06:26:55 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
s_debug_level = level;
|
|
|
|
UpdateMaxLevel();
|
2009-11-15 06:26:55 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
__ri void Log::WriteToFile(LOGLEVEL level, ConsoleColors color, std::string_view message)
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
std::unique_lock lock(s_file_mutex);
|
|
|
|
if (!s_file_handle) [[unlikely]]
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!message.empty()) [[likely]]
|
2022-05-18 13:27:23 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
if (s_log_timestamps)
|
|
|
|
{
|
|
|
|
std::fprintf(s_file_handle.get(), TIMESTAMP_PRINTF_STRING "%.*s\n", GetCurrentMessageTime(),
|
|
|
|
static_cast<int>(message.size()), message.data());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::fprintf(s_file_handle.get(), "%.*s\n", static_cast<int>(message.size()), message.data());
|
|
|
|
}
|
2022-05-18 13:27:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
if (s_log_timestamps)
|
|
|
|
{
|
|
|
|
std::fprintf(s_file_handle.get(), TIMESTAMP_PRINTF_STRING "\n", GetCurrentMessageTime());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::fputc('\n', s_file_handle.get());
|
|
|
|
}
|
2022-05-18 13:27:23 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
std::fflush(s_file_handle.get());
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
bool Log::IsFileOutputEnabled()
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
return (s_file_level > LOGLEVEL_NONE);
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
bool Log::SetFileOutputLevel(LOGLEVEL level, std::string path)
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
std::unique_lock lock(s_file_mutex);
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
const bool was_enabled = (s_file_level > LOGLEVEL_NONE);
|
|
|
|
const bool new_enabled = (level > LOGLEVEL_NONE && !path.empty());
|
|
|
|
if (was_enabled != new_enabled || (new_enabled && path == s_file_path))
|
|
|
|
{
|
|
|
|
if (new_enabled)
|
|
|
|
{
|
|
|
|
if (!s_file_handle || s_file_path != path)
|
|
|
|
{
|
|
|
|
s_file_handle.reset();
|
|
|
|
s_file_handle = FileSystem::OpenManagedCFile(path.c_str(), "wb");
|
|
|
|
if (s_file_handle)
|
|
|
|
{
|
|
|
|
s_file_path = std::move(path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s_file_path = {};
|
|
|
|
|
|
|
|
if (IsConsoleOutputEnabled())
|
|
|
|
WriteToConsole(LOGLEVEL_ERROR, Color_StrongRed, TinyString::from_fmt("Failed to open log file '{}'", path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s_file_handle.reset();
|
|
|
|
s_file_path = {};
|
|
|
|
}
|
|
|
|
}
|
2009-11-05 19:32:27 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
s_file_level = s_file_handle ? level : LOGLEVEL_NONE;
|
|
|
|
return IsFileOutputEnabled();
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
std::FILE* Log::GetFileLogHandle()
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
std::unique_lock lock(s_file_mutex);
|
|
|
|
return s_file_handle.get();
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
bool Log::IsHostOutputEnabled()
|
2021-01-14 07:13:33 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
return (s_host_level > LOGLEVEL_NONE);
|
2021-01-14 07:13:33 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
void Log::SetHostOutputLevel(LOGLEVEL level, HostCallbackType callback)
|
2021-01-14 07:13:33 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
s_host_callback = callback;
|
|
|
|
s_host_level = callback ? level : LOGLEVEL_NONE;
|
|
|
|
UpdateMaxLevel();
|
2021-01-14 07:13:33 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
bool Log::AreTimestampsEnabled()
|
2021-01-14 07:13:33 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
return s_log_timestamps;
|
2021-01-14 07:13:33 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
void Log::SetTimestampsEnabled(bool enabled)
|
2021-01-14 07:13:33 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
s_log_timestamps = enabled;
|
2021-01-14 07:13:33 +00:00
|
|
|
}
|
2014-06-29 06:57:33 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
LOGLEVEL Log::GetMaxLevel()
|
2009-12-07 22:43:16 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
return s_max_level;
|
2010-08-06 05:46:09 +00:00
|
|
|
}
|
2009-12-07 22:43:16 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
__ri void Log::UpdateMaxLevel()
|
2010-08-06 05:46:09 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
s_max_level = std::max(s_console_level, std::max(s_debug_level, std::max(s_file_level, s_host_level)));
|
2010-08-06 05:46:09 +00:00
|
|
|
}
|
2009-12-07 22:43:16 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
void Log::ExecuteCallbacks(LOGLEVEL level, ConsoleColors color, std::string_view message)
|
2010-08-06 05:46:09 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
// TODO: Cache the message time.
|
|
|
|
|
|
|
|
// Split newlines into separate messages.
|
|
|
|
std::string_view::size_type start_pos = 0;
|
|
|
|
if (std::string_view::size_type end_pos = message.find('\n'); end_pos != std::string::npos) [[unlikely]]
|
2021-09-06 18:28:26 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
std::string_view message_line;
|
|
|
|
if (start_pos != end_pos)
|
|
|
|
message_line = message.substr(start_pos, (end_pos == std::string_view::npos) ? end_pos : end_pos - start_pos);
|
|
|
|
|
|
|
|
ExecuteCallbacks(level, color, message_line);
|
|
|
|
|
|
|
|
if (end_pos == std::string_view::npos)
|
|
|
|
return;
|
|
|
|
|
|
|
|
start_pos = end_pos + 1;
|
|
|
|
end_pos = message.find('\n', start_pos);
|
|
|
|
}
|
|
|
|
return;
|
2021-09-06 18:28:26 +00:00
|
|
|
}
|
2009-12-07 22:43:16 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
pxAssert(level > LOGLEVEL_NONE);
|
|
|
|
if (level <= s_console_level)
|
|
|
|
WriteToConsole(level, color, message);
|
2009-12-07 22:43:16 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
if (level <= s_debug_level)
|
|
|
|
WriteToDebug(level, color, message);
|
2009-12-07 22:43:16 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
if (level <= s_file_level)
|
|
|
|
WriteToFile(level, color, message);
|
2010-08-06 05:46:09 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
if (level <= s_host_level)
|
|
|
|
{
|
|
|
|
// double check in case of race here
|
|
|
|
const HostCallbackType callback = s_host_callback;
|
|
|
|
if (callback)
|
|
|
|
s_host_callback(level, color, message);
|
|
|
|
}
|
2010-08-06 05:46:09 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
void Log::Write(LOGLEVEL level, ConsoleColors color, std::string_view message)
|
2010-08-06 05:46:09 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
if (level > s_max_level)
|
|
|
|
return;
|
2010-08-06 05:46:09 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
ExecuteCallbacks(level, color, message);
|
2010-08-06 05:46:09 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
void Log::Writef(LOGLEVEL level, ConsoleColors color, const char* format, ...)
|
2010-08-06 05:46:09 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
std::va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
Writev(level, color, format, ap);
|
|
|
|
va_end(ap);
|
2009-12-07 22:43:16 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
void Log::Writev(LOGLEVEL level, ConsoleColors color, const char* format, va_list ap)
|
|
|
|
{
|
|
|
|
if (level > s_max_level)
|
|
|
|
return;
|
2009-12-07 22:43:16 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
std::va_list ap_copy;
|
|
|
|
va_copy(ap_copy, ap);
|
Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message). These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros. New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
* Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
* More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
* Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
* Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
const u32 required_size = static_cast<u32>(_vscprintf(format, ap_copy));
|
|
|
|
#else
|
|
|
|
const u32 required_size = std::vsnprintf(nullptr, 0, format, ap_copy);
|
2009-10-05 14:40:46 +00:00
|
|
|
#endif
|
2024-01-11 08:08:16 +00:00
|
|
|
va_end(ap_copy);
|
2009-10-05 14:40:46 +00:00
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
if (required_size < 512)
|
|
|
|
{
|
|
|
|
char buffer[512];
|
|
|
|
const int len = std::vsnprintf(buffer, std::size(buffer), format, ap);
|
|
|
|
if (len > 0)
|
|
|
|
ExecuteCallbacks(level, color, std::string_view(buffer, static_cast<size_t>(len)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char* buffer = new char[required_size + 1];
|
|
|
|
const int len = std::vsnprintf(buffer, required_size + 1, format, ap);
|
|
|
|
if (len > 0)
|
|
|
|
ExecuteCallbacks(level, color, std::string_view(buffer, static_cast<size_t>(len)));
|
|
|
|
delete[] buffer;
|
|
|
|
}
|
2010-08-06 05:46:09 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 08:08:16 +00:00
|
|
|
void Log::WriteFmtArgs(LOGLEVEL level, ConsoleColors color, fmt::string_view fmt, fmt::format_args args)
|
2010-08-06 05:46:09 +00:00
|
|
|
{
|
2024-01-11 08:08:16 +00:00
|
|
|
if (level > s_max_level)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fmt::memory_buffer buffer;
|
|
|
|
fmt::vformat_to(std::back_inserter(buffer), fmt, args);
|
|
|
|
|
|
|
|
ExecuteCallbacks(level, color, std::string_view(buffer.data(), buffer.size()));
|
2010-08-06 05:46:09 +00:00
|
|
|
}
|