-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyLog.h
122 lines (99 loc) · 3.25 KB
/
MyLog.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
///
/// include/optlogger/MyLog.h
///
/// Written by Roberto Bargetto
/// DIGEP
/// Politecnico di Torino
/// Corso Duca degli Abruzzi, 10129, Torino
/// Italy
///
/// Copyright 2023 by Roberto Bargetto
/// roberto.bargetto@polito.it or roberto.bargetto@gmail.com
///
/// This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License (CC BY-NC-SA 4.0)
/// See the license at http://creativecommons.org/licenses/by-nc-sa/4.0/
///
#ifndef MY_LOG_H_
#define MY_LOG_H_
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <tuple>
#include "log.h"
typedef std::tuple<unsigned int, unsigned int, std::string> OptLogMeta;
template<class... Ts>
class OptLogger
{
public:
OptLogger();
OptLogger(const std::vector<std::tuple<unsigned int, unsigned int, std::string>>& cdim_hdr);
void set(const std::vector<std::tuple<unsigned int, unsigned int, std::string>>& cdim_hdr);
std::string getFormatedHeader();
std::string getFormatedLine(const std::tuple<Ts...>&);
private:
std::vector<std::tuple<unsigned int, unsigned int, std::string>> coldim_header;
};
template<class... Ts>
OptLogger<Ts...>::OptLogger()
{
}
template<class... Ts>
OptLogger<Ts...>::OptLogger(const std::vector<std::tuple<unsigned int, unsigned int, std::string>>& cdim_hdr)
{
coldim_header = (cdim_hdr);
}
template<class... Ts>
void OptLogger<Ts...>::set(const std::vector<std::tuple<unsigned int, unsigned int, std::string>>& cdim_hdr)
{
coldim_header = (cdim_hdr);
}
template<class... Ts>
std::string OptLogger<Ts...>::getFormatedHeader()
{
std::stringstream ss;
for(unsigned int i = 0; i < coldim_header.size(); i++)
{
ss << std::setw(std::get<0>(coldim_header[i])) << std::get<2>(coldim_header[i]);
}
return ss.str();
}
// Function to iterate through all values
// I equals number of values in tuple
template <size_t I = 0, typename... Ts>
typename std::enable_if<I == sizeof...(Ts),
void>::type
printTuple(std::stringstream& ss,const std::vector<std::tuple<unsigned int, unsigned int, std::string>>& cdim_hdr, std::tuple<Ts...> tup)
{
// If iterated through all values
// of tuple, then simply return.
return;
}
template <size_t I = 0, typename... Ts>
typename std::enable_if<(I < sizeof...(Ts)),
void>::type
printTuple(std::stringstream& ss, const std::vector<std::tuple<unsigned int, unsigned int, std::string>>& cdim_hdr, std::tuple<Ts...> tup)
{
// Print element of tuple
ss << std::setw(std::get<0>(cdim_hdr[I])) << std::setprecision(std::get<1>(cdim_hdr[I])) << std::fixed << std::get<I>(tup);
// Go to next element
printTuple<I + 1>(ss, cdim_hdr, tup);
}
template<class... Ts>
std::string OptLogger<Ts...>::getFormatedLine(const std::tuple<Ts...>& tupline)
{
std::stringstream ss;
printTuple(ss, coldim_header, tupline);
return ss.str();
}
/*
#define LOG_INFO(msg) \
{ \
(FILE_LOG(logINFO) << "<" << _FILE_ << ":" << _LINE_ << "> " << msg),\
(void)0\
}
*/
//#define FILE_AND_LINE "<" << _FILE_ << ":" << _LINE_ << "> "
//bool logging_initialized = false;
bool init_logging(bool close_it, std::string filename, TLogLevel level = logINFO, std::string md = "w");
#endif /* MY_LOG_H_ */