AARE
Data analysis library for PSI hybrid detectors
Loading...
Searching...
No Matches
logger.hpp
Go to the documentation of this file.
1#pragma once
2#include <filesystem>
3#include <fstream>
4#include <iostream>
5#include <map>
6#include <vector>
7
8#define LOCATION std::string(__FILE__) + std::string(":") + std::to_string(__LINE__) + ":" + std::string(__func__) + ":"
9
10// operator overload to print vectors
11// typename T must be printable (i.e. have the << operator)
12template <typename T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
13 out << "[";
14 size_t last = v.size() - 1;
15 for (size_t i = 0; i < v.size(); ++i) {
16 out << v[i];
17 if (i != last)
18 out << ", ";
19 }
20 out << "]";
21 return out;
22}
23
24// operator overload for std::array
25template <typename T, size_t N> std::ostream &operator<<(std::ostream &out, const std::array<T, N> &v) {
26 out << "[";
27 size_t last = N - 1;
28 for (size_t i = 0; i < N; ++i) {
29 out << v[i];
30 if (i != last)
31 out << ", ";
32 }
33 out << "]";
34 return out;
35}
36// operator overlaod for std::map
37template <typename K, typename V> std::ostream &operator<<(std::ostream &out, const std::map<K, V> &v) {
38 out << "{";
39 size_t i = 0;
40 for (auto &kv : v) {
41 out << kv.first << ": " << kv.second << ((++i != v.size()) ? ", " : "");
42 }
43
44 out << "}";
45 return out;
46}
47
48namespace aare {
49
50namespace logger {
52 DEBUG = 0,
53 INFO = 1,
55 ERROR = 3
56
57};
58
59class Logger {
60
61 std::streambuf *standard_buf = std::cout.rdbuf();
62 std::streambuf *error_buf = std::cerr.rdbuf();
63 std::ostream *standard_output;
64 std::ostream *error_output;
66
67 std::ofstream out_file;
68
69 public:
70 void set_output_file(std::string filename) {
71 if (out_file.is_open())
72 out_file.close();
73 out_file.open(filename);
74 set_streams(out_file.rdbuf());
75 }
76 void set_streams(std::streambuf *out, std::streambuf *err) {
77 delete standard_output;
78 delete error_output;
79 standard_output = new std::ostream(out);
80 error_output = new std::ostream(err);
81 }
82 void set_streams(std::streambuf *out) { set_streams(out, out); }
85 standard_output = new std::ostream(standard_buf);
86 error_output = new std::ostream(error_buf);
87 }
88
90 if (out_file.is_open())
91 out_file.close();
92
93 standard_output->flush();
94 error_output->flush();
95 delete standard_output;
96 delete error_output;
97 }
98 template <LOGGING_LEVEL level, typename... Strings> void log(const Strings... s) {
99 if (level >= VERBOSITY_LEVEL)
100 log_<level>(s...);
101 }
102 template <typename... Strings> void debug(const Strings... s) { log<LOGGING_LEVEL::DEBUG>("[DEBUG]", s...); }
103 template <typename... Strings> void info(const Strings... s) { log<LOGGING_LEVEL::INFO>("[INFO]", s...); }
104 template <typename... Strings> void warn(const Strings... s) { log<LOGGING_LEVEL::WARNING>("[WARN]", s...); }
105 template <typename... Strings> void error(const Strings... s) { log<LOGGING_LEVEL::ERROR>("[ERROR]", s...); }
106
107 private:
108 template <LOGGING_LEVEL level> void log_() {
109 if (level == LOGGING_LEVEL::ERROR) {
110 *error_output << std::endl;
111 } else {
112 *standard_output << std::endl;
113 }
114 }
115 template <LOGGING_LEVEL level, typename First, typename... Strings> void log_(First arg, const Strings... s) {
116 if (level == LOGGING_LEVEL::ERROR) {
117 *error_output << (arg) << ' ';
118 error_output->flush();
119 } else {
120 *standard_output << (arg) << ' ';
121 standard_output->flush();
122 }
123 log_<level>(s...);
124 }
125};
126
127namespace internal {
128
130} // namespace internal
131
132template <LOGGING_LEVEL level, typename... Strings> void log(const Strings... s) {
133 internal::logger_instance.log<level>(s...);
134}
135template <typename... Strings> void debug(const Strings... s) { internal::logger_instance.debug(s...); }
136template <typename... Strings> void info(const Strings... s) { internal::logger_instance.info(s...); }
137template <typename... Strings> void warn(const Strings... s) { internal::logger_instance.warn(s...); }
138template <typename... Strings> void error(const Strings... s) { internal::logger_instance.error(s...); }
139
140extern void set_streams(std::streambuf *out, std::streambuf *err);
141extern void set_streams(std::streambuf *out);
142extern void set_verbosity(LOGGING_LEVEL level);
143extern void set_output_file(std::string filename);
144extern Logger &get_logger_instance();
145
146} // namespace logger
147
148} // namespace aare
Definition logger.hpp:59
void set_streams(std::streambuf *out, std::streambuf *err)
Definition logger.hpp:76
std::ofstream out_file
Definition logger.hpp:67
~Logger()
Definition logger.hpp:89
void set_verbosity(LOGGING_LEVEL level)
Definition logger.hpp:83
void warn(const Strings... s)
Definition logger.hpp:104
Logger()
Definition logger.hpp:84
void log(const Strings... s)
Definition logger.hpp:98
LOGGING_LEVEL VERBOSITY_LEVEL
Definition logger.hpp:65
void info(const Strings... s)
Definition logger.hpp:103
void debug(const Strings... s)
Definition logger.hpp:102
void set_streams(std::streambuf *out)
Definition logger.hpp:82
std::ostream * standard_output
Definition logger.hpp:63
std::ostream * error_output
Definition logger.hpp:64
std::streambuf * error_buf
Definition logger.hpp:62
void set_output_file(std::string filename)
Definition logger.hpp:70
void log_(First arg, const Strings... s)
Definition logger.hpp:115
void log_()
Definition logger.hpp:108
std::streambuf * standard_buf
Definition logger.hpp:61
void error(const Strings... s)
Definition logger.hpp:105
std::ostream & operator<<(std::ostream &out, const std::vector< T > &v)
Definition logger.hpp:12
aare::logger::Logger logger_instance
Definition logger.cpp:6
void warn(const Strings... s)
Definition logger.hpp:137
void set_verbosity(LOGGING_LEVEL level)
Definition logger.cpp:10
void set_output_file(std::string filename)
Definition logger.cpp:12
void log(const Strings... s)
Definition logger.hpp:132
void set_streams(std::streambuf *out, std::streambuf *err)
Definition logger.cpp:8
void error(const Strings... s)
Definition logger.hpp:138
void debug(const Strings... s)
Definition logger.hpp:135
void info(const Strings... s)
Definition logger.hpp:136
Logger & get_logger_instance()
Definition logger.cpp:11
LOGGING_LEVEL
Definition logger.hpp:51
@ WARNING
Definition logger.hpp:54
@ DEBUG
Definition logger.hpp:52
@ ERROR
Definition logger.hpp:55
@ INFO
Definition logger.hpp:53
Frame class to represent a single frame of data model class should be able to work with streams comin...
Definition CircularFifo.hpp:11