commit 2c93c4e75ff10e1382283b4beb42d60c0800f933
parent e41ca868f4dbfac9fb981229395cb2b137ab562a
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Tue, 16 Jun 2020 16:15:56 +0200
Setup a default logger
Diffstat:
15 files changed, 227 insertions(+), 72 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -69,6 +69,7 @@ set(SDIS_FILES_SRC
sdis_green.c
sdis_heat_path.c
sdis_interface.c
+ sdis_log.c
sdis_medium.c
sdis_realisation.c
sdis_scene.c
@@ -88,6 +89,7 @@ set(SDIS_FILES_INC
sdis_heat_path_convective_Xd.h
sdis_heat_path_radiative_Xd.h
sdis_interface_c.h
+ sdis_log.h
sdis_medium_c.h
sdis_realisation.h
sdis_realisation_Xd.h
diff --git a/src/sdis_data.c b/src/sdis_data.c
@@ -15,6 +15,7 @@
#include "sdis.h"
#include "sdis_device_c.h"
+#include "sdis_log.h"
#include <rsys/math.h>
#include <rsys/mem_allocator.h>
diff --git a/src/sdis_device.c b/src/sdis_device.c
@@ -15,7 +15,9 @@
#include "sdis.h"
#include "sdis_device_c.h"
+#include "sdis_log.h"
+#include <rsys/cstr.h>
#include <rsys/logger.h>
#include <rsys/mem_allocator.h>
@@ -28,21 +30,6 @@
* Helper functions
******************************************************************************/
static void
-log_msg
- (struct sdis_device* dev,
- const enum log_type stream,
- const char* msg,
- va_list vargs)
-{
- ASSERT(dev && msg);
- if(dev->verbose) {
- res_T res; (void)res;
- res = logger_vprint(dev->logger, stream, msg, vargs);
- ASSERT(res == RES_OK);
- }
-}
-
-static void
device_release(ref_T* ref)
{
struct sdis_device* dev;
@@ -50,6 +37,7 @@ device_release(ref_T* ref)
dev = CONTAINER_OF(ref, struct sdis_device, ref);
if(dev->s2d_dev) S2D(device_ref_put(dev->s2d_dev));
if(dev->s3d_dev) S3D(device_ref_put(dev->s3d_dev));
+ if(dev->logger == &dev->logger__) logger_release(&dev->logger__);
ASSERT(flist_name_is_empty(&dev->interfaces_names));
ASSERT(flist_name_is_empty(&dev->media_names));
flist_name_release(&dev->interfaces_names);
@@ -78,19 +66,21 @@ sdis_device_create
goto error;
}
- log = logger ? logger : LOGGER_DEFAULT;
allocator = mem_allocator ? mem_allocator : &mem_default_allocator;
dev = MEM_CALLOC(allocator, 1, sizeof(struct sdis_device));
if(!dev) {
if(verbose) {
- /* Do not use helper log functions since dev is not initialised */
- CHK(logger_print(log, LOG_ERROR,
- "%s: could not allocate the Stardis device.\n", FUNC_NAME) == RES_OK);
+ #define ERR_STR STR(FUNC_NAME)": could not allocate the Stardis device -- %s."
+ if(logger) {
+ logger_print(logger, LOG_ERROR, ERR_STR, res_to_cstr(res));
+ } else {
+ fprintf(stderr, MSG_ERROR_PREFIX ERR_STR, res_to_cstr(res));
+ }
+ #undef ERR_STR
}
res = RES_MEM_ERR;
goto error;
}
- dev->logger = log;
dev->allocator = allocator;
dev->verbose = verbose;
dev->nthreads = MMIN(nthreads_hint, (unsigned)omp_get_num_procs());
@@ -98,16 +88,26 @@ sdis_device_create
flist_name_init(allocator, &dev->interfaces_names);
flist_name_init(allocator, &dev->media_names);
+ if(logger) {
+ dev->logger = logger;
+ } else {
+ setup_log_default(dev);
+ }
+ log_info(dev, "use %lu %s.\n", (unsigned long)dev->nthreads,
+ dev->nthreads == 1 ? "thread" : "threads");
+
res = s2d_device_create(log, allocator, 0, &dev->s2d_dev);
if(res != RES_OK) {
log_err(dev,
- "%s: could not create the Star-2D device on Stardis.\n", FUNC_NAME);
+ "%s: could not create the Star-2D device on Stardis -- %s.\n",
+ FUNC_NAME, res_to_cstr(res));
}
res = s3d_device_create(log, allocator, 0, &dev->s3d_dev);
if(res != RES_OK) {
log_err(dev,
- "%s: could not create the Star-3D device on Stardis.\n", FUNC_NAME);
+ "%s: could not create the Star-3D device on Stardis -- %s.\n",
+ FUNC_NAME, res_to_cstr(res));
goto error;
}
@@ -138,28 +138,3 @@ sdis_device_ref_put(struct sdis_device* dev)
return RES_OK;
}
-/*******************************************************************************
- * Local functions
- ******************************************************************************/
-void
-log_err(struct sdis_device* dev, const char* msg, ...)
-{
- va_list vargs_list;
- ASSERT(dev && msg);
-
- va_start(vargs_list, msg);
- log_msg(dev, LOG_ERROR, msg, vargs_list);
- va_end(vargs_list);
-}
-
-void
-log_warn(struct sdis_device* dev, const char* msg, ...)
-{
- va_list vargs_list;
- ASSERT(dev && msg);
-
- va_start(vargs_list, msg);
- log_msg(dev, LOG_WARNING, msg, vargs_list);
- va_end(vargs_list);
-}
-
diff --git a/src/sdis_device_c.h b/src/sdis_device_c.h
@@ -20,6 +20,7 @@
#include <rsys/dynamic_array.h>
#include <rsys/free_list.h>
+#include <rsys/logger.h>
#include <rsys/ref_count.h>
struct name { FITEM; };
@@ -28,6 +29,7 @@ struct name { FITEM; };
struct sdis_device {
struct logger* logger;
+ struct logger logger__; /* Default logger */
struct mem_allocator* allocator;
unsigned nthreads;
int verbose;
@@ -41,29 +43,5 @@ struct sdis_device {
ref_T ref;
};
-/* Conditionally log a message on the LOG_ERROR stream of the device logger,
- * with respect to the device verbose flag */
-extern LOCAL_SYM void
-log_err
- (struct sdis_device* dev,
- const char* msg,
- ...)
-#ifdef COMPILER_GCC
- __attribute((format(printf, 2, 3)))
-#endif
-;
-
-/* Conditionally log a message on the LOG_WARNING stream of the device logger,
- * with respect to the device verbose flag */
-extern LOCAL_SYM void
-log_warn
- (struct sdis_device* dev,
- const char* msg,
- ...)
-#ifdef COMPILER_GCC
- __attribute((format(printf, 2, 3)))
-#endif
-;
-
#endif /* SDIS_DEVICE_C_H */
diff --git a/src/sdis_estimator_buffer.c b/src/sdis_estimator_buffer.c
@@ -17,6 +17,7 @@
#include "sdis_device_c.h"
#include "sdis_estimator_c.h"
#include "sdis_estimator_buffer_c.h"
+#include "sdis_log.h"
struct sdis_estimator_buffer {
struct sdis_estimator** estimators; /* Row major per pixe lestimators */
diff --git a/src/sdis_green.c b/src/sdis_green.c
@@ -16,6 +16,7 @@
#include "sdis_device_c.h"
#include "sdis_estimator_c.h"
#include "sdis_green.h"
+#include "sdis_log.h"
#include "sdis_medium_c.h"
#include "sdis_misc.h"
#include "sdis_interface_c.h"
diff --git a/src/sdis_heat_path_radiative_Xd.h b/src/sdis_heat_path_radiative_Xd.h
@@ -17,6 +17,7 @@
#include "sdis_green.h"
#include "sdis_heat_path.h"
#include "sdis_interface_c.h"
+#include "sdis_log.h"
#include "sdis_medium_c.h"
#include "sdis_misc.h"
#include "sdis_scene_c.h"
diff --git a/src/sdis_interface.c b/src/sdis_interface.c
@@ -16,6 +16,7 @@
#include "sdis.h"
#include "sdis_device_c.h"
#include "sdis_interface_c.h"
+#include "sdis_log.h"
#include "sdis_scene_c.h"
#include <rsys/double2.h>
diff --git a/src/sdis_log.c b/src/sdis_log.c
@@ -0,0 +1,119 @@
+/* Copyright (C) 2016-2019 |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/>. */
+
+#define _POSIX_C_SOURCE 200112L /* snprintf support */
+
+#include "sdis_device_c.h"
+#include "sdis_log.h"
+
+#include <rsys/logger.h>
+
+#include <stdarg.h>
+
+/*******************************************************************************
+ * Helper functions
+ ******************************************************************************/
+static void
+log_msg
+ (const struct sdis_device* dev,
+ const enum log_type stream,
+ const char* msg,
+ va_list vargs)
+{
+ ASSERT(dev && msg);
+ if(dev->verbose) {
+ CHK(logger_vprint(dev->logger, stream, msg, vargs) == 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 sdis_device* dev)
+{
+ res_T res = RES_OK;
+ ASSERT(dev);
+
+ res = logger_init(dev->allocator, &dev->logger__);
+ if(res != RES_OK) {
+ if(dev->verbose) print_err("Could not setup the logger.\n", NULL);
+ goto error;
+ }
+ logger_set_stream(&dev->logger__, LOG_OUTPUT, print_info, NULL);
+ logger_set_stream(&dev->logger__, LOG_ERROR, print_err, NULL);
+ logger_set_stream(&dev->logger__, LOG_WARNING, print_warn, NULL);
+ dev->logger = &dev->logger__;
+
+exit:
+ return res;
+error:
+ goto exit;
+}
+
+void
+log_info(const struct sdis_device* dev, const char* msg, ...)
+{
+ va_list vargs_list;
+ ASSERT(dev && msg);
+
+ va_start(vargs_list, msg);
+ log_msg(dev, LOG_OUTPUT, msg, vargs_list);
+ va_end(vargs_list);
+}
+
+void
+log_err(const struct sdis_device* dev, const char* msg, ...)
+{
+ va_list vargs_list;
+ ASSERT(dev && msg);
+
+ va_start(vargs_list, msg);
+ log_msg(dev, LOG_ERROR, msg, vargs_list);
+ va_end(vargs_list);
+}
+
+void
+log_warn(const struct sdis_device* dev, const char* msg, ...)
+{
+ va_list vargs_list;
+ ASSERT(dev && msg);
+
+ va_start(vargs_list, msg);
+ log_msg(dev, LOG_WARNING, msg, vargs_list);
+ va_end(vargs_list);
+}
+
diff --git a/src/sdis_log.h b/src/sdis_log.h
@@ -0,0 +1,72 @@
+/* Copyright (C) 2016-2019 |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 SDIS_LOG_H
+#define SDIS_LOG_H
+
+#include <rsys/rsys.h>
+
+#ifdef OS_UNIX
+ /* On UNIX assume a VT100-like terminal emulator */
+ #define MSG_INFO_PREFIX "stardis-solver (\x1b[1m\x1b[32minfo\x1b[0m): "
+ #define MSG_ERROR_PREFIX "stardis-solver (\x1b[1m\x1b[31merror\x1b[0m): "
+ #define MSG_WARNING_PREFIX "stardis-solver (\x1b[1m\x1b[33mwarning\x1b[0m): "
+#else
+ #define MSG_INFO_PREFIX "stardis-solver (info): "
+ #define MSG_ERROR_PREFIX "stardis-solver (error): "
+ #define MSG_WARNING_PREFIX "stardis-solver (warning): "
+#endif
+
+extern LOCAL_SYM res_T
+setup_log_default
+ (struct sdis_device* dev);
+
+/* Conditionally log a message on the LOG_OUTPUT stream of the sdis logger,
+ * with respect to its verbose flag */
+extern LOCAL_SYM void
+log_info
+ (const struct sdis_device* dev,
+ const char* msg,
+ ...)
+#ifdef COMPILER_GCC
+ __attribute((format(printf, 2, 3)))
+#endif
+;
+
+/* Conditionally log a message on the LOG_ERROR stream of the sdis logger,
+ * with respect to its verbose flag */
+extern LOCAL_SYM void
+log_err
+ (const struct sdis_device* dev,
+ const char* msg,
+ ...)
+#ifdef COMPILER_GCC
+ __attribute((format(printf, 2, 3)))
+#endif
+;
+
+/* Conditionally log a message on the LOG_WARNING stream of the sdis logger,
+ * with respect to its verbose flag */
+extern LOCAL_SYM void
+log_warn
+ (const struct sdis_device* dev,
+ const char* msg,
+ ...)
+#ifdef COMPILER_GCC
+ __attribute((format(printf, 2, 3)))
+#endif
+;
+
+#endif /* SDIS_LOG_H */
diff --git a/src/sdis_medium.c b/src/sdis_medium.c
@@ -15,6 +15,7 @@
#include "sdis.h"
#include "sdis_device_c.h"
+#include "sdis_log.h"
#include "sdis_medium_c.h"
#include <rsys/mem_allocator.h>
diff --git a/src/sdis_realisation_Xd.h b/src/sdis_realisation_Xd.h
@@ -16,6 +16,7 @@
#include "sdis_device_c.h"
#include "sdis_heat_path.h"
#include "sdis_interface_c.h"
+#include "sdis_log.h"
#include "sdis_medium_c.h"
#include "sdis_misc.h"
#include "sdis_scene_c.h"
diff --git a/src/sdis_scene_Xd.h b/src/sdis_scene_Xd.h
@@ -19,6 +19,7 @@
#define SDIS_SCENE_XD_H
#include "sdis_interface_c.h"
+#include "sdis_log.h"
#include "sdis_medium_c.h"
#include "sdis_scene_c.h"
diff --git a/src/sdis_solve_probe_boundary_Xd.h b/src/sdis_solve_probe_boundary_Xd.h
@@ -15,6 +15,7 @@
#include "sdis_device_c.h"
#include "sdis_estimator_c.h"
+#include "sdis_log.h"
#include "sdis_medium_c.h"
#include "sdis_misc.h"
#include "sdis_realisation.h"
diff --git a/src/test_sdis_scene.c b/src/test_sdis_scene.c
@@ -341,7 +341,7 @@ main(int argc, char** argv)
interface_shader.convection_coef = DUMMY_INTERFACE_SHADER.convection_coef;
OK(mem_init_proxy_allocator(&allocator, &mem_default_allocator));
- OK(sdis_device_create(NULL, &allocator, 1, 0, &dev));
+ OK(sdis_device_create(NULL, &allocator, SDIS_NTHREADS_DEFAULT, 1, &dev));
OK(sdis_fluid_create(dev, &fluid_shader, NULL, &fluid));
OK(sdis_solid_create(dev, &solid_shader, NULL, &solid));