Browse Source

More adaptions for release build

Kajetan Johannes Hammerle 2 months ago
parent
commit
2e73e51e42

+ 9 - 7
CMakeLists.txt

@@ -67,14 +67,16 @@ set(SRC_PERFORMANCE
     "test/Test.cpp"
 )
 
-set(DEBUG_COMPILE "")
-set(DEBUG_LINK "")
-set(LOG_LEVEL 4)
-
-if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
+if("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
+    set(DEBUG_COMPILE "")
+    set(DEBUG_LINK "")
+    set(LOG_LEVEL 2)
+    set(ERROR_SIMULATOR "")
+else()
     set(DEBUG_COMPILE --coverage)
     set(DEBUG_LINK gcov)
     set(LOG_LEVEL 4)
+    set(ERROR_SIMULATOR "ERROR_SIMULATOR")
 endif()
 
 add_library(core STATIC ${SRC})
@@ -178,7 +180,7 @@ target_compile_options(core PUBLIC
 )
 target_compile_definitions(core 
     PUBLIC CORE_LOG_LEVEL=${LOG_LEVEL}
-    PRIVATE ERROR_SIMULATOR=true
+    PRIVATE ${ERROR_SIMULATOR}
 )
 target_link_libraries(core 
     PUBLIC -nodefaultlibs c m
@@ -231,7 +233,7 @@ install(TARGETS core FILE_SET HEADERS)
 
 add_executable(test ${SRC_TESTS})
 target_link_libraries(test PRIVATE core)
-target_compile_definitions(test PRIVATE ERROR_SIMULATOR=true)
+target_compile_definitions(test PRIVATE ${ERROR_SIMULATOR})
 
 add_executable(performance ${SRC_PERFORMANCE})
 target_link_libraries(performance PRIVATE core)

+ 30 - 9
include/core/utils/Logger.hpp

@@ -7,6 +7,12 @@ namespace Core::Logger {
     enum class Level { ERROR, WARNING, INFO, DEBUG };
     extern Level level;
 
+    [[maybe_unused]] static constexpr const char* COLOR_RED = "\33[1;31m";
+    [[maybe_unused]] static constexpr const char* COLOR_YELLOW = "\33[1;33m";
+    [[maybe_unused]] static constexpr const char* COLOR_GRAY = "\33[1;37m";
+    [[maybe_unused]] static constexpr const char* COLOR_GREEN = "\33[1;32m";
+    [[maybe_unused]] static constexpr const char* COLOR_RESET = "\33[0m";
+
     const char* getFileName(const char* path);
 
     inline bool filterError(Error e) {
@@ -15,25 +21,37 @@ namespace Core::Logger {
 
     // aborts on critical logging failure
     template<typename... Args>
-    void log(Level l, const char* file, int line, const char* start,
-             const char* format, Args&&... args) {
+    void log(Level l, const char* file, int line, const char* prefix,
+             const char* tag, const char* format, Args&&... args) {
         if(Core::Logger::level < l) {
             return;
         }
         file = getFileName(file);
         Core::String32<2048> s;
-        if(filterError(s.append(start)) || filterError(s.append("#:# | ")) ||
+        if(filterError(s.append(prefix)) || filterError(s.append(tag)) ||
+           filterError(s.append("#:# | ")) ||
            filterError(s.format(file, line)) || filterError(s.append(format)) ||
            filterError(s.format(Core::forward<Args>(args)...)) ||
-           filterError(s.append("\33[0m\n")) || filterError(s.print())) {
-            CORE_EXIT(1);
+           filterError(s.append(COLOR_RESET)) || filterError(s.printLine())) {
+            CORE_EXIT(1); // CoverageIgnore
+        }
+    }
+
+    template<typename... Args>
+    void log(const char* prefix, const char* format, Args&&... args) {
+        Core::String32<2048> s;
+        if(filterError(s.append(prefix)) || filterError(s.append(format)) ||
+           filterError(s.format(Core::forward<Args>(args)...)) ||
+           filterError(s.append(COLOR_RESET)) || filterError(s.printLine())) {
+            CORE_EXIT(1); // CoverageIgnore
         }
     }
 }
 
 #if defined(CORE_LOG_LEVEL) && CORE_LOG_LEVEL >= 1
 #define CORE_LOG_ERROR(format, ...)                                            \
-    log(Core::Logger::Level::ERROR, __FILE__, __LINE__, "\33[1;31m[ERROR] ",   \
+    log(Core::Logger::Level::ERROR, __FILE__, __LINE__,                        \
+        Core::Logger::COLOR_RED, "[ERROR] ",                                   \
         format __VA_OPT__(, ) __VA_ARGS__);
 #else
 #define CORE_LOG_ERROR(format, ...)
@@ -42,14 +60,16 @@ namespace Core::Logger {
 #if defined(CORE_LOG_LEVEL) && CORE_LOG_LEVEL >= 2
 #define CORE_LOG_WARNING(format, ...)                                          \
     log(Core::Logger::Level::WARNING, __FILE__, __LINE__,                      \
-        "\33[1;33m[WARNING] ", format __VA_OPT__(, ) __VA_ARGS__);
+        Core::Logger::COLOR_YELLOW, "[WARNING] ",                              \
+        format __VA_OPT__(, ) __VA_ARGS__);
 #else
 #define CORE_LOG_WARNING(format, ...)
 #endif
 
 #if defined(CORE_LOG_LEVEL) && CORE_LOG_LEVEL >= 3
 #define CORE_LOG_INFO(format, ...)                                             \
-    log(Core::Logger::Level::INFO, __FILE__, __LINE__, "\33[1;37m[INFO] ",     \
+    log(Core::Logger::Level::INFO, __FILE__, __LINE__,                         \
+        Core::Logger::COLOR_GRAY, "[INFO] ",                                   \
         format __VA_OPT__(, ) __VA_ARGS__);
 #else
 #define CORE_LOG_INFO(format, ...)
@@ -57,7 +77,8 @@ namespace Core::Logger {
 
 #if defined(CORE_LOG_LEVEL) && CORE_LOG_LEVEL >= 4
 #define CORE_LOG_DEBUG(format, ...)                                            \
-    log(Core::Logger::Level::DEBUG, __FILE__, __LINE__, "\33[1;32m[DEBUG] ",   \
+    log(Core::Logger::Level::DEBUG, __FILE__, __LINE__,                        \
+        Core::Logger::COLOR_GREEN, "[DEBUG] ",                                 \
         format __VA_OPT__(, ) __VA_ARGS__);
 #else
 #define CORE_LOG_DEBUG(format, ...)

+ 28 - 24
performance/Main.cpp

@@ -4,24 +4,25 @@
 #include "core/utils/Clock.hpp"
 #include "core/utils/Random.hpp"
 
-using Millis = Core::Clock::Nanos;
+using Nanos = Core::Clock::Nanos;
+namespace Logger = Core::Logger;
 
 struct Timer {
-    Core::Clock::Nanos nanos;
+    Nanos nanos;
 
     Timer() : nanos(0) {
         CORE_TEST_ERROR(Core::Clock::getNanos(nanos));
     }
 
-    Core::Clock::Nanos get() const {
-        Core::Clock::Nanos nanos2 = 0;
+    Nanos get() const {
+        Nanos nanos2 = 0;
         CORE_TEST_ERROR(Core::Clock::getNanos(nanos2));
         return nanos2 - nanos;
     }
 };
 
 template<typename Map>
-static Core::Clock::Nanos testSearch(const Map& m) {
+static Nanos testSearch(const Map& m) {
     Timer t;
     volatile int sum = 0;
     for(int i = 0; i < 10000; i++) {
@@ -36,7 +37,7 @@ static Core::Clock::Nanos testSearch(const Map& m) {
 }
 
 template<typename Map>
-static Core::Clock::Nanos testEmptySearch(const Map& m) {
+static Nanos testEmptySearch(const Map& m) {
     Timer t;
     volatile int sum = 0;
     for(int i = 0; i < 100'000'000; i++) {
@@ -65,12 +66,12 @@ static void fillChaos(Map& m) {
 }
 
 template<typename Map>
-static Millis average(Map& m, Core::Clock::Nanos (*f)(const Map&), int n) {
-    Core::Clock::Nanos sum = 0;
+static int average(Map& m, Nanos (*f)(const Map&), int n) {
+    Nanos sum = 0;
     for(int i = 0; i < n; i++) {
         sum += f(m);
     }
-    return sum / (n * 1'000'000);
+    return static_cast<int>(sum / (n * 1'000'000));
 }
 
 static void order(int n) {
@@ -78,11 +79,12 @@ static void order(int n) {
     Core::ProbingHashMap<int, int> m2;
     fillOrder(m);
     fillOrder(m2);
-    CORE_LOG_INFO("Order Chaining | Order Probing");
-    CORE_LOG_INFO("Search | # ms | # ms", average(m, testSearch, n),
-                  average(m2, testSearch, n));
-    CORE_LOG_INFO("EmptySearch | # ms | # ms", average(m, testEmptySearch, n),
-                  average(m2, testEmptySearch, n));
+    Logger::log(Logger::COLOR_GRAY, "Order Chaining | Order Probing");
+    Logger::log(Logger::COLOR_GRAY, "Search | # ms | # ms",
+                average(m, testSearch, n), average(m2, testSearch, n));
+    Logger::log(Logger::COLOR_GRAY, "EmptySearch | # ms | # ms",
+                average(m, testEmptySearch, n),
+                average(m2, testEmptySearch, n));
 }
 
 static void chaos(int n) {
@@ -90,11 +92,12 @@ static void chaos(int n) {
     Core::ProbingHashMap<int, int> m2;
     fillChaos(m);
     fillChaos(m2);
-    CORE_LOG_INFO("Chaos Chaining | Chaos Probing");
-    CORE_LOG_INFO("Search | # ms | # ms", average(m, testSearch, n),
-                  average(m2, testSearch, n));
-    CORE_LOG_INFO("EmptySearch | # ms | # ms", average(m, testEmptySearch, n),
-                  average(m2, testEmptySearch, n));
+    Logger::log(Logger::COLOR_GRAY, "Chaos Chaining | Chaos Probing");
+    Logger::log(Logger::COLOR_GRAY, "Search | # ms | # ms",
+                average(m, testSearch, n), average(m2, testSearch, n));
+    Logger::log(Logger::COLOR_GRAY, "EmptySearch | # ms | # ms",
+                average(m, testEmptySearch, n),
+                average(m2, testEmptySearch, n));
 }
 
 static void testProbing(int n) {
@@ -102,11 +105,12 @@ static void testProbing(int n) {
     Core::ProbingHashMap<int, int> m2;
     fillOrder(m);
     fillChaos(m2);
-    CORE_LOG_INFO("Order | Chaos");
-    CORE_LOG_INFO("Search | # ms | # ms", average(m, testSearch, n),
-                  average(m2, testSearch, n));
-    CORE_LOG_INFO("EmptySearch | # ms | # ms", average(m, testEmptySearch, n),
-                  average(m2, testEmptySearch, n));
+    Logger::log(Logger::COLOR_GRAY, "Order | Chaos");
+    Logger::log(Logger::COLOR_GRAY, "Search | # ms | # ms",
+                average(m, testSearch, n), average(m2, testSearch, n));
+    Logger::log(Logger::COLOR_GRAY, "EmptySearch | # ms | # ms",
+                average(m, testEmptySearch, n),
+                average(m2, testEmptySearch, n));
 }
 
 int main() {

+ 1 - 1
src/ErrorSimulator.hpp

@@ -15,7 +15,7 @@ namespace Core::Fail {
 #define CORE_FILE_CLOSE_FAIL Core::Fail::fileClose
 #define CORE_TIME_GET_FAIL Core::Fail::timeGet
 #else
-#define CORE_REALLOC_FAIL false
+#define CORE_REALLOC_FAIL(...) false
 #define CORE_FILE_CLOSE_FAIL false
 #define CORE_TIME_GET_FAIL false
 #endif

+ 4 - 2
src/Utility.cpp

@@ -5,14 +5,16 @@
 #include <string.h>
 
 #include "ErrorSimulator.hpp"
+#include "core/utils/Logger.hpp"
 
 static Core::ExitHandler exitHandler = nullptr;
 static void* exitData = nullptr;
 
 void Core::exitWithHandler(const char* file, int line, int value) {
     if(value != 0) {
-        printf("\33[1;31mExit from %s:%d with value %d\33[39;49m\n", file, line,
-               value);
+        printf("%sExit from %s:%d with value %d%s\n", Core::Logger::COLOR_RED,
+               Core::Logger::getFileName(file), line, value,
+               Core::Logger::COLOR_RESET);
     }
     if(exitHandler != nullptr) {
         exitHandler(value, exitData);

+ 6 - 6
tasks

@@ -47,7 +47,6 @@ elif [ "$task" = "build" ]; then
         build_release=true
     elif [ "$type" = "all" ]; then
         build_debug=true
-        build_test_release=true
         build_release=true
     else
         echo "Valid build types are: debug, release, all"
@@ -66,12 +65,12 @@ elif [ "$task" = "test" ]; then
         build_debug=true
         test_debug=true
     elif [ "$type" = "release" ]; then
-        build_test_release=true
+        build_release=true
         test_release=true
     elif [ "$type" = "all" ]; then
         build_debug=true
         test_debug=true
-        build_test_release=true
+        build_release=true
         test_release=true
     else
         echo "Valid test types are: debug, release, all"
@@ -83,12 +82,12 @@ elif [ "$task" = "valgrind" ]; then
         build_debug=true
         test_debug=true
     elif [ "$type" = "release" ]; then
-        build_test_release=true
+        build_release=true
         test_release=true
     elif [ "$type" = "all" ]; then
         build_debug=true
         test_debug=true
-        build_test_release=true
+        build_release=true
         test_release=true
     else
         echo "Valid valgrind types are: debug, release, all"
@@ -177,5 +176,6 @@ if $time; then
     printf "$output" | sort -n
 fi
 if $coverage; then
-    gcovr -r . build_debug -e test -e performance
+    gcovr -r . build_debug -e test -e performance \
+        --exclude-lines-by-pattern ".*CoverageIgnore.*"
 fi

+ 1 - 1
test/Main.cpp

@@ -65,7 +65,7 @@ int main(int argAmount, const char** args) {
     Core::setExitHandler(onExit, &data);
 
     char buffer[] = {-100, 0};
-    CORE_LOG_DEBUG(buffer);
+    CORE_LOG_ERROR(buffer);
 
     return 0;
 }

+ 15 - 16
test/Test.cpp

@@ -1,29 +1,28 @@
 #include "Test.hpp"
 
-Core::HashMap<Core::Test::Internal::FileName, Core::Test::Internal::Result>
-    Core::Test::Internal::results;
+namespace Internal = Core::Test::Internal;
+namespace Logger = Core::Logger;
 
-void Core::Test::Internal::warn(const char* file, int line, Error e) {
-    CORE_LOG_WARNING("#:# | #", Core::Logger::getFileName(file), line, e);
+Core::HashMap<Internal::FileName, Internal::Result> Internal::results;
+
+void Internal::warn(const char* file, int line, Error e) {
+    Logger::log("#:# | #", Logger::getFileName(file), line, e);
 }
 
-bool Core::Test::Internal::checkFloat(const char* file, int line, float wanted,
-                                      float actual, float error) {
+bool Internal::checkFloat(const char* file, int line, float wanted,
+                          float actual, float error) {
     float diff = wanted - actual;
     diff = diff < 0.0f ? -diff : diff;
     return check(file, line, wanted, actual, diff <= error);
 }
 
 void Core::Test::finalize() {
-    using namespace Internal;
-    for(const auto& e : results) {
-        if(e.value.successTests == e.value.tests) {
-            CORE_LOG_DEBUG("# - # / # tests succeeded", e.getKey(),
-                           e.value.successTests, e.value.tests);
-        } else {
-            CORE_LOG_ERROR("# - # / # tests succeeded", e.getKey(),
-                           e.value.successTests, e.value.tests);
-        }
+    for(const auto& e : Internal::results) {
+        const char* color = e.value.successTests == e.value.tests
+                                ? Logger::COLOR_GREEN
+                                : Logger::COLOR_RED;
+        Logger::log(color, "# - # / # tests succeeded", e.getKey(),
+                    e.value.successTests, e.value.tests);
     }
-    results.clear();
+    Internal::results.clear();
 }

+ 3 - 2
test/Test.hpp

@@ -37,8 +37,9 @@ namespace Core::Test {
                 result->successTests++;
                 return true;
             }
-            CORE_LOG_ERROR("#:# - expected '#' got '#'", fileName, line, wanted,
-                           actual);
+            Core::Logger::log(Core::Logger::COLOR_RED,
+                              "#:# - expected '#' got '#'", fileName, line,
+                              wanted, actual);
             return false;
         }
 

+ 2 - 0
test/modules/BitArrayTests.cpp

@@ -143,10 +143,12 @@ static void testNegativeArgument() {
 }
 
 static void testOutOfMemory() {
+#ifdef ERROR_SIMULATOR
     Core::BitArray bits;
     Core::Fail::leftAllocations = 0;
     CORE_TEST_EQUAL(Core::Error::OUT_OF_MEMORY, bits.resize(8, 4));
     Core::Fail::leftAllocations = -1;
+#endif
 }
 
 void Core::testBitArray(bool outOfMemoryTest) {

+ 2 - 0
test/modules/ClockTests.cpp

@@ -38,10 +38,12 @@ static void testWait(Core::Clock::Nanos wait) {
 }
 
 static void testFail() {
+#ifdef ERROR_SIMULATOR
     Core::Clock::Nanos n = 0;
     Core::Fail::timeGet = true;
     CORE_TEST_EQUAL(Core::Error::TIME_NOT_AVAILABLE, Core::Clock::getNanos(n));
     Core::Fail::timeGet = false;
+#endif
 }
 
 void Core::testClock(bool light) {

+ 2 - 0
test/modules/ComponentsTests.cpp

@@ -187,6 +187,7 @@ static void testRemove() {
 }
 
 static void testOutOfMemory() {
+#ifdef ERROR_SIMULATOR
     IntComponent c;
     int* i1 = nullptr;
     int memFails = 0;
@@ -199,6 +200,7 @@ static void testOutOfMemory() {
     }
     Core::Fail::leftAllocations = -1;
     CORE_TEST_TRUE(memFails != 0);
+#endif
 }
 
 static void testConstSearch() {

+ 2 - 0
test/modules/FileReaderTests.cpp

@@ -97,12 +97,14 @@ static void testInvalidAccessPath() {
 }
 
 static void testCloseFail() {
+#ifdef ERROR_SIMULATOR
     Core::Fail::fileClose = true;
     {
         Core::FileReader r;
         CORE_TEST_ERROR(r.open(TEST_FILE));
     }
     Core::Fail::fileClose = false;
+#endif
 }
 
 void Core::testFileReader() {

+ 2 - 0
test/modules/HashMapTests.cpp

@@ -312,6 +312,7 @@ static void testTypes() {
 }
 
 static void testOutOfMemory() {
+#ifdef ERROR_SIMULATOR
     IntMap map;
     int memFails = 0;
     for(int i = 0; i < 40; i++) {
@@ -328,6 +329,7 @@ static void testOutOfMemory() {
     }
     Core::Fail::leftAllocations = -1;
     CORE_TEST_TRUE(memFails != 0);
+#endif
 }
 
 void Core::testHashMap(bool light, bool outOfMemoryTest) {

+ 2 - 0
test/modules/LinkedListTests.cpp

@@ -275,6 +275,7 @@ static void testRemoveLast() {
 }
 
 static void testOutOfMemory() {
+#ifdef ERROR_SIMULATOR
     IntList list;
     int memFails = 0;
     for(int i = 0; i < 40; i++) {
@@ -289,6 +290,7 @@ static void testOutOfMemory() {
     }
     Core::Fail::leftAllocations = -1;
     CORE_TEST_TRUE(memFails != 0);
+#endif
 }
 
 void Core::testLinkedList(bool light, bool outOfMemoryTest) {

+ 2 - 0
test/modules/ProbingHashMapTests.cpp

@@ -313,6 +313,7 @@ static void testTypes() {
 }
 
 static void testOutOfMemory() {
+#ifdef ERROR_SIMULATOR
     IntMap map;
     int memFails = 0;
     for(int i = 0; i < 40; i++) {
@@ -329,6 +330,7 @@ static void testOutOfMemory() {
     }
     Core::Fail::leftAllocations = -1;
     CORE_TEST_TRUE(memFails != 0);
+#endif
 }
 
 static void testInsertInvalid() {

+ 2 - 0
test/modules/UtilityTests.cpp

@@ -38,11 +38,13 @@ static void testNegativeRellocate() {
 }
 
 static void testReallocateFail() {
+#ifdef ERROR_SIMULATOR
     char* buffer = nullptr;
     Core::Fail::realloc = true;
     CORE_TEST_EQUAL(Core::Error::OUT_OF_MEMORY, Core::reallocate(buffer, 16));
     CORE_TEST_TRUE(buffer == nullptr);
     Core::Fail::realloc = false;
+#endif
 }
 
 void Core::testUtility() {