diff --git a/CMakeLists.txt b/CMakeLists.txt index 7cfcb32..98e112d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,9 @@ include(cmake/CPM.cmake) add_library(${PROJECT_NAME} src/cracon.cpp) add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) +if (CRACON_ENABLE_LOG) + target_compile_definitions(${PROJECT_NAME} PUBLIC CRACON_ENABLE_LOG) +endif() target_include_directories(${PROJECT_NAME} PUBLIC $ diff --git a/include/cracon/cracon.hpp b/include/cracon/cracon.hpp index 4feb1db..579724c 100644 --- a/include/cracon/cracon.hpp +++ b/include/cracon/cracon.hpp @@ -2,13 +2,16 @@ #define CRACON_CRACON_HPP #include -#include #include #include #include -#include #include +#include +#include + +#include + namespace cracon { class File { public: @@ -50,12 +53,12 @@ class File { should_write_config_ = true; val = new_value; if (val.is_null()) { - printf("[cracon] [WARN] The key didn't exist for %s\n", accessor.c_str()); + CRACON_LOG_WARNING("The key didn't exist for %s\n", accessor.c_str()); } else { if (!is_similar(val)) { - printf( - "[cracon] [WARN] The new key is not a similar type to the " - "precedent configuration: %s replaced by %s\n", + CRACON_LOG_WARNING( + "The new key is not a similar type to the precedent configuration: " + "%s replaced by %s\n", accessor.c_str(), val.dump().c_str()); } } @@ -86,8 +89,8 @@ class File { try { auto &val = config_.at(pointer); if (val.is_null()) { - printf( - "[cracon] [INFO] The requested key doesn't exist for %s defaulted " + CRACON_LOG_INFO( + "The requested key doesn't exist for %s defaulted " "to \n", default_[pointer].dump().c_str()); return default_val; @@ -95,19 +98,20 @@ class File { // This can happen if: The config file is the wrong type or the code is // using the wrong type; It is considered the code is right; if (!is_similar(val)) { - fprintf(stderr, - "[cracon] [ERROR] The read value %s is not a similar type to " - "%s at %s defaulted to %s\n", - val.dump().c_str(), typeid(T).name(), accessor.c_str(), - default_[pointer].dump().c_str()); + CRACON_LOG_ERROR( + "The read value %s is not a similar type to " + "%s at %s defaulted to %s\n", + val.dump().c_str(), typeid(T).name(), accessor.c_str(), + default_[pointer].dump().c_str()); return default_val; } return val.get(); } catch (std::exception const &ex) { - printf( - "[cracon] [INFO] The requested key doesn't exist for %s defaulted " + CRACON_LOG_INFO( + "The requested key doesn't exist for %s defaulted " "to %s. Error: %s\n", accessor.c_str(), default_[pointer].dump().c_str(), ex.what()); + (void)ex; return default_val; } } diff --git a/include/cracon/log.hpp b/include/cracon/log.hpp new file mode 100644 index 0000000..35a125e --- /dev/null +++ b/include/cracon/log.hpp @@ -0,0 +1,23 @@ +#ifndef CRACON_LOG_HPP +#define CRACON_LOG_HPP + +#include + +#ifdef CRACON_ENABLE_LOG +#define CRACON_LOG_IMPL_DEBUG(...) fprintf(stdout, __VA_ARGS__) +#define CRACON_LOG_IMPL_INFO(...) fprintf(stdout, __VA_ARGS__) +#define CRACON_LOG_IMPL_WARNING(...) fprintf(stderr, __VA_ARGS__) +#define CRACON_LOG_IMPL_ERROR(...) fprintf(stderr, __VA_ARGS__) +#else +#define CRACON_LOG_IMPL_DEBUG(...) {} +#define CRACON_LOG_IMPL_INFO(...) {} +#define CRACON_LOG_IMPL_WARNING(...) {} +#define CRACON_LOG_IMPL_ERROR(...) {} +#endif + +#define CRACON_LOG_DEBUG(...) CRACON_LOG_IMPL_DEBUG("[cracon] [DEBUG] " __VA_ARGS__) +#define CRACON_LOG_INFO(...) CRACON_LOG_IMPL_INFO("[cracon] [INFO] " __VA_ARGS__) +#define CRACON_LOG_WARNING(...) CRACON_LOG_IMPL_WARNING("[cracon] [WARN] " __VA_ARGS__) +#define CRACON_LOG_ERROR(...) CRACON_LOG_IMPL_ERROR("[cracon] [ERROR] " __VA_ARGS__) + +#endif diff --git a/include/cracon/similarity_traits.hpp b/include/cracon/similarity_traits.hpp index c198a36..116dcbd 100644 --- a/include/cracon/similarity_traits.hpp +++ b/include/cracon/similarity_traits.hpp @@ -1,5 +1,5 @@ -#ifndef CRACON_SIMILARITY_TRAIT -#define CRACON_SIMILARITY_TRAIT +#ifndef CRACON_SIMILARITY_TRAIT_HPP +#define CRACON_SIMILARITY_TRAIT_HPP #include #include @@ -133,4 +133,4 @@ bool is_similar(nlohmann::json const &value) { } } // namespace cracon -#endif // CRACON_SIMILARITY_TRAIT +#endif // CRACON_SIMILARITY_TRAIT_HPP diff --git a/src/cracon.cpp b/src/cracon.cpp index 938c0f4..0f050db 100644 --- a/src/cracon.cpp +++ b/src/cracon.cpp @@ -33,7 +33,7 @@ bool File::write_to_file(std::string const &filename, nlohmann::json const &config) { try { if (filename.empty()) { - printf("[cracon] [ERROR] The filename is not set, did you init?\n"); + CRACON_LOG_ERROR("The filename is not set, did you init?\n"); assert(false); return false; } @@ -42,8 +42,9 @@ bool File::write_to_file(std::string const &filename, output_file.close(); return true; } catch (std::exception const &ex) { - fprintf(stderr, "[Cracon] [ERROR] Error writing the file %s: %s\n", - filename.c_str(), ex.what()); + CRACON_LOG_ERROR("Error writing the file %s: %s\n", filename.c_str(), + ex.what()); + (void)ex; return false; } }