eduEngine 1.0
Course framework for DA376B
Loading...
Searching...
No Matches
logstreamer.h
1// Created by Carl Johan Gribel.
2// Licensed under the MIT License. See LICENSE file for details.
3
4#ifndef streamlogger_h
5#define streamlogger_h
6
7#include <vector>
8#include <ostream>
9#include <fstream>
10
11namespace logstreamer
12{
13
15 //
16 // Note: ostream <=> basic_ostream<char>
17 //
19 {
20 std::vector< std::pair<std::ostream*, int> > strms; // Streams & priorities
21 std::vector<std::ofstream*> file_strms; // Open file streams
22 int priolvl = 0; // Current priority
23
24 // Signature for std stream manipulators such as endl, setw, setfill ...
25 using iomanip_func = std::ostream& (*)(std::ostream&);
26
27 public:
28
30 {
31 int level;
32 priority_t(int level) : level(level) {}
33 };
34
35 logstreamer_t() = default;
36
37 logstreamer_t(std::ostream& strm, int priolvl)
38 {
39 add_ostream(strm, priolvl);
40 }
41
42 logstreamer_t(const std::string& file, int priolvl)
43 {
44 add_ofstream(file, priolvl);
45 }
46
47 // logstreamer_t(std::ostream& strm, int strm_priolvl, const std::string& file, int file_priolvl)
48 // {
49 // add_ostream(strm, strm_priolvl);
50 // add_ofstream(file, file_priolvl);
51 // }
52
53 void add_ostream(std::ostream& strm, int priolvl)
54 {
55 strms.push_back( {{&strm}, {priolvl}} );
56 }
57
58 void add_ofstream(const std::string& file, int priolvl)
59 {
60 std::ofstream *filestrm = new std::ofstream;
61 filestrm->open(file, std::ios::out);
62 if (filestrm->is_open())
63 {
64 strms.push_back( {{filestrm},{priolvl}});
65 file_strms.push_back(filestrm);
66 }
67 }
68
69 template<class T>
70 logstreamer_t& operator << (const T& item)
71 {
72 for (auto& strm : strms)
73 if (strm.second <= priolvl)
74 *(strm.first) << item;
75
76 return *this;
77
78 }
79
80 logstreamer_t& operator << (iomanip_func iofunc)
81 {
82 for (auto& strm : strms)
83 if (strm.second <= priolvl)
84 *(strm.first) << iofunc;
85
86 return *this;
87 }
88
89 logstreamer_t& operator << (const priority_t& priority)
90 {
91 priolvl = priority.level;
92 return *this;
93 }
94
95 ~logstreamer_t()
96 {
97 for (auto* fstrm : file_strms) { fstrm->close(); delete fstrm; }
98 }
99 };
100
101 enum { PRTVERBOSE = 0, PRTSTRICT };
102 using priority = logstreamer_t::priority_t;
103
104}
105
106#if 0
107#include <iomanip>
108
109static void loggertest()
110{
111 using namespace logstreamer;
112
113 logstreamer_t streams;
114 //streams.add_ostream(std::cout, HIGH_VERBOSE);
115 streams.add_ofstream("/Users/ag1498/Desktop/loggertest_verbose.txt", VERBOSE);
116 streams.add_ofstream("/Users/ag1498/Desktop/loggertest_strict.txt", STRICT);
117
118 int a = 5; std::string b = "<string-text>";
119 streams << priority(VERBOSE);
120 streams << "hello 1 " << a << ", " << b << std::endl;
121 streams << "hello 2 " << a << ", " << b << std::endl;
122 streams << "body" << std::setw(3) << std::setfill('0') << 1 << std::endl;
123 streams << "body" << std::setw(3) << std::setfill('0') << 12 << std::endl;
124 streams << "body" << std::setw(3) << std::setfill('0') << 123 << std::endl;
125 streams << priority(VERBOSE) << "prio at HIGH..." << std::endl;
126 streams << priority(STRICT) << "prio at STRICT..." << std::endl;
127
128 // Output
129 /*
130 hello 1 5, string-text
131 hello 2 5, string-text
132 body001
133 body012
134 body123
135 priority = 0
136 prio at 0...
137 priority = 1
138 prio at 1...
139 priority = 2
140 prio at 2...
141
142 */
143}
144#endif
145
146#if 0
147struct logstrm_t
148{
149 bool strm_stdout, strm_file;
150 enum { MODE_STDOUT, MODE_FILE, MODE_STDOUT_FILE };
151 std::ofstream outstrm;
152
153 logstrm_t(unsigned mode, const std::string& file = "")
154 {
155 strm_stdout = (mode != MODE_FILE);
156 if (mode != MODE_STDOUT) {
157 outstrm.open(file, std::ios::out);
158 strm_file = outstrm.is_open();
159 }
160 }
161
162 template<class T>
163 logstrm_t& operator << (const T& data)
164 {
165 if (strm_stdout) std::cout << data;
166 if (strm_file) strm_file << data;
167 }
168
169 ~logstrm_t() { if (outstrm.is_open()) outstrm.close(); }
170
171} logstrm = {logstrm_t::MODE_FILE, ""};
172#endif
173
174#endif /* streamlogger_h */
Distributes log output to multiple stream with priority.
Definition logstreamer.h:19
Definition logstreamer.h:30