2014-06-17 15:44:03 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include <iostream>
|
|
|
|
#include <cinttypes>
|
2014-08-25 14:56:13 +00:00
|
|
|
#include "Thread.h"
|
2015-04-24 21:38:11 +00:00
|
|
|
#include "File.h"
|
2015-12-19 11:40:52 +00:00
|
|
|
#include "Log.h"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Windows.h>
|
|
|
|
#endif
|
2014-06-17 15:44:03 +00:00
|
|
|
|
|
|
|
using namespace Log;
|
|
|
|
|
2015-04-25 13:29:05 +00:00
|
|
|
std::unique_ptr<LogManager> g_log_manager;
|
2014-06-17 15:44:03 +00:00
|
|
|
|
2014-09-27 20:56:27 +00:00
|
|
|
u32 LogMessage::size() const
|
2014-06-17 15:44:03 +00:00
|
|
|
{
|
|
|
|
//1 byte for NULL terminator
|
2015-06-30 22:25:52 +00:00
|
|
|
return (u32)(sizeof(LogMessage::size_type) + sizeof(LogType) + sizeof(Severity) + sizeof(std::string::value_type) * mText.size() + 1);
|
2014-06-17 15:44:03 +00:00
|
|
|
}
|
|
|
|
|
2014-09-27 20:56:27 +00:00
|
|
|
void LogMessage::serialize(char *output) const
|
2014-06-17 15:44:03 +00:00
|
|
|
{
|
|
|
|
LogMessage::size_type size = this->size();
|
|
|
|
memcpy(output, &size, sizeof(LogMessage::size_type));
|
|
|
|
output += sizeof(LogMessage::size_type);
|
|
|
|
memcpy(output, &mType, sizeof(LogType));
|
|
|
|
output += sizeof(LogType);
|
2015-06-30 22:25:52 +00:00
|
|
|
memcpy(output, &mServerity, sizeof(Severity));
|
|
|
|
output += sizeof(Severity);
|
2014-06-17 15:44:03 +00:00
|
|
|
memcpy(output, mText.c_str(), mText.size() );
|
|
|
|
output += sizeof(std::string::value_type)*mText.size();
|
|
|
|
*output = '\0';
|
|
|
|
|
|
|
|
}
|
|
|
|
LogMessage LogMessage::deserialize(char *input, u32* size_out)
|
|
|
|
{
|
|
|
|
LogMessage msg;
|
|
|
|
LogMessage::size_type msgSize = *(reinterpret_cast<LogMessage::size_type*>(input));
|
|
|
|
input += sizeof(LogMessage::size_type);
|
|
|
|
msg.mType = *(reinterpret_cast<LogType*>(input));
|
|
|
|
input += sizeof(LogType);
|
2015-06-30 22:25:52 +00:00
|
|
|
msg.mServerity = *(reinterpret_cast<Severity*>(input));
|
|
|
|
input += sizeof(Severity);
|
2014-06-17 15:44:03 +00:00
|
|
|
if (msgSize > 9000)
|
|
|
|
{
|
|
|
|
int wtf = 6;
|
|
|
|
}
|
2015-06-30 22:25:52 +00:00
|
|
|
msg.mText.append(input, msgSize - 1 - sizeof(Severity) - sizeof(LogType));
|
2014-06-17 15:44:03 +00:00
|
|
|
if (size_out){(*size_out) = msgSize;}
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LogChannel::LogChannel() : LogChannel("unknown")
|
|
|
|
{}
|
|
|
|
|
|
|
|
LogChannel::LogChannel(const std::string& name) :
|
|
|
|
name(name)
|
|
|
|
, mEnabled(true)
|
2015-06-30 22:25:52 +00:00
|
|
|
, mLogLevel(Severity::Warning)
|
2014-06-17 15:44:03 +00:00
|
|
|
{}
|
|
|
|
|
2014-09-27 20:56:27 +00:00
|
|
|
void LogChannel::log(const LogMessage &msg)
|
2014-06-17 15:44:03 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(mListenerLock);
|
|
|
|
for (auto &listener : mListeners)
|
|
|
|
{
|
|
|
|
listener->log(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogChannel::addListener(std::shared_ptr<LogListener> listener)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(mListenerLock);
|
|
|
|
mListeners.insert(listener);
|
|
|
|
}
|
|
|
|
void LogChannel::removeListener(std::shared_ptr<LogListener> listener)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(mListenerLock);
|
|
|
|
mListeners.erase(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CoutListener : LogListener
|
|
|
|
{
|
2015-07-26 13:41:23 +00:00
|
|
|
void log(const LogMessage &msg) override
|
2014-06-17 15:44:03 +00:00
|
|
|
{
|
|
|
|
std::cerr << msg.mText << std::endl;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FileListener : LogListener
|
|
|
|
{
|
2015-04-24 21:38:11 +00:00
|
|
|
fs::file mFile;
|
2014-06-17 15:44:03 +00:00
|
|
|
bool mPrependChannelName;
|
|
|
|
|
2015-12-19 11:40:52 +00:00
|
|
|
FileListener(const std::string& name = _PRGNAME_ ".log", bool prependChannel = true)
|
|
|
|
: mFile(fs::get_config_dir() + name, fom::rewrite)
|
2015-04-19 13:19:24 +00:00
|
|
|
, mPrependChannelName(prependChannel)
|
2014-06-17 15:44:03 +00:00
|
|
|
{
|
2015-04-19 13:19:24 +00:00
|
|
|
if (!mFile)
|
2014-06-17 15:44:03 +00:00
|
|
|
{
|
2015-12-19 11:40:52 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
MessageBoxA(0, ("Can't create log file: " + name).c_str(), "Error", MB_ICONERROR);
|
|
|
|
#else
|
|
|
|
std::printf("Can't create log file: %s\n", name.c_str());
|
|
|
|
#endif
|
2014-06-17 15:44:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-26 13:41:23 +00:00
|
|
|
void log(const LogMessage &msg) override
|
2014-06-17 15:44:03 +00:00
|
|
|
{
|
2014-09-27 20:56:27 +00:00
|
|
|
std::string text = msg.mText;
|
2014-06-17 15:44:03 +00:00
|
|
|
if (mPrependChannelName)
|
|
|
|
{
|
2014-09-27 20:56:27 +00:00
|
|
|
text.insert(0, gTypeNameTable[static_cast<u32>(msg.mType)].mName);
|
2015-02-04 12:16:10 +00:00
|
|
|
|
|
|
|
if (msg.mType == Log::TTY)
|
|
|
|
{
|
2015-02-04 23:52:47 +00:00
|
|
|
text = fmt::escape(text);
|
|
|
|
if (text[text.length() - 1] != '\n')
|
|
|
|
{
|
|
|
|
text += '\n';
|
|
|
|
}
|
2015-02-04 12:16:10 +00:00
|
|
|
}
|
2014-06-17 15:44:03 +00:00
|
|
|
}
|
2015-04-19 13:19:24 +00:00
|
|
|
|
2016-01-05 23:52:48 +00:00
|
|
|
mFile.write(text);
|
2014-06-17 15:44:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
LogManager::LogManager()
|
|
|
|
#ifdef BUFFERED_LOGGING
|
|
|
|
: mExiting(false), mLogConsumer()
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
auto it = mChannels.begin();
|
|
|
|
std::shared_ptr<LogListener> listener(new FileListener());
|
|
|
|
for (const LogTypeName& name : gTypeNameTable)
|
|
|
|
{
|
|
|
|
it->name = name.mName;
|
|
|
|
it->addListener(listener);
|
|
|
|
it++;
|
|
|
|
}
|
2015-12-19 11:40:52 +00:00
|
|
|
std::shared_ptr<LogListener> TTYListener(new FileListener("TTY.log", false));
|
2014-06-17 15:44:03 +00:00
|
|
|
getChannel(TTY).addListener(TTYListener);
|
|
|
|
#ifdef BUFFERED_LOGGING
|
|
|
|
mLogConsumer = std::thread(&LogManager::consumeLog, this);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
LogManager::~LogManager()
|
|
|
|
{
|
|
|
|
#ifdef BUFFERED_LOGGING
|
|
|
|
mExiting = true;
|
|
|
|
mBufferReady.notify_all();
|
|
|
|
mLogConsumer.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogManager::consumeLog()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(mStatusMut);
|
|
|
|
while (!mExiting)
|
|
|
|
{
|
|
|
|
mBufferReady.wait(lock);
|
|
|
|
mBuffer.lockGet();
|
|
|
|
size_t size = mBuffer.size();
|
|
|
|
std::vector<char> local_messages(size);
|
|
|
|
mBuffer.popN(&local_messages.front(), size);
|
|
|
|
mBuffer.unlockGet();
|
|
|
|
|
|
|
|
u32 cursor = 0;
|
|
|
|
u32 removed = 0;
|
|
|
|
while (cursor < size)
|
|
|
|
{
|
|
|
|
Log::LogMessage msg = Log::LogMessage::deserialize(local_messages.data() + cursor, &removed);
|
|
|
|
cursor += removed;
|
|
|
|
getChannel(msg.mType).log(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogManager::log(LogMessage msg)
|
|
|
|
{
|
|
|
|
//don't do any formatting changes or filtering to the TTY output since we
|
|
|
|
//use the raw output to do diffs with the output of a real PS3 and some
|
|
|
|
//programs write text in single bytes to the console
|
|
|
|
if (msg.mType != TTY)
|
|
|
|
{
|
|
|
|
std::string prefix;
|
|
|
|
switch (msg.mServerity)
|
|
|
|
{
|
2015-06-30 22:25:52 +00:00
|
|
|
case Severity::Success:
|
2014-06-17 15:44:03 +00:00
|
|
|
prefix = "S ";
|
|
|
|
break;
|
2015-06-30 22:25:52 +00:00
|
|
|
case Severity::Notice:
|
2014-06-17 15:44:03 +00:00
|
|
|
prefix = "! ";
|
|
|
|
break;
|
2015-06-30 22:25:52 +00:00
|
|
|
case Severity::Warning:
|
2014-06-17 15:44:03 +00:00
|
|
|
prefix = "W ";
|
|
|
|
break;
|
2015-06-30 22:25:52 +00:00
|
|
|
case Severity::Error:
|
2014-06-17 15:44:03 +00:00
|
|
|
prefix = "E ";
|
|
|
|
break;
|
|
|
|
}
|
2015-11-26 08:06:29 +00:00
|
|
|
if (auto thr = thread_ctrl::get_current())
|
2014-06-17 15:44:03 +00:00
|
|
|
{
|
2015-06-30 22:25:52 +00:00
|
|
|
prefix += "{" + thr->get_name() + "} ";
|
2014-06-17 15:44:03 +00:00
|
|
|
}
|
|
|
|
msg.mText.insert(0, prefix);
|
|
|
|
msg.mText.append(1,'\n');
|
|
|
|
}
|
|
|
|
#ifdef BUFFERED_LOGGING
|
|
|
|
size_t size = msg.size();
|
|
|
|
std::vector<char> temp_buffer(size);
|
|
|
|
msg.serialize(temp_buffer.data());
|
|
|
|
mBuffer.pushRange(temp_buffer.begin(), temp_buffer.end());
|
|
|
|
mBufferReady.notify_one();
|
|
|
|
#else
|
|
|
|
mChannels[static_cast<u32>(msg.mType)].log(msg);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-06-27 07:22:00 +00:00
|
|
|
void LogManager::addListener(std::shared_ptr<LogListener> listener)
|
|
|
|
{
|
|
|
|
for (auto& channel : mChannels)
|
|
|
|
{
|
|
|
|
channel.addListener(listener);
|
|
|
|
}
|
|
|
|
}
|
2015-04-25 13:29:05 +00:00
|
|
|
|
2014-06-27 07:22:00 +00:00
|
|
|
void LogManager::removeListener(std::shared_ptr<LogListener> listener)
|
|
|
|
{
|
|
|
|
for (auto& channel : mChannels)
|
|
|
|
{
|
|
|
|
channel.removeListener(listener);
|
|
|
|
}
|
|
|
|
}
|
2014-06-17 15:44:03 +00:00
|
|
|
|
|
|
|
LogManager& LogManager::getInstance()
|
|
|
|
{
|
2015-04-25 13:29:05 +00:00
|
|
|
if (!g_log_manager)
|
2014-06-17 15:44:03 +00:00
|
|
|
{
|
2015-04-25 13:29:05 +00:00
|
|
|
g_log_manager.reset(new LogManager());
|
2014-06-17 15:44:03 +00:00
|
|
|
}
|
2015-04-25 13:29:05 +00:00
|
|
|
|
|
|
|
return *g_log_manager;
|
2014-06-17 15:44:03 +00:00
|
|
|
}
|
2015-04-25 13:29:05 +00:00
|
|
|
|
2014-06-17 15:44:03 +00:00
|
|
|
LogChannel &LogManager::getChannel(LogType type)
|
|
|
|
{
|
|
|
|
return mChannels[static_cast<u32>(type)];
|
2014-07-10 19:45:07 +00:00
|
|
|
}
|
2015-01-18 22:54:56 +00:00
|
|
|
|
2015-06-30 22:25:52 +00:00
|
|
|
void log_message(Log::LogType type, Log::Severity sev, const char* text)
|
2015-01-18 22:54:56 +00:00
|
|
|
{
|
2015-01-19 13:31:02 +00:00
|
|
|
log_message(type, sev, std::string(text));
|
2015-01-18 22:54:56 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 22:25:52 +00:00
|
|
|
void log_message(Log::LogType type, Log::Severity sev, std::string text)
|
2015-01-18 22:54:56 +00:00
|
|
|
{
|
2016-01-05 23:52:48 +00:00
|
|
|
g_log_manager->log({ type, sev, std::move(text) });
|
2015-01-18 22:54:56 +00:00
|
|
|
}
|