star-hitran

Load line-by-line data from the HITRAN database
git clone git://git.meso-star.fr/star-hitran.git
Log | Files | Refs | README | LICENSE

commit 1017e17a3421f01b58bb9fa43ba20656d08a82af
parent 97abb39d972aa5f7f5b6c9e9a458f14e2190f7ef
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Tue, 16 Dec 2025 15:54:34 +0100

Complete refactoring of the logging API.

Replace internal functions with helper macros that directly use the RSys
logging API. This simplifies and streamlines the code.

Note that the proposed macros used variadic arguments, so the library
must be compiled with the C99 dialect. The tests are always compiled in
C89 to ensure that the library API does not lose portability.

Removal of colors from logging messages, as some terminals do not
support them.

Diffstat:
MMakefile | 5++---
Mconfig.mk | 11+++++------
Msrc/shtr.c | 7+------
Msrc/shtr_c.h | 13+++++++++++++
Msrc/shtr_isotope_metadata.c | 49++++++++++++++++++++++++-------------------------
Msrc/shtr_line_list.c | 27+++++++++++++--------------
Msrc/shtr_line_view.c | 24++++++++++++------------
Dsrc/shtr_log.c | 127-------------------------------------------------------------------------------
Dsrc/shtr_log.h | 73-------------------------------------------------------------------------
Msrc/shtr_param.c | 7+++----
10 files changed, 73 insertions(+), 270 deletions(-)

diff --git a/Makefile b/Makefile @@ -36,12 +36,11 @@ SRC = \ src/shtr_isotope_metadata.c \ src/shtr_line_list.c \ src/shtr_line_view.c \ - src/shtr_log.c \ src/shtr_param.c OBJ = $(SRC:.c=.o) DEP = $(SRC:.c=.d) -CFLAGS_LIB = $(CFLAGS_SO) $(INCS) -DSHTR_SHARED_BUILD +CFLAGS_LIB = -std=c99 $(CFLAGS_SO) $(INCS) -DSHTR_SHARED_BUILD LDFLAGS_LIB = $(LDFLAGS_SO) $(LIBS) -lm library: .config $(DEP) @@ -135,7 +134,7 @@ PKG_CONFIG_LOCAL = PKG_CONFIG_PATH="./:$${PKG_CONFIG_PATH}" $(PKG_CONFIG) INCS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags rsys shtr-local) LIBS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs rsys shtr-local) -CFLAGS_TEST = $(CFLAGS_EXE) $(INCS_TEST) +CFLAGS_TEST = -std=c89 $(CFLAGS_EXE) $(INCS_TEST) LDFLAGS_TEST = $(LDFLAGS_EXE) $(LIBS_TEST) -lm tests: library $(TEST_DEP) $(TEST_TGT) diff --git a/config.mk b/config.mk @@ -1,5 +1,10 @@ VERSION = 0.0 + PREFIX = /usr/local +BINPREFIX = $(PREFIX)/bin +LIBPREFIX = $(PREFIX)/lib +INCPREFIX = $(PREFIX)/include +MANPREFIX = $(PREFIX)/share/man LIB_TYPE = SHARED #LIB_TYPE = STATIC @@ -7,11 +12,6 @@ LIB_TYPE = SHARED BUILD_TYPE = RELEASE #BUILD_TYPE = DEBUG -BINPREFIX = $(PREFIX)/bin -LIBPREFIX = $(PREFIX)/lib -INCPREFIX = $(PREFIX)/include -MANPREFIX = $(PREFIX)/share/man - ################################################################################ # Tools ################################################################################ @@ -46,7 +46,6 @@ WFLAGS =\ -Wshadow CFLAGS_HARDENED =\ - -std=c89\ -D_FORTIFY_SOURCES=2\ -fcf-protection=full\ -fstack-clash-protection\ diff --git a/src/shtr.c b/src/shtr.c @@ -18,7 +18,6 @@ #include "shtr.h" #include "shtr_c.h" -#include "shtr_log.h" /******************************************************************************* * Helper functions @@ -70,11 +69,7 @@ shtr_create ref_init(&shtr->ref); shtr->allocator = allocator; shtr->verbose = args->verbose; - if(args->logger) { - shtr->logger = args->logger; - } else { - setup_log_default(shtr); - } + shtr->logger = args->logger ? args->logger : LOGGER_DEFAULT; exit: if(out_shtr) *out_shtr = shtr; diff --git a/src/shtr_c.h b/src/shtr_c.h @@ -22,6 +22,19 @@ #include <rsys/logger.h> #include <rsys/ref_count.h> +#define MSG_INFO_PREFIX "" +#define MSG_ERROR_PREFIX "error: " +#define MSG_WARNING_PREFIX "warning: " + +/* Helper macros for logging */ +#define LOG__(Dev, Lvl, Type, ...) { \ + if ((Dev)->verbose >= (Lvl)) \ + logger_print((Dev)->logger, Type, __VA_ARGS__); \ +} (void)0 +#define ERROR(Dev, ...) LOG__(Dev, 1, LOG_ERROR, MSG_ERROR_PREFIX __VA_ARGS__) +#define WARN(Dev, ...) LOG__(Dev, 2, LOG_WARNING, MSG_WARNING_PREFIX __VA_ARGS__) +#define INFO(Dev, ...) LOG__(Dev, 3, LOG_OUTPUT, MSG_INFO_PREFIX __VA_ARGS__) + struct mem_allocator; struct shtr { diff --git a/src/shtr_isotope_metadata.c b/src/shtr_isotope_metadata.c @@ -20,7 +20,6 @@ #include "shtr.h" #include "shtr_c.h" -#include "shtr_log.h" #include "shtr_param.h" #include <rsys/cstr.h> @@ -104,7 +103,7 @@ struct shtr_isotope_metadata { struct darray_isotope isotopes; /* Map the global identifier of a molecule to its correspond local index into - * the dynamic aray into which it is registered */ + * the dynamic array into which it is registered */ int molid2idx[SHTR_MAX_MOLECULES_COUNT]; struct shtr* shtr; @@ -125,7 +124,7 @@ create_isotope_metadata metadata = MEM_CALLOC(shtr->allocator, 1, sizeof(*metadata)); if(!metadata) { - log_err(shtr, + ERROR(shtr, "Could not allocate the isotope metadata data structure.\n"); res = RES_MEM_ERR; goto error; @@ -163,7 +162,7 @@ flush_molecule molecule->isotopes_range[1] = darray_isotope_size_get(&metadata->isotopes); if(molecule->isotopes_range[0] >= molecule->isotopes_range[1]) { - log_warn(metadata->shtr, + WARN(metadata->shtr, "%s:%lu: the %s molecule does not have any isotope.\n", txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr), str_cget(&molecule->name)); @@ -176,7 +175,7 @@ flush_molecule /* Store the molecule */ res = darray_molecule_push_back(&metadata->molecules, molecule); if(res != RES_OK) { - log_err(metadata->shtr, + ERROR(metadata->shtr, "%s: error storing the %s molecule -- %s.\n", txtrdr_get_name(txtrdr), str_cget(&molecule->name), res_to_cstr(res)); goto error; @@ -186,7 +185,7 @@ flush_molecule if(metadata->molid2idx[molecule->id] >= 0) { const struct molecule* molecule2 = NULL; molecule2 = darray_molecule_cdata_get(&metadata->molecules) + *pimolecule; - log_err(metadata->shtr, + ERROR(metadata->shtr, "%s: cannot register the %s molecule. " "The %s molecule has the same identifier %i.\n", txtrdr_get_name(txtrdr), @@ -226,7 +225,7 @@ parse_molecule id = strtok_r(NULL, " \t", &tk_ctx); if(!name) { - log_err(metadata->shtr, "%s:%lu: molecule name is missing.\n", + ERROR(metadata->shtr, "%s:%lu: molecule name is missing.\n", txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr)); res = RES_BAD_ARG; goto error; @@ -234,7 +233,7 @@ parse_molecule res = str_set(&molecule->name, name); if(res != RES_OK) { - log_err(metadata->shtr, + ERROR(metadata->shtr, "%s:%lu: error seting the molecule name `%s' -- %s.\n", txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr), name, res_to_cstr(res)); @@ -243,7 +242,7 @@ parse_molecule len = strlen(id); if(!id || !len || id[0] != '(' || id[len-1] != ')') { - log_err(metadata->shtr, + ERROR(metadata->shtr, "%s:%lu: invalid definition of the molecule identifier.\n", txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr)); res = RES_BAD_ARG; @@ -254,7 +253,7 @@ parse_molecule res = cstr_to_int(id+1/*Rm leading parenthesis*/, &molecule->id); if(res != RES_OK || !MOLECULE_IS_VALID(molecule)) { id[len-1] = ')'; /* Re-add the trailing parenthesis */ - log_err(metadata->shtr, "%s:%lu: invalid molecule identifier `%s'.\n", + ERROR(metadata->shtr, "%s:%lu: invalid molecule identifier `%s'.\n", txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr), id); res = RES_BAD_ARG; goto error; @@ -262,7 +261,7 @@ parse_molecule tk = strtok_r(NULL, " \t", &tk_ctx); if(tk) { - log_warn(metadata->shtr, "%s:%lu: unexpected text `%s'.\n", + WARN(metadata->shtr, "%s:%lu: unexpected text `%s'.\n", txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr), tk); } @@ -345,7 +344,7 @@ parse_isotope /* Store the isotope */ res = darray_isotope_push_back(&metadata->isotopes, &isotope); if(res != RES_OK) { - log_err(shtr, "%s:%lu: error storing the isotope %d -- %s.\n", + ERROR(shtr, "%s:%lu: error storing the isotope %d -- %s.\n", param_desc.path, param_desc.line, isotope.id, res_to_cstr(res)); res = RES_OK; goto error; @@ -383,7 +382,7 @@ parse_line if(res != RES_OK) goto error; } else { if(!MOLECULE_IS_VALID(molecule)) { - log_err(metadata->shtr, "%s:%lu: missing a molecule definition.\n", + ERROR(metadata->shtr, "%s:%lu: missing a molecule definition.\n", txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr)); res = RES_BAD_ARG; goto error; @@ -419,7 +418,7 @@ load_stream res = txtrdr_stream(metadata->shtr->allocator, stream, name, 0/*No comment char*/, &txtrdr); if(res != RES_OK) { - log_err(shtr, "%s: error creating the text reader -- %s.\n", + ERROR(shtr, "%s: error creating the text reader -- %s.\n", name, res_to_cstr(res)); goto error; } @@ -427,13 +426,13 @@ load_stream #define READ_LINE { \ res = txtrdr_read_line(txtrdr); \ if(res != RES_OK) { \ - log_err(shtr, "%s: error reading the line `%lu' -- %s.\n", \ + ERROR(shtr, "%s: error reading the line `%lu' -- %s.\n", \ name, (unsigned long)txtrdr_get_line_num(txtrdr), res_to_cstr(res)); \ goto error; \ } \ } (void)0 - /* Skip the 1st line that is a comment line*/ + /* Skip the 1st line that is a comment line */ READ_LINE; if(!txtrdr_get_cline(txtrdr)) goto exit; @@ -499,7 +498,7 @@ write_molecules exit: return res; error: - log_err(metadata->shtr, "%s: error writing molecules\n", caller); + ERROR(metadata->shtr, "%s: error writing molecules\n", caller); goto exit; } @@ -558,7 +557,7 @@ exit: darray_char_release(&name); return res; error: - log_err(metadata->shtr, "%s: error reading molecules -- %s.\n", + ERROR(metadata->shtr, "%s: error reading molecules -- %s.\n", caller, res_to_cstr(res)); goto exit; } @@ -579,7 +578,7 @@ write_isotopes #define WRITE(Var, Nb) { \ if(fwrite((Var), sizeof(*(Var)), (Nb), stream) != (Nb)) { \ - log_err(metadata->shtr, "%s: error writing isotopes\n", caller); \ + ERROR(metadata->shtr, "%s: error writing isotopes\n", caller); \ res = RES_IO_ERR; \ goto error; \ } \ @@ -625,7 +624,7 @@ read_isotopes exit: return res; error: - log_err(metadata->shtr, "%s: error reading isotopes -- %s.\n", + ERROR(metadata->shtr, "%s: error reading isotopes -- %s.\n", caller, res_to_cstr(res)); goto exit; } @@ -663,7 +662,7 @@ shtr_isotope_metadata_load file = fopen(path, "r"); if(!file) { - log_err(shtr, "%s: error opening file `%s'.\n", FUNC_NAME, path); + ERROR(shtr, "%s: error opening file `%s'.\n", FUNC_NAME, path); res = RES_IO_ERR; goto error; } @@ -717,14 +716,14 @@ shtr_isotope_metadata_create_from_stream } else { \ res = RES_UNKNOWN_ERR; \ } \ - log_err(shtr, "%s: error reading isotope metadata -- %s.\n", \ + ERROR(shtr, "%s: error reading isotope metadata -- %s.\n", \ FUNC_NAME, res_to_cstr(res)); \ goto error; \ } \ } (void)0 READ(&version, 1); if(version != SHTR_ISOTOPE_METADATA_VERSION) { - log_err(shtr, + ERROR(shtr, "%s: unexpected isotope metadata version %d. " "Expecting a isotope metadata in version %d.\n", FUNC_NAME, version, SHTR_ISOTOPE_METADATA_VERSION); @@ -803,7 +802,7 @@ shtr_isotope_metadata_get_molecule goto error; } if(imolecule >= darray_molecule_size_get(&metadata->molecules)) { - log_err(metadata->shtr, "%s: invalid molecule index %lu.\n", + ERROR(metadata->shtr, "%s: invalid molecule index %lu.\n", FUNC_NAME, (unsigned long)imolecule); res = RES_BAD_ARG; goto error; @@ -866,7 +865,7 @@ shtr_isotope_metadata_write #define WRITE(Var, Nb) { \ if(fwrite((Var), sizeof(*(Var)), (Nb), stream) != (Nb)) { \ - log_err(metadata->shtr, "%s: error writing metadata\n", FUNC_NAME); \ + ERROR(metadata->shtr, "%s: error writing metadata\n", FUNC_NAME); \ res = RES_IO_ERR; \ goto error; \ } \ diff --git a/src/shtr_line_list.c b/src/shtr_line_list.c @@ -19,7 +19,6 @@ #include "shtr.h" #include "shtr_c.h" #include "shtr_line_list_c.h" -#include "shtr_log.h" #include "shtr_param.h" #include <rsys/cstr.h> @@ -37,7 +36,7 @@ create_line_list(struct shtr* shtr, struct shtr_line_list** out_list) list = MEM_CALLOC(shtr->allocator, 1, sizeof(*list)); if(!list) { - log_err(shtr, "Could not allocate the list of lines.\n"); + ERROR(shtr, "Could not allocate the list of lines.\n"); res = RES_MEM_ERR; goto error; } @@ -118,14 +117,14 @@ parse_line(struct shtr_line_list* list, struct txtrdr* txtrdr) /* Handle unavailable lower state energy */ PARSE(&ln.lower_state_energy, 10, double, "lower state energy",-INF,INF,1,1); if(ln.lower_state_energy == -1) { - log_warn(shtr, + WARN(shtr, "%s:%lu: the lower state energy is unavailable for this line, so it is " "ignored.\n", txtrdr_get_name(txtrdr), txtrdr_get_line_num(txtrdr)); goto exit; /* Skip the line */ } /* Check the domain validity */ if(ln.lower_state_energy < 0) { - log_err(shtr, + ERROR(shtr, "%s:%lu: invalid lower state energy %g. It must be in [0, INF].\n", txtrdr_get_name(txtrdr), txtrdr_get_line_num(txtrdr), ln.lower_state_energy); @@ -146,7 +145,7 @@ parse_line(struct shtr_line_list* list, struct txtrdr* txtrdr) *end = backup; str = end; if(strlen(str) != 93) { - log_err(list->shtr, "%s:%lu: missing data after delta air.\n", + ERROR(list->shtr, "%s:%lu: missing data after delta air.\n", param.path, (unsigned long)param.line); res = RES_BAD_ARG; goto error; @@ -156,7 +155,7 @@ parse_line(struct shtr_line_list* list, struct txtrdr* txtrdr) const struct shtr_line* last_ln = darray_line_cdata_get(&list->lines) + darray_line_size_get(&list->lines) - 1; if(last_ln->wavenumber > ln.wavenumber) { - log_err(list->shtr, + ERROR(list->shtr, "%s:%lu: lines are not sorted in ascending order wrt their wavenumber.\n", txtrdr_get_name(txtrdr), txtrdr_get_line_num(txtrdr)); res = RES_BAD_ARG; @@ -166,7 +165,7 @@ parse_line(struct shtr_line_list* list, struct txtrdr* txtrdr) res = darray_line_push_back(&list->lines, &ln); if(res != RES_OK) { - log_err(list->shtr, + ERROR(list->shtr, "%s:%lu: error storing the line -- %s.\n", param.path, (unsigned long)param.line, res_to_cstr(res)); goto error; @@ -196,7 +195,7 @@ load_stream res = txtrdr_stream(list->shtr->allocator, stream, name, 0/*No comment char*/, &txtrdr); if(res != RES_OK) { - log_err(shtr, "%s: error creating the text reader -- %s.\n", + ERROR(shtr, "%s: error creating the text reader -- %s.\n", name, res_to_cstr(res)); goto error; } @@ -204,7 +203,7 @@ load_stream for(;;) { res = txtrdr_read_line(txtrdr); if(res != RES_OK) { - log_err(shtr, "%s: error reading the line `%lu' -- %s.\n", + ERROR(shtr, "%s: error reading the line `%lu' -- %s.\n", name, (unsigned long)txtrdr_get_line_num(txtrdr), res_to_cstr(res)); goto error; } @@ -258,7 +257,7 @@ shtr_line_list_load file = fopen(path, "r"); if(!file) { - log_err(shtr, "%s: error opening file `%s'.\n", FUNC_NAME, path); + ERROR(shtr, "%s: error opening file `%s'.\n", FUNC_NAME, path); res = RES_IO_ERR; goto error; } @@ -313,14 +312,14 @@ shtr_line_list_create_from_stream } else { \ res = RES_UNKNOWN_ERR; \ } \ - log_err(shtr, "%s: error reading isotope metadata -- %s.\n", \ + ERROR(shtr, "%s: error reading isotope metadata -- %s.\n", \ FUNC_NAME, res_to_cstr(res)); \ goto error; \ } \ } (void)0 READ(&version, 1); if(version != SHTR_LINE_LIST_VERSION) { - log_err(shtr, + ERROR(shtr, "%s: unexpected line list version %d. " "Expecting a line list in version %d.\n", FUNC_NAME, version, SHTR_LINE_LIST_VERSION); @@ -331,7 +330,7 @@ shtr_line_list_create_from_stream READ(&nlines, 1); res = darray_line_resize(&list->lines, nlines); if(res != RES_OK) { - log_err(shtr, "%s: error allocating the line list -- %s.\n", + ERROR(shtr, "%s: error allocating the line list -- %s.\n", FUNC_NAME, res_to_cstr(res)); goto error; } @@ -403,7 +402,7 @@ shtr_line_list_write #define WRITE(Var, Nb) { \ if(fwrite((Var), sizeof(*(Var)), (Nb), stream) != (Nb)) { \ - log_err(list->shtr, "%s: error writing line list.\n", FUNC_NAME); \ + ERROR(list->shtr, "%s: error writing line list.\n", FUNC_NAME); \ res = RES_IO_ERR; \ goto error; \ } \ diff --git a/src/shtr_line_view.c b/src/shtr_line_view.c @@ -21,7 +21,6 @@ #include "shtr.h" #include "shtr_c.h" #include "shtr_line_list_c.h" -#include "shtr_log.h" #include <rsys/cstr.h> #include <rsys/dynamic_array_size_t.h> @@ -58,7 +57,7 @@ check_shtr_isotope_selection ASSERT(caller && molecule); if((size_t)molecule->id >= SHTR_MAX_MOLECULES_COUNT) { - log_err(shtr, + ERROR(shtr, "%s: molecule %d: invalid molecule identifier. " "It must be less than %d.\n", caller, molecule->id, SHTR_MAX_MOLECULES_COUNT); @@ -66,14 +65,14 @@ check_shtr_isotope_selection } if(molecule->cutoff <= 0) { - log_err(shtr, "%s: molecule %d: invalid cutoff %g.\n", + ERROR(shtr, "%s: molecule %d: invalid cutoff %g.\n", caller, molecule->id, molecule->cutoff); return RES_BAD_ARG; } FOR_EACH(i, 0, molecule->nisotopes) { if(molecule->isotope_ids_local[i] >= SHTR_MAX_ISOTOPES_COUNT) { - log_err(shtr, + ERROR(shtr, "%s: molecule %d: isotope %d: invalid isotope local identifier. " "It must be less than %d.\n", caller, @@ -99,13 +98,13 @@ check_shtr_line_view_create_args if(!args) return RES_BAD_ARG; if(args->wavenumber_range[0] > args->wavenumber_range[1]) { - log_err(shtr, "%s: invalid line view spectral range [%g, %g].\n", + ERROR(shtr, "%s: invalid line view spectral range [%g, %g].\n", caller, args->wavenumber_range[0], args->wavenumber_range[1]); return RES_BAD_ARG; } if(args->pressure < 0) { - log_err(shtr, "%s: invalid pressure %g.\n", caller, args->pressure); + ERROR(shtr, "%s: invalid pressure %g.\n", caller, args->pressure); return RES_BAD_ARG; } @@ -127,7 +126,7 @@ create_line_view(struct shtr* shtr, struct shtr_line_view** out_view) view = MEM_CALLOC(shtr->allocator, 1, sizeof(*view)); if(!view) { - log_err(shtr, "Could not allocate the line view.\n"); + ERROR(shtr, "Could not allocate the line view.\n"); res = RES_MEM_ERR; goto error; } @@ -210,7 +209,7 @@ select_lines res = darray_size_t_push_back(&view->line_ids, &iline); if(res != RES_OK) { - log_err(view->list->shtr, + ERROR(view->list->shtr, "%s: could not register the line into the view -- %s.\n", caller, res_to_cstr(res)); goto error; @@ -293,14 +292,14 @@ shtr_line_view_create_from_stream } else { \ res = RES_UNKNOWN_ERR; \ } \ - log_err(shtr, "%s: error reading isotope metadata -- %s.\n", \ + ERROR(shtr, "%s: error reading isotope metadata -- %s.\n", \ FUNC_NAME, res_to_cstr(res)); \ goto error; \ } \ } (void)0 READ(&version, 1); if(version != SHTR_LINE_VIEW_VERSION) { - log_err(shtr, + ERROR(shtr, "%s: unexpected line view version %d. " "Expecting a line view in version %d.\n", FUNC_NAME, version, SHTR_LINE_VIEW_VERSION); @@ -314,7 +313,8 @@ shtr_line_view_create_from_stream READ(&nids, 1); res = darray_size_t_resize(&view->line_ids, nids); if(res != RES_OK) { - log_err(shtr, "%s: error allocating the line view -- %s.\n", + ERROR(shtr, + "%s: error allocating the line indices of the line view -- %s.\n", FUNC_NAME, res_to_cstr(res)); goto error; } @@ -387,7 +387,7 @@ shtr_line_view_write #define WRITE(Var, Nb) { \ if(fwrite((Var), sizeof(*(Var)), (Nb), stream) != (Nb)) { \ - log_err(view->list->shtr, "%s: error writing line view.\n", FUNC_NAME); \ + ERROR(view->list->shtr, "%s: error writing line view.\n", FUNC_NAME); \ res = RES_IO_ERR; \ goto error; \ } \ diff --git a/src/shtr_log.c b/src/shtr_log.c @@ -1,127 +0,0 @@ -/* Copyright (C) 2022, 2025 |Méso|Star> (contact@meso-star.com) - * Copyright (C) 2025 Université de Lorraine - * Copyright (C) 2022 Centre National de la Recherche Scientifique - * Copyright (C) 2022 Université Paul Sabatier - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. */ - -#include "shtr_c.h" -#include "shtr_log.h" - -#include <rsys/cstr.h> -#include <rsys/logger.h> - -#include <stdarg.h> - -/******************************************************************************* - * Helper functions - ******************************************************************************/ -static INLINE void -log_msg - (const struct shtr* shtr, - const enum log_type stream, - const char* msg, - va_list vargs) -{ - ASSERT(shtr && msg); - if(shtr->verbose) { - res_T res; (void)res; - res = logger_vprint(shtr->logger, stream, msg, vargs); - ASSERT(res == RES_OK); - } -} - -static void -print_info(const char* msg, void* ctx) -{ - (void)ctx; - fprintf(stderr, MSG_INFO_PREFIX"%s", msg); -} - -static void -print_err(const char* msg, void* ctx) -{ - (void)ctx; - fprintf(stderr, MSG_ERROR_PREFIX"%s", msg); -} - -static void -print_warn(const char* msg, void* ctx) -{ - (void)ctx; - fprintf(stderr, MSG_WARNING_PREFIX"%s", msg); -} - -/******************************************************************************* - * Local functions - ******************************************************************************/ -res_T -setup_log_default(struct shtr* shtr) -{ - res_T res = RES_OK; - ASSERT(shtr); - - res = logger_init(shtr->allocator, &shtr->logger__); - if(res != RES_OK) { - if(shtr->verbose) { - fprintf(stderr, - MSG_ERROR_PREFIX - "Could not setup the default logger for the Star-HITRAN library -- %s.\n", - res_to_cstr(res)); - } - goto error; - } - logger_set_stream(&shtr->logger__, LOG_OUTPUT, print_info, NULL); - logger_set_stream(&shtr->logger__, LOG_ERROR, print_err, NULL); - logger_set_stream(&shtr->logger__, LOG_WARNING, print_warn, NULL); - shtr->logger = &shtr->logger__; - -exit: - return res; -error: - goto exit; -} - -void -log_info(const struct shtr* shtr, const char* msg, ...) -{ - va_list vargs_list; - ASSERT(shtr && msg); - - va_start(vargs_list, msg); - log_msg(shtr, LOG_OUTPUT, msg, vargs_list); - va_end(vargs_list); -} - -void -log_err(const struct shtr* shtr, const char* msg, ...) -{ - va_list vargs_list; - ASSERT(shtr && msg); - - va_start(vargs_list, msg); - log_msg(shtr, LOG_ERROR, msg, vargs_list); - va_end(vargs_list); -} - -void -log_warn(const struct shtr* shtr, const char* msg, ...) -{ - va_list vargs_list; - ASSERT(shtr && msg); - - va_start(vargs_list, msg); - log_msg(shtr, LOG_WARNING, msg, vargs_list); - va_end(vargs_list); -} diff --git a/src/shtr_log.h b/src/shtr_log.h @@ -1,73 +0,0 @@ -/* Copyright (C) 2022, 2025 |Méso|Star> (contact@meso-star.com) - * Copyright (C) 2025 Université de Lorraine - * Copyright (C) 2022 Centre National de la Recherche Scientifique - * Copyright (C) 2022 Université Paul Sabatier - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. */ - -#ifndef SHTR_LOG_H -#define SHTR_LOG_H - -#include <rsys/rsys.h> - -#define MSG_INFO_PREFIX "Star-HITRAN:\x1b[1m\x1b[32minfo\x1b[0m: " -#define MSG_ERROR_PREFIX "Star-HITRAN:\x1b[1m\x1b[31merror\x1b[0m: " -#define MSG_WARNING_PREFIX "Star-HITRAN:\x1b[1m\x1b[33mwarning\x1b[0m: " - -struct shtr; -struct logger; - -extern LOCAL_SYM res_T -setup_log_default - (struct shtr* shtr); - -/* Conditionally log a message on the LOG_OUTPUT stream of the shtr logger, - * with respect to its verbose flag */ -extern LOCAL_SYM void -log_info - (const struct shtr* shtr, - const char* msg, - ...) -#ifdef COMPILER_GCC - __attribute((format(printf, 2, 3))) -#endif -; - -/* Conditionally log a message on the LOG_ERROR stream of the shtr logger, - * with respect to its verbose flag */ -extern LOCAL_SYM void -log_err - (const struct shtr* shtr, - const char* msg, - ...) -#ifdef COMPILER_GCC - __attribute((format(printf, 2, 3))) -#endif -; - -/* Conditionally log a message on the LOG_WARNING stream of the shtr logger, - * with respect to its verbose flag */ -extern LOCAL_SYM void -log_warn - (const struct shtr* shtr, - const char* msg, - ...) -#ifdef COMPILER_GCC - __attribute((format(printf, 2, 3))) -#endif -; - - -#endif /* SHTR_LOG_H */ - diff --git a/src/shtr_param.c b/src/shtr_param.c @@ -17,7 +17,6 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include "shtr_c.h" -#include "shtr_log.h" #include "shtr_param.h" #include <rsys/cstr.h> @@ -44,7 +43,7 @@ || (desc->low == desc->upp && desc->is_low_incl && desc->is_upp_incl));\ \ if(!str) { \ - log_err(shtr, "%s:%lu: %s is missing.\n", \ + ERROR(shtr, "%s:%lu: %s is missing.\n", \ desc->path, (unsigned long)desc->line, desc->name); \ res = RES_BAD_ARG; \ goto error; \ @@ -52,7 +51,7 @@ \ res = cstr_to_##Type(str, &param); \ if(res != RES_OK) { \ - log_err(shtr, "%s:%lu: invalid %s `%s'.\n", \ + ERROR(shtr, "%s:%lu: invalid %s `%s'.\n", \ desc->path, (unsigned long)desc->line, desc->name, str); \ res = RES_BAD_ARG; \ goto error; \ @@ -60,7 +59,7 @@ \ if(param < desc->low || (param == desc->low && !desc->is_low_incl) \ || param > desc->upp || (param == desc->upp && !desc->is_upp_incl)) { \ - log_err(shtr, \ + ERROR(shtr, \ "%s:%lu: invalid %s `%s'. It must be in " \ "%c"CONCAT(C_FORMAT_, Type)", "CONCAT(C_FORMAT_, Type)"%c.\n", \ desc->path, (unsigned long)desc->line, desc->name, str, \