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
|
|
|
|
|
2010-06-24 03:24:02 +02:00
|
|
|
#ifndef AGI_PRE
|
2010-05-31 22:55:29 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2010-06-24 03:24:09 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
2010-06-24 03:24:02 +02:00
|
|
|
#include <memory>
|
|
|
|
#endif
|
|
|
|
|
2010-05-31 22:55:29 +02:00
|
|
|
#include "libaegisub/log.h"
|
2010-05-31 23:57:08 +02:00
|
|
|
#include "libaegisub/mutex.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.
|
2010-06-24 03:24:02 +02:00
|
|
|
std::auto_ptr<LogSink> log(new LogSink());
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-24 03:24:09 +02:00
|
|
|
LogSink::LogSink() {
|
2010-05-31 22:55:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LogSink::~LogSink() {
|
|
|
|
/// @todo This needs to flush all log data to disk on quit.
|
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();
|
|
|
|
sm->len = 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
|