From aff64937000fbce5c55b49bee98f0120e09e399e Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Sun, 10 May 2026 15:16:48 -0400 Subject: Implement logging facility WARN, INFO, DEBUG * The original macros have been updated * Default log level is WARN * Log level increases per `-v` CLI argument * Tests always use DEBUG level --- src/lib/core/CMakeLists.txt | 1 + src/lib/core/include/core_message.h | 22 +++++---- src/lib/core/include/log.h | 29 +++++++++++ src/lib/core/log.c | 96 +++++++++++++++++++++++++++++++++++++ 4 files changed, 138 insertions(+), 10 deletions(-) create mode 100644 src/lib/core/include/log.h create mode 100644 src/lib/core/log.c (limited to 'src/lib/core') diff --git a/src/lib/core/CMakeLists.txt b/src/lib/core/CMakeLists.txt index 462d7d8..3a54d94 100644 --- a/src/lib/core/CMakeLists.txt +++ b/src/lib/core/CMakeLists.txt @@ -1,5 +1,6 @@ add_library(stasis_core STATIC globals.c + log.c timespec.c str.c strlist.c diff --git a/src/lib/core/include/core_message.h b/src/lib/core/include/core_message.h index aab7203..6ac9cb4 100644 --- a/src/lib/core/include/core_message.h +++ b/src/lib/core/include/core_message.h @@ -2,18 +2,20 @@ #ifndef STASIS_CORE_MESSAGE_H #define STASIS_CORE_MESSAGE_H -#define SYSERROR(MSG, ...) do { \ - fprintf(stderr, STASIS_COLOR_RED "ERROR: " STASIS_COLOR_RESET STASIS_COLOR_WHITE "%s:%d:%s()" STASIS_COLOR_RESET ":%s - ", path_basename(__FILE__), __LINE__, __FUNCTION__, (errno > 0) ? strerror(errno) : "info"); \ - fprintf(stderr, MSG LINE_SEP, __VA_ARGS__); \ +#define SYSERROR(FMT, ...) do { \ + log_print_error(EXECPOINT, (FMT), ##__VA_ARGS__); \ } while (0) -#ifdef DEBUG -#define SYSDEBUG(MSG, ...) do { \ - fprintf(stderr, STASIS_COLOR_BLUE "DEBUG: " STASIS_COLOR_RESET STASIS_COLOR_WHITE "%s:%d:%s()" STASIS_COLOR_RESET ": ", path_basename(__FILE__), __LINE__, __FUNCTION__); \ - fprintf(stderr, MSG LINE_SEP, __VA_ARGS__); \ +#define SYSWARN(FMT, ...) do { \ + log_print_warning(EXECPOINT, (FMT), ##__VA_ARGS__); \ +} while (0) + +#define SYSINFO(FMT, ...) do { \ + log_print_info(EXECPOINT, (FMT), ##__VA_ARGS__); \ +} while (0) + +#define SYSDEBUG(FMT, ...) do { \ + log_print_debug(EXECPOINT, (FMT), ##__VA_ARGS__); \ } while (0) -#else -#define SYSDEBUG(MSG, ...) do {} while (0) -#endif #endif //STASIS_CORE_MESSAGE_H diff --git a/src/lib/core/include/log.h b/src/lib/core/include/log.h new file mode 100644 index 0000000..18617c4 --- /dev/null +++ b/src/lib/core/include/log.h @@ -0,0 +1,29 @@ +#ifndef STASIS_EXECPOINT_H +#define STASIS_EXECPOINT_H + +#include + +enum LogLevel { + LOG_LEVEL_WARN = 0, + LOG_LEVEL_INFO, + LOG_LEVEL_DEBUG, +}; +extern enum LogLevel LOG_LEVEL; + +struct ExecPoint { + const int line; // line number + const char *file; // file name of origin + const char *function; // function of origin +}; + +#define EXECPOINT (struct ExecPoint) {.line = __LINE__, .file = __FILE__, .function = __FUNCTION__} + +void log_print_error(struct ExecPoint ep, const char *fmt, ...); +void log_print_warning(struct ExecPoint ep, const char *fmt, ...); +void log_print_info(struct ExecPoint ep, const char *fmt, ...); +void log_print_debug(struct ExecPoint ep, const char *fmt, ...); +int log_msgv(FILE *stream, struct ExecPoint ep, const char *preface_color, const char *preface, const char *fmt, va_list ap); +int log_msg(FILE *stream, struct ExecPoint ep, const char *preface_color, const char *preface, const char *fmt, ...); +const char *log_get_level_str(void); + +#endif // STASIS_EXECPOINT_H diff --git a/src/lib/core/log.c b/src/lib/core/log.c new file mode 100644 index 0000000..aea3231 --- /dev/null +++ b/src/lib/core/log.c @@ -0,0 +1,96 @@ +#include "core.h" +#include "log.h" + +enum LogLevel LOG_LEVEL = LOG_LEVEL_WARN; + +void log_print_error(const struct ExecPoint ep, const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + log_msgv(stderr, ep, STASIS_COLOR_RED, "ERROR", fmt, ap); + va_end(ap); +} + +void log_print_warning(const struct ExecPoint ep, const char *fmt, ...) { + if (LOG_LEVEL < LOG_LEVEL_WARN) { + return; + } + va_list ap; + va_start(ap, fmt); + log_msgv(stderr, ep, STASIS_COLOR_YELLOW, "WARNING", fmt, ap); + va_end(ap); +} + +void log_print_info(const struct ExecPoint ep, const char *fmt, ...) { + if (LOG_LEVEL < LOG_LEVEL_INFO) { + return; + } + va_list ap; + va_start(ap, fmt); + log_msgv(stdout, ep, STASIS_COLOR_WHITE, "INFO", fmt, ap); + va_end(ap); +} + +void log_print_debug(const struct ExecPoint ep, const char *fmt, ...) { + if (LOG_LEVEL < LOG_LEVEL_DEBUG) { + return; + } + va_list ap; + va_start(ap, fmt); + log_msgv(stderr, ep, STASIS_COLOR_BLUE, "DEBUG", fmt, ap); + va_end(ap); +} + +int log_msgv(FILE *stream, const struct ExecPoint ep, const char *preface_color, const char *preface, const char *fmt, va_list ap) { + char header[STASIS_BUFSIZ] = {0}; + + int len = snprintf(header, sizeof(header), + STASIS_COLOR_RESET + "%s%s: " + STASIS_COLOR_RESET + STASIS_COLOR_WHITE + "%s:%d: %s(): " + STASIS_COLOR_RESET, + preface_color ? preface_color : STASIS_COLOR_RED, + preface ? preface : "UNKNOWN", + path_basename((char *) ep.file), + ep.line, + ep.function); + if (len >= (int) sizeof(header)) { + SYSERROR("header format truncated (%d >= %d)", len, (int) sizeof(header)); + return len; + } + + fprintf(stream, "%s", header); + + va_list ap_out; + va_copy(ap_out, ap); + len = vfprintf(stream, fmt ? fmt : "NO MESSAGE", ap_out); + va_end(ap_out); + if (len < 0) { + SYSERROR("\nvfprintf failed"); + return len; + } + fprintf(stderr, LINE_SEP); + return len; +} + +int log_msg(FILE *stream, const struct ExecPoint ep, const char *preface_color, const char *preface, const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + const int ret = log_msgv(stream, ep, preface_color, preface, fmt, ap); + va_end(ap); + return ret; +} + +const char *log_get_level_str(void) { + switch (LOG_LEVEL) { + case LOG_LEVEL_WARN: + return "WARN"; + case LOG_LEVEL_INFO: + return "INFO"; + case LOG_LEVEL_DEBUG: + default: + // Anything higher is still DEBUG + return "DEBUG"; + } +} -- cgit From dfc38629724eb4f6718cb73ff3de108871e377bd Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Sun, 10 May 2026 15:19:44 -0400 Subject: Fix up includes --- src/lib/core/include/str.h | 3 ++- src/lib/core/include/utils.h | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'src/lib/core') diff --git a/src/lib/core/include/str.h b/src/lib/core/include/str.h index be497ed..5097148 100644 --- a/src/lib/core/include/str.h +++ b/src/lib/core/include/str.h @@ -9,8 +9,9 @@ #include #include #include -#include "relocation.h" #include "core.h" +#include "log.h" +#include "relocation.h" #define STASIS_SORT_ALPHA 1 << 0 #define STASIS_SORT_NUMERIC 1 << 1 diff --git a/src/lib/core/include/utils.h b/src/lib/core/include/utils.h index c1ee513..1a5e97f 100644 --- a/src/lib/core/include/utils.h +++ b/src/lib/core/include/utils.h @@ -9,6 +9,7 @@ #include #include #include "core.h" +#include "log.h" #include "copy.h" #include "system.h" #include "strlist.h" -- cgit From 3f10306c6ac67eaccbc11b22eed26f6ce2725bfe Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Sun, 10 May 2026 15:31:06 -0400 Subject: Update SYS* macro calls --- src/lib/core/artifactory.c | 14 +++++++------- src/lib/core/conda.c | 16 ++++++++-------- src/lib/core/copy.c | 6 +++--- src/lib/core/download.c | 18 +++++++++--------- src/lib/core/environment.c | 8 ++++---- src/lib/core/ini.c | 2 +- src/lib/core/junitxml.c | 14 +++++++------- src/lib/core/multiprocessing.c | 6 +++--- src/lib/core/relocation.c | 2 +- src/lib/core/semaphore.c | 2 +- src/lib/core/str.c | 2 +- src/lib/core/template.c | 26 +++++++++++++------------- src/lib/core/utils.c | 22 +++++++++++----------- src/lib/core/wheel.c | 4 ++-- 14 files changed, 71 insertions(+), 71 deletions(-) (limited to 'src/lib/core') diff --git a/src/lib/core/artifactory.c b/src/lib/core/artifactory.c index 54f4ba0..1e9de84 100644 --- a/src/lib/core/artifactory.c +++ b/src/lib/core/artifactory.c @@ -8,7 +8,7 @@ int artifactory_download_cli(char *dest, char *os, char *arch, char *remote_filename) { - SYSDEBUG("%s", "ARGS follow"); + SYSDEBUG("ARGS follow"); SYSDEBUG("dest=%s", dest); SYSDEBUG("jfrog_artifactory_base_url=%s", jfrog_artifactory_base_url); SYSDEBUG("jfrog_artifactory_product=%s", jfrog_artifactory_product); @@ -24,7 +24,7 @@ int artifactory_download_cli(char *dest, char arch_ident[STASIS_NAME_MAX] = {0}; // convert platform string to lower-case - SYSDEBUG("%s", "Set os_ident"); + SYSDEBUG("Set os_ident"); strncpy(os_ident, os, sizeof(os_ident) - 1); os_ident[sizeof(os_ident) - 1] = '\0'; tolower_s(os_ident); @@ -43,7 +43,7 @@ int artifactory_download_cli(char *dest, } // translate ARCH identifier - SYSDEBUG("%s", "Set arch_ident"); + SYSDEBUG("Set arch_ident"); strncpy(arch_ident, arch, sizeof(arch_ident) - 1); arch_ident[sizeof(arch_ident) - 1] = '\0'; SYSDEBUG("arch_ident=%s", arch_ident); @@ -65,7 +65,7 @@ int artifactory_download_cli(char *dest, arch_ident[sizeof(arch_ident) - 1] = '\0'; - SYSDEBUG("%s", "Construct URL"); + SYSDEBUG("Construct URL"); snprintf(url, sizeof(url), "%s/%s/%s/%s/%s-%s-%s/%s", jfrog_artifactory_base_url, // https://releases.jfrog.io/artifactory jfrog_artifactory_product, // jfrog-cli @@ -83,7 +83,7 @@ int artifactory_download_cli(char *dest, return -1; } - SYSDEBUG("%s", "Construct path to write data"); + SYSDEBUG("Construct path to write data"); SYSDEBUG("path buffer contents: '%s'", path); SYSDEBUG("path buffer size: %zu", sizeof(path)); SYSDEBUG("path strlen: %zu", strlen(path)); @@ -91,10 +91,10 @@ int artifactory_download_cli(char *dest, snprintf(path + strlen(path), sizeof(path) - strlen(path), "/%s", remote_filename); char *errmsg = NULL; - SYSDEBUG("%s", "Downloading..."); + SYSDEBUG("Downloading..."); long fetch_status = download(url, path, &errmsg); if (HTTP_ERROR(fetch_status)) { - SYSERROR("download failed: %s: %s\n", errmsg, url); + SYSERROR("download failed: %s: %s", errmsg, url); return -1; } chmod(path, 0755); diff --git a/src/lib/core/conda.c b/src/lib/core/conda.c index 4597128..0259e82 100644 --- a/src/lib/core/conda.c +++ b/src/lib/core/conda.c @@ -20,7 +20,7 @@ int micromamba(const struct MicromambaInfo *info, char *command, ...) { } if (!info->download_dir || isempty(info->download_dir)) { - SYSERROR("%s", "micromamba inf->download_dir is NULL, or empty"); + SYSERROR("micromamba inf->download_dir is NULL, or empty"); return -1; } @@ -65,12 +65,12 @@ int micromamba(const struct MicromambaInfo *info, char *command, ...) { va_list args; int cmd_len = snprintf(cmd, sizeof(cmd), "%s -r %s -p %s ", mmbin, info->conda_prefix, info->conda_prefix); if (cmd_len < 0) { - SYSERROR("%s", "Unable to build argument list for micromamba"); + SYSERROR("Unable to build argument list for micromamba"); va_end(args); return -1; } if ((size_t) cmd_len > sizeof(cmd)) { - SYSERROR("%s", "micromamba command truncated"); + SYSERROR("micromamba command truncated"); va_end(args); return -1; } @@ -78,12 +78,12 @@ int micromamba(const struct MicromambaInfo *info, char *command, ...) { va_start(args, command); cmd_len = vsnprintf(cmd + strlen(cmd), sizeof(cmd) - strlen(cmd), command, args); if (cmd_len < 0) { - SYSERROR("%s", "Unable to append arguments to micromamba command"); + SYSERROR("Unable to append arguments to micromamba command"); va_end(args); return -1; } if ((size_t) cmd_len > sizeof(cmd)) { - SYSERROR("%s", "micromamba command truncated while appending arguments"); + SYSERROR("micromamba command truncated while appending arguments"); va_end(args); return -1; } @@ -111,7 +111,7 @@ int python_exec(const char *args) { char *command = NULL; if (asprintf(&command, "%s%s", command_base, args) < 0 || !command) { - SYSERROR("%s", "Unable to allocate command string"); + SYSERROR("Unable to allocate command string"); return -1; } msg(STASIS_MSG_L3, "Executing: %s\n", command); @@ -126,7 +126,7 @@ int pip_exec(const char *args) { char *command = NULL; if (asprintf(&command, "%s%s", command_base, args) < 0 || !command) { - SYSERROR("%s", "Unable to allocate command string"); + SYSERROR("Unable to allocate command string"); return -1; } msg(STASIS_MSG_L3, "Executing: %s\n", command); @@ -646,7 +646,7 @@ int conda_env_create_from_uri(char *name, char *uri, char *python_version) { const char *fmt = "env create -n '%s' --file='%s'"; char *env_command = NULL; if (asprintf(&env_command, fmt, name, tempfile) < 0 || !env_command) { - SYSERROR("%s", "unable to allocate environment command"); + SYSERROR("unable to allocate environment command"); return -1; } diff --git a/src/lib/core/copy.c b/src/lib/core/copy.c index 6697f59..ed545cc 100644 --- a/src/lib/core/copy.c +++ b/src/lib/core/copy.c @@ -48,14 +48,14 @@ int copy2(const char *src, const char *dest, unsigned int op) { } else if (S_ISREG(src_stat.st_mode)) { char buf[STASIS_BUFSIZ] = {0}; size_t bytes_read; - SYSDEBUG("%s", "Opening source file for reading"); + SYSDEBUG("Opening source file for reading"); FILE *fp1 = fopen(src, "rb"); if (!fp1) { perror(src); return -1; } - SYSDEBUG("%s", "Opening destination file for writing"); + SYSDEBUG("Opening destination file for writing"); FILE *fp2 = fopen(dest, "w+b"); if (!fp2) { perror(dest); @@ -86,6 +86,6 @@ int copy2(const char *src, const char *dest, unsigned int op) { errno = EOPNOTSUPP; return -1; } - SYSDEBUG("%s", "Data copied"); + SYSDEBUG("Data copied"); return 0; } diff --git a/src/lib/core/download.c b/src/lib/core/download.c index eb24351..ef1474f 100644 --- a/src/lib/core/download.c +++ b/src/lib/core/download.c @@ -11,7 +11,7 @@ size_t download_writer(void *fp, size_t size, size_t nmemb, void *stream) { } long download(char *url, const char *filename, char **errmsg) { - SYSDEBUG("%s", "ARGS follow"); + SYSDEBUG("ARGS follow"); SYSDEBUG("url=%s", url); SYSDEBUG("filename=%s", filename); SYSDEBUG("errmsg=%s (NULL is OK)", *errmsg); @@ -19,7 +19,7 @@ long download(char *url, const char *filename, char **errmsg) { char user_agent[STASIS_NAME_MAX] = {0}; snprintf(user_agent, sizeof(user_agent), "stasis/%s", STASIS_VERSION); - SYSDEBUG("%s", "Setting timeout"); + SYSDEBUG("Setting timeout"); size_t timeout_default = 30L; size_t timeout = timeout_default; const char *timeout_str = getenv("STASIS_DOWNLOAD_TIMEOUT"); @@ -31,7 +31,7 @@ long download(char *url, const char *filename, char **errmsg) { } } - SYSDEBUG("%s", "Setting max_retries"); + SYSDEBUG("Setting max_retries"); const size_t max_retries_default = 5; size_t max_retries = max_retries_default; const char *max_retries_str = getenv("STASIS_DOWNLOAD_RETRY_MAX"); @@ -43,7 +43,7 @@ long download(char *url, const char *filename, char **errmsg) { } } - SYSDEBUG("%s", "Setting max_retry_seconds"); + SYSDEBUG("Setting max_retry_seconds"); const size_t max_retry_seconds_default = 3; size_t max_retry_seconds = max_retry_seconds_default; const char *max_retry_seconds_str = getenv("STASIS_DOWNLOAD_RETRY_SECONDS"); @@ -56,7 +56,7 @@ long download(char *url, const char *filename, char **errmsg) { } - SYSDEBUG("%s", "Initializing curl context"); + SYSDEBUG("Initializing curl context"); curl_global_init(CURL_GLOBAL_ALL); CURL *c = curl_easy_init(); for (size_t retry = 0; retry < max_retries; retry++) { @@ -64,7 +64,7 @@ long download(char *url, const char *filename, char **errmsg) { fprintf(stderr, "[RETRY %zu/%zu] %s: %s\n", retry + 1, max_retries, *errmsg, url); } - SYSDEBUG("%s", "Configuring curl"); + SYSDEBUG("Configuring curl"); curl_easy_setopt(c, CURLOPT_URL, url); curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, download_writer); FILE *fp = fopen(filename, "wb"); @@ -87,10 +87,10 @@ long download(char *url, const char *filename, char **errmsg) { SYSDEBUG("curl failed with code: %s", curl_easy_strerror(curl_code)); const size_t errmsg_maxlen = 256; if (!*errmsg) { - SYSDEBUG("%s", "allocating memory for error message"); + SYSDEBUG("allocating memory for error message"); *errmsg = calloc(errmsg_maxlen, sizeof(char)); if (!*errmsg) { - SYSERROR("%s", "unable to allocate memory for error message"); + SYSERROR("unable to allocate memory for error message"); goto cleanup; } } @@ -102,7 +102,7 @@ long download(char *url, const char *filename, char **errmsg) { } cleanup: - SYSDEBUG("%s", "Cleaning up"); + SYSDEBUG("Cleaning up"); // Data written. Clean up. fclose(fp); diff --git a/src/lib/core/environment.c b/src/lib/core/environment.c index 1bd9d28..d81c3d1 100644 --- a/src/lib/core/environment.c +++ b/src/lib/core/environment.c @@ -414,19 +414,19 @@ void runtime_set(RuntimeEnv *env, const char *_key, char *_value) { const ssize_t key_offset = runtime_contains(env, _key); char *key = strdup(_key); if (!key) { - SYSERROR("%s", "unable to allocate memory for key"); + SYSERROR("unable to allocate memory for key"); exit(1); } char *value = runtime_expand_var(env, _value); if (!value) { - SYSERROR("%s", "unable to allocate memory for value"); + SYSERROR("unable to allocate memory for value"); exit(1); } lstrip(value); char *now = join((char *[]) {key, value, NULL}, sep); if (!now) { - SYSERROR("%s", "unable to allocate memory for join"); + SYSERROR("unable to allocate memory for join"); exit(1); } @@ -448,7 +448,7 @@ void runtime_apply(RuntimeEnv *env) { for (size_t i = 0; i < strlist_count(env); i++) { char **pair = split(strlist_item(env, i), "=", 1); if (!pair) { - SYSERROR("%s", "unable to allocate memory for runtime_apply"); + SYSERROR("unable to allocate memory for runtime_apply"); return; } setenv(pair[0], pair[1], 1); diff --git a/src/lib/core/ini.c b/src/lib/core/ini.c index ea8c0dd..183aa6b 100644 --- a/src/lib/core/ini.c +++ b/src/lib/core/ini.c @@ -464,7 +464,7 @@ int ini_write(struct INIFILE *ini, FILE **stream, unsigned mode) { } if (!render) { - SYSERROR("%s", "rendered string value can never be NULL!\n"); + SYSERROR("rendered string value can never be NULL!"); return -1; } diff --git a/src/lib/core/junitxml.c b/src/lib/core/junitxml.c index caf21cb..a59cb9d 100644 --- a/src/lib/core/junitxml.c +++ b/src/lib/core/junitxml.c @@ -79,7 +79,7 @@ static struct JUNIT_Failure *testcase_failure_from_attributes(struct StrList *at if (!strcmp(attr_name, "message")) { result->message = strdup(attr_value); if (!result->message) { - SYSERROR("%s", "failed to allocate memory for testcase failure message"); + SYSERROR("failed to allocate memory for testcase failure message"); break; } } @@ -100,7 +100,7 @@ static struct JUNIT_Error *testcase_error_from_attributes(struct StrList *attrs) if (!strcmp(attr_name, "message")) { result->message = strdup(attr_value); if (!result->message) { - SYSERROR("%s", "failed to allocate memory for testcase error message"); + SYSERROR("failed to allocate memory for testcase error message"); break; } } @@ -121,7 +121,7 @@ static struct JUNIT_Skipped *testcase_skipped_from_attributes(struct StrList *at if (!strcmp(attr_name, "message")) { result->message = strdup(attr_value); if (!result->message) { - SYSERROR("%s", "failed to allocate memory for testcase skip message"); + SYSERROR("failed to allocate memory for testcase skip message"); break; } } @@ -133,7 +133,7 @@ static struct JUNIT_Skipped *testcase_skipped_from_attributes(struct StrList *at static struct JUNIT_Testcase *testcase_from_attributes(struct StrList *attrs) { struct JUNIT_Testcase *result = calloc(1, sizeof(*result)); if(!result) { - SYSERROR("%s", "failed to allocate memory for testcase"); + SYSERROR("failed to allocate memory for testcase"); return NULL; } for (size_t x = 0; x < strlist_count(attrs); x += 2) { @@ -143,14 +143,14 @@ static struct JUNIT_Testcase *testcase_from_attributes(struct StrList *attrs) { if (!strcmp(attr_name, "name")) { result->name = strdup(attr_value); if (!result->name) { - SYSERROR("%s", "failed to allocate memory for testcase name"); + SYSERROR("failed to allocate memory for testcase name"); testcase_free(&result); break; } } else if (!strcmp(attr_name, "classname")) { result->classname = strdup(attr_value); if (!result->classname) { - SYSERROR("%s", "failed to allocate memory for testcase class name"); + SYSERROR("failed to allocate memory for testcase class name"); testcase_free(&result); break; } @@ -159,7 +159,7 @@ static struct JUNIT_Testcase *testcase_from_attributes(struct StrList *attrs) { } else if (!strcmp(attr_name, "message")) { result->message = strdup(attr_value); if (!result->message) { - SYSERROR("%s", "failed to allocate memory for testcase message"); + SYSERROR("failed to allocate memory for testcase message"); testcase_free(&result); break; } diff --git a/src/lib/core/multiprocessing.c b/src/lib/core/multiprocessing.c index cf0f3d7..ffc6b95 100644 --- a/src/lib/core/multiprocessing.c +++ b/src/lib/core/multiprocessing.c @@ -158,7 +158,7 @@ static int mp_task_fork(struct MultiProcessingPool *pool, struct MultiProcessing } struct MultiProcessingTask *mp_pool_task(struct MultiProcessingPool *pool, const char *ident, char *working_dir, char *cmd) { - SYSDEBUG("%s", "Finding next available slot"); + SYSDEBUG("Finding next available slot"); struct MultiProcessingTask *slot = mp_pool_next_available(pool); if (pool->num_used != pool->num_alloc) { SYSDEBUG("Using slot %zu of %zu", pool->num_used, pool->num_alloc); @@ -196,14 +196,14 @@ struct MultiProcessingTask *mp_pool_task(struct MultiProcessingPool *pool, const t_name = xmkstemp(&tp, "w"); if (!t_name) { - SYSERROR("%s", "Failed to create temporary file name"); + SYSERROR("Failed to create temporary file name"); if (tp) { fclose(tp); } return NULL; } if (!tp) { - SYSERROR("%s", "Failed to create temporary file"); + SYSERROR("Failed to create temporary file"); guard_free(t_name); return NULL; } diff --git a/src/lib/core/relocation.c b/src/lib/core/relocation.c index 16376b3..348bad1 100644 --- a/src/lib/core/relocation.c +++ b/src/lib/core/relocation.c @@ -36,7 +36,7 @@ int replace_text(char *original, const char *target, const char *replacement, un if (original_len > sizeof(buffer)) { errno = EINVAL; - SYSERROR("The original string is larger than buffer: %zu > %zu\n", original_len, sizeof(buffer)); + SYSERROR("The original string is larger than buffer: %zu > %zu", original_len, sizeof(buffer)); return -1; } diff --git a/src/lib/core/semaphore.c b/src/lib/core/semaphore.c index 3a2ffb6..7ac1549 100644 --- a/src/lib/core/semaphore.c +++ b/src/lib/core/semaphore.c @@ -72,7 +72,7 @@ int semaphore_post(struct Semaphore *s) { void semaphore_destroy(struct Semaphore *s) { if (!s) { - SYSDEBUG("%s", "would have crashed"); + SYSDEBUG("would have crashed"); return; } SYSDEBUG("%s", s->name); diff --git a/src/lib/core/str.c b/src/lib/core/str.c index a08bd2b..7ba6c5b 100644 --- a/src/lib/core/str.c +++ b/src/lib/core/str.c @@ -588,7 +588,7 @@ char **strdup_array(char **array) { // Create new array result = calloc(elems + 1, sizeof(*result)); if (!result) { - SYSERROR("%s", "could not allocate memory for result array"); + SYSERROR("could not allocate memory for result array"); return NULL; } for (size_t i = 0; i < elems; i++) { diff --git a/src/lib/core/template.c b/src/lib/core/template.c index 3b6f58d..cdf8e58 100644 --- a/src/lib/core/template.c +++ b/src/lib/core/template.c @@ -20,7 +20,7 @@ struct tplfunc_frame *tpl_pool_func[1024] = {0}; unsigned tpl_pool_func_used = 0; extern void tpl_reset() { - SYSDEBUG("%s", "Resetting template engine"); + SYSDEBUG("Resetting template engine"); tpl_free(); tpl_pool_used = 0; tpl_pool_func_used = 0; @@ -29,13 +29,13 @@ extern void tpl_reset() { void tpl_register_func(char *key, void *tplfunc_ptr, int argc, void *data_in) { struct tplfunc_frame *frame = calloc(1, sizeof(*frame)); if (!frame) { - SYSERROR("%s", "unable to allocate memory for function frame"); + SYSERROR("unable to allocate memory for function frame"); exit(1); } frame->key = strdup(key); if (!frame->key) { - SYSERROR("%s", "unable to allocate memory for function frame key"); + SYSERROR("unable to allocate memory for function frame key"); exit(1); } frame->argc = argc; @@ -52,12 +52,12 @@ int tpl_key_exists(char *key) { for (size_t i = 0; i < tpl_pool_used; i++) { if (tpl_pool[i]->key) { if (!strcmp(tpl_pool[i]->key, key)) { - SYSDEBUG("%s", "YES"); + SYSDEBUG("YES"); return true; } } } - SYSDEBUG("%s", "NO"); + SYSDEBUG("NO"); return false; } @@ -76,17 +76,17 @@ void tpl_register(char *key, char **ptr) { } } replacing = 1; - SYSDEBUG("%s", "Item will be replaced"); + SYSDEBUG("Item will be replaced"); } else { - SYSDEBUG("%s", "Creating new item"); + SYSDEBUG("Creating new item"); item = calloc(1, sizeof(*item)); if (!item) { - SYSERROR("%s", "unable to allocate memory for new item"); + SYSERROR("unable to allocate memory for new item"); exit(1); } item->key = strdup(key); if (!key) { - SYSERROR("%s", "unable to allocate memory for new key"); + SYSERROR("unable to allocate memory for new key"); exit(1); } } @@ -186,7 +186,7 @@ char *tpl_render(char *str) { } // Read key name - SYSDEBUG("%s", "Reading key"); + SYSDEBUG("Reading key"); size_t key_len = 0; while (isalnum(pos[off]) || pos[off] != '}') { if (isspace(pos[off]) || isblank(pos[off])) { @@ -207,10 +207,10 @@ char *tpl_render(char *str) { int do_func = 0; if (type_stop) { if (!strncmp(key, "env", type_stop - key)) { - SYSDEBUG("%s", "Will render as value of environment variable"); + SYSDEBUG("Will render as value of environment variable"); do_env = 1; } else if (!strncmp(key, "func", type_stop - key)) { - SYSDEBUG("%s", "Will render as output from function"); + SYSDEBUG("Will render as output from function"); do_func = 1; } } @@ -327,7 +327,7 @@ int tpl_render_to_file(char *str, const char *filename) { // Write rendered string to file fprintf(fp, "%s", result); fclose(fp); - SYSDEBUG("%s", "Rendered successfully"); + SYSDEBUG("Rendered successfully"); guard_free(result); return 0; diff --git a/src/lib/core/utils.c b/src/lib/core/utils.c index e6a8315..264f23d 100644 --- a/src/lib/core/utils.c +++ b/src/lib/core/utils.c @@ -466,18 +466,18 @@ void msg(unsigned type, char *fmt, ...) { } if (fprintf(stream, "%s", header) < 0) { - SYSERROR("%s", "unable to write message header to stream"); + SYSERROR("unable to write message header to stream"); return; } const int len = vfprintf(stream, fmt, args); if (len < 0) { - SYSERROR("%s", "unable to write message to stream"); + SYSERROR("unable to write message to stream"); return; } if (fprintf(stream, "%s", STASIS_COLOR_RESET) < 0) { - SYSERROR("%s", "unable to write message footer to stream"); + SYSERROR("unable to write message footer to stream"); return; } va_end(args); @@ -670,7 +670,7 @@ int fix_tox_conf(const char *filename, char **result, size_t maxlen) { // Create new temporary tox configuration file char *tempfile = xmkstemp(&fptemp, "w+"); if (!tempfile) { - SYSERROR("%s", "unable to create temporary file"); + SYSERROR("unable to create temporary file"); return -1; } @@ -828,7 +828,7 @@ struct StrList *listdir(const char *path) { } char *fullpath = join_ex("/", path, rec->d_name, NULL); if (!fullpath) { - SYSERROR("%s", "Unable to allocate bytes to construct full path"); + SYSERROR("Unable to allocate bytes to construct full path"); guard_strlist_free(&node); closedir(dp); return NULL; @@ -878,13 +878,13 @@ char *find_version_spec(char *str) { int env_manipulate_pathstr(const char *key, char *path, int mode) { if (isempty(path)) { - SYSERROR("%s", "New PATH element cannot be zero-length or NULL"); + SYSERROR("New PATH element cannot be zero-length or NULL"); return -1; } const char *system_path_old = getenv("PATH"); if (!system_path_old) { - SYSERROR("%s", "Unable to read PATH"); + SYSERROR("Unable to read PATH"); return -1; } @@ -897,7 +897,7 @@ int env_manipulate_pathstr(const char *key, char *path, int mode) { } if (!system_path_new) { - SYSERROR("%s", "Unable to allocate memory to update PATH"); + SYSERROR("Unable to allocate memory to update PATH"); return -1; } @@ -986,7 +986,7 @@ int grow(const size_t size_new, size_t *size_orig, char **data) { } if (size_new >= *size_orig) { const size_t new_size = *size_orig + size_new + 1; - SYSDEBUG("template data buffer new size: %zu\n", new_size); + SYSDEBUG("template data buffer new size: %zu", new_size); char *tmp = realloc(*data, new_size); if (!tmp) { @@ -1176,7 +1176,7 @@ int get_random_bytes(char *result, size_t maxlen) { if (filename != NULL) { fp = fopen(filename, "rb"); if (!fp) { - SYSERROR("%s", "unable to open random generator"); + SYSERROR("unable to open random generator"); return -1; } } @@ -1189,7 +1189,7 @@ int get_random_bytes(char *result, size_t maxlen) { ch = rand() % 255; } if (fp && ferror(fp)) { - SYSERROR("%s", "unable to read from random generator"); + SYSERROR("unable to read from random generator"); fclose(fp); return -1; } diff --git a/src/lib/core/wheel.c b/src/lib/core/wheel.c index 9f3b3c8..ef491c9 100644 --- a/src/lib/core/wheel.c +++ b/src/lib/core/wheel.c @@ -70,13 +70,13 @@ static ssize_t wheel_parse_wheel(struct Wheel * pkg, const char * data) { if (pair) { char *key = strdup(strip(pair[0])); if (!key) { - SYSERROR("%s", "could not allocate memory for pair wheel key"); + SYSERROR("could not allocate memory for pair wheel key"); return -1; } char *value = strdup(lstrip(pair[1])); if (!value) { - SYSERROR("%s", "could not allocate memory for wheel value"); + SYSERROR("could not allocate memory for wheel value"); guard_free(key); return -1; } -- cgit From 398d8dacc6d0199cb7301259ec31169c83a3a3cd Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Sun, 10 May 2026 15:33:32 -0400 Subject: Move strdup_maybe to str.c and export symbol in str.h --- src/lib/core/include/str.h | 4 ++++ src/lib/core/str.c | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) (limited to 'src/lib/core') diff --git a/src/lib/core/include/str.h b/src/lib/core/include/str.h index 5097148..3e7c3a4 100644 --- a/src/lib/core/include/str.h +++ b/src/lib/core/include/str.h @@ -18,6 +18,10 @@ #define STASIS_SORT_LEN_ASCENDING 1 << 2 #define STASIS_SORT_LEN_DESCENDING 1 << 3 + +char *strdup_maybe_entry(const char * restrict s, struct ExecPoint ep, int exit_code); +#define strdup_maybe(S) strdup_maybe_entry((S), EXECPOINT, 1) + /** * Determine how many times the character `ch` appears in `sptr` string * @param sptr string to scan diff --git a/src/lib/core/str.c b/src/lib/core/str.c index 7ba6c5b..5df7372 100644 --- a/src/lib/core/str.c +++ b/src/lib/core/str.c @@ -4,6 +4,22 @@ #include #include "str.h" + + +char *strdup_maybe_entry(const char * restrict s, const struct ExecPoint ep, const int exit_code) { + // USE MACRO FROM str.h: strdup_maybe() + if (s != NULL) { + char *x = strdup(s); + if (!x) { + SYSERROR("unable to duplicate string"); + log_print_error(ep, "out of memory"); + exit(exit_code); + } + return x; + } + return NULL; +} + int num_chars(const char *sptr, int ch) { int result = 0; for (int i = 0; sptr[i] != '\0'; i++) { -- cgit From f2ec70a12c7daebd79e4c93204e2da69eab4937f Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Sun, 10 May 2026 15:36:07 -0400 Subject: Add all log message macros --- src/lib/core/include/core_mem.h | 1 + 1 file changed, 1 insertion(+) (limited to 'src/lib/core') diff --git a/src/lib/core/include/core_mem.h b/src/lib/core/include/core_mem.h index b67130c..e1ea4c7 100644 --- a/src/lib/core/include/core_mem.h +++ b/src/lib/core/include/core_mem.h @@ -2,6 +2,7 @@ #ifndef STASIS_CORE_MEM_H #define STASIS_CORE_MEM_H +#include "execpoint.h" #include "environment.h" #include "strlist.h" -- cgit From 70f20294bd63e9774ee0c8a7a032ac63e1a1da2e Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Sun, 10 May 2026 15:36:58 -0400 Subject: remove execpoint.h include --- src/lib/core/include/core_mem.h | 1 - 1 file changed, 1 deletion(-) (limited to 'src/lib/core') diff --git a/src/lib/core/include/core_mem.h b/src/lib/core/include/core_mem.h index e1ea4c7..b67130c 100644 --- a/src/lib/core/include/core_mem.h +++ b/src/lib/core/include/core_mem.h @@ -2,7 +2,6 @@ #ifndef STASIS_CORE_MEM_H #define STASIS_CORE_MEM_H -#include "execpoint.h" #include "environment.h" #include "strlist.h" -- cgit