I create a simple log library in CI there are three files
My example file:
Library files:
My main registration function is called logme. I defined different macros as wrappers to indicate different levels of logs:
eg,
In example.c, I call macros:
int int_arg = 55;
WARN(1, "Warn message with level 1");
WARN(1, "Warn message with level %d", int_arg);
A macro is WARNdefined in loglib.h:
logme(LEVEL, 8, " <%s:%d> inside %s() -- "__VA_ARGS__, __FILE__, __LINE__, __func__);
And finally, the function logme:
void slog(int level, int flag, const char *msg, ...)
{
char string[10000];
bzero(string, sizeof(string));
va_list args;
va_start(args, msg);
vsprintf(string, msg, args);
va_end(args);
}
When I run the example file, this is what I get:
inside main () - Warn message with level 1
Segmentation error (kernel flushing)
I get a segmentation error when I call WARNwith formatted strings.
Segmentation error is displayed in vsprintf(string, msg, args);
Is this something wrong with my macro?
make :
CFLAGS = -g -O2 -Wall -lpthread
LIB = -lrt
OBJS = loglib.o
LIBINSTALL = /usr/local/lib
HEADINSTALL = /usr/local/include
.c.o:
$(CC) $(CFLAGS) -c $< $(LIB)
libslog.a: $(OBJS)
$(AR) rcs liblog.a $(OBJS)
@echo [-] Syncing static library
sync
install:
@test -d $(LIBINSTALL) || mkdir $(LIBINSTALL)
@test -d $(HEADINSTALL) || mkdir $(HEADINSTALL)
@install -m 0664 liblog.a $(LIBINSTALL)/
@install -m 0664 loglib.h $(HEADINSTALL)/
@echo [-] Done
loglib.o: loglib.h
.PHONY: clean
clean:
$(RM) liblog.a $(OBJS)