From 51f01ea57999820ea53f620117fda63790960cda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D1=83=D1=81=D0=BB=D0=B0=D0=BD=20=D0=98=D0=B6=D0=B1=D1?= =?UTF-8?q?=83=D0=BB=D0=B0=D1=82=D0=BE=D0=B2?= Date: Tue, 4 Oct 2011 07:52:56 +0400 Subject: [PATCH 7/8] Doxygen comments, code comment and small changes Makes the trailing '/' optional (it is still required as a separator) --- src/util/common_logging.c | 142 ++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 127 insertions(+), 15 deletions(-) diff --git a/src/util/common_logging.c b/src/util/common_logging.c index 4001b1e..0d0afb0 100644 --- a/src/util/common_logging.c +++ b/src/util/common_logging.c @@ -139,24 +139,85 @@ unsigned int skip_log; */ static FILE *GNUNET_stderr; +/** + * Represents a single logging definition + */ struct LogDef { + /** + * Component name. NULL means that this definition matches any component + */ char *component; + + /** + * File name. NULL means that this definition matches any file + */ char *file; + + /** + * Stores strlen(file) + */ int strlen_file; + + /** + * Function name. NULL means that this definition matches any function + */ char *function; + + /** + * Lowest line at which this definition matches. + * Defaults to 0. Must be <= to_line. + */ int from_line; + + /** + * Highest line at which this definition matches. + * Defaults to INT_MAX. Must be >= from_line. + */ int to_line; + + /** + * Maximal log level allowed for calls that match this definition. + * Calls with higher log level will be disabled. + * Must be >= 0 + */ int level; + + /** + * 1 if this definition comes from GNUNET_FORCE_LOG, which means that it + * overrides any configuration options. 0 otherwise. + */ int force; }; +/** + * Dynamic array of logging definitions + */ struct LogDef *logdefs = NULL; + +/** + * Allocated size of logdefs array (in units) + */ int logdefs_size = 0; + +/** + * The number of units used in logdefs array. + */ int logdefs_len = 0; +/** + * GNUNET_YES if GNUNET_LOG environment variable is already parsed. + */ int gnunet_log_parsed = GNUNET_NO; + +/** + * GNUNET_YES if GNUNET_FORCE_LOG environment variable is already parsed. + */ int gnunet_force_log_parsed = GNUNET_NO; + +/** + * GNUNET_YES if at least one definition with forced == 1 is available. + */ int gnunet_force_log_present = GNUNET_NO; #ifdef WINDOWS @@ -191,6 +252,9 @@ get_type (const char *log) return GNUNET_ERROR_TYPE_INVALID; } #if !defined(GNUNET_CULL_LOGGING) +/** + * Utility function - reallocates logdefs array to be twice as large. + */ static void resize_logdefs () { @@ -198,6 +262,17 @@ resize_logdefs () logdefs = GNUNET_realloc (logdefs, logdefs_size * sizeof (struct LogDef)); } +/** + * Utility function - adds a parsed definition to logdefs array. + * + * @param component see struct LogDef, can't be NULL + * @param file see struct LogDef, can't be NULL + * @param function see struct LogDef, can't be NULL + * @param from_line see struct LogDef + * @param to_line see struct LogDef + * @param level see struct LogDef, must be >= 0 + * @param force see struct LogDef + */ static void add_definition (char *component, char *file, char *function, int from_line, int to_line, int level, int force) { @@ -221,6 +296,19 @@ add_definition (char *component, char *file, char *function, int from_line, int logdefs[logdefs_len++] = n; } +/** + * Decides whether a particular logging call should or should not be allowed + * to be made. Used internally by GNUNET_log*() + * + * @param caller_level loglevel the caller wants to use + * @param comp component name the caller uses (NULL means that global + * component name is used) + * @param file file name containing the logging call, usually __FILE__ + * @param function function which tries to make a logging call, + * usually __FUNCTION__ + * @param line line at which the call is made, usually __LINE__ + * @return 0 to disallow the call, 1 to allow it + */ int GNUNET_get_log_call_status (int caller_level, const char *comp, const char *file, const char *function, int line) { @@ -229,10 +317,18 @@ GNUNET_get_log_call_status (int caller_level, const char *comp, const char *file int force_only; size_t strlen_file; int matches = 0; + if (comp == NULL) + /* Use default component */ comp = component_nopid; + + /* We have no definitions to override globally configured log level, + * so just use it right away. + */ if (min_level >= 0 && gnunet_force_log_present == GNUNET_NO) return caller_level <= min_level; + + /* Only look for forced definitions? */ force_only = min_level >= 0; strlen_file = strlen (file); for (i = 0; i < logdefs_len; i++) @@ -247,12 +343,16 @@ GNUNET_get_log_call_status (int caller_level, const char *comp, const char *file (ld->function == NULL || strcmp (function, ld->function) == 0) ) { + /* This definition matched! */ matches += 1; + /* And if it allows the call to be made, then we're finished */ if (caller_level <= ld->level) return 1; } } - /* If some rules did match, but had too low loglevel to allow logging, don't check any further */ + /* If some definitions did match, but had too low loglevel to allow logging, + * don't check any further. + */ if (matches > 0) return 0; /* Otherwise use global level, if defined */ @@ -266,8 +366,10 @@ GNUNET_get_log_call_status (int caller_level, const char *comp, const char *file /** + * Utility function - parses a definition + * * Definition format: - * component;file;function;from_line-to_line;level/component... + * component;file;function;from_line-to_line;level[/component...] * All entries are mandatory, but may be empty. * Empty entries for component, file and function are treated as * "matches anything". @@ -278,7 +380,13 @@ GNUNET_get_log_call_status (int caller_level, const char *comp, const char *file * single character "*" are treated (at the moment) the same way * empty entries are treated (wildcard matching is not implemented (yet?)). * file entry is matched to the end of __FILE__. That is, it might be - * a base name, or a base name with leading directory names. + * a base name, or a base name with leading directory names (some compilers + * define __FILE__ to absolute file path). + * + * @param constname name of the environment variable from which to get the + * string to be parsed + * @param force 1 if definitions found in @constname are to be forced + * @return number of added definitions */ static int parse_definitions (const char *constname, int force) @@ -295,6 +403,7 @@ parse_definitions (const char *constname, int force) int level; int from_line, to_line; int counter = 0; + int keep_looking = 1; tmp = getenv (constname); if (tmp == NULL) return 0; @@ -302,27 +411,25 @@ parse_definitions (const char *constname, int force) level = -1; from_line = 0; to_line = INT_MAX; - for (p = def, state = 0, start = def; p[0] != '\0'; p++) + for (p = def, state = 0, start = def; keep_looking; p++) { switch (p[0]) { - case ';': + case ';': /* found a field separator */ p[0] = '\0'; switch (state) { case 0: /* within a component name */ comp = start; break; - case 1: - /* after a component name there must be a file name */ + case 1: /* within a file name */ file = start; break; - case 2: + case 2: /* within a function name */ /* after a file name there must be a function name */ function = start; break; - case 3: - /* after a function name there must be a from-to line definition */ + case 3: /* within a from-to line range */ if (strlen (start) > 0) { errno = 0; @@ -343,10 +450,10 @@ parse_definitions (const char *constname, int force) return counter; } } - else + else /* one number means "match this line only" */ to_line = from_line; } - else + else /* default to 0-max */ { from_line = 0; to_line = INT_MAX; @@ -356,11 +463,13 @@ parse_definitions (const char *constname, int force) start = p + 1; state += 1; break; - case '/': + case '\0': /* found EOL */ + keep_looking = 0; + /* fall through to '/' */ + case '/': /* found a definition separator */ switch (state) { - case 4: - /* after a from-to line definition there must be a level definition */ + case 4: /* within a log level */ p[0] = '\0'; state = 0; level = get_type ((const char *) start); @@ -384,6 +493,9 @@ parse_definitions (const char *constname, int force) return counter; } +/** + * Utility function - parses GNUNET_LOG and GNUNET_FORCE_LOG. + */ static void parse_all_definitions () { -- 1.7.4