| 
									
										
										
										
											2015-05-24 06:55:12 +02:00
										 |  |  | // Copyright 2009 Dolphin Emulator Project
 | 
					
						
							| 
									
										
										
										
											2015-05-18 01:08:10 +02:00
										 |  |  | // Licensed under GPLv2+
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-20 04:11:52 +01:00
										 |  |  | #include <cstdarg>
 | 
					
						
							|  |  |  | #include <cstring>
 | 
					
						
							|  |  |  | #include <mutex>
 | 
					
						
							|  |  |  | #include <ostream>
 | 
					
						
							|  |  |  | #include <set>
 | 
					
						
							|  |  |  | #include <string>
 | 
					
						
							| 
									
										
										
										
											2010-05-28 23:14:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "Common/FileUtil.h"
 | 
					
						
							| 
									
										
										
										
											2014-09-03 19:48:54 -04:00
										 |  |  | #include "Common/IniFile.h"
 | 
					
						
							| 
									
										
										
										
											2014-06-05 19:29:54 -04:00
										 |  |  | #include "Common/Logging/ConsoleListener.h"
 | 
					
						
							|  |  |  | #include "Common/Logging/Log.h"
 | 
					
						
							|  |  |  | #include "Common/Logging/LogManager.h"
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | #include "Common/StringUtil.h"
 | 
					
						
							|  |  |  | #include "Common/Timer.h"
 | 
					
						
							| 
									
										
										
										
											2010-01-19 19:28:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | void GenericLog(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type, const char* file, int line, | 
					
						
							|  |  |  |                 const char* fmt, ...) | 
					
						
							| 
									
										
										
										
											2009-07-12 21:58:32 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02: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 21:58:32 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 22:18:18 +01:00
										 |  |  | LogManager* LogManager::m_logManager = nullptr; | 
					
						
							| 
									
										
										
										
											2009-03-18 17:17:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-05 06:11:26 +00:00
										 |  |  | LogManager::LogManager() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   // create log containers
 | 
					
						
							|  |  |  |   m_Log[LogTypes::ACTIONREPLAY] = new LogContainer("ActionReplay", "ActionReplay"); | 
					
						
							|  |  |  |   m_Log[LogTypes::AUDIO] = new LogContainer("Audio", "Audio Emulator"); | 
					
						
							|  |  |  |   m_Log[LogTypes::AUDIO_INTERFACE] = new LogContainer("AI", "Audio Interface (AI)"); | 
					
						
							|  |  |  |   m_Log[LogTypes::BOOT] = new LogContainer("BOOT", "Boot"); | 
					
						
							|  |  |  |   m_Log[LogTypes::COMMANDPROCESSOR] = new LogContainer("CP", "CommandProc"); | 
					
						
							|  |  |  |   m_Log[LogTypes::COMMON] = new LogContainer("COMMON", "Common"); | 
					
						
							|  |  |  |   m_Log[LogTypes::CONSOLE] = new LogContainer("CONSOLE", "Dolphin Console"); | 
					
						
							|  |  |  |   m_Log[LogTypes::DISCIO] = new LogContainer("DIO", "Disc IO"); | 
					
						
							|  |  |  |   m_Log[LogTypes::DSPHLE] = new LogContainer("DSPHLE", "DSP HLE"); | 
					
						
							|  |  |  |   m_Log[LogTypes::DSPLLE] = new LogContainer("DSPLLE", "DSP LLE"); | 
					
						
							|  |  |  |   m_Log[LogTypes::DSP_MAIL] = new LogContainer("DSPMails", "DSP Mails"); | 
					
						
							|  |  |  |   m_Log[LogTypes::DSPINTERFACE] = new LogContainer("DSP", "DSPInterface"); | 
					
						
							|  |  |  |   m_Log[LogTypes::DVDINTERFACE] = new LogContainer("DVD", "DVD Interface"); | 
					
						
							|  |  |  |   m_Log[LogTypes::DYNA_REC] = new LogContainer("JIT", "Dynamic Recompiler"); | 
					
						
							|  |  |  |   m_Log[LogTypes::EXPANSIONINTERFACE] = new LogContainer("EXI", "Expansion Interface"); | 
					
						
							|  |  |  |   m_Log[LogTypes::FILEMON] = new LogContainer("FileMon", "File Monitor"); | 
					
						
							|  |  |  |   m_Log[LogTypes::GDB_STUB] = new LogContainer("GDB_STUB", "GDB Stub"); | 
					
						
							|  |  |  |   m_Log[LogTypes::GPFIFO] = new LogContainer("GP", "GPFifo"); | 
					
						
							|  |  |  |   m_Log[LogTypes::HOST_GPU] = new LogContainer("Host GPU", "Host GPU"); | 
					
						
							|  |  |  |   m_Log[LogTypes::MASTER_LOG] = new LogContainer("*", "Master Log"); | 
					
						
							|  |  |  |   m_Log[LogTypes::MEMCARD_MANAGER] = new LogContainer("MemCard Manager", "MemCard Manager"); | 
					
						
							|  |  |  |   m_Log[LogTypes::MEMMAP] = new LogContainer("MI", "MI & memmap"); | 
					
						
							|  |  |  |   m_Log[LogTypes::NETPLAY] = new LogContainer("NETPLAY", "Netplay"); | 
					
						
							|  |  |  |   m_Log[LogTypes::OSHLE] = new LogContainer("HLE", "HLE"); | 
					
						
							|  |  |  |   m_Log[LogTypes::OSREPORT] = new LogContainer("OSREPORT", "OSReport"); | 
					
						
							|  |  |  |   m_Log[LogTypes::PAD] = new LogContainer("PAD", "Pad"); | 
					
						
							|  |  |  |   m_Log[LogTypes::PIXELENGINE] = new LogContainer("PE", "PixelEngine"); | 
					
						
							|  |  |  |   m_Log[LogTypes::PROCESSORINTERFACE] = new LogContainer("PI", "ProcessorInt"); | 
					
						
							|  |  |  |   m_Log[LogTypes::POWERPC] = new LogContainer("PowerPC", "IBM CPU"); | 
					
						
							|  |  |  |   m_Log[LogTypes::SERIALINTERFACE] = new LogContainer("SI", "Serial Interface (SI)"); | 
					
						
							|  |  |  |   m_Log[LogTypes::SP1] = new LogContainer("SP1", "Serial Port 1"); | 
					
						
							|  |  |  |   m_Log[LogTypes::VIDEO] = new LogContainer("Video", "Video Backend"); | 
					
						
							|  |  |  |   m_Log[LogTypes::VIDEOINTERFACE] = new LogContainer("VI", "Video Interface (VI)"); | 
					
						
							|  |  |  |   m_Log[LogTypes::WIIMOTE] = new LogContainer("Wiimote", "Wiimote"); | 
					
						
							|  |  |  |   m_Log[LogTypes::WII_IPC] = new LogContainer("WII_IPC", "WII IPC"); | 
					
						
							|  |  |  |   m_Log[LogTypes::WII_IPC_DVD] = new LogContainer("WII_IPC_DVD", "WII IPC DVD"); | 
					
						
							|  |  |  |   m_Log[LogTypes::WII_IPC_ES] = new LogContainer("WII_IPC_ES", "WII IPC ES"); | 
					
						
							|  |  |  |   m_Log[LogTypes::WII_IPC_FILEIO] = new LogContainer("WII_IPC_FILEIO", "WII IPC FILEIO"); | 
					
						
							|  |  |  |   m_Log[LogTypes::WII_IPC_HID] = new LogContainer("WII_IPC_HID", "WII IPC HID"); | 
					
						
							|  |  |  |   m_Log[LogTypes::WII_IPC_HLE] = new LogContainer("WII_IPC_HLE", "WII IPC HLE"); | 
					
						
							|  |  |  |   m_Log[LogTypes::WII_IPC_SD] = new LogContainer("WII_IPC_SD", "WII IPC SD"); | 
					
						
							|  |  |  |   m_Log[LogTypes::WII_IPC_SSL] = new LogContainer("WII_IPC_SSL", "WII IPC SSL"); | 
					
						
							|  |  |  |   m_Log[LogTypes::WII_IPC_STM] = new LogContainer("WII_IPC_STM", "WII IPC STM"); | 
					
						
							|  |  |  |   m_Log[LogTypes::WII_IPC_NET] = new LogContainer("WII_IPC_NET", "WII IPC NET"); | 
					
						
							|  |  |  |   m_Log[LogTypes::WII_IPC_WC24] = new LogContainer("WII_IPC_WC24", "WII IPC WC24"); | 
					
						
							|  |  |  |   m_Log[LogTypes::WII_IPC_WIIMOTE] = new LogContainer("WII_IPC_WIIMOTE", "WII IPC WIIMOTE"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   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; | 
					
						
							|  |  |  |   options->Get("WriteToFile", &write_file, false); | 
					
						
							|  |  |  |   options->Get("WriteToConsole", &write_console, true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (LogContainer* container : m_Log) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     bool enable; | 
					
						
							|  |  |  |     logs->Get(container->GetShortName(), &enable, false); | 
					
						
							|  |  |  |     container->SetEnable(enable); | 
					
						
							|  |  |  |     if (enable && write_file) | 
					
						
							|  |  |  |       container->AddListener(LogListener::FILE_LISTENER); | 
					
						
							|  |  |  |     if (enable && write_console) | 
					
						
							|  |  |  |       container->AddListener(LogListener::CONSOLE_LISTENER); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2009-03-18 17:17:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-01 07:43:02 +00:00
										 |  |  | LogManager::~LogManager() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   for (LogContainer* container : m_Log) | 
					
						
							|  |  |  |     delete container; | 
					
						
							| 
									
										
										
										
											2010-02-16 04:34:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02: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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | void LogManager::Log(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type, const char* file, | 
					
						
							|  |  |  |                      int line, const char* format, va_list args) | 
					
						
							| 
									
										
										
										
											2011-04-01 07:43:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   char temp[MAX_MSGLEN]; | 
					
						
							|  |  |  |   LogContainer* log = m_Log[type]; | 
					
						
							| 
									
										
										
										
											2009-03-18 17:17:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   if (!log->IsEnabled() || level > log->GetLevel() || !log->HasListeners()) | 
					
						
							|  |  |  |     return; | 
					
						
							| 
									
										
										
										
											2009-07-12 21:58:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   CharArrayFromFormatV(temp, MAX_MSGLEN, format, args); | 
					
						
							| 
									
										
										
										
											2009-03-20 21:23:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02: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], log->GetShortName().c_str(), temp); | 
					
						
							| 
									
										
										
										
											2015-10-04 00:34:36 +13:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   for (auto listener_id : *log) | 
					
						
							|  |  |  |     m_listeners[listener_id]->Log(level, msg.c_str()); | 
					
						
							| 
									
										
										
										
											2009-03-18 17:17:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-16 04:34:41 +00:00
										 |  |  | void LogManager::Init() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   m_logManager = new LogManager(); | 
					
						
							| 
									
										
										
										
											2010-02-16 04:34:41 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void LogManager::Shutdown() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   delete m_logManager; | 
					
						
							|  |  |  |   m_logManager = nullptr; | 
					
						
							| 
									
										
										
										
											2010-02-16 04:34:41 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-05 18:24:10 -04:00
										 |  |  | LogContainer::LogContainer(const std::string& shortName, const std::string& fullName, bool enable) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |     : m_fullName(fullName), m_shortName(shortName), m_enable(enable), m_level(LogTypes::LWARNING) | 
					
						
							| 
									
										
										
										
											2011-04-01 07:43:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-03-20 20:52:37 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-05 18:24:10 -04:00
										 |  |  | FileLogListener::FileLogListener(const std::string& filename) | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   OpenFStream(m_logfile, filename, std::ios::app); | 
					
						
							|  |  |  |   SetEnable(true); | 
					
						
							| 
									
										
										
										
											2009-03-18 17:17:58 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 20:46:25 +01:00
										 |  |  | void FileLogListener::Log(LogTypes::LOG_LEVELS, const char* msg) | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   if (!IsEnabled() || !IsValid()) | 
					
						
							|  |  |  |     return; | 
					
						
							| 
									
										
										
										
											2009-03-18 17:17:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   std::lock_guard<std::mutex> lk(m_log_lock); | 
					
						
							|  |  |  |   m_logfile << msg << std::flush; | 
					
						
							| 
									
										
										
										
											2009-03-18 17:17:58 +00:00
										 |  |  | } |