Skip to content
Snippets Groups Projects
Commit 4811e45b authored by Dominik Loidolt's avatar Dominik Loidolt
Browse files

Updated debug_print(): Enhanced Adaptability for Various Environments

The debug_print() function has been updated for better adaptability.
It now integrates with diverse environments through the customized
implementation of the cmp_debug_puts() function.
parent 04c3fbf3
No related branches found
No related tags found
1 merge request!28Updated debug_print(): Enhanced Adaptability for Various Environments
/**
* @file cmp_debug.c
* @author Dominik Loidolt (dominik.loidolt@univie.ac.at)
* @date 2024
*
* @copyright GPLv2
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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.
*
* @brief compression/decompression debugging printing functions
*/
#ifndef ICU_ASW
# include <stdio.h>
#endif
#include <string.h>
#include <stdarg.h>
#include "cmp_debug.h"
#include "compiler.h"
#include "vsnprintf.h"
/**
* @brief outputs a debug string
*
* This function outputs a string for debugging purposes
*
* @param str The string to output
*/
static void cmp_debug_puts(const char *str)
{
#ifdef ICU_ASW
/* XXX adapt it to your needs */
/* asw_puts(str); */
(void)str;
#else
fputs(str, stderr);
fputs("\n", stderr);
#endif
}
/**
* @brief implements debug printing
*
* This function formats a string and prints it for debugging. It uses a static
* buffer to format the string
*
* @param fmt pointer to a null-terminated byte string specifying how to
* interpret the data
* @param ... arguments specifying data to print
*/
void cmp_debug_print_impl(const char *fmt, ...)
{
static char print_buffer[PRINT_BUFFER_SIZE];
int len;
va_list args;
va_start(args, fmt);
len = my_vsnprintf(print_buffer, sizeof(print_buffer)-1, fmt, args);
va_end(args);
if (len < 0) {
const char str[] = "my_snprintf is broken";
compile_time_assert(sizeof(str) <= sizeof(print_buffer), CMP_DEBUG_PRINT_BUFFER_SIZE_TO_SMALL);
memcpy(print_buffer, str, sizeof(str));
}
if ((size_t)len >= sizeof(print_buffer)-1) {
const char str[] = "cmp_debug print_buffer too small";
compile_time_assert(sizeof(str) <= sizeof(print_buffer), CMP_DEBUG_PRINT_BUFFER_SIZE_TO_SMALL);
memcpy(print_buffer, str, sizeof(str));
}
cmp_debug_puts(print_buffer);
}
......@@ -13,7 +13,7 @@
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* @brief compression/decompression debugging defines
* @brief compression/decompression debugging printing functions
*/
#ifndef CMP_DEBUG_H
......@@ -26,17 +26,16 @@
# define DEBUGLEVEL 0
#endif
#if !defined(ICU_ASW) && (defined(DEBUG) || DEBUGLEVEL > 0)
#include <stdio.h>
#define PRINT_BUFFER_SIZE 256
__extension__
#define debug_print(...) \
do { \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
} while (0)
#if (defined(DEBUG) || DEBUGLEVEL > 0)
# define debug_print(...) cmp_debug_print_impl(__VA_ARGS__)
#else
#define debug_print(...) \
do {} while (0)
# define debug_print(...) do {} while (0)
#endif
void cmp_debug_print_impl(const char *fmt, ...);
#endif /* CMP_DEBUG_H */
......@@ -37,13 +37,6 @@
#endif
/**
* Compile time check usable outside of function scope.
* Stolen from Linux (hpi_internal.h)
*/
#define compile_time_assert(cond, msg) typedef char ASSERT_##msg[(cond) ? 1 : -1]
/**
* same with the stuff below
*/
......@@ -127,7 +120,7 @@
* It also tries to prevent the actual use of the "unused" variables.
*/
#if GNUC_PREREQ(4, 5)
#if GNUC_PREREQ(4, 5) || defined(__clang__)
#define UNUSED __attribute__((unused)) \
__attribute__((deprecated ("parameter declared as UNUSED")))
#elif defined(__GNUC__)
......@@ -154,4 +147,11 @@
#endif
/**
* Compile time check usable outside of function scope.
* Stolen from Linux (hpi_internal.h)
*/
#define compile_time_assert(cond, msg) UNUSED typedef char ASSERT_##msg[(cond) ? 1 : -1]
#endif /* COMPILER_H */
common_sources = files([
'cmp_data_types.c',
'cmp_debug.c',
'cmp_entity.c',
'cmp_max_used_bits.c',
'cmp_support.c'
'cmp_support.c',
'vsnprintf.c'
])
This diff is collapsed.
/**
* @file vsnprintf.h
* @author Dominik Loidolt (dominik.loidolt@univie.ac.at)
* @date 2024
*
* @copyright GPLv2
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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.
*
* @brief Tiny vsnprintf implementation
*/
#ifndef VSNPRINTF_H
#define VSNPRINTF_H
int my_vsnprintf(char* buffer, size_t count, const char* format, va_list va);
#endif /* VSNPRINTF_H */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment