CellModules
logger.hpp
Go to the documentation of this file.
1#ifndef LOGGER_HPP
2#define LOGGER_HPP
3#include <algorithm>
4#include <array>
5#include <chrono>
6#include <cstdio>
7#include <ctime>
8#include <iomanip>
9#include <iostream>
10#include <sstream>
11#include <utility>
12
13using std::ostream;
14using std::string;
15using std::cout;
16using std::cerr;
17using std::endl;
18
19namespace MecaCell {
20// the following are LINUX ONLY terminal color codes.
21#ifdef MECACELL_TERMINAL_COLORS
22const constexpr static char RESET[] = "\033[0m";
23const constexpr static char BLACK[] = "\033[30m";
24const constexpr static char RED[] = "\033[31m";
25const constexpr static char GREEN[] = "\033[32m";
26const constexpr static char YELLOW[] = "\033[33m";
27const constexpr static char BLUE[] = "\033[34m";
28const constexpr static char MAGENTA[] = "\033[35m";
29const constexpr static char CYAN[] = "\033[36m";
30const constexpr static char WHITE[] = "\033[37m";
31const constexpr static char BOLDBLACK[] = "\033[1m\033[30m";
32const constexpr static char BOLDRED[] = "\033[1m\033[31m";
33const constexpr static char BOLDGREEN[] = "\033[1m\033[32m";
34const constexpr static char BOLDYELLOW[] = "\033[1m\033[33m";
35const constexpr static char BOLDBLUE[] = "\033[1m\033[34m";
36const constexpr static char BOLDMAGENTA[] = "\033[1m\033[35m";
37const constexpr static char BOLDCYAN[] = "\033[1m\033[36m";
38const constexpr static char BOLDWHITE[] = "\033[1m\033[37m";
39#else
40const constexpr static char RESET[] = "";
41const constexpr static char BLACK[] = "";
42const constexpr static char RED[] = "";
43const constexpr static char GREEN[] = "";
44const constexpr static char YELLOW[] = "";
45const constexpr static char BLUE[] = "";
46const constexpr static char MAGENTA[] = "";
47const constexpr static char CYAN[] = "";
48const constexpr static char WHITE[] = "";
49const constexpr static char BOLDBLACK[] = "";
50const constexpr static char BOLDRED[] = "";
51const constexpr static char BOLDGREEN[] = "";
52const constexpr static char BOLDYELLOW[] = "";
53const constexpr static char BOLDBLUE[] = "";
54const constexpr static char BOLDMAGENTA[] = "";
55const constexpr static char BOLDCYAN[] = "";
56const constexpr static char BOLDWHITE[] = "";
57#endif
58
59// maybe needed
60//constexpr decltype(WARN::tag) WARN::tag;
61//constexpr decltype(ERR::tag) ERR::tag;
62//constexpr decltype(INF::tag) INF::tag;
63//constexpr decltype(DBG::tag) DBG::tag;
64//constexpr decltype(SUC::tag) SUC::tag;
65//constexpr decltype(WARN::color) WARN::color;
66//constexpr decltype(ERR::color) ERR::color;
67//constexpr decltype(INF::color) INF::color;
68//constexpr decltype(DBG::color) DBG::color;
69//constexpr decltype(SUC::color) SUC::color;
70
71template <typename T> std::string sublogger(T&& t) {
72 std::ostringstream os;
73 os << t;
74 return os.str();
75}
76
77template <typename T, typename... Args> std::string sublogger(T&& t, Args&&... args) {
78 std::ostringstream os;
79 os << t << sublogger(std::forward<Args>(args)...);
80 return os.str();
81}
82
83struct WARN {
84#ifndef MECACELL_LOGGER_WARN_DISABLE
85 static constexpr const bool enabled = true;
86#else
87 static constexpr const bool enabled = false;
88#endif
89 static constexpr const auto color = YELLOW;
90 static constexpr const auto tag = "⚠ ";
91};
92struct ERR {
93#ifndef MECACELL_LOGGER_ERR_DISABLE
94 static constexpr const bool enabled = true;
95#else
96 static constexpr const bool enabled = false;
97#endif
98 static constexpr const auto color = RED;
99 static constexpr const auto tag = " ✖ ";
100};
101struct INF {
102#ifndef MECACELL_LOGGER_INF_DISABLE
103 static constexpr const bool enabled = true;
104#else
105 static constexpr const bool enabled = false;
106#endif
107 static constexpr const auto color = BLUE;
108 static constexpr const auto tag = "⟢ ";
109};
110struct DBG {
111#ifndef MECACELL_LOGGER_DBG_DISABLE
112 static constexpr const bool enabled = true;
113#else
114 static constexpr const bool enabled = false;
115#endif
116 static constexpr const auto color = MAGENTA;
117 static constexpr const auto tag = "☵ ";
118};
119struct SUC {
120#ifndef MECACELL_LOGGER_SUC_DISABLE
121 static constexpr const bool enabled = true;
122#else
123 static constexpr const bool enabled = false;
124#endif
125 static constexpr const auto color = BOLDGREEN;
126 static constexpr const auto tag = " ✓ ";
127};
128
129template <typename Type, typename... Args> void logger(Args&&... args) {
130 if (Type::enabled) {
131 time_t rawtime;
132 time(&rawtime);
133 struct tm* timeinfo = localtime(&rawtime);
134 char buffer[80];
135 strftime(buffer, 80, "%F %H:%M:%S", timeinfo);
136
137 std::ostringstream os;
138 os << BOLDBLACK << "[" << buffer << "]" << RESET << Type::color << " " << Type::tag
139 << BOLDBLACK << " : " << RESET;
140 os << sublogger(std::forward<Args>(args)...) << std::endl;
141 std::cerr << os.str();
142 }
143}
144}
145#endif
this file contains various miscellanious utility functions & helpers *
const static constexpr char BLUE[]
Definition: logger.hpp:45
const static constexpr char BOLDMAGENTA[]
Definition: logger.hpp:54
const static constexpr char BLACK[]
Definition: logger.hpp:41
const static constexpr char GREEN[]
Definition: logger.hpp:43
const static constexpr char BOLDRED[]
Definition: logger.hpp:50
const static constexpr char YELLOW[]
Definition: logger.hpp:44
const static constexpr char BOLDBLACK[]
Definition: logger.hpp:49
void logger(Args &&... args)
Definition: logger.hpp:129
const static constexpr char MAGENTA[]
Definition: logger.hpp:46
std::string sublogger(T &&t)
Definition: logger.hpp:71
const static constexpr char BOLDYELLOW[]
Definition: logger.hpp:52
const static constexpr char BOLDBLUE[]
Definition: logger.hpp:53
const static constexpr char BOLDWHITE[]
Definition: logger.hpp:56
const static constexpr char CYAN[]
Definition: logger.hpp:47
const static constexpr char BOLDGREEN[]
Definition: logger.hpp:51
const static constexpr char WHITE[]
Definition: logger.hpp:48
const static constexpr char RED[]
Definition: logger.hpp:42
const static constexpr char BOLDCYAN[]
Definition: logger.hpp:55
const static constexpr char RESET[]
Definition: logger.hpp:40
iostream cout
Standard output stream.
Definition: std.hpp:267
iostream cerr
Standard error stream.
Definition: std.hpp:281
iostream endl
End-of-line manipulator.
Definition: std.hpp:274
static constexpr const auto tag
Definition: logger.hpp:117
static constexpr const bool enabled
Definition: logger.hpp:112
static constexpr const auto color
Definition: logger.hpp:116
static constexpr const auto tag
Definition: logger.hpp:99
static constexpr const bool enabled
Definition: logger.hpp:94
static constexpr const auto color
Definition: logger.hpp:98
static constexpr const bool enabled
Definition: logger.hpp:103
static constexpr const auto tag
Definition: logger.hpp:108
static constexpr const auto color
Definition: logger.hpp:107
static constexpr const bool enabled
Definition: logger.hpp:121
static constexpr const auto color
Definition: logger.hpp:125
static constexpr const auto tag
Definition: logger.hpp:126
static constexpr const auto color
Definition: logger.hpp:89
static constexpr const bool enabled
Definition: logger.hpp:85
static constexpr const auto tag
Definition: logger.hpp:90