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.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file log.cpp
|
|
|
|
/// @brief Logging
|
|
|
|
/// @ingroup libaegisub
|
|
|
|
|
2011-07-27 00:25:21 +02:00
|
|
|
#ifndef LAGI_PRE
|
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
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
|
|
|
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)
|
|
|
|
, message(NULL)
|
|
|
|
, len(0)
|
|
|
|
{
|
2010-05-31 22:55:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SinkMessage::~SinkMessage() {
|
2010-06-24 03:24:09 +02:00
|
|
|
delete message;
|
2010-05-31 22:55:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-04 20:42:21 +01:00
|
|
|
LogSink::LogSink(const std::string& dir_log): dir_log(dir_log) {
|
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
|
|
|
util::time_log(time_start);
|
2010-05-31 22:55:29 +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
|
|
|
/// @todo The log files need to be trimed after N amount.
|
2010-05-31 22:55:29 +02:00
|
|
|
LogSink::~LogSink() {
|
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
|
|
|
json::Object root;
|
|
|
|
json::Array array;
|
|
|
|
|
|
|
|
agi_timeval time_close;
|
|
|
|
util::time_log(time_close);
|
|
|
|
|
|
|
|
std::stringstream str;
|
|
|
|
str << dir_log << time_start.tv_sec << ".json";
|
|
|
|
io::Save file(str.str());
|
|
|
|
|
|
|
|
for (unsigned int i=0; i < sink.size(); i++) {
|
|
|
|
json::Object entry;
|
2011-10-18 00:00:09 +02:00
|
|
|
entry["sec"] = sink[i]->tv.tv_sec;
|
|
|
|
entry["usec"] = sink[i]->tv.tv_usec;
|
|
|
|
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;
|
2011-10-17 23:59:59 +02:00
|
|
|
entry["message"] = std::string(sink[i]->message, sink[i]->len);
|
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
|
|
|
|
2011-10-17 23:59:35 +02:00
|
|
|
array.push_back(entry);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
json::Array timeval_open;
|
2011-10-18 00:00:09 +02:00
|
|
|
timeval_open.push_back(time_start.tv_sec);
|
|
|
|
timeval_open.push_back(time_start.tv_usec);
|
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
|
|
|
root["timeval"]["open"] = timeval_open;
|
|
|
|
|
|
|
|
json::Array timeval_close;
|
2011-10-18 00:00:09 +02:00
|
|
|
timeval_close.push_back(time_close.tv_sec);
|
|
|
|
timeval_close.push_back(time_close.tv_usec);
|
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
|
|
|
root["timeval"]["close"] = timeval_close;
|
|
|
|
|
|
|
|
root["log"] = array;
|
|
|
|
|
|
|
|
json::Writer::Write(root, file.Get());
|
|
|
|
|
2010-06-24 03:24:09 +02:00
|
|
|
agi::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);
|
2010-05-31 22:55:29 +02:00
|
|
|
|
2010-06-24 03:24:09 +02:00
|
|
|
std::for_each(
|
|
|
|
emitters.begin(),
|
|
|
|
emitters.end(),
|
|
|
|
std::bind2nd(std::mem_fun(&Emitter::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) {
|
|
|
|
emitters.erase(std::remove(emitters.begin(), emitters.end(), em), emitters.end());
|
2010-07-25 19:30:12 +02:00
|
|
|
LOG_D("agi/log/emitter/unsubscribe") << "Un-Ssubscribe: " << 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);
|
|
|
|
}
|
|
|
|
|
2010-06-24 03:24:09 +02:00
|
|
|
Emitter::Emitter() {
|
|
|
|
}
|
2010-05-31 22:55:29 +02:00
|
|
|
|
2010-06-24 03:24:09 +02:00
|
|
|
Emitter::~Emitter() {
|
|
|
|
Disable();
|
2010-05-31 22:55:29 +02:00
|
|
|
}
|
|
|
|
|
2010-06-24 03:24:09 +02:00
|
|
|
void Emitter::Enable() {
|
|
|
|
agi::log::log->Subscribe(this);
|
|
|
|
}
|
2010-05-31 22:55:29 +02:00
|
|
|
|
|
|
|
void Emitter::Disable() {
|
2010-06-24 03:24:09 +02:00
|
|
|
agi::log::log->Unsubscribe(this);
|
2010-05-31 22:55:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace log
|
|
|
|
} // namespace agi
|