Shinya Kitaoka 810553
#pragma once
Shinya Kitaoka 810553
Toshihiro Shimizu 890ddd
#ifndef TLOGGER_INCLUDED
Toshihiro Shimizu 890ddd
#define TLOGGER_INCLUDED
Toshihiro Shimizu 890ddd
Shinya Kitaoka 262a92
#include <memory></memory>
Shinya Kitaoka 262a92
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// TLogger
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// usage example:
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//    TFilePath fp;
Toshihiro Shimizu 890ddd
//    TLogger::info() << "Loading " << fp << " ...";
Toshihiro Shimizu 890ddd
//    TLogger::error() << "File " << fp << "not found";
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// N.B.
Toshihiro Shimizu 890ddd
//    TLogger::debug() << ....
Toshihiro Shimizu 890ddd
//    is automatically removed in the release
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tcommon.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef DVAPI
Toshihiro Shimizu 890ddd
#undef DVVAR
Toshihiro Shimizu 890ddd
#ifdef TSYSTEM_EXPORTS
Toshihiro Shimizu 890ddd
#define DVAPI DV_EXPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_EXPORT_VAR
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define DVAPI DV_IMPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_IMPORT_VAR
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TFilePath;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI TLogger
Toshihiro Shimizu 890ddd
{ // singleton
Toshihiro Shimizu 890ddd
	class Imp;
Shinya Kitaoka 262a92
	std::unique_ptr<imp> m_imp;</imp>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TLogger();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	~TLogger();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	static TLogger *instance();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	enum MessageType {
Toshihiro Shimizu 890ddd
		Debug = 1,
Toshihiro Shimizu 890ddd
		Info,
Toshihiro Shimizu 890ddd
		Warning,
Toshihiro Shimizu 890ddd
		Error
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	class DVAPI Message
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		MessageType m_type;
Shinya Kitaoka 3bfa54
		std::string m_timestamp;
Shinya Kitaoka 3bfa54
		std::string m_text;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	public:
Shinya Kitaoka 3bfa54
		Message(MessageType type, std::string text);
Toshihiro Shimizu 890ddd
		MessageType getType() const { return m_type; }
Shinya Kitaoka 3bfa54
		std::string getTimestamp() const { return m_timestamp; }
Shinya Kitaoka 3bfa54
		std::string getText() const { return m_text; }
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	class Listener
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	public:
Toshihiro Shimizu 890ddd
		virtual void onLogChanged() = 0;
Toshihiro Shimizu 890ddd
		virtual ~Listener() {}
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void addMessage(const Message &msg);
Toshihiro Shimizu 890ddd
	void clearMessages();
Toshihiro Shimizu 890ddd
	int getMessageCount() const;
Toshihiro Shimizu 890ddd
	Message getMessage(int index) const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void addListener(Listener *listener);
Toshihiro Shimizu 890ddd
	void removeListener(Listener *listener);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	class DVAPI Stream
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		MessageType m_type;
Shinya Kitaoka 3bfa54
		std::string m_text;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	public:
Toshihiro Shimizu 890ddd
		Stream(MessageType type);
Toshihiro Shimizu 890ddd
		~Stream();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
		Stream &operator<<(std::string v);
Toshihiro Shimizu 890ddd
		Stream &operator<<(int v);
Toshihiro Shimizu 890ddd
		Stream &operator<<(double v);
Toshihiro Shimizu 890ddd
		Stream &operator<<(const TFilePath &v);
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	class DVAPI NullStream
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	public:
Toshihiro Shimizu 890ddd
		NullStream() {}
Toshihiro Shimizu 890ddd
		~NullStream() {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
		NullStream &operator<<(std::string) { return *this; }
Toshihiro Shimizu 890ddd
		NullStream &operator<<(int) { return *this; }
Toshihiro Shimizu 890ddd
		NullStream &operator<<(double) { return *this; }
Toshihiro Shimizu 890ddd
		NullStream &operator<<(const TFilePath &) { return *this; }
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef NDEBUG
Toshihiro Shimizu 890ddd
	static NullStream debug()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return NullStream();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	static Stream debug()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return Stream(Debug);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	static Stream info()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return Stream(Info);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	static Stream warning() { return Stream(Warning); }
Toshihiro Shimizu 890ddd
	static Stream error() { return Stream(Error); }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif