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