Utilities
Utilities header only library
LogReader.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_READER_H
7 #define UTILS_LOG_READER_H
8 
9 #include <ostream>
10 #include <mutex>
11 
12 #include <Utils/EnumFlags.h>
13 #include <Utils/LogEntry.h>
14 #include <Utils/StrongOptionalType.h>
15 
16 
17 
18 namespace utils
19 {
20 
21  using ProgressPercentage = utils::StrongOptionalType<float, class LogReader>;
22 
34  class LogReader
35  {
36  public:
37  LogReader()
38  : percentage_(std::nullopt)
39  {}
40  ~LogReader() = default;
41 
42  LogReader(const LogReader &) = delete;
43  LogReader &operator=(const LogReader &) = delete;
44  LogReader(LogReader &&) = delete;
45  LogReader &operator=(LogReader &&) = delete;
46 
47 
48 
53  [[nodiscard]]
54  bool IsPercentageEnabled() const
55  {
56  return percentage_.HasValue();
57  }
58 
60  [[nodiscard]]
61  float GetPercentage() const
62  {
63  if (percentage_.HasValue())
64  {
65  std::lock_guard<std::mutex> lock(percentage_access_);
66  return percentage_.Value();
67  }
68  return 0.0f;
69  }
70 
75  void SetOutputStream(std::ostream *output_log_stream)
76  {
77  output_stream_ = output_log_stream;
78  }
79 
81  [[nodiscard]]
82  std::ostream *GetOutputStream()
83  {
84  return output_stream_;
85  }
86 
88  std::vector<std::shared_ptr<LogEntry>> ViewUnread(const utils::EnumFlags<LogEntry::Type> &types = LogEntry::GetWECTypes()) const
89  {
90  std::lock_guard<std::mutex> lock(log_access_);
91  return std_ext::SelectIf(log_entries_, [&types](const auto entry) { return entry != nullptr && types.IsSet(entry->GetType()) && !entry->IsRead(); });
92  }
93 
95  std::string ViewUnreadToString(const utils::EnumFlags<LogEntry::Type> &types = LogEntry::GetIWECTypes()) const
96  {
97  const auto unread = ViewUnread(types);
98  std::stringstream ss;
99  ss << unread;
100  return ss.str();
101  }
102 
104  [[nodiscard]]
105  std::vector<std::shared_ptr<LogEntry>> ReadUnread(const utils::EnumFlags<LogEntry::Type> &types)
106  {
107  const auto unread = ViewUnread(types);
108  for (const auto &entry : unread)
109  {
110  entry->Read();
111  }
112  return unread;
113  }
114 
115 
116  protected:
117  std::ostream *output_stream_ = nullptr;
118 
119  ProgressPercentage percentage_;
120  mutable std::mutex percentage_access_;
121 
123  std::vector<std::shared_ptr<LogEntry>> log_entries_;
125  mutable std::mutex log_access_;
126  };
127 
128 }
129 
130 #endif /* !UTILS_LOG_READER_H */
std::mutex log_access_
mutex to ensure thread safety of the Log.
Definition: LogReader.h:125
std::ostream * GetOutputStream()
Returns pointer to currently active output stream.
Definition: LogReader.h:82
std::vector< std::shared_ptr< LogEntry > > log_entries_
Saved log entries.
Definition: LogReader.h:123
void SetOutputStream(std::ostream *output_log_stream)
Allows user to specify custom output stream to receive verbose information about progress. Default value is nullptr.
Definition: LogReader.h:75
auto SelectIf(const C &container, const P &Predicate)
Copies all elements fulfilling a given condition into a new vector. Uses std::copy_if.
Definition: ExtSTD.h:928
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
Template implementation of Enum Flags. Allows the setting and checking of enum values.
Definition: EnumFlags.h:34
float GetPercentage() const
Returns current percentage value of progress in range 0, 1.
Definition: LogReader.h:61
General namespace for utilities.
Definition: BulletConversions.h:9
std::string ViewUnreadToString(const utils::EnumFlags< LogEntry::Type > &types=LogEntry::GetIWECTypes()) const
Get all unread messages specified by their types and return them and output as std::string.
Definition: LogReader.h:95
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
Log - accumulate typed messages reported by the application. Includes percentage value in range <0...
Definition: LogReader.h:34
bool IsPercentageEnabled() const
Returns true if percentage is currently enabled. Some processing functions may not support percentage...
Definition: LogReader.h:54
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