mrumtl

Describe materials that vary spectrally
git clone git://git.meso-star.fr/mrumtl.git
Log | Files | Refs | README | LICENSE

commit 118f07a5f8c4d75f26427d23dbccc841c69e70eb
parent 4ab084c8678074ec4c532ebc4c3e8fcd7de2e783
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Thu,  7 Jul 2022 12:03:11 +0200

Refactor log functions

Diffstat:
Mcmake/CMakeLists.txt | 4++--
Msrc/mrumtl.c | 159+++----------------------------------------------------------------------------
Asrc/mrumtl_c.h | 84+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/mrumtl_log.c | 124+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/mrumtl_log.h | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 283 insertions(+), 156 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -40,8 +40,8 @@ set(VERSION_MINOR 0) set(VERSION_PATCH 1) set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) -set(MRUMTL_FILES_SRC mrumtl.c) -set(MRUMTL_FILES_INC ) +set(MRUMTL_FILES_SRC mrumtl.c mrumtl_log.c) +set(MRUMTL_FILES_INC mrumtl_c.h mrumtl_log.h) set(MRUMTL_FILES_INC_API mrumtl.h) set(MRUMTL_FILES_DOC COPYING README.md) diff --git a/src/mrumtl.c b/src/mrumtl.c @@ -16,74 +16,15 @@ #define _POSIX_C_SOURCE 200112L /* strtok_r support */ #include "mrumtl.h" +#include "mrumtl_c.h" +#include "mrumtl_log.h" #include <rsys/algorithm.h> #include <rsys/cstr.h> -#include <rsys/dynamic_array.h> -#include <rsys/logger.h> -#include <rsys/mem_allocator.h> -#include <rsys/ref_count.h> -#include <rsys/str.h> #include <rsys/text_reader.h> #include <string.h> -enum brdf_list_type { - BRDF_LIST_BAND, - BRDF_LIST_WLEN, - BRDF_LIST_NONE__ -}; - -struct brdf_lambertian { - double reflectivity; -}; - -struct brdf_specular { - double reflectivity; -}; - -struct mrumtl_brdf { - enum mrumtl_brdf_type type; - union { - struct brdf_lambertian lambertian; - struct brdf_specular specular; - } value; -}; - -struct brdf_wlen { - double wlen; /* In nanometers */ - struct mrumtl_brdf brdf; -}; - -struct brdf_band { - double wlen_min; /* Inclusive bound in nanometers */ - double wlen_max; /* Exclusive bound In nanometers */ - struct mrumtl_brdf brdf; -}; - -/* Define the dynamic array of per wavelength BRDF */ -#define DARRAY_NAME brdf_wlen -#define DARRAY_DATA struct brdf_wlen -#include <rsys/dynamic_array.h> - -/* Define the dynamic array of per band BRDF */ -#define DARRAY_NAME brdf_band -#define DARRAY_DATA struct brdf_band -#include <rsys/dynamic_array.h> - -struct mrumtl { - enum brdf_list_type brdf_list_type; - struct darray_brdf_wlen brdf_wlens; - struct darray_brdf_band brdf_bands; - struct str name; /* Name of the loaded material */ - - int verbose; - struct logger* logger; - struct logger logger__; - struct mem_allocator* allocator; - ref_T ref; -}; - #define MSG_INFO_PREFIX "MruMtl:\x1b[1m\x1b[32minfo\x1b[0m: " #define MSG_ERROR_PREFIX "MruMtl:\x1b[1m\x1b[31merror\x1b[0m: " #define MSG_WARNING_PREFIX "MruMtl:\x1b[1m\x1b[33mwarning\x1b[0m: " @@ -98,96 +39,6 @@ check_mrumtl_create_args(const struct mrumtl_create_args* args) return args ? RES_OK : RES_BAD_ARG; } -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); -} - -static res_T -setup_default_logger(struct mem_allocator* allocator, struct logger* logger) -{ - res_T res = RES_OK; - ASSERT(logger); - res = logger_init(allocator, logger); - if(res != RES_OK) return res; - logger_set_stream(logger, LOG_OUTPUT, print_info, NULL); - logger_set_stream(logger, LOG_ERROR, print_err, NULL); - logger_set_stream(logger, LOG_WARNING, print_warn, NULL); - return RES_OK; -} - -static INLINE void -log_msg - (const struct mrumtl* mrumtl, - const enum log_type stream, - const char* msg, - va_list vargs) -{ - ASSERT(mrumtl && msg); - if(mrumtl->verbose) { - res_T res; (void)res; - res = logger_vprint(mrumtl->logger, stream, msg, vargs); - ASSERT(res == RES_OK); - } -} - -static INLINE void -log_err - (const struct mrumtl* mrumtl, - const char* msg, ...) -#ifdef COMPILER_GCC - __attribute((format(printf, 2, 3))) -#endif -; - -static INLINE void -log_warn - (const struct mrumtl* mrumtl, - const char* msg, ...) -#ifdef COMPILER_GCC - __attribute((format(printf, 2, 3))) -#endif -; - -void -log_err(const struct mrumtl* mrumtl, const char* msg, ...) -{ - va_list vargs_list; - ASSERT(mrumtl && msg); - - va_start(vargs_list, msg); - log_msg(mrumtl, LOG_ERROR, msg, vargs_list); - va_end(vargs_list); -} - -void -log_warn(const struct mrumtl* mrumtl, const char* msg, ...) -{ - va_list vargs_list; - ASSERT(mrumtl && msg); - - va_start(vargs_list, msg); - log_msg(mrumtl, LOG_WARNING, msg, vargs_list); - - va_end(vargs_list); -} - static res_T parse_brdf_lambertian (struct mrumtl* mrumtl, @@ -848,7 +699,7 @@ mrumtl_create mrumtl = MEM_CALLOC(allocator, 1, sizeof(*mrumtl)); if(!mrumtl) { if(args->verbose) { - #define ERR_STR "Could not allocate the MRUMTL handler.\n" + #define ERR_STR "Could not allocate the MruMtl handler.\n" if(args->logger) { logger_print(args->logger, LOG_ERROR, ERR_STR); } else { @@ -870,11 +721,11 @@ mrumtl_create if(args->logger) { mrumtl->logger = args->logger; } else { - res = setup_default_logger(mrumtl->allocator, &mrumtl->logger__); + res = setup_log_default(mrumtl); if(res != RES_OK) { if(args->verbose) { fprintf(stderr, MSG_ERROR_PREFIX - "Could not setup the MRUMTL logger.\n"); + "Could not setup the MruMtl logger.\n"); } goto error; } diff --git a/src/mrumtl_c.h b/src/mrumtl_c.h @@ -0,0 +1,84 @@ +/* Copyright (C) 2020, 2021, 2022 |Meso|Star> (contact@meso-star.com) + * + * 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 MRUMTL_C_H +#define MRUMTL_C_H + +#include "mrumtl.h" + +#include <rsys/dynamic_array.h> +#include <rsys/mem_allocator.h> +#include <rsys/logger.h> +#include <rsys/ref_count.h> +#include <rsys/str.h> + +enum brdf_list_type { + BRDF_LIST_BAND, + BRDF_LIST_WLEN, + BRDF_LIST_NONE__ +}; + +struct brdf_lambertian { + double reflectivity; +}; + +struct brdf_specular { + double reflectivity; +}; + +struct mrumtl_brdf { + enum mrumtl_brdf_type type; + union { + struct brdf_lambertian lambertian; + struct brdf_specular specular; + } value; +}; + +struct brdf_wlen { + double wlen; /* In nanometers */ + struct mrumtl_brdf brdf; +}; + +struct brdf_band { + double wlen_min; /* Inclusive bound in nanometers */ + double wlen_max; /* Exclusive bound In nanometers */ + struct mrumtl_brdf brdf; +}; + +/* Define the dynamic array of per wavelength BRDF */ +#define DARRAY_NAME brdf_wlen +#define DARRAY_DATA struct brdf_wlen +#include <rsys/dynamic_array.h> + +/* Define the dynamic array of per band BRDF */ +#define DARRAY_NAME brdf_band +#define DARRAY_DATA struct brdf_band +#include <rsys/dynamic_array.h> + + +struct mrumtl { + enum brdf_list_type brdf_list_type; + struct darray_brdf_wlen brdf_wlens; + struct darray_brdf_band brdf_bands; + struct str name; /* Name of the loaded material */ + + int verbose; + struct logger* logger; + struct logger logger__; + struct mem_allocator* allocator; + ref_T ref; +}; + +#endif /* MRUMTL_C_H */ diff --git a/src/mrumtl_log.c b/src/mrumtl_log.c @@ -0,0 +1,124 @@ +/* Copyright (C) 2020, 2021, 2022 |Meso|Star> (contact@meso-star.com) + * + * 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 "mrumtl_c.h" +#include "mrumtl_log.h" + +#include <rsys/cstr.h> +#include <rsys/logger.h> + +#include <stdarg.h> + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static INLINE void +log_msg + (const struct mrumtl* mrumtl, + const enum log_type stream, + const char* msg, + va_list vargs) +{ + ASSERT(mrumtl && msg); + if(mrumtl->verbose) { + res_T res; (void)res; + res = logger_vprint(mrumtl->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 mrumtl* mrumtl) +{ + res_T res = RES_OK; + ASSERT(mrumtl); + + res = logger_init(mrumtl->allocator, &mrumtl->logger__); + if(res != RES_OK) { + if(mrumtl->verbose) { + fprintf(stderr, + MSG_ERROR_PREFIX + "Could not setup the default logger -- %s.\n", + res_to_cstr(res)); + } + goto error; + } + logger_set_stream(&mrumtl->logger__, LOG_OUTPUT, print_info, NULL); + logger_set_stream(&mrumtl->logger__, LOG_ERROR, print_err, NULL); + logger_set_stream(&mrumtl->logger__, LOG_WARNING, print_warn, NULL); + mrumtl->logger = &mrumtl->logger__; + +exit: + return res; +error: + goto exit; +} + +void +log_info(const struct mrumtl* mrumtl, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(mrumtl && msg); + + va_start(vargs_list, msg); + log_msg(mrumtl, LOG_OUTPUT, msg, vargs_list); + va_end(vargs_list); +} + +void +log_err(const struct mrumtl* mrumtl, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(mrumtl && msg); + + va_start(vargs_list, msg); + log_msg(mrumtl, LOG_ERROR, msg, vargs_list); + va_end(vargs_list); +} + +void +log_warn(const struct mrumtl* mrumtl, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(mrumtl && msg); + + va_start(vargs_list, msg); + log_msg(mrumtl, LOG_WARNING, msg, vargs_list); + va_end(vargs_list); +} diff --git a/src/mrumtl_log.h b/src/mrumtl_log.h @@ -0,0 +1,68 @@ +/* Copyright (C) 2020, 2021, 2022 |Meso|Star> (contact@meso-star.com) + * + * 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 MRUMTL_LOG_H +#define MRUMTL_LOG_H + +#include <rsys/rsys.h> + +#define MSG_INFO_PREFIX "MruMtl:\x1b[1m\x1b[32minfo\x1b[0m: " +#define MSG_ERROR_PREFIX "MruMtl:\x1b[1m\x1b[31merror\x1b[0m: " +#define MSG_WARNING_PREFIX "MruMtl:\x1b[1m\x1b[33mwarning\x1b[0m: " + +struct mrumtl; +struct logger; + +extern LOCAL_SYM res_T +setup_log_default + (struct mrumtl* mrumtl); + +/* Conditionally log a message on the LOG_OUTPUT stream of the mrumtl logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_info + (const struct mrumtl* mrumtl, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +/* Conditionally log a message on the LOG_ERROR stream of the mrumtl logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_err + (const struct mrumtl* mrumtl, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +/* Conditionally log a message on the LOG_WARNING stream of the mrumtl logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_warn + (const struct mrumtl* mrumtl, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +#endif /* MRUMTL_LOG_H */