Skip to content

Commit

Permalink
Change global pattern for logger;
Browse files Browse the repository at this point in the history
Add function for pretty print bool and casting to bool objects
  • Loading branch information
muratovv committed Aug 6, 2017
1 parent 87bf391 commit 05bcf5d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
8 changes: 6 additions & 2 deletions libs/logger/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ namespace logger {

std::string input(const std::string &string) { return red("<--- " + string); }

void setGlobalPattern(){
spdlog::set_pattern("[%H:%M:%S] [%n] [th: %t] [%l] << %v");
void setGlobalPattern() {
spdlog::set_pattern("[%H:%M:%S][th: %t][%l] [%n] << %v");
}

std::shared_ptr<spdlog::logger> createLogger(const std::string &tag) {
Expand All @@ -52,4 +52,8 @@ namespace logger {
return logger;
}

std::string boolRepr(bool value) {
return value ? "true" : "false";
}

} // namespace logger
18 changes: 18 additions & 0 deletions libs/logger/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ namespace logger {

std::shared_ptr<spdlog::logger> log(const std::string &tag);

/**
* Convert bool value to human readable string repr
* @param value value for transformation
* @return "true" or "false"
*/
std::string boolRepr(bool value);

/**
* Converts object to bool and provides string repr of it
* @tparam T - type of object, T must implement bool operator
* @param val - value for convertation
* @return string representation of bool object
*/
template <typename T>
std::string logBool(T val) {
return boolRepr(bool(val));
}

} // namespace logger

#endif
1 change: 0 additions & 1 deletion test/module/irohad/logger/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
AddTest(logger_test logger_test.cpp)
target_link_libraries(logger_test
logger
spdlog
)

10 changes: 10 additions & 0 deletions test/module/irohad/logger/logger_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,13 @@ TEST(LoggerTest, getLoggerTest) {
another_logger->info(logger::yellow(
"color args output {} // note: require char *").c_str(), "=^._.^=");
}

TEST(LoggerTest, boolReprTest) {
ASSERT_EQ("true", logger::boolRepr(true));
ASSERT_EQ("false", logger::boolRepr(false));
}

TEST(LoggerTest, logBoolTest) {
ASSERT_EQ("true", logger::logBool(1));
ASSERT_EQ("false", logger::boolRepr(nullptr));
}

0 comments on commit 05bcf5d

Please sign in to comment.