00001 #ifndef __LOGGER 00002 #define __LOGGER 00003 00004 #include <ishtar/settings.h> 00005 #include <ostream> 00006 #include <fstream> 00007 #include <vector> 00008 00017 namespace Teem 00018 { 00021 class Logger 00022 { 00023 public: 00024 00026 Logger(const char* fileName) : logFile(fileName), inited(false) { } 00028 virtual ~Logger(); 00029 00031 template<typename T> void addVariable(const std::string &label, const T &ref) { add(label, new LoggableVariable<T>(ref)); } 00032 00034 template<typename T> void addIshtarVariable(const std::string &label, const char* id) { add(label, new LoggableIshtarVariable<T>(id)); } 00035 00037 template<typename ForwardIterator> void addVariables(const std::string &label, ForwardIterator from, ForwardIterator to) { while(from != to) { addVariable(label, *from); ++from; } } 00038 00040 void step(); 00041 00042 protected: 00043 00047 class Loggable 00048 { 00049 public: 00051 virtual ~Loggable() { } 00053 virtual void print(std::ostream &s) const = 0; 00054 }; 00055 00058 template<typename T> class LoggableVariable : public Loggable 00059 { 00060 public: 00062 LoggableVariable(const T &r) : ref(r) { } 00063 virtual void print(std::ostream &s) const { s << ref; } 00064 protected: 00065 const T& ref; 00066 }; 00067 00069 template<typename T> class LoggableIshtarVariable : public Loggable 00070 { 00071 public: 00073 LoggableIshtarVariable(const char* id) : variable(id) { } 00074 virtual void print(std::ostream &s) const { s << variable; } 00075 protected: 00076 Ishtar::Variable<T> variable; 00077 }; 00078 00079 friend std::ostream& operator<< (std::ostream &os, const Logger::Loggable &loggable); 00080 00082 typedef std::map<std::string, std::vector<Loggable*> > LoggableMap; 00083 std::ofstream logFile; 00084 bool inited; 00085 LoggableMap loggables; 00086 00088 void add(const std::string &label, Loggable* loggable); 00089 00091 virtual void initLog(std::ostream &log) = 0; 00093 virtual void dumpSnapshot(std::ostream &log) = 0; 00094 }; 00095 00098 class OctaveLogger : public Logger 00099 { 00100 public: 00104 OctaveLogger(const char* fileName, bool matlab = false) : Logger(fileName), matlabMode(matlab) { } 00106 virtual ~OctaveLogger() { } 00107 00108 protected: 00109 00110 const bool matlabMode; 00111 00113 virtual void initLog(std::ostream &log); 00115 virtual void dumpSnapshot(std::ostream &log); 00116 }; 00117 00120 inline std::ostream& operator<< (std::ostream &os, const Logger::Loggable &loggable) 00121 { 00122 loggable.print(os); 00123 return os; 00124 } 00125 } 00126 00127 #endif