Skip to content

Commit 4104e7e

Browse files
committed
Introduce Logger class with tests
1 parent 8327938 commit 4104e7e

File tree

10 files changed

+528
-16
lines changed

10 files changed

+528
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- Upgraded to Spack 1.0.0.
2929
- Added `GenClassical` as supported device class in JSON parser.
3030
- Added more context information to JSON parser errors.
31+
- Added `Logger` class.
3132

3233
## v0.1
3334

src/Utilities/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ add_library(Utilities INTERFACE)
22
target_include_directories(Utilities INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
33

44
add_library(GRIDKIT::Utilities ALIAS Utilities)
5+
6+
add_subdirectory(Logger)

src/Utilities/Colors.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
namespace GridKit
4+
{
5+
namespace Utilities
6+
{
7+
namespace Colors
8+
{
9+
// must be const pointer and const dest for
10+
// const string declarations to pass -Wwrite-strings
11+
static const char* const RED = "\033[1;31m";
12+
static const char* const GREEN = "\033[1;32m";
13+
static const char* const YELLOW = "\033[33;1m";
14+
static const char* const BLUE = "\033[34;1m";
15+
static const char* const ORANGE = "\u001b[38;5;208m";
16+
static const char* const CLEAR = "\033[0m";
17+
} // namespace Colors
18+
} // namespace Utilities
19+
} // namespace GridKit
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
gridkit_add_library(utilities_logger
2+
SOURCES
3+
Logger.cpp
4+
OUTPUT_NAME
5+
gridkit_utilities_logger)
6+
7+
target_link_libraries(Utilities INTERFACE GRIDKIT::utilities_logger)

src/Utilities/Logger/Logger.cpp

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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

src/Utilities/Logger/Logger.hpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @file Logger.hpp
3+
*/
4+
5+
#pragma once
6+
7+
#include <fstream>
8+
#include <iostream>
9+
#include <vector>
10+
11+
namespace GridKit
12+
{
13+
namespace Utilities
14+
{
15+
/**
16+
* @brief Manages and logs outputs from GridKit code.
17+
*
18+
* All methods and data in this class are static.
19+
*
20+
*/
21+
class Logger
22+
{
23+
public:
24+
/// Enum specifying verbosity level for the output.
25+
enum Verbosity
26+
{
27+
NONE = 0,
28+
ERRORS,
29+
WARNINGS,
30+
SUMMARY,
31+
EVERYTHING
32+
};
33+
34+
// All methods and data are static so delete constructor and destructor.
35+
Logger() = delete;
36+
~Logger() = delete;
37+
38+
static std::ostream& error();
39+
static std::ostream& warning();
40+
static std::ostream& summary();
41+
static std::ostream& misc();
42+
43+
static void setOutput(std::ostream& out);
44+
static void openOutputFile(std::string filename);
45+
static void closeOutputFile();
46+
static void setVerbosity(Verbosity v);
47+
static Verbosity verbosity();
48+
49+
static std::vector<std::ostream*>& init();
50+
51+
private:
52+
static void updateVerbosity(std::vector<std::ostream*>& output_streams);
53+
54+
private:
55+
static std::ostream nullstream_;
56+
static std::ofstream file_;
57+
static std::ostream* logger_;
58+
static std::vector<std::ostream*> output_streams_;
59+
static std::vector<std::ostream*> tmp_;
60+
static Verbosity verbosity_;
61+
};
62+
} // namespace Utilities
63+
} // namespace GridKit

src/Utilities/TestHelpers.hpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,10 @@
1313
#include <iostream>
1414
#include <limits>
1515

16+
#include <Utilities/Colors.hpp>
17+
1618
namespace GridKit
1719
{
18-
19-
namespace Colors
20-
{
21-
// must be const pointer and const dest for
22-
// const string declarations to pass -Wwrite-strings
23-
static const char* const RED = "\033[1;31m";
24-
static const char* const GREEN = "\033[1;32m";
25-
static const char* const YELLOW = "\033[33;1m";
26-
static const char* const BLUE = "\033[34;1m";
27-
static const char* const ORANGE = "\u001b[38;5;208m";
28-
static const char* const CLEAR = "\033[0m";
29-
} // namespace Colors
30-
3120
namespace Testing
3221
{
3322
enum TestOutcome
@@ -95,7 +84,7 @@ namespace GridKit
9584

9685
TestOutcome report(const char* funcname)
9786
{
98-
using namespace Colors;
87+
using namespace Utilities::Colors;
9988

10089
if (expectFailure_)
10190
{
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
add_executable(test_case_format runCaseFormatTests.cpp)
2-
target_include_directories(test_case_format PRIVATE ${GRIDKIT_THIRD_PARTY_DIR}/nlohmann-json/single_include ${GRIDKIT_THIRD_PARTY_DIR}/magic-enum/include)
2+
target_include_directories(test_case_format
3+
PRIVATE
4+
${GRIDKIT_THIRD_PARTY_DIR}/nlohmann-json/single_include
5+
${GRIDKIT_THIRD_PARTY_DIR}/magic-enum/include)
36

4-
add_test(NAME UtilitiesCaseFormatTest COMMAND $<TARGET_FILE:test_case_format>)
7+
add_executable(test_logger runLoggerTests.cpp)
8+
target_link_libraries(test_logger PRIVATE GRIDKIT::utilities_logger)
9+
10+
add_test(NAME UtilitiesCaseFormatTest COMMAND test_case_format)
11+
add_test(NAME UtilitiesLoggerTest COMMAND test_logger)
512

613
install(TARGETS test_case_format)
14+
install(TARGETS test_logger)

0 commit comments

Comments
 (0)