FINAL CUT
flog.h
1 /***********************************************************************
2 * flog.h - Interface of the FINAL CUT logger *
3 * *
4 * This file is part of the FINAL CUT widget toolkit *
5 * *
6 * Copyright 2020-2023 Markus Gans *
7 * *
8 * FINAL CUT is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation; either version 3 of *
11 * the License, or (at your option) any later version. *
12 * *
13 * FINAL CUT is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this program. If not, see *
20 * <http://www.gnu.org/licenses/>. *
21 ***********************************************************************/
22 
23 /* Inheritance diagram
24  * ═══════════════════
25  *
26  * ▕▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▏
27  * ▕ std::stringbuf ▏
28  * ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▏
29  * ▲
30  * │
31  * ▕▔▔▔▔▔▔▏
32  * ▕ FLog ▏
33  * ▕▁▁▁▁▁▁▏
34  */
35 
36 #ifndef FLOG_H
37 #define FLOG_H
38 
39 #if !defined (USE_FINAL_H) && !defined (COMPILE_FINAL_CUT)
40  #error "Only <final/final.h> can be included directly."
41 #endif
42 
43 #include <functional>
44 #include <iostream>
45 #include <mutex>
46 #include <ostream>
47 #include <sstream>
48 #include <string>
49 
50 #include <final/util/fstring.h>
51 
52 namespace finalcut
53 {
54 
55 //----------------------------------------------------------------------
56 // class FLog
57 //----------------------------------------------------------------------
58 
59 class FLog : public std::stringbuf
60 {
61  public:
62  // Using-declaration
63  using FLogPrint = std::function<void(const std::string&)>;
64  using IOManip = decltype(std::endl<char, std::char_traits<char>>);
65 
66  // Enumerations
67  enum class LogLevel
68  {
69  Info, Warn, Error, Debug
70  };
71 
72  enum class LineEnding
73  {
74  LF, CR, CRLF
75  };
76 
77  // Constructor
78  FLog() = default;
79 
80  // Destructor
81  ~FLog() override;
82 
83  template <typename T>
84  auto operator << (const T&) -> FLog&;
85  auto operator << (IOManip) -> FLog&;
86  auto operator << (LogLevel) -> FLog&;
87 
88  virtual auto getClassName() const -> FString;
89  virtual void info (const std::string&) = 0;
90  virtual void warn (const std::string&) = 0;
91  virtual void error (const std::string&) = 0;
92  virtual void debug (const std::string&) = 0;
93  virtual void flush() = 0;
94  virtual void setOutputStream (const std::ostream&) = 0;
95  virtual void setLineEnding (LineEnding) = 0;
96  virtual void enableTimestamp() = 0;
97  virtual void disableTimestamp() = 0;
98 
99  protected:
100  auto sync() -> int override;
101  auto getLevel() const -> const LogLevel&;
102  auto setLevel() -> LogLevel&;
103  auto getEnding() const -> const LineEnding&;
104  auto setEnding() -> LineEnding&;
105 
106  private:
107  // Data member
108  LogLevel level{LogLevel::Info};
109  LineEnding end_of_line{LineEnding::CRLF};
110  FLogPrint current_log{ [this] (const auto& s) { info(s); } };
111  std::mutex current_log_mutex{};
112  std::mutex stream_mutex{};
113  std::ostream stream{this};
114 
115  // Friend Non-member operator functions
116  friend auto operator << (std::ostream&, LogLevel) -> std::ostream&;
117 };
118 
119 // non-member function forward declarations
120 //----------------------------------------------------------------------
121 void handleOutOfRangeError (const std::out_of_range& ex);
122 
123 // FLog inline functions
124 //----------------------------------------------------------------------
125 template <typename T>
126 inline auto FLog::operator << (const T& s) -> FLog&
127 {
128  std::lock_guard<std::mutex> lock_guard(stream_mutex);
129  stream << s;
130  return *this;
131 }
132 
133 //----------------------------------------------------------------------
134 inline auto FLog::operator << (IOManip pf) -> FLog&
135 {
136  std::lock_guard<std::mutex> lock_guard(stream_mutex);
137  pf(stream);
138  return *this;
139 }
140 
141 //----------------------------------------------------------------------
142 inline auto FLog::getClassName() const -> FString
143 {
144  return "FLog";
145 }
146 
147 //----------------------------------------------------------------------
148 inline auto FLog::getLevel() const -> const LogLevel&
149 {
150  return level;
151 }
152 
153 //----------------------------------------------------------------------
154 inline auto FLog::setLevel() -> LogLevel&
155 {
156  return level;
157 }
158 
159 //----------------------------------------------------------------------
160 inline auto FLog::getEnding() const -> const LineEnding&
161 {
162  return end_of_line;
163 }
164 
165 //----------------------------------------------------------------------
166 inline auto FLog::setEnding() -> LineEnding&
167 {
168  return end_of_line;
169 }
170 
171 } // namespace finalcut
172 
173 #endif // FLOG_H
Definition: flog.h:59
Definition: class_template.cpp:25
Definition: fstring.h:79