LRT14  1.0
 All Classes Namespaces Functions Variables Enumerations Enumerator
Logger.h
1 #ifndef LOGGER_H_
2 #define LOGGER_H_
3 
4 #define USE_IOLIB
5 #ifdef USE_IOLIB
6 #include <ioLib.h>
7 #endif
8 #include <WPILib.h>
9 #include <vector>
10 #include <typeinfo>
11 #include <sstream>
12 #include "../Utils/Util.h"
13 #include "../Process/SynchronizedProcess.h"
14 
15 class Loggable;
16 
21 {
22 public:
23  static Logger* Instance();
24  static void Finalize();
25 
26  ~Logger();
27 
31  void Initialize();
32 
36  void Run();
37 
45  template<typename T> void Log(T *field, std::size_t size, std::string name, std::string source)
46  {
47  if (!initialized)
48  {
49  int count = size / sizeof(*field); // Check for dynamically allocated arrays
50  Field f = {count == 1 ? typeid(*field).name() :
51  "A" + Util::ToString(count) + "_" + typeid(*field).name(), source + "/" + name, size}; // Change type to static array
52  fields.push_back(f);
53  dataSize += size;
54  }
55  else
56  Write(field, size);
57  }
58 
65  template<typename T> void Log(T *field, std::string name, std::string source)
66  {
67  Log(field, sizeof(*field), name, source);
68  }
69 
76  template<typename T> void Log(T value, std::string name, std::string source)
77  {
78  Log(&value, sizeof(value), name, source);
79  }
80 
85  static void RegisterLoggable(Loggable* loggable);
86 
87 protected:
88  void Tick();
89 
90 private:
91  typedef struct
92  {
93  std::string type;
94  std::string name;
95  std::size_t size;
96  } Field;
97 
98  Logger();
99 
105  void Write(void* field, std::size_t size);
106 
107  static Logger* m_instance;
108 
109  static std::vector<Loggable*> loggables;
110  std::vector<Field> fields;
111 #ifdef USE_IOLIB
112  int file;
113 #else
114  FILE* file;
115 #endif
116  bool initialized;
117  std::size_t dataSize;
118  char* curLoc;
119  void* startLoc;
120  SEM_ID m_writeSem;
121 };
122 
123 #endif /* LOGGER_H_ */