CLA_Logger.h 3.77 KB
Newer Older
fmg005's avatar
fmg005 committed
1 2 3 4 5

#ifndef CLALOGGER_H
#define CLALOGGER_H

#include <string>
fmg005's avatar
fmg005 committed
6
#include <ctime> /* time_t, time, struct tm, localtime, strftime*/
fmg005's avatar
fmg005 committed
7
#include <fstream>
8 9 10 11
#include <map>
#include <cstdarg>
#include <sys/time.h>
#include <iomanip>
fmg005's avatar
fmg005 committed
12
#include <sstream>
13
#include <iostream>
14 15
#include <cstring> /* strcat() */
#include <sys/time.h> /* timeval, gettimefday*/
fmg005's avatar
fmg005 committed
16
#include <chrono>
17
#include "configure_prosim_paths.h"
fmg005's avatar
fmg005 committed
18 19

using namespace std;
fmg005's avatar
fmg005 committed
20
using namespace std::chrono;
fmg005's avatar
fmg005 committed
21 22 23 24 25

namespace CLA {
    class LOGGER {
	private:
	    string level_name;
fmg005's avatar
fmg005 committed
26
	    const string m_filename;
27
      const string m_patient_name;
28
	    ofstream m_logstream[4];
fmg005's avatar
fmg005 committed
29
	    ostringstream ss;
30 31 32
      bool m_verbosity = true;
	    enum LEVEL {DEBUG, INFO, WARN, ERROR, FATAL, NONE};
      LEVEL DEFAULT_LEVEL = LOGGER::DEBUG;
fmg005's avatar
fmg005 committed
33 34 35
	    string current_time(bool);
	    string format_chrono_time(time_t);
	    string format_chrono_time(system_clock::time_point);
fmg005's avatar
fmg005 committed
36
      string format_chrono_time_ms(system_clock::time_point);
37
	    string convert_level_to_string(LEVEL);
fmg005's avatar
fmg005 committed
38
      string format_duration(time_t);
39
      map<std::string, LEVEL> logging_levels;
fmg005's avatar
fmg005 committed
40
	public:
41 42
	    LOGGER(const string&); // filename
	    LOGGER(const string&, const string&); // filename, patient_name
fmg005's avatar
fmg005 committed
43 44 45
      ~LOGGER();
	    void log_duration(system_clock::time_point, time_t, time_t, time_t, \
                        time_t, time_t);
46 47
      void log_duration(system_clock::time_point, time_t, time_t, time_t, \
                        time_t);
48
      void log_data(double, double, double);
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
      void setLevel(const string&);
      void setVerbosity(bool);
      template <typename T>
      void debug(const T& t) {
        if (LOGGER::DEBUG >= DEFAULT_LEVEL) {
          ss<< current_time(true);
          ss<< setw(2);
          ss<< " ";
          ss<< convert_level_to_string(LOGGER::DEBUG);
          ss<< setw(2);
          ss<< " ";
          ss<< t;
          m_logstream[0]<< ss.str() << endl;
          if (m_verbosity)
            cout << ss.str() <<endl;
          ss.str("");
        }
      }
fmg005's avatar
fmg005 committed
67

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
      template <typename T>
      void info(const T& t) {
        if (LOGGER::INFO >= DEFAULT_LEVEL) {
          ss<< current_time(true);
          ss<< setw(2);
          ss<< " ";
          ss<< convert_level_to_string(LOGGER::INFO);
          ss<< setw(2);
          ss<< " ";
          ss<< " ";
          ss<< t;
          m_logstream[0]<< ss.str() << endl;
          if (m_verbosity)
            cout << ss.str() << endl;
          ss.str("");
        }
      }
85

86 87
      template <typename T>
      void warn(const T& t) {
fmg005's avatar
fmg005 committed
88
        if (LOGGER::WARN >= DEFAULT_LEVEL) {
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
          ss<< current_time(true);
          ss<< setw(2);
          ss<< " ";
          ss<< convert_level_to_string(LOGGER::WARN);
          ss<< setw(2);
          ss<< " ";
          ss<< " ";
          ss<< t;
          m_logstream[0]<< ss.str() << endl;
          if (m_verbosity)
            cout << ss.str() << endl;
          ss.str("");
        }
      }
      template <typename T>
      void error(const T& t) {
        if (LOGGER::ERROR >= DEFAULT_LEVEL) {
          ss<< current_time(true);
          ss<< setw(2);
          ss<< " ";
          ss<< convert_level_to_string(LOGGER::ERROR);
          ss<< setw(2);
          ss<< " ";
          ss<< t;
          m_logstream[0]<< ss.str() << endl;
          if (m_verbosity)
            cout << ss.str() << endl;
          ss.str("");
        }
      }
      template <typename T>
      void fatal(const T& t) {
        if (LOGGER::FATAL >= DEFAULT_LEVEL) {
          ss<< current_time(true);
          ss<< setw(2);
          ss<< " ";
          ss<< convert_level_to_string(LOGGER::FATAL);
          ss<< setw(2);
          ss<< " ";
          ss<< t;
          m_logstream[0]<< ss.str() << endl;
          if (m_verbosity)
            cout << ss.str() << endl;
          ss.str("");
        }
      }
fmg005's avatar
fmg005 committed
135 136 137 138
    };
}

#endif