Utilities
Utilities header only library
LogWriter.h
1 /*
2  Copyright (C) Skeletex Research, s.r.o. - All Rights Reserved
3  Unauthorized copying of this file, via any medium is strictly prohibited
4  Proprietary and confidential
5 */
6 #ifndef UTILS_LOG_WRITER_H
7 #define UTILS_LOG_WRITER_H
8 
9 #include <Utils/Time.h>
10 #include <Utils/ExtGLM.h>
11 #include <Utils/LogReader.h>
12 #include <Utils/Notifier.h>
13 #include <Utils/Filesystem.h>
14 #include <Utils/NamedClass.h>
15 
16 
17 namespace utils
18 {
19 
20  using LogEntryNotifier = DelegatedNotifier<const LogEntry::Type>;
21  using ProgressNotifier = DelegatedNotifier<const ProgressPercentage>;
22 
26  class LogWriter
27  : public LogReader
28  , public LogEntryNotifier
29  , public ProgressNotifier
30  {
31  public:
32  LogWriter() = default;
33  ~LogWriter() = default;
34 
35  LogWriter(const LogWriter &) = delete;
36  LogWriter &operator=(const LogWriter &) = delete;
37  LogWriter(utils::LogWriter &&) = delete;
38  LogWriter &operator=(utils::LogWriter &&) = delete;
39 
40 
41 
43  void ResetPercentage(bool enable_percentage = true)
44  {
45  percentage_ = enable_percentage ? std::optional<float>(0.0f) : std::nullopt;
46  ProgressNotifier::Notify(percentage_);
47  }
48 
50  void SetPercentage(float value)
51  {
52  std::lock_guard<std::mutex> lock(percentage_access_);
53  percentage_ = glm_ext::Clamp01(value);
54 
55  ProgressNotifier::Notify(percentage_);
56  }
57 
59  LogWriter &operator+=(const float percentage_increment)
60  {
61  if (percentage_.HasValue())
62  {
63  std::lock_guard<std::mutex> lock(percentage_access_);
64  percentage_ = glm_ext::Clamp01(percentage_.Value() + percentage_increment);
65 
66  ProgressNotifier::Notify(percentage_);
67  }
68  return *this;
69  }
70 
71 
73  LogWriter &operator<<(const std::shared_ptr<LogEntry> entry);
74 
76  LogWriter &Add(const LogEntry &&entry);
77 
79  LogWriter &Add(const std::shared_ptr<LogEntry> entry);
80 
82  template <typename ...Params>
83  LogWriter &AddEntry(Params &&... params);
84 
85 
87  LogWriter &Inform(const std::string &message, const std::string &caller_name)
88  {
89  return AddEntry(LogEntry::Type::info, message, caller_name);
90  }
91 
93  LogWriter &Warn(const std::string &message, const std::string &caller_name)
94  {
95  return AddEntry(LogEntry::Type::warning, message, caller_name);
96  }
97 
99  LogWriter &Fail(const std::string &message, const std::string &caller_name)
100  {
101  return AddEntry(LogEntry::Type::error, message, caller_name);
102  }
103 
105  LogWriter &Crash(const std::string &message, const std::string &caller_name)
106  {
107  return AddEntry(LogEntry::Type::critical, message, caller_name);
108  }
109 
111  template<class CT>
112  LogWriter &Inform(const std::string &message, const utils::NamedClass<CT> *caller)
113  {
114  return AddEntry(LogEntry::Type::info, message, caller->GetClassName());
115  }
116 
118  template<class CT>
119  LogWriter &Warn(const std::string &message, const utils::NamedClass<CT> *caller)
120  {
121  return AddEntry(LogEntry::Type::warning, message, caller->GetClassName());
122  }
123 
125  template<class CT>
126  LogWriter &Fail(const std::string &message, const utils::NamedClass<CT> *caller)
127  {
128  return AddEntry(LogEntry::Type::error, message, caller->GetClassName());
129  }
130 
132  template<class CT>
133  LogWriter &Crash(const std::string &message, const utils::NamedClass<CT> *caller)
134  {
135  return AddEntry(LogEntry::Type::critical, message, caller->GetClassName());
136  }
137 
138 
140  LogWriter &CopyUnreadEntries(const LogReader &other);
141 
142 
144  void StartTimeMeasure(const std::string &measurement_name)
145  {
146  std::lock_guard<std::mutex> lock(time_measurement_access_);
147  time_logger_.MeasureStart(measurement_name);
148  }
149 
152  {
153  std::lock_guard<std::mutex> lock(time_measurement_access_);
154  std::stringstream ss;
155  time_logger_.MeasureEnd(&ss);
156 
157  const auto ss_str = ss.str();
158  if(!ss_str.empty())
159  {
160  Inform(ss.str(), "Time measurement");
161  }
162  }
163 
165  bool SaveToFile(const std::string &file_path)
166  {
167  std::lock_guard<std::mutex> lock(log_access_);
168  const auto entry_messages = std_ext::Transformed(log_entries_, [](const auto & entry) { return entry->ToString(); });
169  fs::StringToFile(file_path, std_ext::ToString(entry_messages, "\n"));
170  return true;
171  }
172 
174  bool DisplayUnread(const utils::EnumFlags<utils::LogEntry::Type> &types, const std::function<void(const std::wstring &, const std::wstring &)> &ShowMessageBox = nullptr)
175  {
176  const auto unread_log = ReadUnread(types);
177  if (!unread_log.empty())
178  {
179  const auto entry_messages = std_ext::Transformed(unread_log, [](const auto & entry) { return entry->ToString(); });
180  const std::string message_str = std_ext::ToString(entry_messages, "\n");
181  const std::wstring message_wstr(message_str.begin(), message_str.end());
182 
183  if (ShowMessageBox)
184  {
185  ShowMessageBox(message_wstr, L"Warning");
186  }
187  else
188  {
189  std::cout << (L"Warning : " + message_wstr).c_str() << std::endl;
190  }
191  }
192 
193  return !unread_log.empty();
194  }
195 
196 
197  private:
199  time_m::TimeLogger time_logger_;
201  mutable std::mutex time_measurement_access_;
202  };
203 
204 
205 
206  inline LogWriter &utils::LogWriter::operator<<(const std::shared_ptr<LogEntry> entry)
207  {
208  return Add(entry);
209  }
210 
211  inline LogWriter &utils::LogWriter::Add(const LogEntry &&entry)
212  {
213  return Add(std::make_shared<LogEntry>(std::move(entry)));
214  }
215 
216  inline LogWriter &utils::LogWriter::Add(const std::shared_ptr<LogEntry> entry)
217  {
218  log_access_.lock();
219  log_entries_.emplace_back(entry);
220  log_access_.unlock();
221 
222  if (output_stream_)
223  {
224  *(output_stream_) << entry->ToString() << "\n";
225  }
226 
227  LogEntryNotifier::Notify(entry->GetType());
228  return *this;
229  }
230 
231  template <typename ...Params>
232  inline LogWriter &utils::LogWriter::AddEntry(Params &&... params)
233  {
234  return Add(LogEntry(std::forward<Params>(params)...));
235  }
236 
238  {
239  for (const auto &entry : other.ViewUnread(utils::LogEntry::GetIWECTypes()))
240  {
241  this->Add(entry);
242  }
243  return *this;
244  }
245 
246 }
247 #endif /* !UTILS_LOG_WRITER_H */
Extension to LogReader. Provide write access to the log.
Definition: LogWriter.h:26
void StringToFile(const std::string &file_name, const std::string &str)
Exports the string to a text file.
Definition: Filesystem.h:165
LogWriter & Crash(const std::string &message, const std::string &caller_name)
Add a Critical entry to the log. If output stream enabled, print to output stream.
Definition: LogWriter.h:105
bool SaveToFile(const std::string &file_path)
Save all log messages to a file.
Definition: LogWriter.h:165
LogWriter & Inform(const std::string &message, const std::string &caller_name)
Add a Info entry to the log. If output stream enabled, print to output stream.
Definition: LogWriter.h:87
std::mutex log_access_
mutex to ensure thread safety of the Log.
Definition: LogReader.h:125
An extension to Notifier - does not notify by itself. Instead it collects notifications and allows ex...
Definition: Notifier.h:69
LogWriter & Add(const LogEntry &&entry)
Add an entry to the log. If output stream enabled, print to output stream.
Definition: LogWriter.h:211
LogWriter & AddEntry(Params &&... params)
Add an entry to the log. If output stream enabled, print to output stream.
Definition: LogWriter.h:232
LogWriter & CopyUnreadEntries(const LogReader &other)
Copy all unread entries from another LogReader.
Definition: LogWriter.h:237
A simple parent class to guarantee that all who derive from it will have a human-readable name...
Definition: NamedClass.h:17
void SetPercentage(float value)
Set amount of percentage to defined value. Clamped to range <0,1>
Definition: LogWriter.h:50
void StopTimeMeasure()
End time measurements + print to log.
Definition: LogWriter.h:151
void ResetPercentage(bool enable_percentage=true)
Reset percentage value and disable it optionally.
Definition: LogWriter.h:43
A single entry for the logging system.
Definition: LogEntry.h:18
std::vector< std::shared_ptr< LogEntry > > log_entries_
Saved log entries.
Definition: LogReader.h:123
std::string ToString(const std::vector< T > &container, const std::string &separator=", ")
Converts all values in the vector to a string and joins them using specified separator.
Definition: ExtSTD.h:233
LogWriter & Inform(const std::string &message, const utils::NamedClass< CT > *caller)
Add a Info entry to the log. If output stream enabled, print to output stream. Pass caller to get its...
Definition: LogWriter.h:112
void StartTimeMeasure(const std::string &measurement_name)
Start time measurement + print to log.
Definition: LogWriter.h:144
virtual void Notify(Args... args) override
Notify does not execute all notifications - it merely collects the arguments and allows the Owner to ...
Definition: Notifier.h:74
std::vector< std::shared_ptr< LogEntry > > ViewUnread(const utils::EnumFlags< LogEntry::Type > &types=LogEntry::GetWECTypes()) const
Get all unread messages specified by their types and return them.
Definition: LogReader.h:88
LogWriter & Fail(const std::string &message, const std::string &caller_name)
Add a Error entry to the log. If output stream enabled, print to output stream.
Definition: LogWriter.h:99
genType Clamp01(genType x)
Clamps a value or individual vector components to the range 0-1.
Definition: ExtGLM.h:241
auto Transformed(const C &container, F Function)
Simplified wrapper for std::transform.
Definition: ExtSTD.h:831
Template implementation of Enum Flags. Allows the setting and checking of enum values.
Definition: EnumFlags.h:34
LogWriter & Fail(const std::string &message, const utils::NamedClass< CT > *caller)
Add a Error entry to the log. If output stream enabled, print to output stream. Pass caller to get it...
Definition: LogWriter.h:126
General namespace for utilities.
Definition: BulletConversions.h:9
LogWriter & Crash(const std::string &message, const utils::NamedClass< CT > *caller)
Add a Critical entry to the log. If output stream enabled, print to output stream. Pass caller to get its name.
Definition: LogWriter.h:133
bool DisplayUnread(const utils::EnumFlags< utils::LogEntry::Type > &types, const std::function< void(const std::wstring &, const std::wstring &)> &ShowMessageBox=nullptr)
Read all unread messages from log with the selected types and display them supplied message dialog fu...
Definition: LogWriter.h:174
std::vector< std::shared_ptr< LogEntry > > ReadUnread(const utils::EnumFlags< LogEntry::Type > &types)
Get all unread messages specified by their types, set them to read, and return them.
Definition: LogReader.h:105
LogWriter & Warn(const std::string &message, const utils::NamedClass< CT > *caller)
Add a Warning entry to the log. If output stream enabled, print to output stream. Pass caller to get ...
Definition: LogWriter.h:119
Log - accumulate typed messages reported by the application. Includes percentage value in range <0...
Definition: LogReader.h:34
Measures duration of program parts and logs times into a stream.
Definition: Time.h:140
LogWriter & Warn(const std::string &message, const std::string &caller_name)
Add a Warning entry to the log. If output stream enabled, print to output stream. ...
Definition: LogWriter.h:93
LogWriter & operator+=(const float percentage_increment)
Increment progress-bar value by an amount of percentage.
Definition: LogWriter.h:59
bool HasValue() const
Proxy function to access the underlying optional&#39;s has_value() function.
Definition: StrongOptionalType.h:37
const ValueType & Value() const
Proxy function to access the underlying optional&#39;s value() function.
Definition: StrongOptionalType.h:39