cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
ologger.hpp
Go to the documentation of this file.
1 
17 /*
18  * Copyright 2020, arcomber, https://codereview.stackexchange.com/questions/254134/cout-style-logger-in-c
19  */
20 
21 /*
22 ologger or ostream logger, a logger as convenient as using std::cout
23 Features:
24 1. logging as per cout/cerr/clog ie logger.error << "i=" << 3 << std::endl;
25 2. logging obeys log level set. If you set log level error, then logging to info or debug is a no-op.
26 3. log rotation - specify a maximum file size in bytes and maximum number of files
27 4. Configure via path, file name prefix and file name extension.
28 
29 End a line with std::endl
30 
31 Example usage:
32  ologger logger(".", "projectz", "log", 10000, 5, ologger::log_level::LOG_INFO);
33  // nothing should be logged because we have set logger at ERROR level only
34  logger.debug << "low level developer data, X level exceeded, x=" << 9.75 << std::endl;
35  // nothing should be logged because we have set logger at ERROR level only
36  logger.info << "informational stuff about sending x protocol data to server, bytes transferred: " << 1250 << std::endl;
37  // this should be logged
38  logger.error << "!!! Invalid Data Received !!!" << std::endl;
39 */
40 
41 #ifndef OLOGGER_HPP_
42 #define OLOGGER_HPP_
43 
44 #include <string>
45 #include <iostream>
46 #include <fstream>
47 
48 class ologger {
49 public:
50  using endl_type = std::ostream& (std::ostream&);
51 
52  enum class log_level {
53  LOG_NONE,
54  LOG_ERROR,
55  LOG_INFO,
56  LOG_DEBUG
57  };
58 
59  /* Construct with log files path, file name prefix and suffix, max_file_size in bytes, max_files before rotation and logging level */
60  ologger(const std::string& path,
61  const std::string& file_prefix,
62  const std::string& file_suffix,
63  size_t max_file_size,
64  size_t max_files,
66 
67  // prevent copying object
68  ologger(const ologger&) = delete;
69  ologger(ologger&&) = delete;
70  ologger& operator=(const ologger&) = delete;
71  ologger& operator=(ologger&&) = delete;
72 
73  // debug level logging
74  class Debug {
75  public:
76  Debug(ologger& parent);
77 
78  template<typename T>
79  Debug& operator<< (const T& data)
80  {
81  if (parent_.level_ >= log_level::LOG_DEBUG) {
82  if (start_of_line_) {
83  parent_.prefix_message();
84  start_of_line_ = false;
85  }
86 
87  parent_.log_stream_ << data;
88  }
89 
90  return *this;
91  }
92 
93  Debug& operator<<(endl_type endl);
94 
95  private:
96  ologger& parent_;
97  bool start_of_line_;
98  };
99 
100  // info level logging
101  class Info {
102  public:
103  Info(ologger& parent);
104 
105  template<typename T>
106  Info& operator<< (const T& data)
107  {
108  if (parent_.level_ >= log_level::LOG_INFO) {
109  if (start_of_line_) {
110  parent_.prefix_message();
111  start_of_line_ = false;
112  }
113  parent_.log_stream_ << data;
114  }
115  return *this;
116  }
117 
118  Info& operator<<(endl_type endl);
119 
120  private:
121  ologger& parent_;
122  bool start_of_line_;
123  };
124 
125  // error level logging
126  class Error {
127  public:
128  Error(ologger& parent);
129 
130  template<typename T>
131  Error& operator<< (const T& data)
132  {
133  if (parent_.level_ >= log_level::LOG_ERROR) {
134  if (start_of_line_) {
135  parent_.prefix_message();
136  start_of_line_ = false;
137  }
138  parent_.log_stream_ << data;
139  }
140  return *this;
141  }
142 
143  Error& operator<<(endl_type endl);
144 
145  private:
146  ologger& parent_;
147  bool start_of_line_;
148  };
149 
150 private:
151  size_t changeover_if_required();
152  const std::string to_string(ologger::log_level level);
153  void prefix_message();
154  void make_logger(const std::string& path, const std::string& file_prefix, const std::string& file_suffix);
155 
156  const std::string path_;
157  const std::string file_prefix_;
158  const std::string file_suffix_;
159  size_t max_file_size_;
160  size_t max_files_;
161  log_level level_;
162 
163  std::fstream log_stream_;
164 
165 public:
169 };
170 
171 #endif // OLOGGER_HPP_
Debug(ologger &parent)
Debug & operator<<(const T &data)
Definition: ologger.hpp:79
Error(ologger &parent)
Error & operator<<(const T &data)
Definition: ologger.hpp:131
Info & operator<<(const T &data)
Definition: ologger.hpp:106
Info(ologger &parent)
ologger(const ologger &)=delete
ologger & operator=(ologger &&)=delete
Debug debug
Definition: ologger.hpp:166
Error error
Definition: ologger.hpp:168
std::ostream &(std::ostream &) endl_type
Definition: ologger.hpp:50
ologger(const std::string &path, const std::string &file_prefix, const std::string &file_suffix, size_t max_file_size, size_t max_files, log_level level=log_level::LOG_NONE)
Definition: ologger.cpp:78
ologger & operator=(const ologger &)=delete
Info info
Definition: ologger.hpp:167
ologger(ologger &&)=delete
ap_uint< 64 > data
Definition: tb_nal.cpp:832