2 This file is part of GNUnet.
3 (C) 2008 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/test_common_logging_runtime_loglevels.c
23 * @brief testcase for the logging module (runtime log level adjustment)
27 #include "gnunet_common.h"
28 #include "gnunet_scheduler_lib.h"
29 #include "gnunet_network_lib.h"
30 #include "gnunet_disk_lib.h"
31 #include "gnunet_os_lib.h"
33 #define VERBOSE GNUNET_EXTRA_LOGGING
38 static struct GNUNET_OS_Process *proc;
40 /* Pipe to read from started processes stdout (on read end) */
41 static struct GNUNET_DISK_PipeHandle *pipe_stdout;
43 static GNUNET_SCHEDULER_TaskIdentifier die_task;
45 static void runone ();
48 end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
50 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending phase %d, ok is %d\n", phase,
52 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
54 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
56 GNUNET_OS_process_wait (proc);
57 GNUNET_OS_process_close (proc);
59 GNUNET_DISK_pipe_close (pipe_stdout);
71 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failing\n");
75 read_output_line (int phase_from1, int phase_to1, int phase_from2,
76 int phase_to2, char c, char *expect_level,
77 long delay_morethan, long delay_lessthan, int phase,
78 char *p, int *len, long *delay, char level[8])
85 if (!(phase >= phase_from1 && phase <= phase_to1) &&
86 !(phase >= phase_from2 && phase <= phase_to2))
89 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
90 "Trying to match '%c%s \\d\\r\\n' on %s\n", c, expect_level, p);
92 for (i = 0; i < *len && !stop; i++)
96 case 0: /* read first char */
99 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
100 "Expected '%c', but got '%c'\n", c, r[i]);
106 case 1: /* read at most 7 char-long error level string, finished by ' ' */
122 case 2: /* read the delay, finished by '\n' */
125 if (r[i] == '\r' && r[i + 1] == '\n')
129 *delay = strtol (t, NULL, 10);
136 *delay = strtol (t, NULL, 10);
143 if (!stop || strcmp (expect_level, level) != 0 || *delay < 0
145 || (!((*delay < delay_lessthan) || !(*delay > delay_morethan))
146 && c != '1' && c != '2'))
157 read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
159 struct GNUNET_DISK_FileHandle *stdout_read_handle = cls;
166 GNUNET_DISK_file_read (stdout_read_handle, buf_ptr, sizeof (buf) - bytes);
172 fprintf (stderr, "got %d bytes, reading more\n", rd);
174 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
175 stdout_read_handle, &read_call,
176 (void *) stdout_read_handle);
181 fprintf (stderr, "bytes is %d:%s\n", bytes, buf);
184 /* +------CHILD OUTPUT--
195 * | 7D * * * * * * * *
200 if (bytes == 20 * 16 ||
202 read_output_line (0, 3, 4, 9, 'L', "ERROR", -1, 1, phase, p, &bytes,
205 read_output_line (0, 3, 4, 9, '1', "ERROR", 200, 400, phase, p,
206 &bytes, &delays[0], level))
208 read_output_line (1, 3, 5, 9, 'L', "WARNING", -1, 1, phase, p,
209 &bytes, &delay, level))
211 read_output_line (0, 3, 4, 9, '1', "WARNING", 200, 400, phase, p,
212 &bytes, &delays[1], level))
214 read_output_line (2, 3, 6, 7, 'L', "INFO", -1, 1, phase, p, &bytes,
217 read_output_line (0, 3, 4, 9, '1', "INFO", 200, 400, phase, p,
218 &bytes, &delays[2], level))
220 read_output_line (3, 3, 7, 7, 'L', "DEBUG", -1, 1, phase, p,
221 &bytes, &delay, level))
223 read_output_line (0, 3, 4, 9, '1', "DEBUG", 200, 400, phase, p,
224 &bytes, &delays[3], level))
226 read_output_line (0, 3, 4, 9, 'L', "ERROR", -1, 1, phase, p,
227 &bytes, &delay, level))
229 read_output_line (0, 3, 4, 9, '2', "ERROR", 200, 400, phase, p,
230 &bytes, &delays[4], level))
232 read_output_line (0, 3, 5, 9, 'L', "WARNING", -1, 1, phase, p,
233 &bytes, &delay, level))
235 read_output_line (0, 3, 4, 9, '2', "WARNING", 200, 400, phase, p,
236 &bytes, &delays[5], level))
238 read_output_line (-1, -1, 6, 7, 'L', "INFO", -1, 1, phase, p,
239 &bytes, &delay, level))
241 read_output_line (0, 3, 4, 9, '2', "INFO", 200, 400, phase, p,
242 &bytes, &delays[6], level))
244 read_output_line (-1, -1, 7, 7, 'L', "DEBUG", -1, 1, phase, p,
245 &bytes, &delay, level))
247 read_output_line (0, 3, 4, 9, '2', "DEBUG", 200, 400, phase, p,
248 &bytes, &delays[7], level)))
250 if (bytes == 20 * 16)
251 fprintf (stderr, "Ran out of buffer space!\n");
254 GNUNET_SCHEDULER_cancel (die_task);
255 GNUNET_SCHEDULER_add_now (&end_task, NULL);
259 GNUNET_SCHEDULER_cancel (die_task);
260 GNUNET_SCHEDULER_add_now (&end_task, NULL);
266 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
268 pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
270 if (pipe_stdout == NULL)
277 putenv ("GNUNET_LOG=");
278 putenv ("GNUNET_FORCE_LOG=");
282 putenv ("GNUNET_LOG=;;;;ERROR");
285 putenv ("GNUNET_LOG=;;;;WARNING");
288 putenv ("GNUNET_LOG=;;;;INFO");
291 putenv ("GNUNET_LOG=;;;;DEBUG");
294 putenv ("GNUNET_FORCE_LOG=;;;;ERROR");
297 putenv ("GNUNET_FORCE_LOG=;;;;WARNING");
300 putenv ("GNUNET_FORCE_LOG=;;;;INFO");
303 putenv ("GNUNET_FORCE_LOG=;;;;DEBUG");
306 putenv ("GNUNET_LOG=blah;;;;ERROR");
309 putenv ("GNUNET_FORCE_LOG=blah;;;;ERROR");
314 GNUNET_OS_start_process (NULL, pipe_stdout,
315 "./test_common_logging_dummy",
316 "test_common_logging_dummy", NULL);
317 putenv ("GNUNET_FORCE_LOG=");
318 putenv ("GNUNET_LOG=");
320 /* Close the write end of the read pipe */
321 GNUNET_DISK_pipe_close_end (pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
324 GNUNET_DISK_pipe_handle (pipe_stdout, GNUNET_DISK_PIPE_END_READ);
327 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
328 (GNUNET_TIME_UNIT_SECONDS, 10), &end_task,
333 memset (&buf, 0, sizeof (buf));
335 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
336 stdout_read_handle, &read_call,
337 (void *) stdout_read_handle);
341 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
348 * Main method, starts scheduler with task1,
349 * checks that "ok" is correct at the end.
355 GNUNET_SCHEDULER_run (&task, &ok);
361 main (int argc, char *argv[])
365 GNUNET_log_setup ("test-common-logging-runtime-loglevels",
377 /* end of test_common_logging_runtime_loglevels.c */