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