2 This file is part of GNUnet.
3 (C) 2011 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_NO
39 static struct GNUNET_OS_Process *proc;
41 /* Pipe to read from started processes stdout (on read end) */
42 static struct GNUNET_DISK_PipeHandle *pipe_stdout;
44 static GNUNET_SCHEDULER_TaskIdentifier die_task;
46 static GNUNET_SCHEDULER_TaskIdentifier read_task;
53 end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
55 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending phase %d, ok is %d\n", phase,
59 if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
61 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
63 GNUNET_OS_process_wait (proc);
64 GNUNET_OS_process_destroy (proc);
67 if (GNUNET_SCHEDULER_NO_TASK != read_task)
69 GNUNET_SCHEDULER_cancel (read_task);
70 read_task = GNUNET_SCHEDULER_NO_TASK;
72 GNUNET_DISK_pipe_close (pipe_stdout);
84 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "failing\n");
89 read_output_line (int phase_from1, int phase_to1, int phase_from2,
90 int phase_to2, char c, const char *expect_level,
91 long delay_morethan, long delay_lessthan, int phase,
93 int *len, long *delay, char level[8])
102 if (!(phase >= phase_from1 && phase <= phase_to1) &&
103 !(phase >= phase_from2 && phase <= phase_to2))
106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
107 "Trying to match '%c%s \\d\\r\\n' on %s\n", c, expect_level, p);
109 for (i = 0; i < *len && !stop; i++)
113 case 0: /* read first char */
116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected '%c', but got '%c'\n", c,
123 case 1: /* read at most 7 char-long error level string, finished by ' ' */
139 case 2: /* read the delay, finished by '\n' */
142 if (r[i] == '\r' && r[i + 1] == '\n')
146 *delay = strtol (t, NULL, 10);
153 *delay = strtol (t, NULL, 10);
160 if (!stop || strcmp (expect_level, level) != 0 || *delay < 0 || *delay > 1000000
161 || (!((*delay < delay_lessthan) || (*delay > delay_morethan)) && c != '1'
168 static char buf[20 * 16];
170 static char *buf_ptr;
176 read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
178 const struct GNUNET_DISK_FileHandle *stdout_read_handle = cls;
184 read_task = GNUNET_SCHEDULER_NO_TASK;
185 rd = GNUNET_DISK_file_read (stdout_read_handle, buf_ptr,
186 sizeof (buf) - bytes);
192 FPRINTF (stderr, "got %d bytes, reading more\n", rd);
194 read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
195 stdout_read_handle, &read_call,
196 (void*) stdout_read_handle);
201 FPRINTF (stderr, "bytes is %d:%s\n", bytes, buf);
204 /* +------CHILD OUTPUT--
215 * | 7D * * * * * * * *
221 if (bytes == 20 * 16 ||
223 read_output_line (0, 3, 4, 9, 'L', "ERROR", -1, 1, phase, p, &bytes,
226 read_output_line (0, 3, 4, 9, '1', "ERROR", 200, 400, phase, p, &bytes,
227 &delays[0], level)) ||
229 read_output_line (1, 3, 5, 9, 'L', "WARNING", -1, 1, phase, p, &bytes,
232 read_output_line (0, 3, 4, 9, '1', "WARNING", 200, 400, phase, p,
233 &bytes, &delays[1], level)) ||
235 read_output_line (2, 3, 6, 7, 'L', "INFO", -1, 1, phase, p, &bytes,
238 read_output_line (0, 3, 4, 9, '1', "INFO", 200, 400, phase, p, &bytes,
239 &delays[2], level)) ||
241 read_output_line (3, 3, 7, 7, 'L', "DEBUG", -1, 1, phase, p, &bytes,
244 read_output_line (0, 3, 4, 9, '1', "DEBUG", 200, 400, phase, p, &bytes,
245 &delays[3], level)) ||
247 read_output_line (0, 3, 4, 9, 'L', "ERROR", -1, 1, phase, p, &bytes,
250 read_output_line (0, 3, 4, 9, '2', "ERROR", 200, 400, phase, p, &bytes,
251 &delays[4], level)) ||
253 read_output_line (0, 3, 5, 9, 'L', "WARNING", -1, 1, phase, p, &bytes,
256 read_output_line (0, 3, 4, 9, '2', "WARNING", 200, 400, phase, p,
257 &bytes, &delays[5], level)) ||
259 read_output_line (-1, -1, 6, 7, 'L', "INFO", -1, 1, phase, p, &bytes,
262 read_output_line (0, 3, 4, 9, '2', "INFO", 200, 400, phase, p, &bytes,
263 &delays[6], level)) ||
265 read_output_line (-1, -1, 7, 7, 'L', "DEBUG", -1, 1, phase, p, &bytes,
268 read_output_line (0, 3, 4, 9, '2', "DEBUG", 200, 400, phase, p, &bytes,
271 if (bytes == 20 * 16)
272 FPRINTF (stderr, "%s", "Ran out of buffer space!\n");
275 GNUNET_SCHEDULER_cancel (die_task);
276 GNUNET_SCHEDULER_add_now (&end_task, NULL);
280 GNUNET_SCHEDULER_cancel (die_task);
281 GNUNET_SCHEDULER_add_now (&end_task, NULL);
288 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
290 pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
292 if (pipe_stdout == NULL)
299 putenv ("GNUNET_LOG=");
300 putenv ("GNUNET_FORCE_LOG=");
301 putenv ("GNUNET_FORCE_LOGFILE=");
305 putenv ("GNUNET_LOG=;;;;ERROR");
308 putenv ("GNUNET_LOG=;;;;WARNING");
311 putenv ("GNUNET_LOG=;;;;INFO");
314 putenv ("GNUNET_LOG=;;;;DEBUG");
317 putenv ("GNUNET_FORCE_LOG=;;;;ERROR");
320 putenv ("GNUNET_FORCE_LOG=;;;;WARNING");
323 putenv ("GNUNET_FORCE_LOG=;;;;INFO");
326 putenv ("GNUNET_FORCE_LOG=;;;;DEBUG");
329 putenv ("GNUNET_LOG=blah;;;;ERROR");
332 putenv ("GNUNET_FORCE_LOG=blah;;;;ERROR");
336 proc = GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, pipe_stdout,
338 "test_common_logging_dummy",
340 "./test_common_logging_dummy",
342 "test_common_logging_dummy", NULL);
343 GNUNET_assert (NULL != proc);
344 putenv ("GNUNET_FORCE_LOG=");
345 putenv ("GNUNET_LOG=");
347 /* Close the write end of the read pipe */
348 GNUNET_DISK_pipe_close_end (pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
351 GNUNET_DISK_pipe_handle (pipe_stdout, GNUNET_DISK_PIPE_END_READ);
354 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
355 (GNUNET_TIME_UNIT_SECONDS, 10), &end_task,
360 memset (&buf, 0, sizeof (buf));
362 read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
363 stdout_read_handle, &read_call,
364 (void*) stdout_read_handle);
369 task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
377 main (int argc, char *argv[])
379 GNUNET_log_setup ("test-common-logging-runtime-loglevels",
383 GNUNET_SCHEDULER_run (&task, &ok);
387 /* end of test_common_logging_runtime_loglevels.c */