Make agi::log::LogSink take ownership of the emitters passed to it

Originally committed to SVN as r6246.
This commit is contained in:
Thomas Goyne 2012-01-08 01:36:09 +00:00
parent ba1a1dc737
commit d148bbbf2a
4 changed files with 13 additions and 55 deletions

View File

@ -110,10 +110,10 @@ LogSink::~LogSink() {
void LogSink::log(SinkMessage *sm) {
sink.push_back(sm);
std::for_each(
for_each(
emitters.begin(),
emitters.end(),
std::bind2nd(std::mem_fun(&Emitter::log), sm));
bind2nd(std::mem_fun(&Emitter::log), sm));
}
void LogSink::Subscribe(Emitter *em) {
@ -122,8 +122,9 @@ void LogSink::Subscribe(Emitter *em) {
}
void LogSink::Unsubscribe(Emitter *em) {
emitters.erase(std::remove(emitters.begin(), emitters.end(), em), emitters.end());
LOG_D("agi/log/emitter/unsubscribe") << "Un-Ssubscribe: " << this;
emitters.erase(remove(emitters.begin(), emitters.end(), em), emitters.end());
delete em;
LOG_D("agi/log/emitter/unsubscribe") << "Un-Subscribe: " << this;
}
Message::Message(const char *section,
@ -146,21 +147,5 @@ Message::~Message() {
agi::log::log->log(sm);
}
Emitter::Emitter() {
}
Emitter::~Emitter() {
Disable();
}
void Emitter::Enable() {
agi::log::log->Subscribe(this);
}
void Emitter::Disable() {
agi::log::log->Unsubscribe(this);
}
} // namespace log
} // namespace agi

View File

@ -107,7 +107,7 @@ class LogSink {
/// Log sink
Sink sink;
/// List of function pointers to emitters
/// List of pointers to emitters
std::vector<Emitter*> emitters;
/// Init time for log writing purposes.
@ -116,7 +116,6 @@ class LogSink {
/// Directory to place logfiles.
const std::string dir_log;
public:
/// Constructor
/// @param dir_log Directory to place log files.
@ -128,12 +127,14 @@ public:
/// Insert a message into the sink.
void log(SinkMessage *sm);
/// @brief Subscribe an emitter.
/// @brief Subscribe an emitter
/// @param em Emitter to add
///
/// LogSink takes ownership of the passed emitter
void Subscribe(Emitter *em);
/// @brief Unsubscribe an emitter.
/// @param em Emitter to remove
/// @brief Unsubscribe and delete an emitter
/// @param em Emitter to delete
void Unsubscribe(Emitter *em);
/// @brief @get the complete (current) log.
@ -144,23 +145,13 @@ public:
/// An emitter to produce human readable output for a log sink.
class Emitter {
public:
/// Constructor
Emitter();
/// Destructor
virtual ~Emitter();
/// Enable (subscribe)
void Enable();
/// Disable (unsubscribe)
void Disable();
virtual ~Emitter() { }
/// Accept a single log entry
virtual void log(SinkMessage *sm)=0;
};
/// Generates a message and submits it to the log sink.
class Message {
const int len;

View File

@ -146,10 +146,8 @@ bool AegisubApp::OnInit() {
wxFileName::Mkdir(path_log, 0777, wxPATH_MKDIR_FULL);
agi::log::log = new agi::log::LogSink(STD_STR(path_log));
#ifdef _DEBUG
emit_stdout = new agi::log::EmitSTDOUT();
emit_stdout->Enable();
agi::log::log->Subscribe(new agi::log::EmitSTDOUT());
#endif
// Set config file
@ -305,10 +303,6 @@ int AegisubApp::OnExit() {
AssExportFilterChain::Clear();
#ifdef _DEBUG
delete emit_stdout;
#endif
// Keep this last!
delete agi::log::log;

View File

@ -54,13 +54,6 @@
class FrameMain;
class PluginManager;
#ifdef _DEBUG
namespace agi {
namespace log {
class EmitSTDOUT;
}
}
#endif
/// For holding all configuration-related objects and values.
namespace config {
@ -100,11 +93,6 @@ class AegisubApp: public wxApp {
/// DOCME
PluginManager *plugins;
#ifdef _DEBUG
/// stdout log emitter
agi::log::EmitSTDOUT *emit_stdout;
#endif
bool OnInit();
int OnExit();
int OnRun();