2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:09:55 +00:00
|
|
|
// Refer to the license.txt file included.
|
2009-03-28 08:57:34 +00:00
|
|
|
|
2019-06-14 14:53:46 +00:00
|
|
|
#include "Common/Logging/LogManager.h"
|
|
|
|
|
2018-08-28 09:48:43 +00:00
|
|
|
#include <algorithm>
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstring>
|
2018-08-28 09:48:43 +00:00
|
|
|
#include <locale>
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <ostream>
|
|
|
|
#include <string>
|
2010-05-28 23:14:16 +00:00
|
|
|
|
2019-06-14 14:53:46 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2016-10-04 07:36:36 +00:00
|
|
|
#include "Common/CommonPaths.h"
|
2017-07-09 22:42:41 +00:00
|
|
|
#include "Common/Config/Config.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2014-06-05 23:29:54 +00:00
|
|
|
#include "Common/Logging/ConsoleListener.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-20 03:11:52 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2014-02-19 00:54:11 +00:00
|
|
|
#include "Common/Timer.h"
|
2010-01-19 19:28:27 +00:00
|
|
|
|
2019-11-28 09:19:24 +00:00
|
|
|
namespace Common::Log
|
|
|
|
{
|
2017-07-07 22:00:25 +00:00
|
|
|
constexpr size_t MAX_MSGLEN = 1024;
|
|
|
|
|
2020-05-02 12:39:40 +00:00
|
|
|
const Config::Info<bool> LOGGER_WRITE_TO_FILE{{Config::System::Logger, "Options", "WriteToFile"},
|
|
|
|
false};
|
|
|
|
const Config::Info<bool> LOGGER_WRITE_TO_CONSOLE{
|
2017-07-09 22:42:41 +00:00
|
|
|
{Config::System::Logger, "Options", "WriteToConsole"}, true};
|
2020-05-02 12:39:40 +00:00
|
|
|
const Config::Info<bool> LOGGER_WRITE_TO_WINDOW{
|
2017-07-09 22:42:41 +00:00
|
|
|
{Config::System::Logger, "Options", "WriteToWindow"}, true};
|
2020-05-02 12:39:40 +00:00
|
|
|
const Config::Info<int> LOGGER_VERBOSITY{{Config::System::Logger, "Options", "Verbosity"}, 0};
|
2017-07-09 22:42:41 +00:00
|
|
|
|
2017-07-07 22:00:25 +00:00
|
|
|
class FileLogListener : public LogListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FileLogListener(const std::string& filename)
|
|
|
|
{
|
|
|
|
File::OpenFStream(m_logfile, filename, std::ios::app);
|
|
|
|
SetEnable(true);
|
|
|
|
}
|
|
|
|
|
2019-11-28 09:19:24 +00:00
|
|
|
void Log(LOG_LEVELS, const char* msg) override
|
2017-07-07 22:00:25 +00:00
|
|
|
{
|
|
|
|
if (!IsEnabled() || !IsValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lk(m_log_lock);
|
|
|
|
m_logfile << msg << std::flush;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsValid() const { return m_logfile.good(); }
|
|
|
|
bool IsEnabled() const { return m_enable; }
|
|
|
|
void SetEnable(bool enable) { m_enable = enable; }
|
2018-04-12 12:18:04 +00:00
|
|
|
|
2017-07-07 22:00:25 +00:00
|
|
|
private:
|
|
|
|
std::mutex m_log_lock;
|
|
|
|
std::ofstream m_logfile;
|
|
|
|
bool m_enable;
|
|
|
|
};
|
|
|
|
|
2019-11-28 09:19:24 +00:00
|
|
|
void GenericLog(LOG_LEVELS level, LOG_TYPE type, const char* file, int line, const char* fmt, ...)
|
2009-07-12 21:58:32 +00:00
|
|
|
{
|
2020-10-20 19:37:02 +00:00
|
|
|
auto* instance = LogManager::GetInstance();
|
|
|
|
if (instance == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!instance->IsEnabled(type, level))
|
|
|
|
return;
|
|
|
|
|
2009-07-12 21:58:32 +00:00
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2020-10-20 19:37:02 +00:00
|
|
|
char message[MAX_MSGLEN];
|
|
|
|
CharArrayFromFormatV(message, MAX_MSGLEN, fmt, args);
|
2009-07-12 21:58:32 +00:00
|
|
|
va_end(args);
|
2020-10-20 19:37:02 +00:00
|
|
|
|
|
|
|
instance->Log(level, type, file, line, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GenericLogFmtImpl(LOG_LEVELS level, LOG_TYPE type, const char* file, int line,
|
2020-11-19 01:36:36 +00:00
|
|
|
fmt::string_view format, const fmt::format_args& args)
|
2020-10-20 19:37:02 +00:00
|
|
|
{
|
|
|
|
auto* instance = LogManager::GetInstance();
|
|
|
|
if (instance == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!instance->IsEnabled(type, level))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto message = fmt::vformat(format, args);
|
|
|
|
instance->Log(level, type, file, line, message.c_str());
|
2009-07-12 21:58:32 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 07:36:36 +00:00
|
|
|
static size_t DeterminePathCutOffPoint()
|
|
|
|
{
|
2018-08-28 09:48:43 +00:00
|
|
|
constexpr const char* pattern = "/source/core/";
|
|
|
|
#ifdef _WIN32
|
|
|
|
constexpr const char* pattern2 = "\\source\\core\\";
|
|
|
|
#endif
|
|
|
|
std::string path = __FILE__;
|
|
|
|
std::transform(path.begin(), path.end(), path.begin(),
|
|
|
|
[](char c) { return std::tolower(c, std::locale::classic()); });
|
|
|
|
size_t pos = path.find(pattern);
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
pos = path.find(pattern2);
|
|
|
|
#endif
|
2016-10-04 07:36:36 +00:00
|
|
|
if (pos != std::string::npos)
|
|
|
|
return pos + strlen(pattern);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-05 06:11:26 +00:00
|
|
|
LogManager::LogManager()
|
|
|
|
{
|
2015-09-10 06:28:44 +00:00
|
|
|
// create log containers
|
2020-06-14 12:37:24 +00:00
|
|
|
m_log[ACTIONREPLAY] = {"ActionReplay", "Action Replay"};
|
2019-11-28 09:19:24 +00:00
|
|
|
m_log[AUDIO] = {"Audio", "Audio Emulator"};
|
|
|
|
m_log[AUDIO_INTERFACE] = {"AI", "Audio Interface"};
|
|
|
|
m_log[BOOT] = {"BOOT", "Boot"};
|
2020-06-14 12:37:24 +00:00
|
|
|
m_log[COMMANDPROCESSOR] = {"CP", "Command Processor"};
|
2019-11-28 09:19:24 +00:00
|
|
|
m_log[COMMON] = {"COMMON", "Common"};
|
|
|
|
m_log[CONSOLE] = {"CONSOLE", "Dolphin Console"};
|
|
|
|
m_log[CORE] = {"CORE", "Core"};
|
|
|
|
m_log[DISCIO] = {"DIO", "Disc IO"};
|
|
|
|
m_log[DSPHLE] = {"DSPHLE", "DSP HLE"};
|
|
|
|
m_log[DSPLLE] = {"DSPLLE", "DSP LLE"};
|
|
|
|
m_log[DSP_MAIL] = {"DSPMails", "DSP Mails"};
|
2020-06-14 12:37:24 +00:00
|
|
|
m_log[DSPINTERFACE] = {"DSP", "DSP Interface"};
|
2019-11-28 09:19:24 +00:00
|
|
|
m_log[DVDINTERFACE] = {"DVD", "DVD Interface"};
|
2020-06-14 12:37:24 +00:00
|
|
|
m_log[DYNA_REC] = {"JIT", "JIT Dynamic Recompiler"};
|
2019-11-28 09:19:24 +00:00
|
|
|
m_log[EXPANSIONINTERFACE] = {"EXI", "Expansion Interface"};
|
|
|
|
m_log[FILEMON] = {"FileMon", "File Monitor"};
|
2020-11-09 18:45:04 +00:00
|
|
|
m_log[FRAMEDUMP] = {"FRAMEDUMP", "FrameDump"};
|
2019-11-28 09:19:24 +00:00
|
|
|
m_log[GDB_STUB] = {"GDB_STUB", "GDB Stub"};
|
2020-06-14 12:37:24 +00:00
|
|
|
m_log[GPFIFO] = {"GP", "GatherPipe FIFO"};
|
2019-11-28 09:19:24 +00:00
|
|
|
m_log[HOST_GPU] = {"Host GPU", "Host GPU"};
|
|
|
|
m_log[IOS] = {"IOS", "IOS"};
|
|
|
|
m_log[IOS_DI] = {"IOS_DI", "IOS - Drive Interface"};
|
|
|
|
m_log[IOS_ES] = {"IOS_ES", "IOS - ETicket Services"};
|
|
|
|
m_log[IOS_FS] = {"IOS_FS", "IOS - Filesystem Services"};
|
|
|
|
m_log[IOS_SD] = {"IOS_SD", "IOS - SDIO"};
|
|
|
|
m_log[IOS_SSL] = {"IOS_SSL", "IOS - SSL"};
|
|
|
|
m_log[IOS_STM] = {"IOS_STM", "IOS - State Transition Manager"};
|
|
|
|
m_log[IOS_NET] = {"IOS_NET", "IOS - Network"};
|
|
|
|
m_log[IOS_USB] = {"IOS_USB", "IOS - USB"};
|
|
|
|
m_log[IOS_WC24] = {"IOS_WC24", "IOS - WiiConnect24"};
|
|
|
|
m_log[IOS_WFS] = {"IOS_WFS", "IOS - WFS"};
|
|
|
|
m_log[IOS_WIIMOTE] = {"IOS_WIIMOTE", "IOS - Wii Remote"};
|
|
|
|
m_log[MASTER_LOG] = {"MASTER", "Master Log"};
|
2020-06-14 12:37:24 +00:00
|
|
|
m_log[MEMCARD_MANAGER] = {"MemCard Manager", "Memory Card Manager"};
|
|
|
|
m_log[MEMMAP] = {"MI", "Memory Interface & Memory Map"};
|
2019-11-28 09:19:24 +00:00
|
|
|
m_log[NETPLAY] = {"NETPLAY", "Netplay"};
|
2020-06-14 12:37:24 +00:00
|
|
|
m_log[OSHLE] = {"HLE", "OSHLE"};
|
2019-11-28 09:19:24 +00:00
|
|
|
m_log[OSREPORT] = {"OSREPORT", "OSReport"};
|
|
|
|
m_log[PAD] = {"PAD", "Pad"};
|
2020-06-14 12:37:24 +00:00
|
|
|
m_log[PIXELENGINE] = {"PE", "Pixel Engine"};
|
|
|
|
m_log[PROCESSORINTERFACE] = {"PI", "Processor Interface"};
|
|
|
|
m_log[POWERPC] = {"PowerPC", "PowerPC IBM CPU"};
|
2019-11-28 09:19:24 +00:00
|
|
|
m_log[SERIALINTERFACE] = {"SI", "Serial Interface"};
|
|
|
|
m_log[SP1] = {"SP1", "Serial Port 1"};
|
|
|
|
m_log[SYMBOLS] = {"SYMBOLS", "Symbols"};
|
|
|
|
m_log[VIDEO] = {"Video", "Video Backend"};
|
|
|
|
m_log[VIDEOINTERFACE] = {"VI", "Video Interface"};
|
2020-06-14 12:37:24 +00:00
|
|
|
m_log[WIIMOTE] = {"Wiimote", "Wii Remote"};
|
2019-11-28 09:19:24 +00:00
|
|
|
m_log[WII_IPC] = {"WII_IPC", "WII IPC"};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-10-03 11:34:36 +00:00
|
|
|
RegisterListener(LogListener::FILE_LISTENER,
|
|
|
|
new FileLogListener(File::GetUserPath(F_MAINLOG_IDX)));
|
|
|
|
RegisterListener(LogListener::CONSOLE_LISTENER, new ConsoleListener());
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-06-13 12:30:53 +00:00
|
|
|
// Set up log listeners
|
2017-07-09 22:42:41 +00:00
|
|
|
int verbosity = Config::Get(LOGGER_VERBOSITY);
|
2017-06-13 12:30:53 +00:00
|
|
|
|
|
|
|
// Ensure the verbosity level is valid
|
|
|
|
if (verbosity < 1)
|
|
|
|
verbosity = 1;
|
|
|
|
if (verbosity > MAX_LOGLEVEL)
|
|
|
|
verbosity = MAX_LOGLEVEL;
|
|
|
|
|
2019-11-28 09:19:24 +00:00
|
|
|
SetLogLevel(static_cast<LOG_LEVELS>(verbosity));
|
2017-07-09 22:42:41 +00:00
|
|
|
EnableListener(LogListener::FILE_LISTENER, Config::Get(LOGGER_WRITE_TO_FILE));
|
|
|
|
EnableListener(LogListener::CONSOLE_LISTENER, Config::Get(LOGGER_WRITE_TO_CONSOLE));
|
|
|
|
EnableListener(LogListener::LOG_WINDOW_LISTENER, Config::Get(LOGGER_WRITE_TO_WINDOW));
|
2017-07-07 22:08:57 +00:00
|
|
|
|
2017-07-07 22:40:19 +00:00
|
|
|
for (LogContainer& container : m_log)
|
2017-07-09 22:42:41 +00:00
|
|
|
container.m_enable = Config::Get(
|
2020-05-02 12:39:40 +00:00
|
|
|
Config::Info<bool>{{Config::System::Logger, "Logs", container.m_short_name}, false});
|
2016-10-04 07:36:36 +00:00
|
|
|
|
|
|
|
m_path_cutoff_point = DeterminePathCutOffPoint();
|
2009-03-18 17:17:58 +00:00
|
|
|
}
|
|
|
|
|
2011-04-01 07:43:02 +00:00
|
|
|
LogManager::~LogManager()
|
|
|
|
{
|
2015-10-03 11:34:36 +00:00
|
|
|
// The log window listener pointer is owned by the GUI code.
|
|
|
|
delete m_listeners[LogListener::CONSOLE_LISTENER];
|
|
|
|
delete m_listeners[LogListener::FILE_LISTENER];
|
2009-03-18 17:17:58 +00:00
|
|
|
}
|
|
|
|
|
2017-07-07 23:10:38 +00:00
|
|
|
void LogManager::SaveSettings()
|
|
|
|
{
|
2019-03-03 16:58:37 +00:00
|
|
|
Config::ConfigChangeCallbackGuard config_guard;
|
|
|
|
|
2017-07-09 22:42:41 +00:00
|
|
|
Config::SetBaseOrCurrent(LOGGER_WRITE_TO_FILE, IsListenerEnabled(LogListener::FILE_LISTENER));
|
|
|
|
Config::SetBaseOrCurrent(LOGGER_WRITE_TO_CONSOLE,
|
|
|
|
IsListenerEnabled(LogListener::CONSOLE_LISTENER));
|
|
|
|
Config::SetBaseOrCurrent(LOGGER_WRITE_TO_WINDOW,
|
|
|
|
IsListenerEnabled(LogListener::LOG_WINDOW_LISTENER));
|
|
|
|
Config::SetBaseOrCurrent(LOGGER_VERBOSITY, static_cast<int>(GetLogLevel()));
|
2017-07-07 23:10:38 +00:00
|
|
|
|
|
|
|
for (const auto& container : m_log)
|
2018-05-11 18:34:39 +00:00
|
|
|
{
|
2020-05-02 12:39:40 +00:00
|
|
|
const Config::Info<bool> info{{Config::System::Logger, "Logs", container.m_short_name}, false};
|
2018-05-11 18:34:39 +00:00
|
|
|
Config::SetBaseOrCurrent(info, container.m_enable);
|
|
|
|
}
|
2017-07-07 23:10:38 +00:00
|
|
|
|
2017-07-09 22:42:41 +00:00
|
|
|
Config::Save();
|
2017-07-07 23:10:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 09:19:24 +00:00
|
|
|
void LogManager::Log(LOG_LEVELS level, LOG_TYPE type, const char* file, int line,
|
2020-10-20 19:37:02 +00:00
|
|
|
const char* message)
|
2011-04-01 07:43:02 +00:00
|
|
|
{
|
2017-07-07 22:40:19 +00:00
|
|
|
if (!IsEnabled(type, level) || !static_cast<bool>(m_listener_ids))
|
2009-03-18 17:17:58 +00:00
|
|
|
return;
|
2009-07-12 21:58:32 +00:00
|
|
|
|
2020-10-20 19:37:02 +00:00
|
|
|
LogWithFullPath(level, type, file + m_path_cutoff_point, line, message);
|
|
|
|
}
|
2009-03-20 21:23:36 +00:00
|
|
|
|
2020-10-20 19:37:02 +00:00
|
|
|
void LogManager::LogWithFullPath(LOG_LEVELS level, LOG_TYPE type, const char* file, int line,
|
|
|
|
const char* message)
|
|
|
|
{
|
2019-06-14 14:53:46 +00:00
|
|
|
const std::string msg =
|
|
|
|
fmt::format("{} {}:{} {}[{}]: {}\n", Common::Timer::GetTimeFormatted(), file, line,
|
2020-10-20 19:37:02 +00:00
|
|
|
LOG_LEVEL_TO_CHAR[static_cast<int>(level)], GetShortName(type), message);
|
2015-10-03 11:34:36 +00:00
|
|
|
|
2020-10-20 19:37:02 +00:00
|
|
|
for (const auto listener_id : m_listener_ids)
|
|
|
|
{
|
2017-06-17 08:39:06 +00:00
|
|
|
if (m_listeners[listener_id])
|
|
|
|
m_listeners[listener_id]->Log(level, msg.c_str());
|
2020-10-20 19:37:02 +00:00
|
|
|
}
|
2009-03-18 17:17:58 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 09:19:24 +00:00
|
|
|
LOG_LEVELS LogManager::GetLogLevel() const
|
2017-07-07 22:08:57 +00:00
|
|
|
{
|
|
|
|
return m_level;
|
|
|
|
}
|
|
|
|
|
2019-11-28 09:19:24 +00:00
|
|
|
void LogManager::SetLogLevel(LOG_LEVELS level)
|
2010-02-16 04:34:41 +00:00
|
|
|
{
|
2017-07-07 22:08:57 +00:00
|
|
|
m_level = level;
|
2010-02-16 04:34:41 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 09:19:24 +00:00
|
|
|
void LogManager::SetEnable(LOG_TYPE type, bool enable)
|
2010-02-16 04:34:41 +00:00
|
|
|
{
|
2017-07-07 22:40:19 +00:00
|
|
|
m_log[type].m_enable = enable;
|
2010-02-16 04:34:41 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 09:19:24 +00:00
|
|
|
bool LogManager::IsEnabled(LOG_TYPE type, LOG_LEVELS level) const
|
2011-04-01 07:43:02 +00:00
|
|
|
{
|
2017-07-07 22:40:19 +00:00
|
|
|
return m_log[type].m_enable && GetLogLevel() >= level;
|
2009-03-20 20:52:37 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 17:58:46 +00:00
|
|
|
std::map<std::string, std::string> LogManager::GetLogTypes()
|
|
|
|
{
|
|
|
|
std::map<std::string, std::string> log_types;
|
|
|
|
|
|
|
|
for (const auto& container : m_log)
|
|
|
|
{
|
|
|
|
log_types.emplace(container.m_short_name, container.m_full_name);
|
|
|
|
}
|
|
|
|
return log_types;
|
|
|
|
}
|
|
|
|
|
2019-11-28 09:19:24 +00:00
|
|
|
const char* LogManager::GetShortName(LOG_TYPE type) const
|
2011-03-11 10:21:46 +00:00
|
|
|
{
|
2017-07-07 22:40:19 +00:00
|
|
|
return m_log[type].m_short_name;
|
2009-03-18 17:17:58 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 09:19:24 +00:00
|
|
|
const char* LogManager::GetFullName(LOG_TYPE type) const
|
2011-03-11 10:21:46 +00:00
|
|
|
{
|
2017-07-07 22:40:19 +00:00
|
|
|
return m_log[type].m_full_name;
|
2017-07-07 22:00:25 +00:00
|
|
|
}
|
2009-03-18 17:17:58 +00:00
|
|
|
|
2017-07-07 22:00:25 +00:00
|
|
|
void LogManager::RegisterListener(LogListener::LISTENER id, LogListener* listener)
|
|
|
|
{
|
|
|
|
m_listeners[id] = listener;
|
|
|
|
}
|
|
|
|
|
2017-07-07 22:23:47 +00:00
|
|
|
void LogManager::EnableListener(LogListener::LISTENER id, bool enable)
|
2017-07-07 22:00:25 +00:00
|
|
|
{
|
2017-07-07 22:23:47 +00:00
|
|
|
m_listener_ids[id] = enable;
|
2017-07-07 22:00:25 +00:00
|
|
|
}
|
|
|
|
|
2017-07-07 22:23:47 +00:00
|
|
|
bool LogManager::IsListenerEnabled(LogListener::LISTENER id) const
|
2017-07-07 22:00:25 +00:00
|
|
|
{
|
2017-07-07 22:23:47 +00:00
|
|
|
return m_listener_ids[id];
|
2017-07-07 22:00:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Singleton. Ugh.
|
|
|
|
static LogManager* s_log_manager;
|
|
|
|
|
|
|
|
LogManager* LogManager::GetInstance()
|
|
|
|
{
|
|
|
|
return s_log_manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogManager::Init()
|
|
|
|
{
|
|
|
|
s_log_manager = new LogManager();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogManager::Shutdown()
|
|
|
|
{
|
2017-07-07 23:10:38 +00:00
|
|
|
if (s_log_manager)
|
|
|
|
s_log_manager->SaveSettings();
|
2017-07-07 22:00:25 +00:00
|
|
|
delete s_log_manager;
|
|
|
|
s_log_manager = nullptr;
|
2009-03-18 17:17:58 +00:00
|
|
|
}
|
2019-11-28 09:19:24 +00:00
|
|
|
} // namespace Common::Log
|