atrtp

Thermodynamic properties of a medium in combustion
git clone git://git.meso-star.fr/atrtp.git
Log | Files | Refs | README | LICENSE

atrtp_log.c (3183B)


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