diff options
author | Yorhel <git@yorhel.nl> | 2013-06-04 16:48:23 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2013-06-04 16:48:23 +0200 |
commit | 5a631446b8e8bf1a1dac5574aeafe9e0f36452b7 (patch) | |
tree | 47d65dcd1cb7b6c2872a090d874def8a68aeb14c | |
parent | a510c7269d2223110614047e1deed55f06c1b20d (diff) |
ylog: Add <default_level> pattern as alias for a wildcard match
This simplifies the common case where you just want to specify a single
log level for all files. Instead of writing "*:5", a simple "5" will do
with this addition.
-rw-r--r-- | test/ylog.c | 7 | ||||
-rw-r--r-- | ylog.c | 11 | ||||
-rw-r--r-- | ylog.h | 26 |
3 files changed, 30 insertions, 14 deletions
diff --git a/test/ylog.c b/test/ylog.c index 3a3009c..e8c2b1f 100644 --- a/test/ylog.c +++ b/test/ylog.c @@ -64,6 +64,7 @@ int main(int argc, char **argv) { ylog_set_file_level(&file); assert(file.level == -1); } while(0); + T("", "5", 5); T("", "*:5", 5); T("", "*:0", 0); T("", "*:9999", 9999); /* YLOG_MAX */ @@ -89,6 +90,12 @@ int main(int argc, char **argv) { T("somex/file", "abc:3,some/*:4", -1); T("somex/file", "abc:3,some/*:4,*:1", 1); T("some/file", "abc:3,some/*:4,*:1", 4); + T("somex/file", "abc:3,some/*:4,1", 1); + T("some/file", "abc:3,some/*:4,1", 4); + T("some/file", "*:3,some/file:4", 3); + T("some/file", "3,some/file:4", 3); + T("some/file", "abc:1,*:3,some/file:4", 3); + T("some/file", "abc:1,3,some/file:4", 3); #undef T return 0; @@ -65,14 +65,18 @@ static int ylog_match(const char *pat, const char *fn) { /* Assumes the mutex is locked */ static void ylog_set_file_level(ylog_file_t *file) { - char *pat = ylog_pattern; + char *start, *pat = ylog_pattern; int val; - const char *start; while(pat && *pat) { start = pat; - while(*pat && *pat != ':') + while(*pat && *pat != ':' && *pat != ',') pat++; + /* No colon, assume that this is the <default_level> */ + if(*pat != ':') { + pat = start; + break; + } *pat = 0; val = ylog_match(start, file->name); *(pat++) = ':'; @@ -82,6 +86,7 @@ static void ylog_set_file_level(ylog_file_t *file) { pat++; } + /* Early break, this means that we expect the numeric level for this string. */ if(pat && *pat) { start = pat; val = 0; @@ -55,13 +55,11 @@ * If logging functions in your application can be called from multiple * threads, be sure to compile ylog.c with thread-safety enabled: * -DYLOG_PTHREAD - * (Currently, only pthread is supported) * * This library uses the fnmatch() function, available on every POSIX OS. If * you want to use this library on Windows, you can probably find an * implementation of fnmatch() somewhere (musl-libc has a fairly simple - * implementation that should be trivially portable to windows). I also welcome - * an alternative matching approach that isn't too large and works with pure C. + * implementation that should be trivially portable to windows). * * * Caveats: @@ -230,20 +228,25 @@ void ylog_set_handler(ylog_handler_cb cb); * patterns, describing the log level used for individual files. The format is * as follows: * - * <pattern_1>:<level_1>,<pattern_2>:<level_2>,... + * <pattern_1>:<level_1>,<pattern_2>:<level_2>,...,<default_level> * * Where <pattern_n> is a file-name pattern given to fnmatch(), and <level_n> a * decimal number indicating the log level used for files that match the * pattern. The list of patterns is traversed in the order given in the list, - * the first matching pattern is used. The .c, .h, .cc, .cpp, and .hpp file - * extensions are removed from the file name before matching, and any directory - * prefixes not mentioned in the pattern are ignored. Examples: + * the first matching pattern is used. A <default_level> can be specified at + * the end of the pattern, indicating the log level used when none of the + * previous patterns matched. The .c, .h, .cc, .cpp, and .hpp file extensions + * are removed from the file name before matching, and any directory prefixes + * not mentioned in the pattern are ignored. Examples: * - * "main:1,*:2" + * "3" or "*:3" + * Use log level 3 for all files. + * + * "main:1,2" * Use 1 as log level for main.(c,h,cc,cpp,hpp) in any directory, * Use 2 for all other files. */ -// "*_util:4,net/*:3,*:9" +// "*_util:4,net/*:3,9" // Use 4 for any filename ending with _util.(c,h,cc,cpp,hpp) in any // directory. // Use 3 for any file in a net/ directory, not matching *_util, @@ -251,8 +254,9 @@ void ylog_set_handler(ylog_handler_cb cb); // Use 9 for all other files. /* * If no pattern exists for a file (and there is no final catch-all pattern, as - * in the examples above), default_level will be used instead. This function - * overrides any previously used configuration. + * in the examples above), default_level will be used instead. default_level + * may also be used if the pattern string does not follow the above format. + * This function overrides any previously used configuration. */ void ylog_set_level(int default_level, const char *patterns); |