star-gs

Literate program for a geometric sensitivity calculation
git clone git://git.meso-star.fr/star-gs.git
Log | Files | Refs | README | LICENSE

sgs_log.c (3243B)


      1 /* Copyright (C) 2021-2023 Centre National de la Recherche Scientifique
      2  * Copyright (C) 2021-2023 INSA Lyon
      3  * Copyright (C) 2021-2023 Institut Mines Télécom Albi-Carmaux
      4  * Copyright (C) 2021-2023 |Méso|Star> (contact@meso-star.com)
      5  * Copyright (C) 2021-2023 Institut Pascal
      6  * Copyright (C) 2021-2023 PhotonLyX (info@photonlyx.com)
      7  * Copyright (C) 2021-2023 Université de Lorraine
      8  * Copyright (C) 2021-2023 Université Paul Sabatier
      9  * Copyright (C) 2021-2023 Université Toulouse - Jean Jaurès
     10  *
     11  * This program is free software: you can redistribute it and/or modify
     12  * it under the terms of the GNU General Public License as published by
     13  * the Free Software Foundation, either version 3 of the License, or
     14  * (at your option) any later version.
     15  *
     16  * This program is distributed in the hope that it will be useful,
     17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     19  * GNU General Public License for more details.
     20  *
     21  * You should have received a copy of the GNU General Public License
     22  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     23 
     24 #include "sgs_c.h"
     25 #include "sgs_log.h"
     26 
     27 #include <rsys/logger.h>
     28 
     29 /*******************************************************************************
     30  * Helper functions
     31  ******************************************************************************/
     32 static void
     33 print_out(const char* msg, void* ctx)
     34 {
     35   ASSERT(msg);
     36   (void)ctx;
     37   fprintf(stdout, SGS_LOG_INFO_PREFIX"%s", msg);
     38 }
     39 
     40 static void
     41 print_err(const char* msg, void* ctx)
     42 {
     43   ASSERT(msg);
     44   (void)ctx;
     45   fprintf(stderr, SGS_LOG_ERROR_PREFIX"%s", msg);
     46 }
     47 
     48 static void
     49 print_warn(const char* msg, void* ctx)
     50 {
     51   ASSERT(msg);
     52   (void)ctx;
     53   fprintf(stderr, SGS_LOG_WARNING_PREFIX"%s", msg);
     54 }
     55 
     56 static void
     57 log_msg
     58   (struct sgs* sgs,
     59    const enum log_type stream,
     60    const char* msg,
     61    va_list vargs)
     62 {
     63   ASSERT(sgs && msg);
     64   CHK(logger_vprint(&sgs->logger, stream, msg, vargs) == RES_OK);
     65 }
     66 
     67 /*******************************************************************************
     68  * sgs log API
     69  ******************************************************************************/
     70 void
     71 sgs_log(struct sgs* sgs, const char* msg, ...)
     72 {
     73   va_list vargs_list;
     74   ASSERT(sgs && msg);
     75   va_start(vargs_list, msg);
     76   log_msg(sgs, LOG_OUTPUT, msg, vargs_list);
     77   va_end(vargs_list);
     78 }
     79 
     80 void
     81 sgs_log_err(struct sgs* sgs, const char* msg, ...)
     82 {
     83   va_list vargs_list;
     84   ASSERT(sgs && msg);
     85   va_start(vargs_list, msg);
     86   log_msg(sgs, LOG_ERROR, msg, vargs_list);
     87   va_end(vargs_list);
     88 }
     89 
     90 void
     91 sgs_log_warn(struct sgs* sgs, const char* msg, ...)
     92 {
     93   va_list vargs_list;
     94   ASSERT(sgs && msg);
     95   va_start(vargs_list, msg);
     96   log_msg(sgs, LOG_WARNING, msg, vargs_list);
     97   va_end(vargs_list);
     98 }
     99 
    100 /*******************************************************************************
    101  * Local function
    102  ******************************************************************************/
    103 void
    104 setup_logger(struct sgs* sgs)
    105 {
    106   logger_init(sgs->allocator, &sgs->logger);
    107   logger_set_stream(&sgs->logger, LOG_OUTPUT, print_out, NULL);
    108   logger_set_stream(&sgs->logger, LOG_ERROR, print_err, NULL);
    109   logger_set_stream(&sgs->logger, LOG_WARNING, print_warn, NULL);
    110 }