LSLOpt  1.0
OutputUtils.hpp
1 #pragma once
2 
3 #include <ostream>
4 
5 
6 namespace LSLOpt {
7 
11 enum class OutputLevel {
12  Nothing,
13  Error,
14  Warning,
15  Status,
16  Debug,
17 };
18 
25 inline std::ostream& operator<< (std::ostream& os, const OutputLevel& level)
26 {
27  switch (level) {
28  case OutputLevel::Error:
29  os << "[ERROR ] ";
30  break;
32  os << "[WARNING ] ";
33  break;
35  os << "[STATUS ] ";
36  break;
37  case OutputLevel::Debug:
38  os << "[DEBUG ] ";
39  break;
40  default:
41  os << "[????????] ";
42  }
43 
44  return os;
45 }
46 
53 struct NoOutput {
63  template<typename T>
64  NoOutput& operator<<(const T& t)
65  {
66  return *this;
67  }
68 
70  const OutputLevel output_level = OutputLevel::Nothing;
71 };
72 
79 struct OstreamOutput {
85  OstreamOutput(OutputLevel output_level, std::ostream& os)
86  : output_level(output_level)
87  , current_level(OutputLevel::Nothing)
88  , os(os)
89  {
90 
91  }
92 
98  {
99  current_level = level;
100  output(level);
101  return *this;
102  }
103 
111  template<typename T>
113  {
114  output(t);
115  return *this;
116  }
117 
120 
121  private:
122  template<typename T>
123  void output(const T& t)
124  {
125  if (current_level != OutputLevel::Nothing &&
126  static_cast<unsigned>(current_level) <= static_cast<unsigned>(output_level)) {
127  os << t;
128  }
129  }
130 
131  OutputLevel current_level;
132  std::ostream& os;
133 };
134 
143 template<typename OutputFunction>
145  OutputFunction& output_function,
146  OutputLevel curr_level,
147  decltype(output_function.output_level)* = nullptr)
148 {
149  OutputLevel level = output_function.output_level;
150 
151  if (std::is_same<OutputFunction, NoOutput>::value) {
152  return false;
153  }
154 
155  return curr_level != OutputLevel::Nothing
156  && static_cast<unsigned>(curr_level) <= static_cast<unsigned>(level);
157 }
158 
162 inline bool is_output_enabled(...)
163 {
164  return true;
165 }
166 
167 }
show status messages
OutputLevel output_level
The current output level.
Definition: OutputUtils.hpp:119
std::ostream & operator<<(std::ostream &os, const Status &status)
Output of Status on std::ostream.
Definition: OptimizationStatus.hpp:47
OutputLevel
The output level for filtering.
Definition: OutputUtils.hpp:11
OstreamOutput & operator<<(const OutputLevel &level)
Set and output the level.
Definition: OutputUtils.hpp:97
OstreamOutput & operator<<(const T &t)
Output variable to the output stream.
Definition: OutputUtils.hpp:112
Class for the output to an std::ostream.
Definition: OutputUtils.hpp:79
BFGS optimizations.
Definition: BFGS.hpp:24
NoOutput struct.
Definition: OutputUtils.hpp:53
bool is_output_enabled(OutputFunction &output_function, OutputLevel curr_level, decltype(output_function.output_level) *=nullptr)
Definition: OutputUtils.hpp:144
OstreamOutput(OutputLevel output_level, std::ostream &os)
Construct OstreamOutput.
Definition: OutputUtils.hpp:85
NoOutput & operator<<(const T &t)
Output operator.
Definition: OutputUtils.hpp:64