scam_log.c (3040B)
1 /* Copyright (C) 2021-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 "scam_c.h" 17 #include "scam_log.h" 18 19 #include <rsys/cstr.h> 20 #include <rsys/logger.h> 21 22 /******************************************************************************* 23 * Helper functions 24 ******************************************************************************/ 25 static INLINE void 26 log_msg 27 (const struct scam* cam, 28 const enum log_type stream, 29 const char* msg, 30 va_list vargs) 31 { 32 ASSERT(cam && msg); 33 if(cam->verbose) { 34 res_T res; (void)res; 35 res = logger_vprint(cam->logger, stream, msg, vargs); 36 ASSERT(res == RES_OK); 37 } 38 } 39 40 static void 41 print_info(const char* msg, void* ctx) 42 { 43 (void)ctx; 44 fprintf(stderr, MSG_INFO_PREFIX"%s", msg); 45 } 46 47 static void 48 print_err(const char* msg, void* ctx) 49 { 50 (void)ctx; 51 fprintf(stderr, MSG_ERROR_PREFIX"%s", msg); 52 } 53 54 static void 55 print_warn(const char* msg, void* ctx) 56 { 57 (void)ctx; 58 fprintf(stderr, MSG_WARNING_PREFIX"%s", msg); 59 } 60 61 /******************************************************************************* 62 * Local functions 63 ******************************************************************************/ 64 res_T 65 setup_log_default(struct scam* cam) 66 { 67 res_T res = RES_OK; 68 ASSERT(cam); 69 70 res = logger_init(cam->allocator, &cam->logger__); 71 if(res != RES_OK) { 72 if(cam->verbose) { 73 fprintf(stderr, 74 MSG_ERROR_PREFIX 75 "Could not setup the Star-Camera default logger -- %s.\n", 76 res_to_cstr(res)); 77 } 78 goto error; 79 } 80 logger_set_stream(&cam->logger__, LOG_OUTPUT, print_info, NULL); 81 logger_set_stream(&cam->logger__, LOG_ERROR, print_err, NULL); 82 logger_set_stream(&cam->logger__, LOG_WARNING, print_warn, NULL); 83 cam->logger = &cam->logger__; 84 85 exit: 86 return res; 87 error: 88 goto exit; 89 } 90 91 void 92 log_info(const struct scam* cam, const char* msg, ...) 93 { 94 va_list vargs_list; 95 ASSERT(cam && msg); 96 97 va_start(vargs_list, msg); 98 log_msg(cam, LOG_OUTPUT, msg, vargs_list); 99 va_end(vargs_list); 100 } 101 102 void 103 log_err(const struct scam* cam, const char* msg, ...) 104 { 105 va_list vargs_list; 106 ASSERT(cam && msg); 107 108 va_start(vargs_list, msg); 109 log_msg(cam, LOG_ERROR, msg, vargs_list); 110 va_end(vargs_list); 111 } 112 113 void 114 log_warn(const struct scam* cam, const char* msg, ...) 115 { 116 va_list vargs_list; 117 ASSERT(cam && msg); 118 119 va_start(vargs_list, msg); 120 log_msg(cam, LOG_WARNING, msg, vargs_list); 121 va_end(vargs_list); 122 }