diff options
Diffstat (limited to 'src/common/log_manager.cpp')
| -rw-r--r-- | src/common/log_manager.cpp | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/src/common/log_manager.cpp b/src/common/log_manager.cpp new file mode 100644 index 000000000..b5b034846 --- /dev/null +++ b/src/common/log_manager.cpp | |||
| @@ -0,0 +1,200 @@ | |||
| 1 | // Copyright 2013 Dolphin Emulator Project | ||
| 2 | // Licensed under GPLv2 | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | |||
| 7 | #ifdef ANDROID | ||
| 8 | #include "Host.h" | ||
| 9 | #endif | ||
| 10 | #include "log_manager.h" | ||
| 11 | #include "console_listener.h" | ||
| 12 | #include "timer.h" | ||
| 13 | #include "thread.h" | ||
| 14 | #include "file_util.h" | ||
| 15 | |||
| 16 | void GenericLog(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type, | ||
| 17 | const char *file, int line, const char* fmt, ...) | ||
| 18 | { | ||
| 19 | va_list args; | ||
| 20 | va_start(args, fmt); | ||
| 21 | if (LogManager::GetInstance()) | ||
| 22 | LogManager::GetInstance()->Log(level, type, | ||
| 23 | file, line, fmt, args); | ||
| 24 | va_end(args); | ||
| 25 | } | ||
| 26 | |||
| 27 | LogManager *LogManager::m_logManager = NULL; | ||
| 28 | |||
| 29 | LogManager::LogManager() | ||
| 30 | { | ||
| 31 | // create log files | ||
| 32 | m_Log[LogTypes::MASTER_LOG] = new LogContainer("*", "Master Log"); | ||
| 33 | m_Log[LogTypes::BOOT] = new LogContainer("BOOT", "Boot"); | ||
| 34 | m_Log[LogTypes::COMMON] = new LogContainer("COMMON", "Common"); | ||
| 35 | m_Log[LogTypes::DISCIO] = new LogContainer("DIO", "Disc IO"); | ||
| 36 | m_Log[LogTypes::FILEMON] = new LogContainer("FileMon", "File Monitor"); | ||
| 37 | m_Log[LogTypes::PAD] = new LogContainer("PAD", "Pad"); | ||
| 38 | m_Log[LogTypes::PIXELENGINE] = new LogContainer("PE", "PixelEngine"); | ||
| 39 | m_Log[LogTypes::COMMANDPROCESSOR] = new LogContainer("CP", "CommandProc"); | ||
| 40 | m_Log[LogTypes::VIDEOINTERFACE] = new LogContainer("VI", "VideoInt"); | ||
| 41 | m_Log[LogTypes::SERIALINTERFACE] = new LogContainer("SI", "SerialInt"); | ||
| 42 | m_Log[LogTypes::PROCESSORINTERFACE] = new LogContainer("PI", "ProcessorInt"); | ||
| 43 | m_Log[LogTypes::MEMMAP] = new LogContainer("MI", "MI & memmap"); | ||
| 44 | m_Log[LogTypes::SP1] = new LogContainer("SP1", "Serial Port 1"); | ||
| 45 | m_Log[LogTypes::STREAMINGINTERFACE] = new LogContainer("Stream", "StreamingInt"); | ||
| 46 | m_Log[LogTypes::DSPINTERFACE] = new LogContainer("DSP", "DSPInterface"); | ||
| 47 | m_Log[LogTypes::DVDINTERFACE] = new LogContainer("DVD", "DVDInterface"); | ||
| 48 | m_Log[LogTypes::GPFIFO] = new LogContainer("GP", "GPFifo"); | ||
| 49 | m_Log[LogTypes::EXPANSIONINTERFACE] = new LogContainer("EXI", "ExpansionInt"); | ||
| 50 | m_Log[LogTypes::GDB_STUB] = new LogContainer("GDB_STUB", "GDB Stub"); | ||
| 51 | m_Log[LogTypes::AUDIO_INTERFACE] = new LogContainer("AI", "AudioInt"); | ||
| 52 | m_Log[LogTypes::ARM11] = new LogContainer("ARM11", "ARM11"); | ||
| 53 | m_Log[LogTypes::OSHLE] = new LogContainer("HLE", "HLE"); | ||
| 54 | m_Log[LogTypes::DSPHLE] = new LogContainer("DSPHLE", "DSP HLE"); | ||
| 55 | m_Log[LogTypes::DSPLLE] = new LogContainer("DSPLLE", "DSP LLE"); | ||
| 56 | m_Log[LogTypes::DSP_MAIL] = new LogContainer("DSPMails", "DSP Mails"); | ||
| 57 | m_Log[LogTypes::VIDEO] = new LogContainer("Video", "Video Backend"); | ||
| 58 | m_Log[LogTypes::AUDIO] = new LogContainer("Audio", "Audio Emulator"); | ||
| 59 | m_Log[LogTypes::DYNA_REC] = new LogContainer("JIT", "JIT"); | ||
| 60 | m_Log[LogTypes::CONSOLE] = new LogContainer("CONSOLE", "Dolphin Console"); | ||
| 61 | m_Log[LogTypes::OSREPORT] = new LogContainer("OSREPORT", "OSReport"); | ||
| 62 | m_Log[LogTypes::TIME] = new LogContainer("Time", "Core Timing"); | ||
| 63 | m_Log[LogTypes::LOADER] = new LogContainer("Loader", "Loader"); | ||
| 64 | m_Log[LogTypes::FILESYS] = new LogContainer("FileSys", "File System"); | ||
| 65 | m_Log[LogTypes::WII_IPC_HID] = new LogContainer("WII_IPC_HID", "WII IPC HID"); | ||
| 66 | m_Log[LogTypes::WII_IPC_HLE] = new LogContainer("WII_IPC_HLE", "WII IPC HLE"); | ||
| 67 | m_Log[LogTypes::WII_IPC_DVD] = new LogContainer("WII_IPC_DVD", "WII IPC DVD"); | ||
| 68 | m_Log[LogTypes::WII_IPC_ES] = new LogContainer("WII_IPC_ES", "WII IPC ES"); | ||
| 69 | m_Log[LogTypes::WII_IPC_FILEIO] = new LogContainer("WII_IPC_FILEIO", "WII IPC FILEIO"); | ||
| 70 | m_Log[LogTypes::RENDER] = new LogContainer("RENDER", "RENDER"); | ||
| 71 | m_Log[LogTypes::LCD] = new LogContainer("LCD", "LCD"); | ||
| 72 | m_Log[LogTypes::WII_IPC_NET] = new LogContainer("WII_IPC_NET", "WII IPC NET"); | ||
| 73 | m_Log[LogTypes::WII_IPC_WC24] = new LogContainer("WII_IPC_WC24", "WII IPC WC24"); | ||
| 74 | m_Log[LogTypes::WII_IPC_SSL] = new LogContainer("WII_IPC_SSL", "WII IPC SSL"); | ||
| 75 | m_Log[LogTypes::HW] = new LogContainer("HARDWARE", "HARDWARE"); | ||
| 76 | m_Log[LogTypes::ACTIONREPLAY] = new LogContainer("ActionReplay", "ActionReplay"); | ||
| 77 | m_Log[LogTypes::MEMCARD_MANAGER] = new LogContainer("MemCard Manager", "MemCard Manager"); | ||
| 78 | m_Log[LogTypes::NETPLAY] = new LogContainer("NETPLAY", "Netplay"); | ||
| 79 | |||
| 80 | m_fileLog = new FileLogListener(File::GetUserPath(F_MAINLOG_IDX).c_str()); | ||
| 81 | m_consoleLog = new ConsoleListener(); | ||
| 82 | m_debuggerLog = new DebuggerLogListener(); | ||
| 83 | |||
| 84 | for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i) | ||
| 85 | { | ||
| 86 | m_Log[i]->SetEnable(true); | ||
| 87 | m_Log[i]->AddListener(m_fileLog); | ||
| 88 | m_Log[i]->AddListener(m_consoleLog); | ||
| 89 | #ifdef _MSC_VER | ||
| 90 | if (IsDebuggerPresent()) | ||
| 91 | m_Log[i]->AddListener(m_debuggerLog); | ||
| 92 | #endif | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | LogManager::~LogManager() | ||
| 97 | { | ||
| 98 | for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i) | ||
| 99 | { | ||
| 100 | m_logManager->RemoveListener((LogTypes::LOG_TYPE)i, m_fileLog); | ||
| 101 | m_logManager->RemoveListener((LogTypes::LOG_TYPE)i, m_consoleLog); | ||
| 102 | m_logManager->RemoveListener((LogTypes::LOG_TYPE)i, m_debuggerLog); | ||
| 103 | } | ||
| 104 | |||
| 105 | for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i) | ||
| 106 | delete m_Log[i]; | ||
| 107 | |||
| 108 | delete m_fileLog; | ||
| 109 | delete m_consoleLog; | ||
| 110 | delete m_debuggerLog; | ||
| 111 | } | ||
| 112 | |||
| 113 | void LogManager::Log(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type, | ||
| 114 | const char *file, int line, const char *format, va_list args) | ||
| 115 | { | ||
| 116 | char temp[MAX_MSGLEN]; | ||
| 117 | char msg[MAX_MSGLEN * 2]; | ||
| 118 | LogContainer *log = m_Log[type]; | ||
| 119 | |||
| 120 | if (!log->IsEnabled() || level > log->GetLevel() || ! log->HasListeners()) | ||
| 121 | return; | ||
| 122 | |||
| 123 | CharArrayFromFormatV(temp, MAX_MSGLEN, format, args); | ||
| 124 | |||
| 125 | static const char level_to_char[7] = "-NEWID"; | ||
| 126 | sprintf(msg, "%s %s:%u %c[%s]: %s\n", | ||
| 127 | Common::Timer::GetTimeFormatted().c_str(), | ||
| 128 | file, line, level_to_char[(int)level], | ||
| 129 | log->GetShortName(), temp); | ||
| 130 | #ifdef ANDROID | ||
| 131 | Host_SysMessage(msg); | ||
| 132 | #endif | ||
| 133 | printf(msg); // TODO(ShizZy): RemoveMe when I no longer need this | ||
| 134 | log->Trigger(level, msg); | ||
| 135 | } | ||
| 136 | |||
| 137 | void LogManager::Init() | ||
| 138 | { | ||
| 139 | m_logManager = new LogManager(); | ||
| 140 | } | ||
| 141 | |||
| 142 | void LogManager::Shutdown() | ||
| 143 | { | ||
| 144 | delete m_logManager; | ||
| 145 | m_logManager = NULL; | ||
| 146 | } | ||
| 147 | |||
| 148 | LogContainer::LogContainer(const char* shortName, const char* fullName, bool enable) | ||
| 149 | : m_enable(enable) | ||
| 150 | { | ||
| 151 | strncpy(m_fullName, fullName, 128); | ||
| 152 | strncpy(m_shortName, shortName, 32); | ||
| 153 | m_level = LogTypes::LWARNING; | ||
| 154 | } | ||
| 155 | |||
| 156 | // LogContainer | ||
| 157 | void LogContainer::AddListener(LogListener *listener) | ||
| 158 | { | ||
| 159 | std::lock_guard<std::mutex> lk(m_listeners_lock); | ||
| 160 | m_listeners.insert(listener); | ||
| 161 | } | ||
| 162 | |||
| 163 | void LogContainer::RemoveListener(LogListener *listener) | ||
| 164 | { | ||
| 165 | std::lock_guard<std::mutex> lk(m_listeners_lock); | ||
| 166 | m_listeners.erase(listener); | ||
| 167 | } | ||
| 168 | |||
| 169 | void LogContainer::Trigger(LogTypes::LOG_LEVELS level, const char *msg) | ||
| 170 | { | ||
| 171 | std::lock_guard<std::mutex> lk(m_listeners_lock); | ||
| 172 | |||
| 173 | std::set<LogListener*>::const_iterator i; | ||
| 174 | for (i = m_listeners.begin(); i != m_listeners.end(); ++i) | ||
| 175 | { | ||
| 176 | (*i)->Log(level, msg); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | FileLogListener::FileLogListener(const char *filename) | ||
| 181 | { | ||
| 182 | OpenFStream(m_logfile, filename, std::ios::app); | ||
| 183 | SetEnable(true); | ||
| 184 | } | ||
| 185 | |||
| 186 | void FileLogListener::Log(LogTypes::LOG_LEVELS, const char *msg) | ||
| 187 | { | ||
| 188 | if (!IsEnabled() || !IsValid()) | ||
| 189 | return; | ||
| 190 | |||
| 191 | std::lock_guard<std::mutex> lk(m_log_lock); | ||
| 192 | m_logfile << msg << std::flush; | ||
| 193 | } | ||
| 194 | |||
| 195 | void DebuggerLogListener::Log(LogTypes::LOG_LEVELS, const char *msg) | ||
| 196 | { | ||
| 197 | #if _MSC_VER | ||
| 198 | ::OutputDebugStringA(msg); | ||
| 199 | #endif | ||
| 200 | } | ||