diff options
-rw-r--r-- | .gitattributes | 3 | ||||
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | include/nslog/nslog.h | 11 | ||||
-rw-r--r-- | src/Makefile | 12 | ||||
-rw-r--r-- | src/core.c | 33 | ||||
-rw-r--r-- | src/filter-lexer.l | 2 | ||||
-rw-r--r-- | test/basictests.c | 14 |
7 files changed, 60 insertions, 17 deletions
diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..970f400 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +.gitignore export-ignore +.gitattributes export-ignore +.editorconfig export-ignore @@ -7,7 +7,7 @@ # Component settings COMPONENT := nslog -COMPONENT_VERSION := 0.0.0 +COMPONENT_VERSION := 0.1.3 # Default to a static library COMPONENT_TYPE ?= lib-static diff --git a/include/nslog/nslog.h b/include/nslog/nslog.h index 9113527..d63ed48 100644 --- a/include/nslog/nslog.h +++ b/include/nslog/nslog.h @@ -48,6 +48,17 @@ typedef enum { */ const char *nslog_level_name(nslog_level level); +/** + * Convert a logging level to a short string. + * + * The returned string is owned by the nslog library (static) and should + * not be freed. It will be exactly four characters wide and suitable for + * logging to a file neatly. + * + * \param level The level for which you want the 'short' name. + */ +const char *nslog_short_level_name(nslog_level level); + #define NSLOG_LEVEL_DD NSLOG_LEVEL_DEEPDEBUG #define NSLOG_LEVEL_DBG NSLOG_LEVEL_DEBUG #define NSLOG_LEVEL_CHAT NSLOG_LEVEL_VERBOSE diff --git a/src/Makefile b/src/Makefile index 3ca70cd..d598cf5 100644 --- a/src/Makefile +++ b/src/Makefile @@ -4,11 +4,17 @@ CFLAGS := $(CFLAGS) -I$(BUILDDIR) -Isrc/ SOURCES := $(SOURCES) $(BUILDDIR)/filter-parser.c $(BUILDDIR)/filter-lexer.c -$(BUILDDIR)/%-lexer.c $(BUILDDIR)/%-lexer.h: src/%-lexer.l +$(BUILDDIR)/%-lexer.inc $(BUILDDIR)/%-lexer.h: src/%-lexer.l $(VQ)$(ECHO) " FLEX: $<" - $(Q)$(FLEX) --outfile=$(BUILDDIR)/$(*F)-lexer.c --header-file=$(BUILDDIR)/$(*F)-lexer.h $< + $(Q)$(FLEX) --outfile=$(BUILDDIR)/$(*F)-lexer.inc --header-file=$(BUILDDIR)/$(*F)-lexer.h $< +.PRECIOUS: $(BUILDDIR)/%-lexer.inc -$(BUILDDIR)/%-lexer.c: $(BUILDDIR)/%-parser.h +$(BUILDDIR)/%-lexer.inc: $(BUILDDIR)/%-parser.h + +$(BUILDDIR)/%-lexer.c: $(BUILDDIR)/%-lexer.inc + $(Q)echo "#ifndef __clang_analyzer__" > $@ + $(Q)echo "#include \"$(notdir $<)\"" >> $@ + $(Q)echo "#endif" >> $@ # Bison 3.0 and later require api.prefix in curly braces # Bison 2.6 and later require api.prefix @@ -49,6 +49,28 @@ const char *nslog_level_name(nslog_level level) return "**UNKNOWN**"; } +const char *nslog_short_level_name(nslog_level level) +{ + switch (level) { + case NSLOG_LEVEL_DEEPDEBUG: + return "DDBG"; + case NSLOG_LEVEL_DEBUG: + return "DBG "; + case NSLOG_LEVEL_VERBOSE: + return "VERB"; + case NSLOG_LEVEL_INFO: + return "INFO"; + case NSLOG_LEVEL_WARNING: + return "WARN"; + case NSLOG_LEVEL_ERROR: + return "ERR "; + case NSLOG_LEVEL_CRITICAL: + return "CRIT"; + }; + + return "?UNK"; +} + static void nslog__normalise_category(nslog_category_t *cat) { @@ -59,11 +81,10 @@ static void nslog__normalise_category(nslog_category_t *cat) cat->namelen = strlen(cat->name); } else { nslog__normalise_category(cat->parent); - cat->name = malloc(strlen(cat->parent->name) + strlen(cat->cat_name) + 2); - strcpy(cat->name, cat->parent->name); - strcat(cat->name, "/"); - strcat(cat->name, cat->cat_name); - cat->namelen = strlen(cat->name); + int bufsz = strlen(cat->parent->name) + strlen(cat->cat_name) + 2 /* a slash and a NUL */; + cat->name = malloc(bufsz); + snprintf(cat->name, bufsz, "%s/%s", cat->parent->name, cat->cat_name); + cat->namelen = bufsz - 1; } cat->next = nslog__all_categories; nslog__all_categories = cat; @@ -81,7 +102,7 @@ static void nslog__log_corked(nslog_entry_context_t *ctx, return; } newcork->context = *ctx; - vsprintf(newcork->message, fmt, args); + vsnprintf(newcork->message, measured_len + 1, fmt, args); if (nslog__cork_chain == NULL) { nslog__cork_chain = nslog__cork_chain_last = newcork; } else { diff --git a/src/filter-lexer.l b/src/filter-lexer.l index bbd11eb..f640d48 100644 --- a/src/filter-lexer.l +++ b/src/filter-lexer.l @@ -26,6 +26,8 @@ #define YYLTYPE FILTER_LTYPE #endif +/* Ensure we use yylloc to silence "variable `yylloc` set but not used" warning */ +#define YY_USER_ACTION yylloc->first_line = yylloc->last_line = yylineno; %} diff --git a/test/basictests.c b/test/basictests.c index c6dd9dd..2650bfe 100644 --- a/test/basictests.c +++ b/test/basictests.c @@ -83,7 +83,7 @@ START_TEST (test_nslog_trivial_corked_message) "Captured message wasn't correct"); fail_unless(strcmp(captured_context.filename, "test/basictests.c") == 0, "Captured message wasn't correct filename"); - fail_unless(strcmp(captured_context.funcname, "test_nslog_trivial_corked_message") == 0, + fail_unless(strcmp(captured_context.funcname, __func__) == 0, "Captured message wasn't correct function name"); } END_TEST @@ -109,7 +109,7 @@ START_TEST (test_nslog_trivial_uncorked_message) "Captured message wasn't correct"); fail_unless(strcmp(captured_context.filename, "test/basictests.c") == 0, "Captured message wasn't correct filename"); - fail_unless(strcmp(captured_context.funcname, "test_nslog_trivial_uncorked_message") == 0, + fail_unless(strcmp(captured_context.funcname, __func__) == 0, "Captured message wasn't correct function name"); } END_TEST @@ -150,7 +150,7 @@ START_TEST (test_nslog_two_corked_messages) "Captured message wasn't correct"); fail_unless(strcmp(captured_context.filename, "test/basictests.c") == 0, "Captured message wasn't correct filename"); - fail_unless(strcmp(captured_context.funcname, "test_nslog_two_corked_messages") == 0, + fail_unless(strcmp(captured_context.funcname, __func__) == 0, "Captured message wasn't correct function name"); } END_TEST @@ -223,7 +223,7 @@ START_TEST (test_nslog_simple_filter_corked_message) "Captured message wasn't correct"); fail_unless(strcmp(captured_context.filename, "test/basictests.c") == 0, "Captured message wasn't correct filename"); - fail_unless(strcmp(captured_context.funcname, "test_nslog_simple_filter_corked_message") == 0, + fail_unless(strcmp(captured_context.funcname, __func__) == 0, "Captured message wasn't correct function name"); } @@ -250,7 +250,7 @@ START_TEST (test_nslog_simple_filter_uncorked_message) "Captured message wasn't correct"); fail_unless(strcmp(captured_context.filename, "test/basictests.c") == 0, "Captured message wasn't correct filename"); - fail_unless(strcmp(captured_context.funcname, "test_nslog_simple_filter_uncorked_message") == 0, + fail_unless(strcmp(captured_context.funcname, __func__) == 0, "Captured message wasn't correct function name"); } @@ -277,7 +277,7 @@ START_TEST (test_nslog_simple_filter_subcategory_message) "Captured message wasn't correct"); fail_unless(strcmp(captured_context.filename, "test/basictests.c") == 0, "Captured message wasn't correct filename"); - fail_unless(strcmp(captured_context.funcname, "test_nslog_simple_filter_subcategory_message") == 0, + fail_unless(strcmp(captured_context.funcname, __func__) == 0, "Captured message wasn't correct function name"); } @@ -524,7 +524,7 @@ END_TEST START_TEST (test_nslog_filter_funcname) { nslog_filter_t *filter; - fail_unless(nslog_filter_funcname_new("test_nslog_filter_funcname", &filter) == NSLOG_NO_ERROR, + fail_unless(nslog_filter_funcname_new(__func__, &filter) == NSLOG_NO_ERROR, "Unable to create level filter"); fail_unless(nslog_filter_set_active(filter, NULL) == NSLOG_NO_ERROR, "Unable to set active filter to dir:test"); |