New event API (RHBZ#664558).
[libguestfs.git] / capitests / test-debug-to-file.c
1 /* libguestfs
2  * Copyright (C) 2011 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 /* Test that we can use the new event API to capture all debugging
20  * messages to a file.
21  */
22
23 #include <config.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include "guestfs.h"
31
32 static void
33 debug_to_file (guestfs_h *g,
34                void *opaque,
35                uint64_t event,
36                int event_handle,
37                int flags,
38                const char *buf, size_t buf_len,
39                const uint64_t *array, size_t array_len)
40 {
41   FILE *fp = opaque;
42
43   fwrite (buf, 1, buf_len, fp);
44 }
45
46 int
47 main (int argc, char *argv[])
48 {
49   guestfs_h *g;
50   const char *filename = "test.log";
51   FILE *debugfp;
52
53   debugfp = fopen (filename, "w");
54   if (debugfp == NULL) {
55     perror (filename);
56     exit (EXIT_FAILURE);
57   }
58
59   g = guestfs_create ();
60   if (g == NULL) {
61     fprintf (stderr, "failed to create handle\n");
62     exit (EXIT_FAILURE);
63   }
64
65   if (guestfs_set_event_callback
66       (g, debug_to_file,
67        GUESTFS_EVENT_LIBRARY|GUESTFS_EVENT_APPLIANCE|GUESTFS_EVENT_TRACE,
68        0, debugfp) == -1)
69     exit (EXIT_FAILURE);
70
71   if (guestfs_set_verbose (g, 1) == -1)
72     exit (EXIT_FAILURE);
73
74   if (guestfs_set_trace (g, 1) == -1)
75     exit (EXIT_FAILURE);
76
77   if (guestfs_add_drive_opts (g, "/dev/null",
78                               GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
79                               GUESTFS_ADD_DRIVE_OPTS_READONLY, 1,
80                               -1) == -1)
81     exit (EXIT_FAILURE);
82
83   if (guestfs_launch (g) == -1)
84     exit (EXIT_FAILURE);
85
86   guestfs_close (g);
87
88   exit (EXIT_SUCCESS);
89 }