taler-xotp_fw

xOTP generator firmware
Log | Files | Refs | Submodules | README

logger.c (1851B)


      1 /**
      2  * @file logger.c
      3  * @author Adrian STEINER (steia19@bfh.ch)
      4  * @brief Logger module to send debug and info messages
      5  * @version 0.1
      6  * @date 2024-06-07
      7  *
      8  * @copyright (C) 2025 Adrian STEINER
      9  * This program is free software: you can redistribute it and/or modify
     10  * it under the terms of the GNU General Public License as published by
     11  * the Free Software Foundation, either version 3 of the License, or
     12  * (at your option) any later version.
     13  *
     14  * This program is distributed in the hope that it will be useful,
     15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17  * GNU General Public License for more details.
     18  *
     19  * You should have received a copy of the GNU General Public License
     20  * along with this program.  If not, see <https: //www.gnu.org/licenses/>.
     21  *
     22  */
     23 
     24 #include <stdarg.h>
     25 #include <stdint.h>
     26 #include <stdio.h>
     27 #include <string.h>
     28 
     29 #include "logger.h"
     30 
     31 #define BUFFERSIZE (256)
     32 
     33 logger_output userCallback = NULL;
     34 
     35 const char *logLevelMsg[] = {
     36     [LOG_OFF] = "LOGGER OFF", [LOG_ERROR] = "ERROR", [LOG_WARN] = "WARN",
     37     [LOG_INFO] = "INFO",      [LOG_DEBUG] = "DEBUG", [LOG_TRACE] = "TRACE",
     38 };
     39 
     40 void logger_init(logger_output outputCallback)
     41 {
     42   if (NULL == userCallback) {
     43     userCallback = outputCallback;
     44   } else {
     45     LOGGER_WARN("The logger has already been initialized");
     46   }
     47   return;
     48 }
     49 
     50 void logger_sendMsg(logger_logLevel logLevel, const char *format, ...)
     51 {
     52   if (NULL == userCallback || logLevel <= LOG_OFF) {
     53     return;
     54   }
     55 
     56   char buff[BUFFERSIZE];
     57 
     58   // Add log level
     59   snprintf(buff, BUFFERSIZE, "%s: ", logLevelMsg[logLevel]);
     60 
     61   uint8_t preMsgLen = strlen(buff);
     62 
     63   va_list args;
     64   va_start(args, format);
     65   vsnprintf(buff + preMsgLen, BUFFERSIZE - preMsgLen, format, args);
     66   va_end(args);
     67 
     68   userCallback(buff);
     69   return;
     70 }