2
2
This file is part of GNUnet.
3
(C) 2006 Christian Grothoff (and other contributing authors)
3
(C) 2006, 2008 Christian Grothoff (and other contributing authors)
5
5
GNUnet is free software; you can redistribute it and/or modify
6
6
it under the terms of the GNU General Public License as published
25
25
* @author Christian Grothoff
27
27
#include "platform.h"
28
#include "gnunet_util_error.h"
28
#include "gnunet_util.h"
34
* After how many seconds do we always print
35
* that "message X was repeated N times"?
37
#define BULK_DELAY_THRESHOLD (90 * GNUNET_CRON_SECONDS)
40
* After how many repetitions do we always print
41
* that "message X was repeated N times"? (even if
42
* we have not yet reached the delay threshold)
44
#define BULK_REPEAT_THRESHOLD 1000
47
* How many characters do we use for matching of
50
#define BULK_TRACK_SIZE 256
53
* How many characters can a date/time string
56
#define DATE_STR_SIZE 64
33
61
* Default context for logging errors; used
34
62
* if NULL is passed to GNUNET_GE_LOG.
38
66
typedef struct GNUNET_GE_Context
69
* Mask that determines which events to log.
40
71
GNUNET_GE_KIND mask;
74
* Handler to call for each event.
41
76
GNUNET_GE_LogHandler handler;
79
* Extra argument to handler.
84
* Function to call to destroy this context.
43
86
GNUNET_GE_CtxFree destruct;
44
88
GNUNET_GE_Confirm confirm;
91
* The last "bulk" error message that we have been logging.
92
* Note that this message maybe truncated to the first BULK_TRACK_SIZE
93
* characters, in which case it is NOT 0-terminated!
95
char last_bulk[BULK_TRACK_SIZE];
98
* Type of the last bulk message.
100
GNUNET_GE_KIND last_bulk_kind;
103
* Time of the last bulk error message (0 for none)
105
GNUNET_CronTime last_bulk_time;
108
* Number of times that bulk message has been repeated since.
110
unsigned int last_bulk_repeat;
45
112
} GNUNET_GE_Context;
57
124
(both & GNUNET_GE_USERKIND) && (both & GNUNET_GE_ROUTEKIND));
128
flush_bulk (struct GNUNET_GE_Context *ctx, const char *datestr)
130
char msg[DATE_STR_SIZE + BULK_TRACK_SIZE + 256];
135
if ( (ctx == NULL) ||
136
(ctx->last_bulk_time == 0) ||
137
(ctx->last_bulk_repeat == 0) )
139
now = GNUNET_get_time ();
141
last = memchr (ctx->last_bulk, '\0', BULK_TRACK_SIZE);
143
last = &ctx->last_bulk[BULK_TRACK_SIZE - 1];
144
else if (last != ctx->last_bulk)
153
_("Message `%.*s' repeated %u times in the last %llus\n"),
156
ctx->last_bulk_repeat,
157
(now - ctx->last_bulk_time) / GNUNET_CRON_SECONDS);
161
ctx->handler (ctx->cls, ctx->last_bulk_kind, datestr, msg);
163
fprintf (stderr, "%s %s", datestr, msg);
164
ctx->last_bulk_time = now;
165
ctx->last_bulk_repeat = 0;
61
169
GNUNET_GE_LOG (struct GNUNET_GE_Context *ctx, GNUNET_GE_KIND kind,
62
170
const char *message, ...)
173
char date[DATE_STR_SIZE];
72
181
ctx = defaultContext;
88
197
VSNPRINTF (buf, size, message, va);
200
memset (date, 0, DATE_STR_SIZE);
92
201
tmptr = localtime (&timetmp);
93
strftime (date, 64, "%b %d %H:%M:%S", tmptr);
202
strftime (date, DATE_STR_SIZE, "%b %d %H:%M:%S", tmptr);
203
now = GNUNET_get_time ();
205
((kind & GNUNET_GE_BULK) != 0))
207
if ((ctx->last_bulk_time != 0) &&
208
(0 == strncmp (buf, ctx->last_bulk, sizeof (ctx->last_bulk))))
210
ctx->last_bulk_repeat++;
211
if ((now - ctx->last_bulk_time > BULK_DELAY_THRESHOLD) ||
212
(ctx->last_bulk_repeat > BULK_REPEAT_THRESHOLD))
213
flush_bulk (ctx, date);
219
if (ctx->last_bulk_time != 0)
220
flush_bulk (ctx, date);
221
strncpy (ctx->last_bulk, buf, sizeof (ctx->last_bulk));
222
ctx->last_bulk_repeat = 0;
223
ctx->last_bulk_time = now;
224
ctx->last_bulk_kind = kind;
227
if ( (ctx != NULL) &&
228
((now - ctx->last_bulk_time > BULK_DELAY_THRESHOLD) ||
229
(ctx->last_bulk_repeat > BULK_REPEAT_THRESHOLD)) )
231
flush_bulk (ctx, date);
232
ctx->last_bulk_time = 0;
95
235
ctx->handler (ctx->cls, kind, date, buf);
158
298
GNUNET_GE_free_context (GNUNET_GE_Context * ctx)
300
char date[DATE_STR_SIZE];
307
memset (date, 0, DATE_STR_SIZE);
308
tmptr = localtime (&timetmp);
309
strftime (date, DATE_STR_SIZE, "%b %d %H:%M:%S", tmptr);
310
flush_bulk (ctx, date);
162
311
if (ctx->destruct != NULL)
163
312
ctx->destruct (ctx->cls);