doc: (debug, log) add documentation
This commit is contained in:
parent
46f0872247
commit
e0ab3286a2
39
lib/debug.c
39
lib/debug.c
|
@ -3,6 +3,20 @@
|
|||
#include <stdlib.h>
|
||||
#include "log.h"
|
||||
|
||||
/**
|
||||
* allocate memory using malloc() and print a fitting debug message
|
||||
*
|
||||
* @param size: the number of bytes to allocate
|
||||
* @param file: the file in which malloc() was called
|
||||
* @param line: the source code line where malloc() was called
|
||||
*
|
||||
* @return the pointer to the allocated memory, or NULL in case of error
|
||||
*
|
||||
* @errno see malloc(3)
|
||||
*
|
||||
* @usage the header `debug.h` defines a macro `malloc(size)` that
|
||||
* automatically inserts filename and line.
|
||||
*/
|
||||
void *
|
||||
debug_malloc (size_t size, const char *file, size_t line)
|
||||
{
|
||||
|
@ -17,6 +31,21 @@ debug_malloc (size_t size, const char *file, size_t line)
|
|||
return ptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* reallocate memory using realloc() and print a fitting debug message
|
||||
*
|
||||
* @param ptr: the pointer to reallocate
|
||||
* @param size: the new number of bytes
|
||||
* @param file: the file in which realloc() was called
|
||||
* @param line: the source code line where realloc() was called
|
||||
*
|
||||
* @return the pointer to the reallocated memory, or NULL in case of error
|
||||
*
|
||||
* @errno see realloc(3)
|
||||
*
|
||||
* @usage the header `debug.h` defines a macro `realloc(ptr, size)` that
|
||||
* automatically inserts filename and line.
|
||||
*/
|
||||
void *
|
||||
debug_realloc (void *ptr, size_t size, const char *file, size_t line)
|
||||
{
|
||||
|
@ -35,6 +64,16 @@ debug_realloc (void *ptr, size_t size, const char *file, size_t line)
|
|||
return new_ptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* free memory using free() and print a fitting debug message
|
||||
*
|
||||
* @param ptr: the pointer to free
|
||||
* @param file: the file in which free() was called
|
||||
* @param line: the source code line where free() was called
|
||||
*
|
||||
* @usage the header `debug.h` defines a macro `free(ptr)` that automatically
|
||||
* inserts filename and line.
|
||||
*/
|
||||
void
|
||||
debug_free (void *ptr, const char *file, size_t line)
|
||||
{
|
||||
|
|
|
@ -2,15 +2,16 @@
|
|||
#define CUTILS_DEBUG_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef CUTILS_DEBUG_TRACE
|
||||
# define malloc(size) debug_malloc (size, __FILE__, __LINE__);
|
||||
# define realloc(ptr, size) debug_realloc (ptr, size, __FILE__, __LINE__);
|
||||
# define free(ptr) debug_free (ptr, __FILE__, __LINE__);
|
||||
# define malloc(size) debug_malloc (size, __FILE__, __LINE__) /* macro wrapper for debug_malloc() */
|
||||
# define realloc(ptr, size) debug_realloc (ptr, size, __FILE__, __LINE__) /* macro wrapper for debug_realloc() */
|
||||
# define free(ptr) debug_free (ptr, __FILE__, __LINE__) /* macro wrapper for debug_free() */
|
||||
#endif
|
||||
|
||||
void *debug_malloc (size_t size, const char *file, size_t line);
|
||||
void *debug_realloc (void *ptr, size_t size, const char *file, size_t line);
|
||||
void debug_free (void *ptr, const char *file, size_t line);
|
||||
void *debug_malloc (size_t size, const char *file, size_t line); /* allocate memory using malloc() and print a fitting debug message */
|
||||
void *debug_realloc (void *ptr, size_t size, const char *file, size_t line); /* reallocate memory using realloc() and print a fitting debug message */
|
||||
void debug_free (void *ptr, const char *file, size_t line); /* free memory using free() and print a fitting debug message */
|
||||
|
||||
#endif // CUTILS_DEBUG_H_
|
||||
|
|
|
@ -3,18 +3,19 @@
|
|||
|
||||
#include <stddef.h>
|
||||
|
||||
#define log_error(...) log_print (LOG_LEVEL_ERROR, __VA_ARGS__)
|
||||
#define log_warn(...) log_print (LOG_LEVEL_WARN, __VA_ARGS__)
|
||||
#define log_info(...) log_print (LOG_LEVEL_INFO, __VA_ARGS__)
|
||||
#define log_ok(...) log_print (LOG_LEVEL_OK, __VA_ARGS__)
|
||||
#define log_debug(...) log_print (LOG_LEVEL_DEBUG, __VA_ARGS__)
|
||||
#define log_error_fl(...) log_print_fl (LOG_LEVEL_ERROR, __FILE__, __LINE__, __VA_ARGS__)
|
||||
#define log_warn_fl(...) log_print_fl (LOG_LEVEL_WARN, __FILE__, __LINE__, __VA_ARGS__)
|
||||
#define log_info_fl(...) log_print_fl (LOG_LEVEL_INFO, __FILE__, __LINE__, __VA_ARGS__)
|
||||
#define log_ok_fl(...) log_print_fl (LOG_LEVEL_OK, __FILE__, __LINE__, __VA_ARGS__)
|
||||
#define log_debug_fl(...) log_print_fl (LOG_LEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
|
||||
#define log_error(...) log_print (LOG_LEVEL_ERROR, __VA_ARGS__) /* print a log message with level error */
|
||||
#define log_warn(...) log_print (LOG_LEVEL_WARN, __VA_ARGS__) /* print a log message with level warning */
|
||||
#define log_info(...) log_print (LOG_LEVEL_INFO, __VA_ARGS__) /* print a log message with level info */
|
||||
#define log_ok(...) log_print (LOG_LEVEL_OK, __VA_ARGS__) /* print a log message with level ok */
|
||||
#define log_debug(...) log_print (LOG_LEVEL_DEBUG, __VA_ARGS__) /* print a log message with level debug */
|
||||
#define log_error_fl(...) log_print_fl (LOG_LEVEL_ERROR, __FILE__, __LINE__, __VA_ARGS__) /* print a log message with the current file and line and log level error */
|
||||
#define log_warn_fl(...) log_print_fl (LOG_LEVEL_WARN, __FILE__, __LINE__, __VA_ARGS__) /* print a log message with the current file and line and log level warning */
|
||||
#define log_info_fl(...) log_print_fl (LOG_LEVEL_INFO, __FILE__, __LINE__, __VA_ARGS__) /* print a log message with the current file and line and log level info */
|
||||
#define log_ok_fl(...) log_print_fl (LOG_LEVEL_OK, __FILE__, __LINE__, __VA_ARGS__) /* print a log message with the current file and line and log level ok */
|
||||
#define log_debug_fl(...) log_print_fl (LOG_LEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__) /* print a log message with the current file and line and log level debug */
|
||||
|
||||
typedef enum {
|
||||
typedef enum
|
||||
{
|
||||
LOG_LEVEL_ERROR,
|
||||
LOG_LEVEL_WARNING,
|
||||
LOG_LEVEL_INFO,
|
||||
|
@ -22,7 +23,7 @@ typedef enum {
|
|||
LOG_LEVEL_DEBUG,
|
||||
} log_level_t;
|
||||
|
||||
__attribute__ ((format (printf, 2, 3))) void log_print (log_level_t type, const char *fmt, ...);
|
||||
__attribute__ ((format (printf, 4, 5))) void log_print_fl (log_level_t type, const char *file, size_t line, const char *fmt, ...);
|
||||
__attribute__ ((format (printf, 2, 3))) void log_print (log_level_t type, const char *fmt, ...); /* print a log message with given log level and format */
|
||||
__attribute__ ((format (printf, 4, 5))) void log_print_fl (log_level_t type, const char *file, size_t line, const char *fmt, ...); /* print a log message with given log level and format, and add the filename and line that it was called in */
|
||||
|
||||
#endif // CUTILS_LOG_H_
|
||||
|
|
26
lib/log.c
26
lib/log.c
|
@ -2,6 +2,7 @@
|
|||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/* print the log level */
|
||||
static void
|
||||
_log_print_type (log_level_t type)
|
||||
{
|
||||
|
@ -25,6 +26,17 @@ _log_print_type (log_level_t type)
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* print a log message with given log level and format
|
||||
*
|
||||
* @param type: the log level (see log_level_t)
|
||||
* @param fmt: the format to use for the output (same as printf)
|
||||
* @param ...: format arguments
|
||||
*
|
||||
* @usage the header "log.h" defines various additional macros: log_error,
|
||||
* log_warn, log_info, log_ok and log_debug. the log level does not
|
||||
* need to be passed explicitly to these.
|
||||
*/
|
||||
__attribute__ ((format (printf, 2, 3)))
|
||||
void
|
||||
log_print (log_level_t type, const char *fmt, ...)
|
||||
|
@ -37,6 +49,20 @@ log_print (log_level_t type, const char *fmt, ...)
|
|||
va_end (ap);
|
||||
}
|
||||
|
||||
/**
|
||||
* print a log message with given log level and format, and add the filename
|
||||
* and line that it was called in
|
||||
*
|
||||
* @param type: the log level (see log_level_t)
|
||||
* @param file: the file that the log function was called in
|
||||
* @param line: the line in which the function was called
|
||||
* @param fmt: the format to use for the output (same as printf)
|
||||
* @param ...: format arguments
|
||||
*
|
||||
* @usage the header "log.h" defines various additional macros: log_error_fl,
|
||||
* log_warn_fl, log_info_fl, log_ok_fl and log_debug_fl. the log level,
|
||||
* file name and line do not need to be passed explicitly to these.
|
||||
*/
|
||||
__attribute__ ((format (printf, 4, 5)))
|
||||
void
|
||||
log_print_fl (log_level_t type, const char *file, size_t line, const char *fmt, ...)
|
||||
|
|
Loading…
Reference in New Issue