2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:09:55 -06:00
|
|
|
// Refer to the license.txt file included.
|
2009-03-28 02:57:34 -06:00
|
|
|
|
2014-02-19 20:11:52 -07:00
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstring>
|
|
|
|
#include <mutex>
|
|
|
|
#include <ostream>
|
|
|
|
#include <string>
|
2010-05-28 17:14:16 -06:00
|
|
|
|
2016-10-04 01:36:36 -06:00
|
|
|
#include "Common/CommonPaths.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/FileUtil.h"
|
2014-09-03 17:48:54 -06:00
|
|
|
#include "Common/IniFile.h"
|
2014-06-05 17:29:54 -06:00
|
|
|
#include "Common/Logging/ConsoleListener.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/Logging/LogManager.h"
|
2016-06-24 02:43:46 -06:00
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
#include "Common/Timer.h"
|
2010-01-19 12:28:27 -07:00
|
|
|
|
2017-07-07 16:00:25 -06:00
|
|
|
constexpr size_t MAX_MSGLEN = 1024;
|
|
|
|
|
|
|
|
class FileLogListener : public LogListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FileLogListener(const std::string& filename)
|
|
|
|
{
|
|
|
|
File::OpenFStream(m_logfile, filename, std::ios::app);
|
|
|
|
SetEnable(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Log(LogTypes::LOG_LEVELS, const char* msg) override
|
|
|
|
{
|
|
|
|
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; }
|
|
|
|
// const char* GetName() const { return "file"; }
|
|
|
|
private:
|
|
|
|
std::mutex m_log_lock;
|
|
|
|
std::ofstream m_logfile;
|
|
|
|
bool m_enable;
|
|
|
|
};
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
void GenericLog(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type, const char* file, int line,
|
|
|
|
const char* fmt, ...)
|
2009-07-12 15:58:32 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
if (LogManager::GetInstance())
|
|
|
|
LogManager::GetInstance()->Log(level, type, file, line, fmt, args);
|
|
|
|
va_end(args);
|
2009-07-12 15:58:32 -06:00
|
|
|
}
|
|
|
|
|
2016-10-04 01:36:36 -06:00
|
|
|
static size_t DeterminePathCutOffPoint()
|
|
|
|
{
|
|
|
|
constexpr const char* pattern = DIR_SEP "Source" DIR_SEP "Core" DIR_SEP;
|
|
|
|
size_t pos = std::string(__FILE__).find(pattern);
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
return pos + strlen(pattern);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-04 23:11:26 -07:00
|
|
|
LogManager::LogManager()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
// create log containers
|
2017-07-07 16:40:19 -06:00
|
|
|
m_log[LogTypes::ACTIONREPLAY] = {"ActionReplay", "ActionReplay"};
|
|
|
|
m_log[LogTypes::AUDIO] = {"Audio", "Audio Emulator"};
|
|
|
|
m_log[LogTypes::AUDIO_INTERFACE] = {"AI", "Audio Interface (AI)"};
|
|
|
|
m_log[LogTypes::BOOT] = {"BOOT", "Boot"};
|
|
|
|
m_log[LogTypes::COMMANDPROCESSOR] = {"CP", "CommandProc"};
|
|
|
|
m_log[LogTypes::COMMON] = {"COMMON", "Common"};
|
|
|
|
m_log[LogTypes::CONSOLE] = {"CONSOLE", "Dolphin Console"};
|
|
|
|
m_log[LogTypes::CORE] = {"CORE", "Core"};
|
|
|
|
m_log[LogTypes::DISCIO] = {"DIO", "Disc IO"};
|
|
|
|
m_log[LogTypes::DSPHLE] = {"DSPHLE", "DSP HLE"};
|
|
|
|
m_log[LogTypes::DSPLLE] = {"DSPLLE", "DSP LLE"};
|
|
|
|
m_log[LogTypes::DSP_MAIL] = {"DSPMails", "DSP Mails"};
|
|
|
|
m_log[LogTypes::DSPINTERFACE] = {"DSP", "DSPInterface"};
|
|
|
|
m_log[LogTypes::DVDINTERFACE] = {"DVD", "DVD Interface"};
|
|
|
|
m_log[LogTypes::DYNA_REC] = {"JIT", "Dynamic Recompiler"};
|
|
|
|
m_log[LogTypes::EXPANSIONINTERFACE] = {"EXI", "Expansion Interface"};
|
|
|
|
m_log[LogTypes::FILEMON] = {"FileMon", "File Monitor"};
|
|
|
|
m_log[LogTypes::GDB_STUB] = {"GDB_STUB", "GDB Stub"};
|
|
|
|
m_log[LogTypes::GPFIFO] = {"GP", "GPFifo"};
|
|
|
|
m_log[LogTypes::HOST_GPU] = {"Host GPU", "Host GPU"};
|
|
|
|
m_log[LogTypes::IOS] = {"IOS", "IOS"};
|
|
|
|
m_log[LogTypes::IOS_DI] = {"IOS_DI", "IOS - Drive Interface"};
|
|
|
|
m_log[LogTypes::IOS_ES] = {"IOS_ES", "IOS - ETicket Services"};
|
|
|
|
m_log[LogTypes::IOS_FILEIO] = {"IOS_FILEIO", "IOS - FileIO"};
|
|
|
|
m_log[LogTypes::IOS_SD] = {"IOS_SD", "IOS - SDIO"};
|
|
|
|
m_log[LogTypes::IOS_SSL] = {"IOS_SSL", "IOS - SSL"};
|
|
|
|
m_log[LogTypes::IOS_STM] = {"IOS_STM", "IOS - State Transition Manager"};
|
|
|
|
m_log[LogTypes::IOS_NET] = {"IOS_NET", "IOS - Network"};
|
|
|
|
m_log[LogTypes::IOS_USB] = {"IOS_USB", "IOS - USB"};
|
|
|
|
m_log[LogTypes::IOS_WC24] = {"IOS_WC24", "IOS - WiiConnect24"};
|
|
|
|
m_log[LogTypes::IOS_WIIMOTE] = {"IOS_WIIMOTE", "IOS - Wii Remote"};
|
|
|
|
m_log[LogTypes::MASTER_LOG] = {"*", "Master Log"};
|
|
|
|
m_log[LogTypes::MEMCARD_MANAGER] = {"MemCard Manager", "MemCard Manager"};
|
|
|
|
m_log[LogTypes::MEMMAP] = {"MI", "MI & memmap"};
|
|
|
|
m_log[LogTypes::NETPLAY] = {"NETPLAY", "Netplay"};
|
|
|
|
m_log[LogTypes::OSHLE] = {"HLE", "HLE"};
|
|
|
|
m_log[LogTypes::OSREPORT] = {"OSREPORT", "OSReport"};
|
|
|
|
m_log[LogTypes::PAD] = {"PAD", "Pad"};
|
|
|
|
m_log[LogTypes::PIXELENGINE] = {"PE", "PixelEngine"};
|
|
|
|
m_log[LogTypes::PROCESSORINTERFACE] = {"PI", "ProcessorInt"};
|
|
|
|
m_log[LogTypes::POWERPC] = {"PowerPC", "IBM CPU"};
|
|
|
|
m_log[LogTypes::SERIALINTERFACE] = {"SI", "Serial Interface (SI)"};
|
|
|
|
m_log[LogTypes::SP1] = {"SP1", "Serial Port 1"};
|
|
|
|
m_log[LogTypes::VIDEO] = {"Video", "Video Backend"};
|
|
|
|
m_log[LogTypes::VIDEOINTERFACE] = {"VI", "Video Interface (VI)"};
|
|
|
|
m_log[LogTypes::WIIMOTE] = {"Wiimote", "Wiimote"};
|
|
|
|
m_log[LogTypes::WII_IPC] = {"WII_IPC", "WII IPC"};
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
RegisterListener(LogListener::FILE_LISTENER,
|
|
|
|
new FileLogListener(File::GetUserPath(F_MAINLOG_IDX)));
|
|
|
|
RegisterListener(LogListener::CONSOLE_LISTENER, new ConsoleListener());
|
|
|
|
|
|
|
|
IniFile ini;
|
|
|
|
ini.Load(File::GetUserPath(F_LOGGERCONFIG_IDX));
|
|
|
|
IniFile::Section* logs = ini.GetOrCreateSection("Logs");
|
|
|
|
IniFile::Section* options = ini.GetOrCreateSection("Options");
|
|
|
|
bool write_file;
|
|
|
|
bool write_console;
|
2017-06-13 06:35:42 -06:00
|
|
|
bool write_window;
|
2016-06-24 02:43:46 -06:00
|
|
|
options->Get("WriteToFile", &write_file, false);
|
|
|
|
options->Get("WriteToConsole", &write_console, true);
|
2017-06-13 06:35:42 -06:00
|
|
|
options->Get("WriteToWindow", &write_window, true);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-06-13 06:30:53 -06:00
|
|
|
// Set up log listeners
|
|
|
|
int verbosity;
|
|
|
|
options->Get("Verbosity", &verbosity, 0);
|
|
|
|
|
|
|
|
// Ensure the verbosity level is valid
|
|
|
|
if (verbosity < 1)
|
|
|
|
verbosity = 1;
|
|
|
|
if (verbosity > MAX_LOGLEVEL)
|
|
|
|
verbosity = MAX_LOGLEVEL;
|
|
|
|
|
2017-07-07 16:08:57 -06:00
|
|
|
SetLogLevel(static_cast<LogTypes::LOG_LEVELS>(verbosity));
|
2017-07-07 16:23:47 -06:00
|
|
|
EnableListener(LogListener::FILE_LISTENER, write_file);
|
|
|
|
EnableListener(LogListener::CONSOLE_LISTENER, write_console);
|
|
|
|
EnableListener(LogListener::LOG_WINDOW_LISTENER, write_window);
|
2017-07-07 16:08:57 -06:00
|
|
|
|
2017-07-07 16:40:19 -06:00
|
|
|
for (LogContainer& container : m_log)
|
|
|
|
logs->Get(container.m_short_name, &container.m_enable, false);
|
2016-10-04 01:36:36 -06:00
|
|
|
|
|
|
|
m_path_cutoff_point = DeterminePathCutOffPoint();
|
2009-03-18 11:17:58 -06:00
|
|
|
}
|
|
|
|
|
2011-04-01 01:43:02 -06:00
|
|
|
LogManager::~LogManager()
|
|
|
|
{
|
2016-06-24 02:43:46 -06: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 11:17:58 -06:00
|
|
|
}
|
|
|
|
|
2017-07-07 17:10:38 -06:00
|
|
|
void LogManager::SaveSettings()
|
|
|
|
{
|
|
|
|
IniFile ini;
|
|
|
|
ini.Load(File::GetUserPath(F_LOGGERCONFIG_IDX));
|
|
|
|
|
|
|
|
IniFile::Section* options = ini.GetOrCreateSection("Options");
|
|
|
|
options->Set("Verbosity", GetLogLevel());
|
|
|
|
options->Set("WriteToFile", m_listener_ids[LogListener::FILE_LISTENER]);
|
|
|
|
options->Set("WriteToConsole", m_listener_ids[LogListener::CONSOLE_LISTENER]);
|
|
|
|
options->Set("WriteToWindow", m_listener_ids[LogListener::LOG_WINDOW_LISTENER]);
|
|
|
|
|
|
|
|
// Save all enabled/disabled states of the log types to the config ini.
|
|
|
|
for (const auto& container : m_log)
|
|
|
|
ini.GetOrCreateSection("Logs")->Set(container.m_short_name, container.m_enable);
|
|
|
|
|
|
|
|
ini.Save(File::GetUserPath(F_LOGGERCONFIG_IDX));
|
|
|
|
}
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
void LogManager::Log(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type, const char* file,
|
|
|
|
int line, const char* format, va_list args)
|
2017-04-24 00:55:37 -06:00
|
|
|
{
|
|
|
|
return LogWithFullPath(level, type, file + m_path_cutoff_point, line, format, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogManager::LogWithFullPath(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type,
|
|
|
|
const char* file, int line, const char* format, va_list args)
|
2011-04-01 01:43:02 -06:00
|
|
|
{
|
2017-07-07 16:40:19 -06:00
|
|
|
if (!IsEnabled(type, level) || !static_cast<bool>(m_listener_ids))
|
2016-06-24 02:43:46 -06:00
|
|
|
return;
|
2009-07-12 15:58:32 -06:00
|
|
|
|
2017-07-07 16:40:19 -06:00
|
|
|
char temp[MAX_MSGLEN];
|
2016-06-24 02:43:46 -06:00
|
|
|
CharArrayFromFormatV(temp, MAX_MSGLEN, format, args);
|
2009-03-20 15:23:36 -06:00
|
|
|
|
2017-07-07 16:40:19 -06:00
|
|
|
std::string msg =
|
|
|
|
StringFromFormat("%s %s:%u %c[%s]: %s\n", Common::Timer::GetTimeFormatted().c_str(), file,
|
|
|
|
line, LogTypes::LOG_LEVEL_TO_CHAR[(int)level], GetShortName(type), temp);
|
2015-10-03 05:34:36 -06:00
|
|
|
|
2017-07-07 16:23:47 -06:00
|
|
|
for (auto listener_id : m_listener_ids)
|
2017-06-17 02:39:06 -06:00
|
|
|
if (m_listeners[listener_id])
|
|
|
|
m_listeners[listener_id]->Log(level, msg.c_str());
|
2009-03-18 11:17:58 -06:00
|
|
|
}
|
|
|
|
|
2017-07-07 16:08:57 -06:00
|
|
|
LogTypes::LOG_LEVELS LogManager::GetLogLevel() const
|
|
|
|
{
|
|
|
|
return m_level;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogManager::SetLogLevel(LogTypes::LOG_LEVELS level)
|
2010-02-15 21:34:41 -07:00
|
|
|
{
|
2017-07-07 16:08:57 -06:00
|
|
|
m_level = level;
|
2010-02-15 21:34:41 -07:00
|
|
|
}
|
|
|
|
|
2017-07-07 16:00:25 -06:00
|
|
|
void LogManager::SetEnable(LogTypes::LOG_TYPE type, bool enable)
|
2010-02-15 21:34:41 -07:00
|
|
|
{
|
2017-07-07 16:40:19 -06:00
|
|
|
m_log[type].m_enable = enable;
|
2010-02-15 21:34:41 -07:00
|
|
|
}
|
|
|
|
|
2017-07-07 16:00:25 -06:00
|
|
|
bool LogManager::IsEnabled(LogTypes::LOG_TYPE type, LogTypes::LOG_LEVELS level) const
|
2011-04-01 01:43:02 -06:00
|
|
|
{
|
2017-07-07 16:40:19 -06:00
|
|
|
return m_log[type].m_enable && GetLogLevel() >= level;
|
2009-03-20 14:52:37 -06:00
|
|
|
}
|
|
|
|
|
2017-07-07 16:40:19 -06:00
|
|
|
const char* LogManager::GetShortName(LogTypes::LOG_TYPE type) const
|
2011-03-11 03:21:46 -07:00
|
|
|
{
|
2017-07-07 16:40:19 -06:00
|
|
|
return m_log[type].m_short_name;
|
2009-03-18 11:17:58 -06:00
|
|
|
}
|
|
|
|
|
2017-07-07 16:40:19 -06:00
|
|
|
const char* LogManager::GetFullName(LogTypes::LOG_TYPE type) const
|
2011-03-11 03:21:46 -07:00
|
|
|
{
|
2017-07-07 16:40:19 -06:00
|
|
|
return m_log[type].m_full_name;
|
2017-07-07 16:00:25 -06:00
|
|
|
}
|
2009-03-18 11:17:58 -06:00
|
|
|
|
2017-07-07 16:00:25 -06:00
|
|
|
void LogManager::RegisterListener(LogListener::LISTENER id, LogListener* listener)
|
|
|
|
{
|
|
|
|
m_listeners[id] = listener;
|
|
|
|
}
|
|
|
|
|
2017-07-07 16:23:47 -06:00
|
|
|
void LogManager::EnableListener(LogListener::LISTENER id, bool enable)
|
2017-07-07 16:00:25 -06:00
|
|
|
{
|
2017-07-07 16:23:47 -06:00
|
|
|
m_listener_ids[id] = enable;
|
2017-07-07 16:00:25 -06:00
|
|
|
}
|
|
|
|
|
2017-07-07 16:23:47 -06:00
|
|
|
bool LogManager::IsListenerEnabled(LogListener::LISTENER id) const
|
2017-07-07 16:00:25 -06:00
|
|
|
{
|
2017-07-07 16:23:47 -06:00
|
|
|
return m_listener_ids[id];
|
2017-07-07 16:00:25 -06: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 17:10:38 -06:00
|
|
|
if (s_log_manager)
|
|
|
|
s_log_manager->SaveSettings();
|
2017-07-07 16:00:25 -06:00
|
|
|
delete s_log_manager;
|
|
|
|
s_log_manager = nullptr;
|
2009-03-18 11:17:58 -06:00
|
|
|
}
|