Bladeren bron

Use reporter instead of direct log, tests

Kajetan Johannes Hammerle 2 maanden geleden
bovenliggende
commit
33b518e355
7 gewijzigde bestanden met toevoegingen van 51 en 10 verwijderingen
  1. 7 0
      include/core/Logger.h
  2. 21 0
      src/Logger.c
  3. 6 6
      src/ReadLine.c
  4. 1 1
      src/Terminal.c
  5. 1 1
      src/Thread.c
  6. 7 2
      test/Main.c
  7. 8 0
      test/modules/TerminalTests.c

+ 7 - 0
include/core/Logger.h

@@ -8,6 +8,13 @@ typedef enum { LOG_NONE, LOG_ERROR, LOG_WARNING, LOG_INFO, LOG_DEBUG } LogLevel;
 
 extern LogLevel logLevel;
 
+typedef void (*ReportHandler)(
+    LogLevel l, const char* file, int line, void* data, const char* message);
+check_format(4, 5) void callReportHandler(
+    LogLevel l, const char* file, int line, const char* format, ...);
+void setReportHandler(ReportHandler h, void* data);
+#define REPORT(l, ...) callReportHandler(l, __FILE__, __LINE__, __VA_ARGS__)
+
 const char* getShortFileName(const char* s);
 
 check_format(6, 7) void printLog(

+ 21 - 0
src/Logger.c

@@ -4,6 +4,27 @@
 #include <stdio.h>
 
 LogLevel logLevel = LOG_DEBUG;
+static ReportHandler reportHandler = nullptr;
+static void* reportData = nullptr;
+
+void setReportHandler(ReportHandler h, void* data) {
+    reportHandler = h;
+    reportData = data;
+}
+
+void callReportHandler(
+    LogLevel l, const char* file, int line, const char* format, ...) {
+    char buffer[512];
+
+    va_list args;
+    va_start(args, format);
+    vsnprintf(buffer, sizeof(buffer), format, args);
+    va_end(args);
+
+    if(reportHandler != nullptr) {
+        reportHandler(l, file, line, reportData, buffer);
+    }
+}
 
 const char* getShortFileName(const char* s) {
     const char* r = s;

+ 6 - 6
src/ReadLine.c

@@ -86,13 +86,13 @@ static void addToHistory() {
 
 static void lock() {
     if(mtx_lock(&bufferMutex) != thrd_success || MUTEX_LOCK_FAIL) {
-        LOG_WARNING("could not lock buffer mutex");
+        REPORT(LOG_WARNING, "could not lock buffer mutex");
     }
 }
 
 static void unlock() {
     if(mtx_unlock(&bufferMutex) != thrd_success || MUTEX_UNLOCK_FAIL) {
-        LOG_WARNING("could not unlock buffer mutex");
+        REPORT(LOG_WARNING, "could not unlock buffer mutex");
     }
 }
 
@@ -221,18 +221,18 @@ static int loop(void* data) {
 
 bool startReadLine(void) {
     if(enterRawTerminal()) {
-        LOG_WARNING("cannot set terminal attributes");
+        REPORT(LOG_WARNING, "cannot set terminal attributes");
     }
     initQueueCL(&buffer, 10);
     atomic_store(&running, true);
     if(MUTEX_INIT_FAIL || mtx_init(&bufferMutex, mtx_plain) != thrd_success) {
-        LOG_ERROR("cannot init buffer mutex");
+        REPORT(LOG_ERROR, "cannot init buffer mutex");
         stopReadLine();
         return true;
     } else if(
         THREAD_INIT_FAIL ||
         thrd_create(&readThread, loop, nullptr) != thrd_success) {
-        LOG_ERROR("cannot start read thread");
+        REPORT(LOG_ERROR, "cannot start read thread");
         stopReadLine();
         return true;
     }
@@ -255,7 +255,7 @@ void stopReadLine() {
     atomic_store(&running, false);
     joinThreadSafe(&readThread);
     if(leaveRawTerminal()) {
-        LOG_WARNING("cannot restore terminal attributes");
+        REPORT(LOG_WARNING, "cannot restore terminal attributes");
     }
     destroyQueueCL(&buffer);
     mtx_destroy(&bufferMutex);

+ 1 - 1
src/Terminal.c

@@ -192,7 +192,7 @@ static u64 readEscapeSequence() {
         }
         s[l++] = readChar();
     }
-    LOG_WARNING("Unknown escape sequence starting with '%s'", s);
+    REPORT(LOG_WARNING, "Unknown escape sequence starting with '%s'", s);
     return TERMINAL_KEY_UNKNOWN;
 }
 

+ 1 - 1
src/Thread.c

@@ -9,7 +9,7 @@ bool joinThreadSafe(thrd_t* t) {
     static thrd_t nullThread = {0};
     if(memcmp(t, &nullThread, sizeof(thrd_t)) != 0) {
         if(THREAD_JOIN_FAIL || thrd_join(*t, nullptr) != thrd_success) {
-            LOG_ERROR("cannot join thread");
+            REPORT(LOG_ERROR, "cannot join thread");
             return true;
         }
         *t = (thrd_t){0};

+ 7 - 2
test/Main.c

@@ -19,6 +19,12 @@ static void onExit(int code, void* data) {
     finalize();
 }
 
+static void printReport(
+    LogLevel l, const char* file, int line, void*, const char* message) {
+    printLog(l, file, line, "", "IGNORE: ", "%s", message);
+    setReportHandler(nullptr, nullptr);
+}
+
 int main(int argAmount, const char** args) {
     if(argAmount >= 2 && strcmp(args[1], "help") == 0) {
         puts("alloc");
@@ -55,6 +61,7 @@ int main(int argAmount, const char** args) {
             return 0;
         }
     }
+    setReportHandler(printReport, nullptr);
 
     testBitArray();
     testBox();
@@ -68,11 +75,9 @@ int main(int argAmount, const char** args) {
     testQuaternion();
     testQueue();
     testRandom(light);
-    logLevel = LOG_NONE;
     if(light) {
         testReadLine();
     }
-    logLevel = LOG_DEBUG;
     testSpinLock();
     testTerminal(!light);
     testUnicode();

+ 8 - 0
test/modules/TerminalTests.c

@@ -35,6 +35,14 @@ void testTerminal(bool tty) {
 
     hideCursor();
     showCursor();
+
+    u64 u = TERMINAL_KEY_F5 | TERMINAL_KEY_SHIFT;
+    TEST_TRUE(isSpecialChar(u));
+    SpecialChar s = convertToSpecialChar(u);
+    TEST_U64(TERMINAL_KEY_F5, s.key);
+    TEST_FALSE(s.alt);
+    TEST_FALSE(s.control);
+    TEST_TRUE(s.shift);
 }
 
 void testInteractiveTerminal(void) {