FINAL CUT
flogger.h
1 /***********************************************************************
2 * flogger.h - The FINAL CUT text 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  * ▕▔▔▔▔▔▔▔▔▔▏
37  * ▕ FLogger ▏
38  * ▕▁▁▁▁▁▁▁▁▁▏
39  */
40 
41 #ifndef FLOGGER_H
42 #define FLOGGER_H
43 
44 #if !defined (USE_FINAL_H) && !defined (COMPILE_FINAL_CUT)
45  #error "Only <final/final.h> can be included directly."
46 #endif
47 
48 #include <chrono>
49 #include <functional>
50 #include <iomanip>
51 #include <iostream>
52 #include <sstream>
53 #include <string>
54 
55 #include "final/util/flog.h"
56 
57 namespace finalcut
58 {
59 
60 //----------------------------------------------------------------------
61 // class FLogger
62 //----------------------------------------------------------------------
63 
64 class FLogger : public FLog
65 {
66  public:
67  // Constructor
68  FLogger() = default;
69 
70  // Destructor
71  ~FLogger() noexcept override;
72 
73  // Methods
74  auto getClassName() const -> FString override;
75  void info (const std::string&) override;
76  void warn (const std::string&) override;
77  void error (const std::string&) override;
78  void debug (const std::string&) override;
79  void flush() override;
80  void setOutputStream (const std::ostream&) override;
81  void setLineEnding (LineEnding) override;
82  void enableTimestamp() override;
83  void disableTimestamp() override;
84 
85  private:
86  // Methods
87  void newlineReplace (std::string&, const std::string&) const;
88  auto getTimeString() const -> std::string;
89  auto getEOL() const -> std::string;
90  void printLogLine (const std::string&);
91  auto getLogLevelString() const -> std::string;
92  auto getPrefixString (const std::string&) -> std::string;
93 
94  // Data member
95  bool timestamp{false};
96  std::mutex print_mutex{};
97  std::mutex output_mutex{};
98  std::ostream output{std::cerr.rdbuf()};
99 };
100 
101 // FLogger inline functions
102 //----------------------------------------------------------------------
103 inline auto FLogger::getClassName() const -> FString
104 { return "FLogger"; }
105 
106 //----------------------------------------------------------------------
107 inline void FLogger::info (const std::string& msg)
108 {
109  {
110  std::lock_guard<std::mutex> lock_guard(print_mutex);
111  setLevel() = LogLevel::Info;
112  // Release mutex at end of scope
113  }
114  printLogLine (msg);
115 }
116 
117 //----------------------------------------------------------------------
118 inline void FLogger::warn (const std::string& msg)
119 {
120  {
121  std::lock_guard<std::mutex> lock_guard(print_mutex);
122  setLevel() = LogLevel::Warn;
123  // Release mutex at end of scope
124  }
125  printLogLine (msg);
126 }
127 
128 //----------------------------------------------------------------------
129 inline void FLogger::error (const std::string& msg)
130 {
131  {
132  std::lock_guard<std::mutex> lock_guard(print_mutex);
133  setLevel() = LogLevel::Error;
134  // Release mutex at end of scope
135  }
136  printLogLine (msg);
137 }
138 
139 //----------------------------------------------------------------------
140 inline void FLogger::debug (const std::string& msg)
141 {
142  {
143  std::lock_guard<std::mutex> lock_guard(print_mutex);
144  setLevel() = LogLevel::Debug;
145  // Release mutex at end of scope
146  }
147  printLogLine (msg);
148 }
149 
150 //----------------------------------------------------------------------
151 inline void FLogger::flush()
152 {
153  std::lock_guard<std::mutex> lock_guard(output_mutex);
154  output.flush();
155 }
156 
157 //----------------------------------------------------------------------
158 inline void FLogger::setOutputStream (const std::ostream& os)
159 {
160  std::lock_guard<std::mutex> lock_guard(output_mutex);
161  output.rdbuf(os.rdbuf());
162 }
163 
164 //----------------------------------------------------------------------
165 inline void FLogger::setLineEnding (LineEnding eol)
166 {
167  std::lock_guard<std::mutex> lock_guard(print_mutex);
168  setEnding() = eol;
169 }
170 
171 //----------------------------------------------------------------------
172 inline void FLogger::enableTimestamp()
173 {
174  std::lock_guard<std::mutex> lock_guard(print_mutex);
175  timestamp = true;
176 }
177 
178 //----------------------------------------------------------------------
179 inline void FLogger::disableTimestamp()
180 {
181  std::lock_guard<std::mutex> lock_guard(print_mutex);
182  timestamp = false;
183 }
184 
185 } // namespace finalcut
186 
187 #endif // FLOGGER_H
Definition: flog.h:59
Definition: class_template.cpp:25
Definition: fstring.h:79
Definition: flogger.h:64