6 #ifndef UTILS_LOG_WRITER_H 7 #define UTILS_LOG_WRITER_H 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> 20 using LogEntryNotifier = DelegatedNotifier<const LogEntry::Type>;
21 using ProgressNotifier = DelegatedNotifier<const ProgressPercentage>;
45 percentage_ = enable_percentage ? std::optional<float>(0.0f) : std::nullopt;
52 std::lock_guard<std::mutex> lock(percentage_access_);
63 std::lock_guard<std::mutex> lock(percentage_access_);
73 LogWriter &operator<<(const std::shared_ptr<LogEntry> entry);
82 template <
typename ...Params>
89 return AddEntry(LogEntry::Type::info, message, caller_name);
93 LogWriter &
Warn(
const std::string &message,
const std::string &caller_name)
95 return AddEntry(LogEntry::Type::warning, message, caller_name);
99 LogWriter &
Fail(
const std::string &message,
const std::string &caller_name)
101 return AddEntry(LogEntry::Type::error, message, caller_name);
107 return AddEntry(LogEntry::Type::critical, message, caller_name);
114 return AddEntry(LogEntry::Type::info, message, caller->GetClassName());
121 return AddEntry(LogEntry::Type::warning, message, caller->GetClassName());
128 return AddEntry(LogEntry::Type::error, message, caller->GetClassName());
135 return AddEntry(LogEntry::Type::critical, message, caller->GetClassName());
146 std::lock_guard<std::mutex> lock(time_measurement_access_);
147 time_logger_.MeasureStart(measurement_name);
153 std::lock_guard<std::mutex> lock(time_measurement_access_);
154 std::stringstream ss;
155 time_logger_.MeasureEnd(&ss);
157 const auto ss_str = ss.str();
160 Inform(ss.str(),
"Time measurement");
177 if (!unread_log.empty())
179 const auto entry_messages =
std_ext::Transformed(unread_log, [](
const auto & entry) {
return entry->ToString(); });
181 const std::wstring message_wstr(message_str.begin(), message_str.end());
185 ShowMessageBox(message_wstr, L
"Warning");
189 std::cout << (L
"Warning : " + message_wstr).c_str() << std::endl;
193 return !unread_log.empty();
201 mutable std::mutex time_measurement_access_;
206 inline LogWriter &utils::LogWriter::operator<<(const std::shared_ptr<LogEntry> entry)
213 return Add(std::make_shared<LogEntry>(std::move(entry)));
224 *(output_stream_) << entry->ToString() <<
"\n";
231 template <
typename ...Params>
234 return Add(
LogEntry(std::forward<Params>(params)...));
239 for (
const auto &entry : other.
ViewUnread(utils::LogEntry::GetIWECTypes()))
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's has_value() function.
Definition: StrongOptionalType.h:37
const ValueType & Value() const
Proxy function to access the underlying optional's value() function.
Definition: StrongOptionalType.h:39