1 (* libguestfs OCaml bindings
2 * Copyright (C) 2011 Red Hat Inc.
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.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 let log g ev eh buf array =
24 | Guestfs.EVENT_CLOSE -> "close"
25 | Guestfs.EVENT_SUBPROCESS_QUIT -> "subprocess_quit"
26 | Guestfs.EVENT_LAUNCH_DONE -> "launch_done"
27 | Guestfs.EVENT_PROGRESS -> "progress"
28 | Guestfs.EVENT_APPLIANCE -> "appliance"
29 | Guestfs.EVENT_LIBRARY -> "library"
30 | Guestfs.EVENT_TRACE -> "trace"
31 | Guestfs.EVENT_ENTER -> "enter" in
33 let eh : int = Obj.magic eh in
35 printf "ocaml event logged: event=%s eh=%d buf=%S array=[%s]\n"
37 (String.concat ", " (List.map Int64.to_string (Array.to_list array)))
39 let close_invoked = ref 0
41 let close g ev eh buf array =
46 let g = new Guestfs.guestfs () in
48 (* Grab log, trace and daemon messages into our own custom handler
49 * which prints the messages with a particular prefix.
51 let events = [Guestfs.EVENT_APPLIANCE; Guestfs.EVENT_LIBRARY;
52 Guestfs.EVENT_TRACE] in
53 ignore (g#set_event_callback log events);
55 (* Check that the close event is invoked. *)
56 ignore (g#set_event_callback close [Guestfs.EVENT_CLOSE]);
58 (* Now make sure we see some messages. *)
63 g#add_drive_ro "/dev/null";
66 (* Close the handle -- should call the close callback. *)
67 assert (!close_invoked = 0);
69 assert (!close_invoked = 1);
71 (* Run full garbage collection. *)