~ubuntu-branches/ubuntu/intrepid/gnunet/intrepid

« back to all changes in this revision

Viewing changes to src/util/error/error.c

  • Committer: Bazaar Package Importer
  • Author(s): Bhavani Shankar
  • Date: 2008-08-19 19:44:30 UTC
  • mfrom: (1.2.12 upstream) (3.1.2 lenny)
  • Revision ID: james.westby@ubuntu.com-20080819194430-x5gjv8wd7t7ldkp0
Tags: 0.8.0a-1ubuntu1
* Merge from debian unstable, remaining changes: (LP: #256576)
  - debian/rules:
   + Make use of code from cdbs' clean-la.mk file to clear the
      dependency_libs field in all .la files in the gnunet-dev 
      package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
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)
4
4
 
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
26
26
 */
27
27
#include "platform.h"
28
 
#include "gnunet_util_error.h"
 
28
#include "gnunet_util.h"
29
29
#ifdef MINGW
30
30
#include <conio.h>
31
31
#endif
 
32
 
 
33
/**
 
34
 * After how many seconds do we always print
 
35
 * that "message X was repeated N times"?
 
36
 */
 
37
#define BULK_DELAY_THRESHOLD (90 * GNUNET_CRON_SECONDS)
 
38
 
 
39
/**
 
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)
 
43
 */
 
44
#define BULK_REPEAT_THRESHOLD 1000
 
45
 
 
46
/**
 
47
 * How many characters do we use for matching of
 
48
 * bulk messages?
 
49
 */
 
50
#define BULK_TRACK_SIZE 256
 
51
 
 
52
/**
 
53
 * How many characters can a date/time string
 
54
 * be at most?
 
55
 */
 
56
#define DATE_STR_SIZE 64
 
57
 
 
58
 
 
59
 
32
60
/**
33
61
 * Default context for logging errors; used
34
62
 * if NULL is passed to GNUNET_GE_LOG.
37
65
 
38
66
typedef struct GNUNET_GE_Context
39
67
{
 
68
  /**
 
69
   * Mask that determines which events to log.
 
70
   */
40
71
  GNUNET_GE_KIND mask;
 
72
 
 
73
  /**
 
74
   * Handler to call for each event.
 
75
   */
41
76
  GNUNET_GE_LogHandler handler;
 
77
 
 
78
  /**
 
79
   * Extra argument to handler.
 
80
   */
42
81
  void *cls;
 
82
 
 
83
  /**
 
84
   * Function to call to destroy this context.
 
85
   */
43
86
  GNUNET_GE_CtxFree destruct;
 
87
 
44
88
  GNUNET_GE_Confirm confirm;
 
89
 
 
90
  /**
 
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!
 
94
   */
 
95
  char last_bulk[BULK_TRACK_SIZE];
 
96
 
 
97
  /**
 
98
   * Type of the last bulk message.
 
99
   */
 
100
  GNUNET_GE_KIND last_bulk_kind;
 
101
 
 
102
  /**
 
103
   * Time of the last bulk error message (0 for none)
 
104
   */
 
105
  GNUNET_CronTime last_bulk_time;
 
106
 
 
107
  /**
 
108
   * Number of times that bulk message has been repeated since.
 
109
   */
 
110
  unsigned int last_bulk_repeat;
 
111
 
45
112
} GNUNET_GE_Context;
46
113
 
47
114
/**
57
124
          (both & GNUNET_GE_USERKIND) && (both & GNUNET_GE_ROUTEKIND));
58
125
}
59
126
 
 
127
static void
 
128
flush_bulk (struct GNUNET_GE_Context *ctx, const char *datestr)
 
129
{
 
130
  char msg[DATE_STR_SIZE + BULK_TRACK_SIZE + 256];
 
131
  GNUNET_CronTime now;
 
132
  int rev;
 
133
  char *last;
 
134
  
 
135
  if ( (ctx == NULL) ||
 
136
       (ctx->last_bulk_time == 0) || 
 
137
       (ctx->last_bulk_repeat == 0) )
 
138
    return;
 
139
  now = GNUNET_get_time ();
 
140
  rev = 0;
 
141
  last = memchr (ctx->last_bulk, '\0', BULK_TRACK_SIZE);
 
142
  if (last == NULL)
 
143
    last = &ctx->last_bulk[BULK_TRACK_SIZE - 1];
 
144
  else if (last != ctx->last_bulk)
 
145
    last--;
 
146
  if (last[0] == '\n')
 
147
    {
 
148
      rev = 1;
 
149
      last[0] = '\0';
 
150
    }
 
151
  snprintf (msg,
 
152
            sizeof (msg),
 
153
            _("Message `%.*s' repeated %u times in the last %llus\n"),
 
154
            BULK_TRACK_SIZE,
 
155
            ctx->last_bulk,
 
156
            ctx->last_bulk_repeat,
 
157
            (now - ctx->last_bulk_time) / GNUNET_CRON_SECONDS);
 
158
  if (rev == 1)
 
159
    last[0] = '\n';
 
160
  if (ctx != NULL)
 
161
    ctx->handler (ctx->cls, ctx->last_bulk_kind, datestr, msg);
 
162
  else
 
163
    fprintf (stderr, "%s %s", datestr, msg);
 
164
  ctx->last_bulk_time = now;
 
165
  ctx->last_bulk_repeat = 0;
 
166
}
 
167
 
60
168
void
61
169
GNUNET_GE_LOG (struct GNUNET_GE_Context *ctx, GNUNET_GE_KIND kind,
62
170
               const char *message, ...)
63
171
{
64
172
  va_list va;
65
 
  char date[64];
 
173
  char date[DATE_STR_SIZE];
66
174
  time_t timetmp;
67
175
  struct tm *tmptr;
68
176
  size_t size;
69
177
  char *buf;
 
178
  GNUNET_CronTime now;
70
179
 
71
180
  if (ctx == NULL)
72
181
    ctx = defaultContext;
88
197
  VSNPRINTF (buf, size, message, va);
89
198
  va_end (va);
90
199
  time (&timetmp);
91
 
  memset (date, 0, 64);
 
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 ();
 
204
  if ((ctx != NULL) &&
 
205
      ((kind & GNUNET_GE_BULK) != 0))
 
206
    {
 
207
      if ((ctx->last_bulk_time != 0) &&
 
208
          (0 == strncmp (buf, ctx->last_bulk, sizeof (ctx->last_bulk))))
 
209
        {
 
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);
 
214
          free (buf);
 
215
          return;
 
216
        }
 
217
      else
 
218
        {
 
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;
 
225
        }
 
226
    }
 
227
  if ( (ctx != NULL) &&
 
228
       ((now - ctx->last_bulk_time > BULK_DELAY_THRESHOLD) ||
 
229
        (ctx->last_bulk_repeat > BULK_REPEAT_THRESHOLD)) )
 
230
    {
 
231
      flush_bulk (ctx, date);
 
232
      ctx->last_bulk_time = 0;
 
233
    }
94
234
  if (ctx != NULL)
95
235
    ctx->handler (ctx->cls, kind, date, buf);
96
236
  else
157
297
void
158
298
GNUNET_GE_free_context (GNUNET_GE_Context * ctx)
159
299
{
 
300
  char date[DATE_STR_SIZE];
 
301
  time_t timetmp;
 
302
  struct tm *tmptr;
 
303
 
160
304
  if (ctx == NULL)
161
305
    return;
 
306
  time (&timetmp);
 
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);
164
313
  free (ctx);
326
475
  defaultContext = ctx;
327
476
}
328
477
 
329
 
const char *
330
 
GNUNET_GE_strerror (int errnum)
331
 
{
332
 
  return STRERROR (errnum);
333
 
}
 
478
/* end of error.c */