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"
|
|
|
|
|
2010-06-24 03:24:09 +02:00
|
|
|
#include <algorithm>
|
2011-07-27 00:25:21 +02:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2010-06-24 03:24:09 +02:00
|
|
|
#include <functional>
|
2010-06-24 03:24:02 +02:00
|
|
|
#include <memory>
|
2011-10-28 22:15:10 +02:00
|
|
|
#include <sstream>
|
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"
|
|
|
|
#include "libaegisub/io.h"
|
2010-05-31 22:55:29 +02:00
|
|
|
#include "libaegisub/log.h"
|
2010-06-09 04:07:43 +02:00
|
|
|
#include "libaegisub/types.h"
|
2010-06-01 07:11:18 +02:00
|
|
|
#include "libaegisub/util.h"
|
2010-05-31 22:55:29 +02:00
|
|
|
|
2012-11-13 15:07:39 +01:00
|
|
|
#include <boost/range/algorithm.hpp>
|
|
|
|
|
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
|
|
|
|
2010-06-24 03:24:09 +02:00
|
|
|
SinkMessage::SinkMessage(const char *section, Severity severity,
|
|
|
|
const char *file, const char *func, int line,
|
|
|
|
agi_timeval tv)
|
|
|
|
: section(section)
|
|
|
|
, severity(severity)
|
|
|
|
, file(file)
|
|
|
|
, func(func)
|
|
|
|
, line(line)
|
|
|
|
, tv(tv)
|
2012-11-13 15:07:39 +01:00
|
|
|
, message(nullptr)
|
2010-06-24 03:24:09 +02:00
|
|
|
, len(0)
|
|
|
|
{
|
2010-05-31 22:55:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SinkMessage::~SinkMessage() {
|
2012-01-18 21:08:24 +01:00
|
|
|
delete [] message;
|
2010-05-31 22:55:29 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 22:10:50 +01:00
|
|
|
/// @todo The log files need to be trimmed after N amount.
|
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
|
|
|
|
std::vector<Emitter*> emitters_temp;
|
|
|
|
swap(emitters_temp, emitters);
|
|
|
|
util::delete_clear(emitters_temp);
|
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
|
|
|
|
2012-01-08 02:36:23 +01:00
|
|
|
util::delete_clear(sink);
|
2010-05-31 22:55:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LogSink::log(SinkMessage *sm) {
|
2010-06-24 03:24:09 +02:00
|
|
|
sink.push_back(sm);
|
2012-11-13 15:07:39 +01:00
|
|
|
boost::for_each(emitters, [=](Emitter *em) { em->log(sm); });
|
2010-05-31 22:55:29 +02:00
|
|
|
}
|
|
|
|
|
2010-06-24 03:24:09 +02:00
|
|
|
void LogSink::Subscribe(Emitter *em) {
|
2010-07-25 19:30:12 +02:00
|
|
|
LOG_D("agi/log/emitter/subscribe") << "Subscribe: " << this;
|
2010-06-24 03:24:09 +02:00
|
|
|
emitters.push_back(em);
|
2010-05-31 22:55:29 +02:00
|
|
|
}
|
|
|
|
|
2010-06-24 03:24:09 +02:00
|
|
|
void LogSink::Unsubscribe(Emitter *em) {
|
2012-01-08 02:36:09 +01:00
|
|
|
emitters.erase(remove(emitters.begin(), emitters.end(), em), emitters.end());
|
|
|
|
delete em;
|
|
|
|
LOG_D("agi/log/emitter/unsubscribe") << "Un-Subscribe: " << this;
|
2010-05-31 22:55:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Message::Message(const char *section,
|
|
|
|
Severity severity,
|
|
|
|
const char *file,
|
|
|
|
const char *func,
|
2010-06-24 03:24:09 +02:00
|
|
|
int line)
|
|
|
|
: len(1024)
|
|
|
|
, buf(new char[len])
|
|
|
|
, msg(buf, len)
|
|
|
|
{
|
2010-06-09 04:07:43 +02:00
|
|
|
agi_timeval tv;
|
2010-06-01 07:11:18 +02:00
|
|
|
util::time_log(tv);
|
2010-05-31 22:55:29 +02:00
|
|
|
sm = new SinkMessage(section, severity, file, func, line, tv);
|
|
|
|
}
|
|
|
|
|
|
|
|
Message::~Message() {
|
2010-06-24 03:24:09 +02:00
|
|
|
sm->message = msg.str();
|
2011-01-08 22:58:48 +01:00
|
|
|
sm->len = (size_t)msg.pcount();
|
2010-05-31 22:55:29 +02:00
|
|
|
agi::log::log->log(sm);
|
|
|
|
}
|
|
|
|
|
2012-01-08 02:36:23 +01:00
|
|
|
JsonEmitter::JsonEmitter(std::string const& directory, const agi::log::LogSink *log_sink)
|
|
|
|
: directory(directory)
|
|
|
|
, log_sink(log_sink)
|
|
|
|
{
|
|
|
|
util::time_log(time_start);
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonEmitter::~JsonEmitter() {
|
|
|
|
json::Object root;
|
|
|
|
json::Array &array = root["log"];
|
|
|
|
|
|
|
|
agi_timeval time_close;
|
|
|
|
util::time_log(time_close);
|
|
|
|
|
|
|
|
Sink const& sink = *log_sink->GetSink();
|
|
|
|
for (unsigned int i=0; i < sink.size(); i++) {
|
|
|
|
json::Object entry;
|
2012-02-23 20:27:42 +01:00
|
|
|
entry["sec"] = (int64_t)sink[i]->tv.tv_sec;
|
|
|
|
entry["usec"] = (int64_t)sink[i]->tv.tv_usec;
|
2012-01-08 02:36:23 +01:00
|
|
|
entry["severity"] = sink[i]->severity;
|
|
|
|
entry["section"] = sink[i]->section;
|
|
|
|
entry["file"] = sink[i]->file;
|
|
|
|
entry["func"] = sink[i]->func;
|
|
|
|
entry["line"] = sink[i]->line;
|
|
|
|
entry["message"] = std::string(sink[i]->message, sink[i]->len);
|
|
|
|
|
2012-11-13 15:07:39 +01:00
|
|
|
array.push_back(std::move(entry));
|
2012-01-08 02:36:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
json::Array &timeval_open = root["timeval"]["open"];
|
2012-02-23 20:27:42 +01:00
|
|
|
timeval_open.push_back((int64_t)time_start.tv_sec);
|
|
|
|
timeval_open.push_back((int64_t)time_start.tv_usec);
|
2012-01-08 02:36:23 +01:00
|
|
|
|
|
|
|
json::Array &timeval_close = root["timeval"]["close"];
|
2012-02-23 20:27:42 +01:00
|
|
|
timeval_close.push_back((int64_t)time_close.tv_sec);
|
|
|
|
timeval_close.push_back((int64_t)time_close.tv_usec);
|
2012-01-08 02:36:23 +01:00
|
|
|
|
|
|
|
std::stringstream str;
|
|
|
|
str << directory << time_start.tv_sec << ".json";
|
|
|
|
json::Writer::Write(root, io::Save(str.str()).Get());
|
|
|
|
}
|
|
|
|
|
2010-05-31 22:55:29 +02:00
|
|
|
} // namespace log
|
|
|
|
} // namespace agi
|