2010-05-31 22:55:29 +02:00
|
|
|
// Copyright (c) 2010, Amar Takhar <verm@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
/// @file log.cpp
|
|
|
|
/// @brief Logging
|
|
|
|
/// @ingroup libaegisub
|
|
|
|
|
2011-12-22 22:20:44 +01:00
|
|
|
#include "../config.h"
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include "libaegisub/log.h"
|
2010-06-24 03:24:02 +02:00
|
|
|
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
#include "libaegisub/cajun/elements.h"
|
|
|
|
#include "libaegisub/cajun/writer.h"
|
2013-01-26 00:50:08 +01:00
|
|
|
#include "libaegisub/dispatch.h"
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
#include "libaegisub/io.h"
|
2010-06-01 07:11:18 +02:00
|
|
|
#include "libaegisub/util.h"
|
2010-05-31 22:55:29 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <algorithm>
|
2013-01-22 18:20:32 +01:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <boost/filesystem/fstream.hpp>
|
2012-11-13 15:07:39 +01:00
|
|
|
#include <boost/range/algorithm.hpp>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <cstring>
|
|
|
|
#include <functional>
|
2013-01-26 00:50:08 +01:00
|
|
|
#include <mutex>
|
2012-11-13 15:07:39 +01:00
|
|
|
|
2010-05-31 22:55:29 +02:00
|
|
|
namespace agi {
|
|
|
|
namespace log {
|
|
|
|
|
|
|
|
/// Global log sink.
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
LogSink *log;
|
2010-05-31 22:55:29 +02:00
|
|
|
|
2010-06-01 00:47:38 +02:00
|
|
|
/// Short Severity ID
|
|
|
|
/// Keep this ordered the same as Severity
|
2010-06-01 10:17:27 +02:00
|
|
|
const char *Severity_ID = "EAWID";
|
2010-05-31 22:55:29 +02:00
|
|
|
|
2013-12-12 01:29:48 +01:00
|
|
|
LogSink::LogSink() : queue(dispatch::Create()) { }
|
2013-01-26 00:50:08 +01:00
|
|
|
|
2010-05-31 22:55:29 +02:00
|
|
|
LogSink::~LogSink() {
|
2012-01-08 02:36:23 +01:00
|
|
|
// The destructor for emitters may try to log messages, so disable all the
|
|
|
|
// emitters before destructing any
|
2013-06-12 05:38:12 +02:00
|
|
|
decltype(emitters) emitters_temp;
|
2013-01-26 00:50:08 +01:00
|
|
|
queue->Sync([&]{ swap(emitters_temp, emitters); });
|
2010-05-31 22:55:29 +02:00
|
|
|
}
|
|
|
|
|
2013-01-23 19:41:28 +01:00
|
|
|
void LogSink::Log(SinkMessage const& sm) {
|
2013-01-26 00:50:08 +01:00
|
|
|
queue->Async([=]{
|
|
|
|
messages.push_back(sm);
|
2013-06-12 05:38:12 +02:00
|
|
|
for (auto& em : emitters) em->log(&messages.back());
|
2013-01-26 00:50:08 +01:00
|
|
|
});
|
2010-05-31 22:55:29 +02:00
|
|
|
}
|
|
|
|
|
2013-06-12 05:38:12 +02:00
|
|
|
void LogSink::Subscribe(std::unique_ptr<Emitter> em) {
|
2010-07-25 19:30:12 +02:00
|
|
|
LOG_D("agi/log/emitter/subscribe") << "Subscribe: " << this;
|
2013-06-12 05:38:12 +02:00
|
|
|
auto tmp = em.release();
|
|
|
|
queue->Sync([=] { emitters.emplace_back(tmp); });
|
2010-05-31 22:55:29 +02:00
|
|
|
}
|
|
|
|
|
2010-06-24 03:24:09 +02:00
|
|
|
void LogSink::Unsubscribe(Emitter *em) {
|
2013-01-26 00:50:08 +01:00
|
|
|
queue->Sync([=] {
|
2013-06-12 05:38:12 +02:00
|
|
|
emitters.erase(
|
|
|
|
boost::remove_if(emitters, [=](std::unique_ptr<Emitter> const& e) { return e.get() == em; }),
|
|
|
|
emitters.end());
|
2013-01-26 00:50:08 +01:00
|
|
|
});
|
2012-01-08 02:36:09 +01:00
|
|
|
LOG_D("agi/log/emitter/unsubscribe") << "Un-Subscribe: " << this;
|
2010-05-31 22:55:29 +02:00
|
|
|
}
|
|
|
|
|
2013-01-26 00:50:08 +01:00
|
|
|
decltype(LogSink::messages) LogSink::GetMessages() const {
|
2013-06-12 05:38:12 +02:00
|
|
|
decltype(messages) ret;
|
2013-01-26 00:50:08 +01:00
|
|
|
queue->Sync([&] { ret = messages; });
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-07 18:39:39 +01:00
|
|
|
Message::Message(const char *section, Severity severity, const char *file, const char *func, int line)
|
|
|
|
: msg(buffer, sizeof buffer)
|
|
|
|
{
|
2013-12-12 04:16:16 +01:00
|
|
|
sm.section = section;
|
|
|
|
sm.severity = severity;
|
|
|
|
sm.file = file;
|
|
|
|
sm.func = func;
|
|
|
|
sm.line = line;
|
|
|
|
sm.tv = util::time_log();
|
2010-05-31 22:55:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Message::~Message() {
|
2013-01-23 19:41:28 +01:00
|
|
|
sm.message = std::string(msg.str(), (std::string::size_type)msg.pcount());
|
|
|
|
agi::log::log->Log(sm);
|
2013-01-22 18:02:20 +01:00
|
|
|
msg.freeze(false);
|
2010-05-31 22:55:29 +02:00
|
|
|
}
|
|
|
|
|
2013-01-22 18:20:32 +01:00
|
|
|
JsonEmitter::JsonEmitter(fs::path const& directory)
|
|
|
|
: fp(new boost::filesystem::ofstream(unique_path(directory/util::strftime("%Y-%m-%d-%H-%M-%S-%%%%%%%%.json"))))
|
2012-01-08 02:36:23 +01:00
|
|
|
{
|
2013-01-22 18:20:32 +01:00
|
|
|
WriteTime("open");
|
2012-01-08 02:36:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
JsonEmitter::~JsonEmitter() {
|
2013-01-22 18:20:32 +01:00
|
|
|
WriteTime("close");
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonEmitter::log(SinkMessage *sm) {
|
|
|
|
json::Object entry;
|
|
|
|
entry["sec"] = (int64_t)sm->tv.tv_sec;
|
|
|
|
entry["usec"] = (int64_t)sm->tv.tv_usec;
|
|
|
|
entry["severity"] = sm->severity;
|
|
|
|
entry["section"] = sm->section;
|
|
|
|
entry["file"] = sm->file;
|
|
|
|
entry["func"] = sm->func;
|
|
|
|
entry["line"] = sm->line;
|
|
|
|
entry["message"] = sm->message;
|
|
|
|
json::Writer::Write(entry, *fp);
|
|
|
|
fp->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonEmitter::WriteTime(const char *key) {
|
|
|
|
auto time = util::time_log();
|
|
|
|
json::Object obj;
|
|
|
|
json::Array &timeval = obj[key];
|
|
|
|
timeval.push_back((int64_t)time.tv_sec);
|
|
|
|
timeval.push_back((int64_t)time.tv_usec);
|
|
|
|
json::Writer::Write(obj, *fp);
|
2012-01-08 02:36:23 +01:00
|
|
|
}
|
|
|
|
|
2010-05-31 22:55:29 +02:00
|
|
|
} // namespace log
|
|
|
|
} // namespace agi
|