2010-05-31 20:55:29 +00: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.h
|
|
|
|
/// @brief Logging
|
|
|
|
/// @ingroup libaegisub
|
|
|
|
|
2013-01-04 07:01:50 -08:00
|
|
|
#include <libaegisub/fs_fwd.h>
|
2013-01-21 21:31:35 -08:00
|
|
|
#include <libaegisub/time.h>
|
2010-06-01 05:11:18 +00:00
|
|
|
|
2011-07-26 22:25:21 +00:00
|
|
|
#include <ctime>
|
2013-01-23 10:41:28 -08:00
|
|
|
#include <boost/circular_buffer.hpp>
|
2013-01-22 09:20:32 -08:00
|
|
|
#include <iosfwd>
|
2010-07-07 02:41:46 +00:00
|
|
|
#include <memory>
|
2013-01-04 07:01:50 -08:00
|
|
|
#include <vector>
|
|
|
|
|
2010-05-31 20:55:29 +00:00
|
|
|
#ifdef __DEPRECATED // Dodge GCC warnings
|
|
|
|
# undef __DEPRECATED
|
|
|
|
# include <strstream>
|
|
|
|
# define __DEPRECATED
|
|
|
|
#else
|
|
|
|
# include <strstream>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// These macros below aren't a perm solution, it will depend on how annoying they are through
|
|
|
|
// actual usage, and also depends on msvc support.
|
2010-10-11 04:25:02 +00:00
|
|
|
#define LOG_SINK(section, severity) agi::log::Message(section, severity, __FILE__, __FUNCTION__, __LINE__).stream()
|
2010-05-31 20:55:29 +00:00
|
|
|
#define LOG_E(section) LOG_SINK(section, agi::log::Exception)
|
|
|
|
#define LOG_A(section) LOG_SINK(section, agi::log::Assert)
|
|
|
|
#define LOG_W(section) LOG_SINK(section, agi::log::Warning)
|
|
|
|
#define LOG_I(section) LOG_SINK(section, agi::log::Info)
|
|
|
|
#define LOG_D(section) LOG_SINK(section, agi::log::Debug)
|
|
|
|
|
2010-06-08 23:21:39 +00:00
|
|
|
#define LOG_W_IF(cond, section) if (cond) LOG_SINK(section, agi::log::Warning)
|
|
|
|
#define LOG_I_IF(cond, section) if (cond) LOG_SINK(section, agi::log::Info)
|
|
|
|
#define LOG_D_IF(cond, section) if (cond) LOG_SINK(section, agi::log::Debug)
|
2010-05-31 20:55:29 +00:00
|
|
|
|
|
|
|
namespace agi {
|
2013-01-25 15:50:08 -08:00
|
|
|
namespace dispatch { class Queue; }
|
|
|
|
|
2010-05-31 20:55:29 +00:00
|
|
|
namespace log {
|
2010-06-02 23:45:51 +00:00
|
|
|
class LogSink;
|
2010-05-31 20:55:29 +00:00
|
|
|
|
|
|
|
/// Severity levels
|
|
|
|
enum Severity {
|
|
|
|
Exception, ///< Used when exceptions are thrown
|
|
|
|
Assert, ///< Fatal and non-fatal assert logging
|
|
|
|
Warning, ///< Warnings
|
|
|
|
Info, ///< Information only
|
|
|
|
Debug ///< Enabled by default when compiled in debug mode.
|
|
|
|
};
|
|
|
|
|
2010-06-01 08:17:27 +00:00
|
|
|
/// Short Severity ID
|
|
|
|
/// Set in common/log.cpp, keep this ordered the same as Severity.
|
|
|
|
extern const char *Severity_ID;
|
|
|
|
|
2010-06-02 23:45:51 +00:00
|
|
|
/// 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 13:00:46 +00:00
|
|
|
extern LogSink *log;
|
2010-06-01 08:17:27 +00:00
|
|
|
|
2010-05-31 20:55:29 +00:00
|
|
|
/// Container to hold a single message
|
2013-01-22 09:02:20 -08:00
|
|
|
struct SinkMessage {
|
2010-05-31 20:55:29 +00:00
|
|
|
const char *section; ///< Section info eg "video/open" "video/seek" etc
|
|
|
|
Severity severity; ///< Severity
|
|
|
|
const char *file; ///< Source file
|
|
|
|
const char *func; ///< Function name
|
|
|
|
int line; ///< Source line
|
2010-06-09 02:07:43 +00:00
|
|
|
agi_timeval tv; ///< Time at execution
|
2013-01-22 09:02:20 -08:00
|
|
|
std::string message; ///< Formatted message
|
2010-05-31 20:55:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Emitter;
|
|
|
|
|
|
|
|
/// Log sink, single destination for all messages
|
|
|
|
class LogSink {
|
2013-01-23 10:41:28 -08:00
|
|
|
boost::circular_buffer<SinkMessage> messages;
|
2013-01-25 15:50:08 -08:00
|
|
|
std::unique_ptr<dispatch::Queue> queue;
|
2010-05-31 20:55:29 +00:00
|
|
|
|
2012-01-08 01:36:09 +00:00
|
|
|
/// List of pointers to emitters
|
2010-05-31 20:55:29 +00:00
|
|
|
std::vector<Emitter*> emitters;
|
|
|
|
|
|
|
|
public:
|
2013-01-25 15:50:08 -08:00
|
|
|
LogSink();
|
2010-05-31 20:55:29 +00:00
|
|
|
~LogSink();
|
|
|
|
|
|
|
|
/// Insert a message into the sink.
|
2013-01-23 10:41:28 -08:00
|
|
|
void Log(SinkMessage const& sm);
|
2010-05-31 20:55:29 +00:00
|
|
|
|
2012-01-08 01:36:09 +00:00
|
|
|
/// @brief Subscribe an emitter
|
2010-06-24 01:24:09 +00:00
|
|
|
/// @param em Emitter to add
|
2012-01-08 01:36:09 +00:00
|
|
|
///
|
|
|
|
/// LogSink takes ownership of the passed emitter
|
2010-06-24 01:24:09 +00:00
|
|
|
void Subscribe(Emitter *em);
|
2010-05-31 20:55:29 +00:00
|
|
|
|
2012-01-08 01:36:09 +00:00
|
|
|
/// @brief Unsubscribe and delete an emitter
|
|
|
|
/// @param em Emitter to delete
|
2010-06-24 01:24:09 +00:00
|
|
|
void Unsubscribe(Emitter *em);
|
2010-05-31 20:55:29 +00:00
|
|
|
|
|
|
|
/// @brief @get the complete (current) log.
|
2010-06-02 23:45:51 +00:00
|
|
|
/// @return Const pointer to internal sink.
|
2013-01-25 15:50:08 -08:00
|
|
|
decltype(messages) GetMessages() const;
|
2010-05-31 20:55:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// An emitter to produce human readable output for a log sink.
|
|
|
|
class Emitter {
|
|
|
|
public:
|
|
|
|
/// Destructor
|
2012-01-08 01:36:09 +00:00
|
|
|
virtual ~Emitter() { }
|
2010-05-31 20:55:29 +00:00
|
|
|
|
|
|
|
/// Accept a single log entry
|
|
|
|
virtual void log(SinkMessage *sm)=0;
|
|
|
|
};
|
|
|
|
|
2013-01-22 09:20:32 -08:00
|
|
|
/// A simple emitter which writes the log to a file in json format
|
2012-01-08 01:36:23 +00:00
|
|
|
class JsonEmitter : public Emitter {
|
2013-01-22 09:20:32 -08:00
|
|
|
std::unique_ptr<std::ostream> fp;
|
2012-01-08 01:36:23 +00:00
|
|
|
|
2013-01-22 09:20:32 -08:00
|
|
|
void WriteTime(const char *key);
|
2012-01-08 01:36:23 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// Constructor
|
|
|
|
/// @param directory Directory to write the log file in
|
2013-01-22 09:20:32 -08:00
|
|
|
JsonEmitter(fs::path const& directory);
|
2012-01-08 01:36:23 +00:00
|
|
|
/// Destructor
|
|
|
|
~JsonEmitter();
|
|
|
|
|
2013-01-22 09:20:32 -08:00
|
|
|
void log(SinkMessage *);
|
2012-01-08 01:36:23 +00:00
|
|
|
};
|
|
|
|
|
2010-05-31 20:55:29 +00:00
|
|
|
/// Generates a message and submits it to the log sink.
|
|
|
|
class Message {
|
2010-06-24 01:24:09 +00:00
|
|
|
std::ostrstream msg;
|
2013-01-23 10:41:28 -08:00
|
|
|
SinkMessage sm;
|
2010-05-31 20:55:29 +00:00
|
|
|
|
|
|
|
public:
|
2013-01-22 09:02:20 -08:00
|
|
|
Message(const char *section, Severity severity, const char *file, const char *func, int line);
|
2010-05-31 20:55:29 +00:00
|
|
|
~Message();
|
2010-06-24 01:24:09 +00:00
|
|
|
std::ostream& stream() { return msg; }
|
2010-05-31 20:55:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Emit log entries to stdout.
|
|
|
|
class EmitSTDOUT: public Emitter {
|
|
|
|
public:
|
|
|
|
void log(SinkMessage *sm);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace log
|
|
|
|
} // namespace agi
|