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
Shinya Kitaoka 120a6e
class DVAPI TLogger {  // singleton
Shinya Kitaoka 120a6e
  class Imp;
Shinya Kitaoka 120a6e
  std::unique_ptr<imp> m_imp;</imp>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TLogger();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  ~TLogger();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  static TLogger *instance();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  enum MessageType { Debug = 1, Info, Warning, Error };
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  class DVAPI Message {
Shinya Kitaoka 120a6e
    MessageType m_type;
Shinya Kitaoka 120a6e
    std::string m_timestamp;
Shinya Kitaoka 120a6e
    std::string m_text;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  public:
Shinya Kitaoka 120a6e
    Message(MessageType type, std::string text);
Shinya Kitaoka 120a6e
    MessageType getType() const { return m_type; }
Shinya Kitaoka 120a6e
    std::string getTimestamp() const { return m_timestamp; }
Shinya Kitaoka 120a6e
    std::string getText() const { return m_text; }
Shinya Kitaoka 120a6e
  };
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  class Listener {
Shinya Kitaoka 120a6e
  public:
Shinya Kitaoka 120a6e
    virtual void onLogChanged() = 0;
Shinya Kitaoka 120a6e
    virtual ~Listener() {}
Shinya Kitaoka 120a6e
  };
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void addMessage(const Message &msg);
Shinya Kitaoka 120a6e
  void clearMessages();
Shinya Kitaoka 120a6e
  int getMessageCount() const;
Shinya Kitaoka 120a6e
  Message getMessage(int index) const;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void addListener(Listener *listener);
Shinya Kitaoka 120a6e
  void removeListener(Listener *listener);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  class DVAPI Stream {
Shinya Kitaoka 120a6e
    MessageType m_type;
Shinya Kitaoka 120a6e
    std::string m_text;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  public:
Shinya Kitaoka 120a6e
    Stream(MessageType type);
Shinya Kitaoka 120a6e
    ~Stream();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    Stream &operator<<(std::string v);
Shinya Kitaoka 120a6e
    Stream &operator<<(int v);
Shinya Kitaoka 120a6e
    Stream &operator<<(double v);
Shinya Kitaoka 120a6e
    Stream &operator<<(const TFilePath &v);
Shinya Kitaoka 120a6e
  };
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  class DVAPI NullStream {
Shinya Kitaoka 120a6e
  public:
Shinya Kitaoka 120a6e
    NullStream() {}
Shinya Kitaoka 120a6e
    ~NullStream() {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    NullStream &operator<<(std::string) { return *this; }
Shinya Kitaoka 120a6e
    NullStream &operator<<(int) { return *this; }
Shinya Kitaoka 120a6e
    NullStream &operator<<(double) { return *this; }
Shinya Kitaoka 120a6e
    NullStream &operator<<(const TFilePath &) { return *this; }
Shinya Kitaoka 120a6e
  };
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef NDEBUG
Shinya Kitaoka 120a6e
  static NullStream debug() { return NullStream(); }
Toshihiro Shimizu 890ddd
#else
Shinya Kitaoka 120a6e
  static Stream debug() { return Stream(Debug); }
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  static Stream info() { return Stream(Info); }
Shinya Kitaoka 120a6e
  static Stream warning() { return Stream(Warning); }
Shinya Kitaoka 120a6e
  static Stream error() { return Stream(Error); }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif