Skip to content

Commit

Permalink
Allow to disable logging information
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisTM committed Jun 6, 2024
1 parent 5870ba4 commit ec18d67
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 21 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
Expand Down
34 changes: 19 additions & 15 deletions include/cracon/cracon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
#define CRACON_CRACON_HPP

#include <cassert>
#include <cracon/similarity_traits.hpp>
#include <fstream>
#include <memory>
#include <mutex>
#include <nlohmann/json.hpp>
#include <string>

#include <cracon/log.hpp>
#include <cracon/similarity_traits.hpp>

#include <nlohmann/json.hpp>

namespace cracon {
class File {
public:
Expand Down Expand Up @@ -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<T>(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());
}
}
Expand Down Expand Up @@ -86,28 +89,29 @@ 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;
}
// 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<T>(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<T>();
} 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;
}
}
Expand Down
23 changes: 23 additions & 0 deletions include/cracon/log.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef CRACON_LOG_HPP
#define CRACON_LOG_HPP

#include <cstdio>

#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
6 changes: 3 additions & 3 deletions include/cracon/similarity_traits.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef CRACON_SIMILARITY_TRAIT
#define CRACON_SIMILARITY_TRAIT
#ifndef CRACON_SIMILARITY_TRAIT_HPP
#define CRACON_SIMILARITY_TRAIT_HPP

#include <cassert>
#include <cstdint>
Expand Down Expand Up @@ -133,4 +133,4 @@ bool is_similar(nlohmann::json const &value) {
}
} // namespace cracon

#endif // CRACON_SIMILARITY_TRAIT
#endif // CRACON_SIMILARITY_TRAIT_HPP
7 changes: 4 additions & 3 deletions src/cracon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
}
Expand Down

0 comments on commit ec18d67

Please sign in to comment.