|
| 1 | +/** |
| 2 | + * @file Logger.cpp |
| 3 | + * @brief Contains definition of Logger class. |
| 4 | + * @author Slaven Peles <[email protected]> |
| 5 | + */ |
| 6 | + |
| 7 | +#include "Logger.hpp" |
| 8 | + |
| 9 | +#include <Utilities/Colors.hpp> |
| 10 | + |
| 11 | +namespace GridKit |
| 12 | +{ |
| 13 | + namespace Utilities |
| 14 | + { |
| 15 | + /// @brief Default verbosity is to print error and warning messages |
| 16 | + Logger::Verbosity Logger::verbosity_ = Logger::WARNINGS; |
| 17 | + |
| 18 | + /// @brief Default output is standard output |
| 19 | + std::ostream* Logger::logger_ = &std::cout; |
| 20 | + |
| 21 | + /// @brief User provided output file stream |
| 22 | + std::ofstream Logger::file_; |
| 23 | + |
| 24 | + /// @brief Stream to null device |
| 25 | + std::ostream Logger::nullstream_(nullptr); |
| 26 | + |
| 27 | + /// @brief Auxiliary vector of output streams |
| 28 | + std::vector<std::ostream*> Logger::tmp_; |
| 29 | + |
| 30 | + /// @brief Vector of different output streams |
| 31 | + std::vector<std::ostream*> Logger::output_streams_(Logger::init()); |
| 32 | + |
| 33 | + /** |
| 34 | + * @brief Sets verbosity level |
| 35 | + * |
| 36 | + * @pre `output_streams_` vector is allocated |
| 37 | + * @post Verbosity level is set to user supplied value `v` and outputs |
| 38 | + * for `output_streams_` are set accordingly. |
| 39 | + */ |
| 40 | + void Logger::setVerbosity(Verbosity v) |
| 41 | + { |
| 42 | + verbosity_ = v; |
| 43 | + updateVerbosity(output_streams_); |
| 44 | + } |
| 45 | + |
| 46 | + /// @brief Gets verbosity level |
| 47 | + Logger::Verbosity Logger::verbosity() |
| 48 | + { |
| 49 | + return verbosity_; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @brief Private method to update verbosity. |
| 54 | + * |
| 55 | + * This function directs each output stream <= `verbosity_` to user |
| 56 | + * selected output and sets all others to null device. Each output stream |
| 57 | + * corresponds to different verbosity level. |
| 58 | + * |
| 59 | + * @param[in] output_streams - vector of pointers to output streams |
| 60 | + * |
| 61 | + * @pre Vector `output_streams` is allocated and correctly initialized. |
| 62 | + * @post All streams `output_stream_[i]`, where `i <= verbosity_` are |
| 63 | + * directed to stream `logger_`. The rest are sent to null device |
| 64 | + * (not printed). |
| 65 | + */ |
| 66 | + void Logger::updateVerbosity(std::vector<std::ostream*>& output_streams) |
| 67 | + { |
| 68 | + for (std::size_t i = NONE; i <= EVERYTHING; ++i) |
| 69 | + { |
| 70 | + output_streams[i] = i > verbosity_ ? &nullstream_ : logger_; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @brief Delivers default values for output streams. |
| 76 | + */ |
| 77 | + std::vector<std::ostream*>& Logger::init() |
| 78 | + { |
| 79 | + tmp_.resize(Logger::EVERYTHING + 1); |
| 80 | + updateVerbosity(tmp_); |
| 81 | + return tmp_; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @brief Returns reference to output stream for error messages. |
| 86 | + * |
| 87 | + * @return Reference to error messages stream in `output_streams_`. |
| 88 | + * |
| 89 | + * @pre `output_streams_` vector is allocated and correctly initialized. |
| 90 | + */ |
| 91 | + std::ostream& Logger::error() |
| 92 | + { |
| 93 | + using namespace Colors; |
| 94 | + *(output_streams_[ERRORS]) << "[" << RED << "ERROR" << CLEAR << "] "; |
| 95 | + return *(output_streams_[ERRORS]); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @brief Returns reference to output stream for warning messages. |
| 100 | + * |
| 101 | + * @return Reference to warning messages stream in `output_streams_`. |
| 102 | + * |
| 103 | + * @pre `output_streams_` vector is allocated and correctly initialized. |
| 104 | + */ |
| 105 | + std::ostream& Logger::warning() |
| 106 | + { |
| 107 | + using namespace Colors; |
| 108 | + *(output_streams_[WARNINGS]) << "[" << YELLOW << "WARNING" << CLEAR << "] "; |
| 109 | + return *(output_streams_[WARNINGS]); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @brief Returns reference to analysis summary messages output stream. |
| 114 | + * |
| 115 | + * @return Reference to analysis summary messages stream in `output_streams_`. |
| 116 | + * |
| 117 | + * @pre `output_streams_` vector is allocated and correctly initialized. |
| 118 | + */ |
| 119 | + std::ostream& Logger::summary() |
| 120 | + { |
| 121 | + *(output_streams_[SUMMARY]) << "[SUMMARY] "; |
| 122 | + return *(output_streams_[SUMMARY]); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @brief Returns reference to output stream for all other messages. |
| 127 | + * |
| 128 | + * @return Reference to output stream to miscellaneous messages |
| 129 | + * in `output_streams_`. |
| 130 | + * |
| 131 | + * @pre `output_streams_` vector is allocated and correctly initialized. |
| 132 | + */ |
| 133 | + std::ostream& Logger::misc() |
| 134 | + { |
| 135 | + *(output_streams_[EVERYTHING]) << "[MESSAGE] "; |
| 136 | + return *(output_streams_[EVERYTHING]); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * @brief Open file `filename` and update outputs for different verbosities |
| 141 | + * streams. |
| 142 | + * |
| 143 | + * @param[in] filename - The name of the output file. |
| 144 | + * |
| 145 | + * @pre `output_streams_` vector is allocated and correctly initialized. |
| 146 | + * @post All active streams are directed to user supplied file `filename`. |
| 147 | + */ |
| 148 | + void Logger::openOutputFile(std::string filename) |
| 149 | + { |
| 150 | + file_.open(filename); |
| 151 | + logger_ = &file_; |
| 152 | + updateVerbosity(output_streams_); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * @brief Set outputs of active streams to user provided `std::ostream` object. |
| 157 | + * |
| 158 | + * All active outputs are redirected to `out` stream. All inactive ones are |
| 159 | + * directed to null device. |
| 160 | + * |
| 161 | + * @param[in] out - User provided output stream. |
| 162 | + * |
| 163 | + * @pre `output_streams_` vector is allocated and correctly initialized. |
| 164 | + * @post All active streams (`output_streams_[i]` where `i <= verbosity_`) |
| 165 | + * are set to user provided `out` output stream. |
| 166 | + */ |
| 167 | + void Logger::setOutput(std::ostream& out) |
| 168 | + { |
| 169 | + logger_ = &out; |
| 170 | + updateVerbosity(output_streams_); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * @brief Close output file. |
| 175 | + * |
| 176 | + * @pre Output file `file_` has been opened. |
| 177 | + * @post Output file `file_` is closed and active output streams are |
| 178 | + * set to default output `std::cout`. |
| 179 | + */ |
| 180 | + void Logger::closeOutputFile() |
| 181 | + { |
| 182 | + file_.close(); |
| 183 | + logger_ = &std::cout; |
| 184 | + updateVerbosity(output_streams_); |
| 185 | + } |
| 186 | + } // namespace Utilities |
| 187 | +} // namespace GridKit |
0 commit comments