init
This commit is contained in:
commit
46f0872247
|
@ -0,0 +1,2 @@
|
|||
/bin
|
||||
/obj
|
|
@ -0,0 +1,43 @@
|
|||
#include "debug.h"
|
||||
#include <inttypes.h>
|
||||
#include <stdlib.h>
|
||||
#include "log.h"
|
||||
|
||||
void *
|
||||
debug_malloc (size_t size, const char *file, size_t line)
|
||||
{
|
||||
void *ptr;
|
||||
|
||||
ptr = malloc (size);
|
||||
if (ptr == NULL)
|
||||
log_print_fl (LOG_LEVEL_DEBUG, file, line, "malloc of size %zu \e[31mreturned null\e[0m\n", size);
|
||||
else
|
||||
log_print_fl (LOG_LEVEL_DEBUG, file, line, "malloc of size %zu to ptr %p\n", size, ptr);
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void *
|
||||
debug_realloc (void *ptr, size_t size, const char *file, size_t line)
|
||||
{
|
||||
void *new_ptr;
|
||||
uintptr_t old_ptr;
|
||||
|
||||
old_ptr = (uintptr_t) ptr;
|
||||
new_ptr = realloc (ptr, size);
|
||||
if (new_ptr == NULL && size != 0)
|
||||
log_print_fl (LOG_LEVEL_DEBUG, file, line, "realloc of ptr %#" PRIxPTR " to size %zu \e[31mreturned null\e[0m\n", old_ptr, size);
|
||||
else if (new_ptr == ptr)
|
||||
log_print_fl (LOG_LEVEL_DEBUG, file, line, "realloc of ptr %p to size %zu\n", new_ptr, size);
|
||||
else
|
||||
log_print_fl (LOG_LEVEL_DEBUG, file, line, "realloc of ptr %#" PRIxPTR " to new ptr %p with size %zu\n", old_ptr, new_ptr, size);
|
||||
|
||||
return new_ptr;
|
||||
}
|
||||
|
||||
void
|
||||
debug_free (void *ptr, const char *file, size_t line)
|
||||
{
|
||||
log_print_fl (LOG_LEVEL_DEBUG, file, line, "free ptr %p\n", ptr);
|
||||
free (ptr);
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
#ifndef CUTILS_DEBUG_H_
|
||||
#define CUTILS_DEBUG_H_
|
||||
|
||||
#include <stddef.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__);
|
||||
#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);
|
||||
|
||||
#endif // CUTILS_DEBUG_H_
|
|
@ -0,0 +1,28 @@
|
|||
#ifndef CUTILS_LOG_H_
|
||||
#define CUTILS_LOG_H_
|
||||
|
||||
#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__)
|
||||
|
||||
typedef enum {
|
||||
LOG_LEVEL_ERROR,
|
||||
LOG_LEVEL_WARNING,
|
||||
LOG_LEVEL_INFO,
|
||||
LOG_LEVEL_OK,
|
||||
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, ...);
|
||||
|
||||
#endif // CUTILS_LOG_H_
|
|
@ -0,0 +1,51 @@
|
|||
#include "log.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static void
|
||||
_log_print_type (log_level_t type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case LOG_LEVEL_ERROR:
|
||||
printf ("[\e[31merror\e[0m] ");
|
||||
break;
|
||||
case LOG_LEVEL_WARNING:
|
||||
printf ("[\e[33mwarn\e[0m] ");
|
||||
break;
|
||||
case LOG_LEVEL_INFO:
|
||||
printf ("[\e[34minfo\e[0m] ");
|
||||
break;
|
||||
case LOG_LEVEL_OK:
|
||||
printf ("[\e[32mok\e[0m] ");
|
||||
break;
|
||||
case LOG_LEVEL_DEBUG:
|
||||
printf ("[\e[35mdebug\e[0m] ");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
__attribute__ ((format (printf, 2, 3)))
|
||||
void
|
||||
log_print (log_level_t type, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start (ap, fmt);
|
||||
_log_print_type (type);
|
||||
vprintf (fmt, ap);
|
||||
va_end (ap);
|
||||
}
|
||||
|
||||
__attribute__ ((format (printf, 4, 5)))
|
||||
void
|
||||
log_print_fl (log_level_t type, const char *file, size_t line, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start (ap, fmt);
|
||||
_log_print_type (type);
|
||||
printf ("(\e[36m%s:%zu\e[0m) ", file, line);
|
||||
vprintf (fmt, ap);
|
||||
va_end (ap);
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
CC := gcc
|
||||
CFLAGS := -Wall -Wextra -Werror=implicit -Og -g -Ilib/inc
|
||||
|
||||
SRCFILES := $(wildcard lib/*.c)
|
||||
SRCFILES += $(wildcard test/*.c)
|
||||
OBJFILES := $(SRCFILES:%.c=obj/%.o)
|
||||
DEPFILES := $(SRCFILES:%.c=obj/%.dep)
|
||||
DEPFLAGS = -M -MT $@ -MMD -MP -MF $(@:.o=.dep)
|
||||
TARGET := bin/cutils-test
|
||||
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
clean:
|
||||
rm -rf bin/ obj/
|
||||
|
||||
test: $(TARGET)
|
||||
./$(TARGET)
|
||||
|
||||
.PHONY: all clean test
|
||||
|
||||
|
||||
$(TARGET): $(OBJFILES) mkdir
|
||||
$(CC) $(CFLAGS) $(OBJFILES) -o $(TARGET)
|
||||
|
||||
obj/%.o: %.c
|
||||
@$(CC) $(CFLAGS) $(DEPFLAGS) $<
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
mkdir:
|
||||
@mkdir -p bin/ obj/lib/ obj/test/
|
||||
|
||||
|
||||
$(DEPFILES): mkdir
|
||||
|
||||
include $(DEPFILES)
|
|
@ -0,0 +1,26 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define CUTILS_DEBUG_TRACE
|
||||
#include "debug.h"
|
||||
#include "log.h"
|
||||
|
||||
int
|
||||
main (void)
|
||||
{
|
||||
char *ptr;
|
||||
|
||||
ptr = malloc ((sizeof *ptr) * 1);
|
||||
ptr = realloc (ptr, (sizeof *ptr) * 4096);
|
||||
ptr = realloc (ptr, (sizeof *ptr) * 524288);
|
||||
free (ptr);
|
||||
|
||||
log_print (LOG_LEVEL_ERROR, "error! %s\n", "string fmt");
|
||||
log_print (LOG_LEVEL_WARNING, "warning! %c\n", 'c');
|
||||
log_print (LOG_LEVEL_INFO, "info! %d\n", 42);
|
||||
log_print (LOG_LEVEL_OK, "okay! %#x\n", 69);
|
||||
log_print (LOG_LEVEL_DEBUG, "debug! %04.04f\n", 13.37);
|
||||
log_print_fl (LOG_LEVEL_OK, __FILE__, __LINE__, "file and line\n");
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Reference in New Issue