#include "gnunet_strings_lib.h"
#include "gnunet_time_lib.h"
+#include <regex.h>
+
/**
* After how many milliseconds do we always print
* that "message X was repeated N times"? Use 12h.
struct LogDef
{
/**
- * Component name. NULL means that this definition matches any component
- */
- char *component;
-
- /**
- * File name. NULL means that this definition matches any file
+ * Component name regex
*/
- char *file;
+ regex_t component_regex;
/**
- * Stores strlen(file)
+ * File name regex
*/
- int strlen_file;
+ regex_t file_regex;
/**
- * Function name. NULL means that this definition matches any function
+ * Function name regex
*/
- char *function;
+ regex_t function_regex;
/**
* Lowest line at which this definition matches.
logdefs = GNUNET_realloc (logdefs, logdefs_size * sizeof (struct LogDef));
}
+
+/**
+ * Abort the process, generate a core dump if possible.
+ */
+void
+GNUNET_abort ()
+{
+#if WINDOWS
+ DebugBreak ();
+#endif
+ abort ();
+}
+
+
/**
* Utility function - adds a parsed definition to logdefs array.
*
* @param to_line see struct LogDef
* @param level see struct LogDef, must be >= 0
* @param force see struct LogDef
+ * @return 0 on success, regex-specific error otherwise
*/
-static void
+static int
add_definition (char *component, char *file, char *function, int from_line,
- int to_line, int level, int force)
+ int to_line, int level, int force)
{
+ struct LogDef n;
+ int r;
+
if (logdefs_size == logdefs_len)
resize_logdefs ();
- struct LogDef n;
memset (&n, 0, sizeof (n));
- if (strlen (component) > 0 && component[0] != '*')
- n.component = strdup (component);
- if (strlen (file) > 0 && file[0] != '*')
- {
- n.file = strdup (file);
- n.strlen_file = strlen (file);
- }
- if ( (NULL != function) &&
- (strlen (function) > 0) &&
- (function[0] != '*') )
- n.function = strdup (function);
+ if (strlen (component) == 0)
+ component = (char *) ".*";
+ r = regcomp (&n.component_regex, (const char *) component, REG_NOSUB);
+ if (r != 0)
+ {
+ return r;
+ }
+ if (strlen (file) == 0)
+ file = (char *) ".*";
+ r = regcomp (&n.file_regex, (const char *) file, REG_NOSUB);
+ if (r != 0)
+ {
+ regfree (&n.component_regex);
+ return r;
+ }
+ if ((NULL == function) || (strlen (function) == 0))
+ function = (char *) ".*";
+ r = regcomp (&n.function_regex, (const char *) function, REG_NOSUB);
+ if (r != 0)
+ {
+ regfree (&n.component_regex);
+ regfree (&n.file_regex);
+ return r;
+ }
n.from_line = from_line;
n.to_line = to_line;
n.level = level;
n.force = force;
logdefs[logdefs_len++] = n;
+ return 0;
}
*/
int
GNUNET_get_log_call_status (int caller_level, const char *comp,
- const char *file, const char *function, int line)
+ const char *file, const char *function, int line)
{
struct LogDef *ld;
int i;
int force_only;
- size_t strlen_file;
if (comp == NULL)
/* Use default component */
/* Only look for forced definitions? */
force_only = min_level >= 0;
- strlen_file = strlen (file);
for (i = 0; i < logdefs_len; i++)
+ {
+ ld = &logdefs[i];
+ if ((!force_only || ld->force) &&
+ (line >= ld->from_line && line <= ld->to_line) &&
+ (regexec (&ld->component_regex, comp, 0, NULL, 0) == 0) &&
+ (regexec (&ld->file_regex, file, 0, NULL, 0) == 0) &&
+ (regexec (&ld->function_regex, function, 0, NULL, 0) == 0))
{
- ld = &logdefs[i];
- if ((!force_only || ld->force) &&
- (line >= ld->from_line && line <= ld->to_line) &&
- (ld->component == NULL || strcmp (comp, ld->component) == 0) &&
- (ld->file == NULL ||
- (ld->strlen_file <= strlen_file &&
- strcmp (&file[strlen_file - ld->strlen_file], ld->file) == 0)) &&
- (ld->function == NULL || strcmp (function, ld->function) == 0))
- {
- /* We're finished */
- return caller_level <= ld->level;
- }
+ /* We're finished */
+ return caller_level <= ld->level;
}
+ }
/* No matches - use global level, if defined */
if (min_level >= 0)
return caller_level <= min_level;
*
* @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
+ * @param force 1 if definitions found in constname are to be forced
* @return number of added definitions
*/
static int
int from_line, to_line;
int counter = 0;
int keep_looking = 1;
+
tmp = getenv (constname);
if (tmp == NULL)
return 0;
- def = strdup (tmp);
- level = -1;
+ def = GNUNET_strdup (tmp);
from_line = 0;
to_line = INT_MAX;
for (p = def, state = 0, start = def; keep_looking; p++)
+ {
+ switch (p[0])
{
- switch (p[0])
- {
- case ';': /* found a field separator */
- p[0] = '\0';
- switch (state)
- {
- case 0: /* within a component name */
- comp = start;
- break;
- case 1: /* within a file name */
- file = start;
- break;
- case 2: /* within a function name */
- /* after a file name there must be a function name */
- function = start;
- break;
- case 3: /* within a from-to line range */
- if (strlen (start) > 0)
- {
- errno = 0;
- from_line = strtol (start, &t, 10);
- if (errno != 0 || from_line < 0)
- {
- free (def);
- return counter;
- }
- if (t < p && t[0] == '-')
- {
- errno = 0;
- start = t + 1;
- to_line = strtol (start, &t, 10);
- if (errno != 0 || to_line < 0 || t != p)
- {
- free (def);
- return counter;
- }
- }
- else /* one number means "match this line only" */
- to_line = from_line;
- }
- else /* default to 0-max */
- {
- from_line = 0;
- to_line = INT_MAX;
- }
- break;
- }
- start = p + 1;
- state += 1;
- break;
- case '\0': /* found EOL */
- keep_looking = 0;
- /* fall through to '/' */
- case '/': /* found a definition separator */
- switch (state)
- {
- case 4: /* within a log level */
- p[0] = '\0';
- state = 0;
- level = get_type ((const char *) start);
- if (level == GNUNET_ERROR_TYPE_INVALID
- || level == GNUNET_ERROR_TYPE_UNSPECIFIED)
- {
- free (def);
- return counter;
- }
- add_definition (comp, file, function, from_line, to_line, level,
- force);
- counter += 1;
- start = p + 1;
- break;
- default:
- break;
- }
- default:
- break;
- }
+ case ';': /* found a field separator */
+ p[0] = '\0';
+ switch (state)
+ {
+ case 0: /* within a component name */
+ comp = start;
+ break;
+ case 1: /* within a file name */
+ file = start;
+ break;
+ case 2: /* within a function name */
+ /* after a file name there must be a function name */
+ function = start;
+ break;
+ case 3: /* within a from-to line range */
+ if (strlen (start) > 0)
+ {
+ errno = 0;
+ from_line = strtol (start, &t, 10);
+ if (errno != 0 || from_line < 0)
+ {
+ GNUNET_free (def);
+ return counter;
+ }
+ if (t < p && t[0] == '-')
+ {
+ errno = 0;
+ start = t + 1;
+ to_line = strtol (start, &t, 10);
+ if (errno != 0 || to_line < 0 || t != p)
+ {
+ GNUNET_free (def);
+ return counter;
+ }
+ }
+ else /* one number means "match this line only" */
+ to_line = from_line;
+ }
+ else /* default to 0-max */
+ {
+ from_line = 0;
+ to_line = INT_MAX;
+ }
+ break;
+ }
+ start = p + 1;
+ state += 1;
+ break;
+ case '\0': /* found EOL */
+ keep_looking = 0;
+ /* fall through to '/' */
+ case '/': /* found a definition separator */
+ switch (state)
+ {
+ case 4: /* within a log level */
+ p[0] = '\0';
+ state = 0;
+ level = get_type ((const char *) start);
+ if (level == GNUNET_ERROR_TYPE_INVALID ||
+ level == GNUNET_ERROR_TYPE_UNSPECIFIED ||
+ 0 != add_definition (comp, file, function, from_line, to_line,
+ level, force))
+ {
+ GNUNET_free (def);
+ return counter;
+ }
+ counter += 1;
+ start = p + 1;
+ break;
+ default:
+ break;
+ }
+ default:
+ break;
}
- free (def);
+ }
+ GNUNET_free (def);
return counter;
}
gnunet_log_parsed = GNUNET_YES;
if (gnunet_force_log_parsed == GNUNET_NO)
gnunet_force_log_present =
- parse_definitions ("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO;
+ parse_definitions ("GNUNET_FORCE_LOG", 1) > 0 ? GNUNET_YES : GNUNET_NO;
gnunet_force_log_parsed = GNUNET_YES;
}
#endif
int dirwarn;
char *fn;
const char *env_logfile = NULL;
+ int altlog_fd;
min_level = get_type (loglevel);
#if !defined(GNUNET_CULL_LOGGING)
GNUNET_free_non_null (component);
GNUNET_asprintf (&component, "%s-%d", comp, getpid ());
GNUNET_free_non_null (component_nopid);
- component_nopid = strdup (comp);
+ component_nopid = GNUNET_strdup (comp);
env_logfile = getenv ("GNUNET_FORCE_LOGFILE");
- if (env_logfile != NULL)
+ if ((env_logfile != NULL) && (strlen (env_logfile) > 0))
logfile = env_logfile;
if (logfile == NULL)
if (NULL == fn)
return GNUNET_SYSERR;
dirwarn = (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn));
- altlog = FOPEN (fn, "a");
- if (altlog == NULL)
+ altlog_fd = OPEN (fn, O_APPEND |
+#if WINDOWS
+ O_BINARY |
+#endif
+ O_WRONLY | O_CREAT,
+#if WINDOWS
+ _S_IREAD | _S_IWRITE
+#else
+ S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
+#endif
+ );
+ if (altlog_fd != -1)
+ {
+ int dup_return;
+ if (GNUNET_stderr != NULL)
+ fclose (GNUNET_stderr);
+ dup_return = dup2 (altlog_fd, 2);
+ close (altlog_fd);
+ if (dup_return != -1)
+ {
+ altlog = fdopen (2, "ab");
+ if (altlog == NULL)
+ {
+ close (2);
+ altlog_fd = -1;
+ }
+ }
+ else
{
- GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", fn);
- if (dirwarn)
- GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
- _
- ("Failed to create or access directory for log file `%s'\n"),
- fn);
- GNUNET_free (fn);
- return GNUNET_SYSERR;
+ altlog_fd = -1;
}
+ }
+ if (altlog_fd == -1)
+ {
+ GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", fn);
+ if (dirwarn)
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ _("Failed to create or access directory for log file `%s'\n"),
+ fn);
+ GNUNET_free (fn);
+ return GNUNET_SYSERR;
+ }
GNUNET_free (fn);
- if (GNUNET_stderr != NULL)
- fclose (GNUNET_stderr);
GNUNET_stderr = altlog;
return GNUNET_OK;
}
prev = NULL;
pos = loggers;
while ((pos != NULL) &&
- ((pos->logger != logger) || (pos->logger_cls != logger_cls)))
- {
- prev = pos;
- pos = pos->next;
- }
+ ((pos->logger != logger) || (pos->logger_cls != logger_cls)))
+ {
+ prev = pos;
+ pos = pos->next;
+ }
GNUNET_assert (pos != NULL);
if (prev == NULL)
loggers = pos->next;
*/
static void
output_message (enum GNUNET_ErrorType kind, const char *comp,
- const char *datestr, const char *msg)
+ const char *datestr, const char *msg)
{
struct CustomLogger *pos;
if (GNUNET_stderr != NULL)
- {
- fprintf (GNUNET_stderr, "%s %s %s %s", datestr, comp,
- GNUNET_error_type_to_string (kind), msg);
- fflush (GNUNET_stderr);
- }
+ {
+ FPRINTF (GNUNET_stderr, "%s %s %s %s", datestr, comp,
+ GNUNET_error_type_to_string (kind), msg);
+ fflush (GNUNET_stderr);
+ }
pos = loggers;
while (pos != NULL)
- {
- pos->logger (pos->logger_cls, kind, comp, datestr, msg);
- pos = pos->next;
- }
+ {
+ pos->logger (pos->logger_cls, kind, comp, datestr, msg);
+ pos = pos->next;
+ }
}
else if (last != last_bulk)
last--;
if (last[0] == '\n')
- {
- rev = 1;
- last[0] = '\0';
- }
- ft =
- GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration
- (last_bulk_time));
+ {
+ rev = 1;
+ last[0] = '\0';
+ }
+ ft = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration
+ (last_bulk_time));
snprintf (msg, sizeof (msg),
- _("Message `%.*s' repeated %u times in the last %s\n"),
- BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft);
+ _("Message `%.*s' repeated %u times in the last %s\n"),
+ BULK_TRACK_SIZE, last_bulk, last_bulk_repeat, ft);
GNUNET_free (ft);
if (rev == 1)
last[0] = '\n';
GNUNET_log_skip (unsigned int n, int check_reset)
{
if (n == 0)
- {
- int ok;
+ {
+ int ok;
- ok = (0 == skip_log);
- skip_log = 0;
- if (check_reset)
- GNUNET_assert (ok);
- }
+ ok = (0 == skip_log);
+ skip_log = 0;
+ if (check_reset)
+ GNUNET_assert (ok);
+ }
else
skip_log += n;
}
tmptr = localtime (&timetmp);
gettimeofday (&timeofday, NULL);
if (NULL != tmptr)
- {
+ {
#ifdef WINDOWS
- LARGE_INTEGER pc;
-
- pc.QuadPart = 0;
- QueryPerformanceCounter (&pc);
- strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%020llu", tmptr);
- snprintf (date, sizeof (date), date2,
- (long long) (pc.QuadPart /
- (performance_frequency.QuadPart / 1000)));
+ LARGE_INTEGER pc;
+
+ pc.QuadPart = 0;
+ QueryPerformanceCounter (&pc);
+ strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%020llu", tmptr);
+ snprintf (date, sizeof (date), date2,
+ (long long) (pc.QuadPart /
+ (performance_frequency.QuadPart / 1000)));
#else
- strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%06u", tmptr);
- snprintf (date, sizeof (date), date2, timeofday.tv_usec);
+ strftime (date2, DATE_STR_SIZE, "%b %d %H:%M:%S-%%06u", tmptr);
+ snprintf (date, sizeof (date), date2, timeofday.tv_usec);
#endif
- }
+ }
else
strcpy (date, "localtime error");
- if ((0 != (kind & GNUNET_ERROR_TYPE_BULK))
- && (last_bulk_time.abs_value != 0)
- && (0 == strncmp (buf, last_bulk, sizeof (last_bulk))))
- {
- last_bulk_repeat++;
- if ((GNUNET_TIME_absolute_get_duration (last_bulk_time).rel_value >
- BULK_DELAY_THRESHOLD)
- || (last_bulk_repeat > BULK_REPEAT_THRESHOLD))
- flush_bulk (date);
- return;
- }
+ if ((0 != (kind & GNUNET_ERROR_TYPE_BULK)) &&
+ (last_bulk_time.abs_value != 0) &&
+ (0 == strncmp (buf, last_bulk, sizeof (last_bulk))))
+ {
+ last_bulk_repeat++;
+ if ((GNUNET_TIME_absolute_get_duration (last_bulk_time).rel_value >
+ BULK_DELAY_THRESHOLD) || (last_bulk_repeat > BULK_REPEAT_THRESHOLD))
+ flush_bulk (date);
+ return;
+ }
flush_bulk (date);
strncpy (last_bulk, buf, sizeof (last_bulk));
last_bulk_repeat = 0;
*/
void
GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
- const char *message, ...)
+ const char *message, ...)
{
va_list va;
char comp_w_pid[128];
return (const char *) ret.encoding;
}
+/**
+ * Convert a peer identity to a string (for printing debug messages).
+ * This is one of the very few calls in the entire API that is
+ * NOT reentrant!
+ *
+ * @param pid the peer identity
+ * @return string form of the pid; will be overwritten by next
+ * call to GNUNET_i2s.
+ */
+const char *
+GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid)
+{
+ static struct GNUNET_CRYPTO_HashAsciiEncoded ret;
+
+ GNUNET_CRYPTO_hash_to_enc (&pid->hashPubKey, &ret);
+ return (const char *) ret.encoding;
+}
/**
if (addr == NULL)
return _("unknown address");
switch (addr->sa_family)
- {
- case AF_INET:
- if (addrlen != sizeof (struct sockaddr_in))
- return "<invalid v4 address>";
- v4 = (const struct sockaddr_in *) addr;
- inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN);
- if (0 == ntohs (v4->sin_port))
- return buf;
- strcat (buf, ":");
- GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v4->sin_port));
- strcat (buf, b2);
- return buf;
- case AF_INET6:
- if (addrlen != sizeof (struct sockaddr_in6))
- return "<invalid v4 address>";
- v6 = (const struct sockaddr_in6 *) addr;
- buf[0] = '[';
- inet_ntop (AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN);
- if (0 == ntohs (v6->sin6_port))
- return &buf[1];
- strcat (buf, "]:");
- GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v6->sin6_port));
- strcat (buf, b2);
- return buf;
- case AF_UNIX:
- if (addrlen <= sizeof (sa_family_t))
- return "<unbound UNIX client>";
- un = (const struct sockaddr_un *) addr;
- off = 0;
- if (un->sun_path[0] == '\0')
- off++;
- snprintf (buf, sizeof (buf), "%s%.*s", (off == 1) ? "@" : "",
- (int) (addrlen - sizeof (sa_family_t) - 1 - off),
- &un->sun_path[off]);
+ {
+ case AF_INET:
+ if (addrlen != sizeof (struct sockaddr_in))
+ return "<invalid v4 address>";
+ v4 = (const struct sockaddr_in *) addr;
+ inet_ntop (AF_INET, &v4->sin_addr, buf, INET_ADDRSTRLEN);
+ if (0 == ntohs (v4->sin_port))
return buf;
- default:
- return _("invalid address");
- }
+ strcat (buf, ":");
+ GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v4->sin_port));
+ strcat (buf, b2);
+ return buf;
+ case AF_INET6:
+ if (addrlen != sizeof (struct sockaddr_in6))
+ return "<invalid v4 address>";
+ v6 = (const struct sockaddr_in6 *) addr;
+ buf[0] = '[';
+ inet_ntop (AF_INET6, &v6->sin6_addr, &buf[1], INET6_ADDRSTRLEN);
+ if (0 == ntohs (v6->sin6_port))
+ return &buf[1];
+ strcat (buf, "]:");
+ GNUNET_snprintf (b2, sizeof (b2), "%u", ntohs (v6->sin6_port));
+ strcat (buf, b2);
+ return buf;
+ case AF_UNIX:
+ if (addrlen <= sizeof (sa_family_t))
+ return "<unbound UNIX client>";
+ un = (const struct sockaddr_un *) addr;
+ off = 0;
+ if (un->sun_path[0] == '\0')
+ off++;
+ snprintf (buf, sizeof (buf), "%s%.*s", (off == 1) ? "@" : "",
+ (int) (addrlen - sizeof (sa_family_t) - 1 - off),
+ &un->sun_path[off]);
+ return buf;
+ default:
+ return _("invalid address");
+ }
}