LCOV - code coverage report
Current view: top level - core - logging.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 88.1 % 59 52
Test Date: 2026-03-04 10:22:18 Functions: 100.0 % 9 9

            Line data    Source code
       1              : #include "cfd/core/logging.h"
       2              : #include "cfd/core/cfd_status.h"
       3              : #include <stdio.h>
       4              : 
       5              : 
       6              : #ifdef _WIN32
       7              : static __declspec(thread) cfd_status_t g_last_status = CFD_SUCCESS;
       8              : static __declspec(thread) char g_last_error_msg[256] = {0};
       9              : static __declspec(thread) cfd_log_callback_t s_log_callback = NULL;
      10              : #else
      11              : #include <pthread.h>
      12              : static __thread cfd_status_t g_last_status = CFD_SUCCESS;
      13              : static __thread char g_last_error_msg[256] = {0};
      14              : static __thread cfd_log_callback_t s_log_callback = NULL;
      15              : #endif
      16              : 
      17              : //=============================================================================
      18              : // ERROR CONTEXT
      19              : //=============================================================================
      20              : 
      21           64 : void cfd_set_error(cfd_status_t status, const char* message) {
      22           64 :     g_last_status = status;
      23           64 :     if (message) {
      24           64 :         snprintf(g_last_error_msg, sizeof(g_last_error_msg), "%s", message);
      25              :     } else {
      26            0 :         g_last_error_msg[0] = '\0';
      27              :     }
      28           64 : }
      29              : 
      30            9 : const char* cfd_get_last_error(void) {
      31            9 :     if (g_last_error_msg[0] == '\0') {
      32            2 :         return NULL;
      33              :     }
      34              :     return g_last_error_msg;
      35              : }
      36              : 
      37           34 : cfd_status_t cfd_get_last_status(void) {
      38           34 :     return g_last_status;
      39              : }
      40              : 
      41            9 : const char* cfd_get_error_string(cfd_status_t status) {
      42            9 :     switch (status) {
      43              :         case CFD_SUCCESS:
      44              :             return "Success";
      45            1 :         case CFD_ERROR:
      46            1 :             return "Generic error";
      47            1 :         case CFD_ERROR_NOMEM:
      48            1 :             return "Out of memory";
      49            1 :         case CFD_ERROR_INVALID:
      50            1 :             return "Invalid argument";
      51            1 :         case CFD_ERROR_IO:
      52            1 :             return "I/O error";
      53            1 :         case CFD_ERROR_UNSUPPORTED:
      54            1 :             return "Operation not supported";
      55            1 :         case CFD_ERROR_DIVERGED:
      56            1 :             return "NSSolver diverged";
      57            1 :         case CFD_ERROR_MAX_ITER:
      58            1 :             return "Max iterations reached";
      59            0 :         case CFD_ERROR_LIMIT_EXCEEDED:
      60            0 :             return "Resource limit exceeded";
      61            0 :         case CFD_ERROR_NOT_FOUND:
      62            0 :             return "Resource not found";
      63            1 :         default:
      64            1 :             return "Unknown error";
      65              :     }
      66              : }
      67              : 
      68          176 : void cfd_clear_error(void) {
      69          176 :     g_last_status = CFD_SUCCESS;
      70          176 :     g_last_error_msg[0] = '\0';
      71          176 : }
      72              : 
      73              : //=============================================================================
      74              : // LOGGING
      75              : //=============================================================================
      76              : 
      77           16 : void cfd_set_log_callback(cfd_log_callback_t callback) {
      78           16 :     s_log_callback = callback;
      79           16 : }
      80              : 
      81            2 : void cfd_error(const char* message) {
      82            2 :     const char* safe_msg = message ? message : "(null)";
      83            2 :     if (s_log_callback) {
      84            2 :         s_log_callback(CFD_LOG_LEVEL_ERROR, safe_msg);
      85              :     } else {
      86            0 :         fprintf(stderr, "ERROR: %s\n", safe_msg);
      87              :     }
      88            2 :     cfd_set_error(CFD_ERROR, safe_msg);
      89            2 : }
      90              : 
      91            9 : void cfd_warning(const char* message) {
      92            9 :     const char* safe_msg = message ? message : "(null)";
      93            9 :     if (s_log_callback) {
      94            3 :         s_log_callback(CFD_LOG_LEVEL_WARNING, safe_msg);
      95              :     } else {
      96            6 :         fprintf(stderr, "WARNING: %s\n", safe_msg);
      97              :     }
      98            9 : }
      99              : 
     100            2 : void cfd_info(const char* message) {
     101            2 :     const char* safe_msg = message ? message : "(null)";
     102            2 :     if (s_log_callback) {
     103            2 :         s_log_callback(CFD_LOG_LEVEL_INFO, safe_msg);
     104              :     } else {
     105            0 :         fprintf(stdout, "INFO: %s\n", safe_msg);
     106              :     }
     107            2 : }
        

Generated by: LCOV version 2.0-1