mrumtl

Describe materials that vary spectrally
git clone git://git.meso-star.fr/mrumtl.git
Log | Files | Refs | README | LICENSE

mrumtl_log.c (3132B)


      1 /* Copyright (C) 2020-2023 |Méso|Star> (contact@meso-star.com)
      2  *
      3  * This program is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published by
      5  * the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #include "mrumtl_c.h"
     17 #include "mrumtl_log.h"
     18 
     19 #include <rsys/cstr.h>
     20 #include <rsys/logger.h>
     21 
     22 #include <stdarg.h>
     23 
     24 /*******************************************************************************
     25  * Helper functions
     26  ******************************************************************************/
     27 static INLINE void
     28 log_msg
     29   (const struct mrumtl* mrumtl,
     30    const enum log_type stream,
     31    const char* msg,
     32    va_list vargs)
     33 {
     34   ASSERT(mrumtl && msg);
     35   if(mrumtl->verbose) {
     36     res_T res; (void)res;
     37     res = logger_vprint(mrumtl->logger, stream, msg, vargs);
     38     ASSERT(res == RES_OK);
     39   }
     40 }
     41 
     42 static void
     43 print_info(const char* msg, void* ctx)
     44 {
     45   (void)ctx;
     46   fprintf(stderr, MSG_INFO_PREFIX"%s", msg);
     47 }
     48 
     49 static void
     50 print_err(const char* msg, void* ctx)
     51 {
     52   (void)ctx;
     53   fprintf(stderr, MSG_ERROR_PREFIX"%s", msg);
     54 }
     55 
     56 static void
     57 print_warn(const char* msg, void* ctx)
     58 {
     59   (void)ctx;
     60   fprintf(stderr, MSG_WARNING_PREFIX"%s", msg);
     61 }
     62 
     63 /*******************************************************************************
     64  * Local functions
     65  ******************************************************************************/
     66 res_T
     67 setup_log_default(struct mrumtl* mrumtl)
     68 {
     69   res_T res = RES_OK;
     70   ASSERT(mrumtl);
     71 
     72   res = logger_init(mrumtl->allocator, &mrumtl->logger__);
     73   if(res != RES_OK) {
     74     if(mrumtl->verbose) {
     75       fprintf(stderr,
     76         MSG_ERROR_PREFIX
     77         "Could not setup the default logger -- %s.\n",
     78         res_to_cstr(res));
     79     }
     80     goto error;
     81   }
     82   logger_set_stream(&mrumtl->logger__, LOG_OUTPUT, print_info, NULL);
     83   logger_set_stream(&mrumtl->logger__, LOG_ERROR, print_err, NULL);
     84   logger_set_stream(&mrumtl->logger__, LOG_WARNING, print_warn, NULL);
     85   mrumtl->logger = &mrumtl->logger__;
     86 
     87 exit:
     88   return res;
     89 error:
     90   goto exit;
     91 }
     92 
     93 void
     94 log_info(const struct mrumtl* mrumtl, const char* msg, ...)
     95 {
     96   va_list vargs_list;
     97   ASSERT(mrumtl && msg);
     98 
     99   va_start(vargs_list, msg);
    100   log_msg(mrumtl, LOG_OUTPUT, msg, vargs_list);
    101   va_end(vargs_list);
    102 }
    103 
    104 void
    105 log_err(const struct mrumtl* mrumtl, const char* msg, ...)
    106 {
    107   va_list vargs_list;
    108   ASSERT(mrumtl && msg);
    109 
    110   va_start(vargs_list, msg);
    111   log_msg(mrumtl, LOG_ERROR, msg, vargs_list);
    112   va_end(vargs_list);
    113 }
    114 
    115 void
    116 log_warn(const struct mrumtl* mrumtl, const char* msg, ...)
    117 {
    118   va_list vargs_list;
    119   ASSERT(mrumtl && msg);
    120 
    121   va_start(vargs_list, msg);
    122   log_msg(mrumtl, LOG_WARNING, msg, vargs_list);
    123   va_end(vargs_list);
    124 }