Make log messages a little saner

This commit is contained in:
Thomas Goyne 2013-01-22 09:02:20 -08:00
parent 69e1744fc7
commit 101721863a
4 changed files with 19 additions and 48 deletions

View file

@ -30,7 +30,6 @@
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <functional> #include <functional>
#include <memory>
namespace agi { namespace agi {
namespace log { namespace log {
@ -42,24 +41,16 @@ LogSink *log;
/// Keep this ordered the same as Severity /// Keep this ordered the same as Severity
const char *Severity_ID = "EAWID"; const char *Severity_ID = "EAWID";
SinkMessage::SinkMessage(const char *section, Severity severity, SinkMessage::SinkMessage(const char *section, Severity severity, const char *file, const char *func, int line, timeval tv)
const char *file, const char *func, int line,
timeval tv)
: section(section) : section(section)
, severity(severity) , severity(severity)
, file(file) , file(file)
, func(func) , func(func)
, line(line) , line(line)
, tv(tv) , tv(tv)
, message(nullptr)
, len(0)
{ {
} }
SinkMessage::~SinkMessage() {
delete [] message;
}
/// @todo The log files need to be trimmed after N amount. /// @todo The log files need to be trimmed after N amount.
LogSink::~LogSink() { LogSink::~LogSink() {
// The destructor for emitters may try to log messages, so disable all the // The destructor for emitters may try to log messages, so disable all the
@ -87,22 +78,16 @@ void LogSink::Unsubscribe(Emitter *em) {
LOG_D("agi/log/emitter/unsubscribe") << "Un-Subscribe: " << this; LOG_D("agi/log/emitter/unsubscribe") << "Un-Subscribe: " << this;
} }
Message::Message(const char *section, Message::Message(const char *section, Severity severity, const char *file, const char *func, int line)
Severity severity, : msg(nullptr, 1024)
const char *file,
const char *func,
int line)
: len(1024)
, buf(new char[len])
, msg(buf, len)
{ {
sm = new SinkMessage(section, severity, file, func, line, util::time_log()); sm = new SinkMessage(section, severity, file, func, line, util::time_log());
} }
Message::~Message() { Message::~Message() {
sm->message = msg.str(); sm->message = std::string(msg.str(), (std::string::size_type)msg.pcount());
sm->len = (size_t)msg.pcount();
agi::log::log->log(sm); agi::log::log->log(sm);
msg.freeze(false);
} }
JsonEmitter::JsonEmitter(agi::fs::path const& directory, const agi::log::LogSink *log_sink) JsonEmitter::JsonEmitter(agi::fs::path const& directory, const agi::log::LogSink *log_sink)
@ -128,7 +113,7 @@ JsonEmitter::~JsonEmitter() {
entry["file"] = sink[i]->file; entry["file"] = sink[i]->file;
entry["func"] = sink[i]->func; entry["func"] = sink[i]->func;
entry["line"] = sink[i]->line; entry["line"] = sink[i]->line;
entry["message"] = std::string(sink[i]->message, sink[i]->len); entry["message"] = sink[i]->message;
array.push_back(std::move(entry)); array.push_back(std::move(entry));
} }

View file

@ -69,10 +69,7 @@ extern const char *Severity_ID;
extern LogSink *log; extern LogSink *log;
/// Container to hold a single message /// Container to hold a single message
class SinkMessage { struct SinkMessage {
SinkMessage(SinkMessage const&);
SinkMessage& operator=(SinkMessage const&);
public:
/// @brief Constructor /// @brief Constructor
/// @param section Section info /// @param section Section info
/// @param severity Severity /// @param severity Severity
@ -82,17 +79,13 @@ public:
/// @param tv Log time /// @param tv Log time
SinkMessage(const char *section, Severity severity, const char *file, const char *func, int line, timeval tv); SinkMessage(const char *section, Severity severity, const char *file, const char *func, int line, timeval tv);
/// Destructor
~SinkMessage();
const char *section; ///< Section info eg "video/open" "video/seek" etc const char *section; ///< Section info eg "video/open" "video/seek" etc
Severity severity; ///< Severity Severity severity; ///< Severity
const char *file; ///< Source file const char *file; ///< Source file
const char *func; ///< Function name const char *func; ///< Function name
int line; ///< Source line int line; ///< Source line
agi_timeval tv; ///< Time at execution agi_timeval tv; ///< Time at execution
char *message; ///< Formatted message std::string message; ///< Formatted message
size_t len; ///< Message length
}; };
class Emitter; class Emitter;
@ -164,28 +157,20 @@ public:
/// Generates a message and submits it to the log sink. /// Generates a message and submits it to the log sink.
class Message { class Message {
const int len;
char *buf;
std::ostrstream msg; std::ostrstream msg;
SinkMessage *sm; SinkMessage *sm;
public: public:
Message(const char *section, Message(const char *section, Severity severity, const char *file, const char *func, int line);
Severity severity,
const char *file,
const char *func,
int line);
~Message(); ~Message();
std::ostream& stream() { return msg; } std::ostream& stream() { return msg; }
}; };
/// Emit log entries to stdout. /// Emit log entries to stdout.
class EmitSTDOUT: public Emitter { class EmitSTDOUT: public Emitter {
public: public:
void log(SinkMessage *sm); void log(SinkMessage *sm);
}; };
} // namespace log } // namespace log
} // namespace agi } // namespace agi

View file

@ -36,7 +36,7 @@ void EmitSTDOUT::log(SinkMessage *sm) {
time_t time = sm->tv.tv_sec; time_t time = sm->tv.tv_sec;
localtime_s(&tmtime, &time); localtime_s(&tmtime, &time);
char buff[1024]; char buff[65536];
_snprintf_s(buff, _TRUNCATE, "%s (%d): %c %02d:%02d:%02d %-6ld <%-25s> [%s] %.*s\n", _snprintf_s(buff, _TRUNCATE, "%s (%d): %c %02d:%02d:%02d %-6ld <%-25s> [%s] %.*s\n",
sm->file, sm->file,
sm->line, sm->line,
@ -47,8 +47,8 @@ void EmitSTDOUT::log(SinkMessage *sm) {
sm->tv.tv_usec, sm->tv.tv_usec,
sm->section, sm->section,
sm->func, sm->func,
sm->len, sm->message.size(),
sm->message); sm->message.c_str());
OutputDebugStringW(charset::ConvertW(buff).c_str()); OutputDebugStringW(charset::ConvertW(buff).c_str());
} }
} // namespace log } // namespace log

View file

@ -36,6 +36,11 @@
#include "dialog_log.h" #include "dialog_log.h"
#include "compat.h"
#include "include/aegisub/context.h"
#include <libaegisub/log.h>
#include <algorithm> #include <algorithm>
#include <ctime> #include <ctime>
#include <functional> #include <functional>
@ -46,10 +51,6 @@
#include <wx/stattext.h> #include <wx/stattext.h>
#include <wx/textctrl.h> #include <wx/textctrl.h>
#include <libaegisub/log.h>
#include "include/aegisub/context.h"
class EmitLog : public agi::log::Emitter { class EmitLog : public agi::log::Emitter {
wxTextCtrl *text_ctrl; wxTextCtrl *text_ctrl;
public: public:
@ -74,7 +75,7 @@ public:
sm->file, sm->file,
sm->func, sm->func,
sm->line, sm->line,
wxString::FromUTF8(sm->message, sm->len)); to_wx(sm->message));
#else #else
wxString log = wxString::Format("%c %-6ld <%-25s> [%s:%s:%d] %s\n", wxString log = wxString::Format("%c %-6ld <%-25s> [%s:%s:%d] %s\n",
agi::log::Severity_ID[sm->severity], agi::log::Severity_ID[sm->severity],
@ -83,7 +84,7 @@ public:
sm->file, sm->file,
sm->func, sm->func,
sm->line, sm->line,
wxString::FromUTF8(sm->message, sm->len)); to_wx(sm->message));
#endif #endif
text_ctrl->AppendText(log); text_ctrl->AppendText(log);
} }