From e5ed24ec98bed363f4a1cdd83f8ab139c0c60b74 Mon Sep 17 00:00:00 2001
From: Andreas Gattringer <gattringera@a772-cvl-ws23.biodiv.univie.ac.at>
Date: Tue, 21 Feb 2023 09:35:47 +0100
Subject: [PATCH] fix logging bugs

---
 src/cats/actions/cats_actions.c                          | 4 +++-
 src/cats/actions/setup_actions.c                         | 2 +-
 src/cats/configuration/check_configuration.c             | 2 +-
 src/cats/configuration/configuration.c                   | 2 +-
 src/cats/configuration/load_configuration.c              | 2 +-
 src/cats/configuration/load_configuration_environments.c | 2 +-
 src/cats/data/cats_datatypes.c                           | 2 +-
 src/cats/debug/debug.c                                   | 2 +-
 src/cats/environment/environment.c                       | 8 ++++----
 src/cats/environment/environment_set.c                   | 2 +-
 src/cats/grids/cats_grid.c                               | 2 +-
 src/cats/hybrid/scalefactor.c                            | 4 ++--
 src/cats/inline_population.h                             | 2 +-
 src/cats/misc/cats_random.c                              | 2 +-
 src/cats/mpi/mpi_grid_helpers.c                          | 2 +-
 src/cats/populations/carrying_capacity.c                 | 2 +-
 src/cats/vital_rates/setup_rates.c                       | 2 +-
 17 files changed, 23 insertions(+), 21 deletions(-)

diff --git a/src/cats/actions/cats_actions.c b/src/cats/actions/cats_actions.c
index 2210b6f..6254641 100644
--- a/src/cats/actions/cats_actions.c
+++ b/src/cats/actions/cats_actions.c
@@ -64,7 +64,9 @@ void run_simulation_phase(struct cats_grid **grid, struct cats_configuration *co
 
 
         for (int action = 0; action < conf->cycle.num_actions; action++) {
-                if (!(conf->cycle.actions[action].stages & stage)) continue;
+                if (!(conf->cycle.actions[action].stages & stage)){
+                        continue;
+                }
                 conf->cycle.current_action = action;
 
                 struct cats_timer timer = start_new_timer();
diff --git a/src/cats/actions/setup_actions.c b/src/cats/actions/setup_actions.c
index 5902def..1c03f48 100644
--- a/src/cats/actions/setup_actions.c
+++ b/src/cats/actions/setup_actions.c
@@ -280,7 +280,7 @@ const char *get_action_registry_name_from_id(const struct cats_configuration *co
 {
         const struct action_function_registry *registry = &conf->action_functions;
         if (id < 0 || id >= registry->count) {
-                log_message(LOG_ERROR, "%s: invalid id %d");
+                log_message(LOG_ERROR, "%s: invalid id %d", __func__, id);
                 exit(EXIT_FAILURE);
         }
 
diff --git a/src/cats/configuration/check_configuration.c b/src/cats/configuration/check_configuration.c
index 3f32ce3..6726076 100644
--- a/src/cats/configuration/check_configuration.c
+++ b/src/cats/configuration/check_configuration.c
@@ -64,7 +64,7 @@ int rate_failed(cats_dt_rates value, char *name)
 int range_float_check_at_least(cats_dt_rates value, cats_dt_rates larger_than, char *name)
 {
         if (value < larger_than) {
-                log_message(LOG_ERROR, "%s out of range: is %d, has to be >= %d", name, value, larger_than);
+                log_message(LOG_ERROR, "%s out of range: is %Lf, has to be >= %Lf", name, value, larger_than);
                 return 1;
         }
 
diff --git a/src/cats/configuration/configuration.c b/src/cats/configuration/configuration.c
index 6841aaf..00b2e74 100644
--- a/src/cats/configuration/configuration.c
+++ b/src/cats/configuration/configuration.c
@@ -192,7 +192,7 @@ void post_process_configuration(struct cats_configuration *conf)
                 if (conf->command_line_options.lambda_years > 0) {
                         years = conf->command_line_options.lambda_years;
                 }
-                log_message(LOG_IMPORTANT, "overriding simulation length to %d for lambda test");
+                log_message(LOG_IMPORTANT, "overriding simulation length to %d for lambda test", years);
                 conf->time.simulation_length = years;
                 conf->overlays.have_overlays = false;
         }
diff --git a/src/cats/configuration/load_configuration.c b/src/cats/configuration/load_configuration.c
index 8cb7da8..c02853f 100644
--- a/src/cats/configuration/load_configuration.c
+++ b/src/cats/configuration/load_configuration.c
@@ -179,7 +179,7 @@ void set_suitability_from_string(cats_dt_rates *value, const struct cats_species
                 bool success = string_to_double(copy, &temp_value);
                 if (success == false || isnan(temp_value)) {
                         log_message(LOG_ERROR,
-                                    "could not interpret '%s' as suitability value",
+                                    "%s: could not interpret '%s' as suitability value",
                                     __func__, copy);
                         exit_cats(EXIT_FAILURE);
                 }
diff --git a/src/cats/configuration/load_configuration_environments.c b/src/cats/configuration/load_configuration_environments.c
index 0a34875..7059ee5 100644
--- a/src/cats/configuration/load_configuration_environments.c
+++ b/src/cats/configuration/load_configuration_environments.c
@@ -38,7 +38,7 @@ enum environment_variable_type get_environment_variable_type_from_string(const c
         if (strcmp(string, "direct rate") == 0) return ENV_VAR_DIRECT;
         if (strcmp(string, "constant") == 0) return ENV_VAR_CONSTANT;
 
-        log_message(LOG_ERROR, "%s: unknown environment variable type '%s', valid values: 'predictor', 'suitability'");
+        log_message(LOG_ERROR, "%s: unknown environment variable type '%s', valid values: 'predictor', 'suitability'", __func__, string);
         exit_cats(EXIT_FAILURE);
 
 }
diff --git a/src/cats/data/cats_datatypes.c b/src/cats/data/cats_datatypes.c
index f1fabcc..656cc83 100644
--- a/src/cats/data/cats_datatypes.c
+++ b/src/cats/data/cats_datatypes.c
@@ -139,7 +139,7 @@ bool string_to_integer(char *string, int32_t *value)
         long converted = strtol(string, &end_pointer, 10);
 
         if (strlen(end_pointer) != 0) {
-                log_message(LOG_WARNING, "%s: invalid or unused characters when converting '%s' to integer %d: '%s'",
+                log_message(LOG_WARNING, "%s: invalid or unused characters when converting '%s' to integer %ld: '%s'",
                             __func__, string, converted, end_pointer);
                 return false;
         }
diff --git a/src/cats/debug/debug.c b/src/cats/debug/debug.c
index 1a44cbf..7bcf237 100644
--- a/src/cats/debug/debug.c
+++ b/src/cats/debug/debug.c
@@ -58,7 +58,7 @@ void reset_threading_grid(struct cats_debug_options *dbg)
 void mark_cell_done(struct cats_debug_options *dbg, cats_dt_coord row, cats_dt_coord col)
 {
         if (dbg->threading_grid[row][col] >= 0) {
-                log_message(LOG_ERROR, "cell %d %d has already been done");
+                log_message(LOG_ERROR, "cell %d %d has already been done", row, col);
                 exit(EXIT_FAILURE);
         }
         dbg->threading_grid[row][col] = timestamp();
diff --git a/src/cats/environment/environment.c b/src/cats/environment/environment.c
index c87fbfc..0f85e76 100644
--- a/src/cats/environment/environment.c
+++ b/src/cats/environment/environment.c
@@ -142,7 +142,7 @@ load_environment_raster(struct cats_configuration *conf, struct cats_environment
         assert(raster != NULL);
         assert(filename != NULL);
         if (!environment->quiet) {
-                log_message(LOG_EMPTY, "");
+                log_message(LOG_EMPTY, " ");
                 log_message(LOG_INFO, "\t%s: loading <%s> from '%s' (year %d)", __func__,
                             get_raster_type_name_specific(raster),
                             filename, year);
@@ -200,7 +200,7 @@ void save_suitability_from_glm(struct cats_grid *grid, struct cats_configuration
 #else
 
 #endif
-        log_message(LOG_ERROR, "%s: not implemented");
+        log_message(LOG_ERROR, "%s: not implemented", __func__);
 }
 
 
@@ -274,7 +274,7 @@ load_environment_interpolated(struct cats_configuration *conf, struct cats_envir
         }
 
 
-        log_message(LOG_EMPTY, "");
+        log_message(LOG_EMPTY, " ");
         log_message(LOG_INFO, "\tcalculating current %s", name);
 
         float scale = get_interpolation_factor(year_start, year_end, year_target,
@@ -289,7 +289,7 @@ load_environment_interpolated(struct cats_configuration *conf, struct cats_envir
 
         interpolate_environment(conf, environment);
         environment->current.year = year_target;
-        log_message(LOG_EMPTY, "");
+        log_message(LOG_EMPTY, " ");
         log_message(LOG_INFO, "%s SUMMARY: now loaded for %s: start %d end %d", short_name, environment->pattern,
                     environment->start.year, environment->end.year);
         print_raster_quick_info(&environment->start);
diff --git a/src/cats/environment/environment_set.c b/src/cats/environment/environment_set.c
index 9f46126..bfa5e43 100644
--- a/src/cats/environment/environment_set.c
+++ b/src/cats/environment/environment_set.c
@@ -180,7 +180,7 @@ void failed_to_find_environment(const struct cats_configuration *conf, const cha
 {
         log_message(LOG_ERROR, "%s: could not find environment set '%s' in registry", __func__, name);
         if (conf->environment.names == NULL || conf->environment.names->count == 0) {
-                log_message(LOG_ERROR, "%s: registry empty", __func__, name);
+                log_message(LOG_ERROR, "%s: registry empty ('%s')", __func__, name);
         } else {
                 for (int32_t i = 0; i < conf->environment.names->count; i++) {
                         log_message(LOG_INFO, "%s: have registry entry: '%s'", __func__,
diff --git a/src/cats/grids/cats_grid.c b/src/cats/grids/cats_grid.c
index 996aa68..e55be91 100644
--- a/src/cats/grids/cats_grid.c
+++ b/src/cats/grids/cats_grid.c
@@ -229,7 +229,7 @@ int64_t raster_cell_count(struct cats_dimension raster)
         const int64_t cols = raster.cols;
 
         if (rows > INT64_MAX / cols) {
-                log_message(LOG_ERROR, "%s: possible integer overflow in %d * %d. Aborting.", raster.rows, raster.cols);
+                log_message(LOG_ERROR, "%s: possible integer overflow in %d * %d. Aborting.", __func__, raster.rows, raster.cols);
                 exit(EXIT_FAILURE);
         }
         return rows * cols;
diff --git a/src/cats/hybrid/scalefactor.c b/src/cats/hybrid/scalefactor.c
index 67e25e6..defe588 100644
--- a/src/cats/hybrid/scalefactor.c
+++ b/src/cats/hybrid/scalefactor.c
@@ -173,7 +173,7 @@ cats_dt_rates calculate_scale_factor_from_lambda(struct cats_configuration *conf
 
 void set_scale_factor_all_species(struct cats_configuration *conf, cats_dt_rates scale_factor)
 {
-        log_message(LOG_INFO, "setting scale factor for all grids directly to %f", scale_factor);
+        log_message(LOG_INFO, "setting scale factor for all grids directly to %Lf", scale_factor);
         for (int id = 0; id < conf->grid_count; id++) {
                 conf->param[id].scale_factor = scale_factor;
         }
@@ -203,7 +203,7 @@ void setup_scale_factor(struct cats_configuration *conf, struct program_options
 
         if (options->direct_scale_factor > 0 && options->direct_scale_factor < 1) {
                 conf->direct_scale_factor = options->direct_scale_factor;
-                log_message(LOG_IMPORTANT, "using pre-calculated scale factor %f", conf->direct_scale_factor);
+                log_message(LOG_IMPORTANT, "using pre-calculated scale factor %Lf", conf->direct_scale_factor);
                 set_scale_factor_all_species(conf, conf->direct_scale_factor);
                 return;
         }
diff --git a/src/cats/inline_population.h b/src/cats/inline_population.h
index 3333ef3..b5a058e 100644
--- a/src/cats/inline_population.h
+++ b/src/cats/inline_population.h
@@ -109,7 +109,7 @@ set_population_ignore_cc(const struct cats_grid *grid, const cats_dt_coord row,
         assert(grid->population[row] != NULL);
 
         if (pop > CATS_MAX_POPULATION || pop < 0.0) {
-                log_message(LOG_ERROR, "%s: population %Lf out of allowed population range [0, %d].",
+                log_message(LOG_ERROR, "%s: population %d out of allowed population range [0, %d].",
                             __func__, pop, CATS_MAX_POPULATION);
                 exit(EXIT_FAILURE);
         }
diff --git a/src/cats/misc/cats_random.c b/src/cats/misc/cats_random.c
index b1328c4..19ad7ac 100644
--- a/src/cats/misc/cats_random.c
+++ b/src/cats/misc/cats_random.c
@@ -101,7 +101,7 @@ int32_t poisson_undampened(const gsl_rng *rng, cats_dt_rates value)
 
 
         if (result > CATS_MAX_POPULATION) {
-                log_message(LOG_WARNING, "poisson called on %Lf resulted in %d > %d, returning %d",
+                log_message(LOG_WARNING, "poisson called on %Lf resulted in %Lf > %d, returning %d",
                             value, result, CATS_MAX_POPULATION, CATS_MAX_POPULATION);
                 return CATS_MAX_POPULATION;
         }
diff --git a/src/cats/mpi/mpi_grid_helpers.c b/src/cats/mpi/mpi_grid_helpers.c
index a9b40dd..fe4db03 100644
--- a/src/cats/mpi/mpi_grid_helpers.c
+++ b/src/cats/mpi/mpi_grid_helpers.c
@@ -38,7 +38,7 @@ int get_cell_count_single_grid(const struct cats_configuration *conf)
 {
         int64_t send_count_needed = raster_cell_count(conf->geometry.dimension);
         if (send_count_needed > INT_MAX) {
-                log_message(LOG_ERROR, "%s: too many elements to send via MPI");
+                log_message(LOG_ERROR, "%s: too many elements to send via MPI", __func__);
                 exit(EXIT_FAILURE);
         }
         int send_count = (int) send_count_needed;
diff --git a/src/cats/populations/carrying_capacity.c b/src/cats/populations/carrying_capacity.c
index a2c2785..a285ee5 100644
--- a/src/cats/populations/carrying_capacity.c
+++ b/src/cats/populations/carrying_capacity.c
@@ -157,7 +157,7 @@ void apply_carrying_capacity(struct cats_grid *grid, struct cats_thread_info *ts
                 return;
         }
 
-        log_message(LOG_ERROR, "%s:: reached point we should have not reached");
+        log_message(LOG_ERROR, "%s:: reached point we should have not reached", __func__);
         exit(EXIT_FAILURE);
 }
 
diff --git a/src/cats/vital_rates/setup_rates.c b/src/cats/vital_rates/setup_rates.c
index a6d7463..4e07efe 100644
--- a/src/cats/vital_rates/setup_rates.c
+++ b/src/cats/vital_rates/setup_rates.c
@@ -207,7 +207,7 @@ void print_link(const struct cats_vital_rate_hybrid_function *link, int32_t coun
 {
         assert(link != NULL);
         if (link->type == LINK_MIN || link->type == LINK_MAX) return;
-        log_message(LOG_INFO, "\tlink function (id % 2d): '%s' ('%s') (type id: % 2d), max rate %", count,
+        log_message(LOG_INFO, "\tlink function (id % 2d): '%s' ('%s') (type id: % 2d)", count,
                     link->description, link->short_name, link->type);
 
 }
-- 
GitLab