2003-08-10 Havoc Pennington <hp@pobox.com>
[platform/upstream/dbus.git] / tools / dbus-print-message.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-print-message.h  Utility function to print out a message
3  *
4  * Copyright (C) 2003 Philip Blundell <philb@gnu.org>
5  * Copyright (C) 2003 Red Hat, Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22 #include "dbus-print-message.h"
23
24 static const char*
25 type_to_name (int message_type)
26 {
27   switch (message_type)
28     {
29     case DBUS_MESSAGE_TYPE_SIGNAL:
30       return "signal";
31     case DBUS_MESSAGE_TYPE_METHOD_CALL:
32       return "method call";
33     case DBUS_MESSAGE_TYPE_METHOD_RETURN:
34       return "method return";
35     case DBUS_MESSAGE_TYPE_ERROR:
36       return "error";
37     default:
38       return "(unknown message type)";
39     }
40 }
41
42 void
43 print_message (DBusMessage *message)
44 {
45   DBusMessageIter iter;
46   const char *sender;
47   int message_type;
48
49   message_type = dbus_message_get_type (message);
50   sender = dbus_message_get_sender (message); 
51   
52   printf ("%s name=%s; sender=%s\n",
53           type_to_name (message_type),
54           dbus_message_get_name (message),
55           sender ? sender : "(no sender)");
56   
57   dbus_message_iter_init (message, &iter);
58
59   do
60     {
61       int type = dbus_message_iter_get_arg_type (&iter);
62       char *str;
63       dbus_uint32_t uint32;
64       dbus_int32_t int32;
65       double d;
66       unsigned char byte;
67       dbus_bool_t boolean;
68
69       if (type == DBUS_TYPE_INVALID)
70         break;
71
72       switch (type)
73         {
74         case DBUS_TYPE_STRING:
75           str = dbus_message_iter_get_string (&iter);
76           printf ("string:%s\n", str);
77           break;
78
79         case DBUS_TYPE_INT32:
80           int32 = dbus_message_iter_get_int32 (&iter);
81           printf ("int32:%d\n", int32);
82           break;
83
84         case DBUS_TYPE_UINT32:
85           uint32 = dbus_message_iter_get_uint32 (&iter);
86           printf ("int32:%u\n", uint32);
87           break;
88
89         case DBUS_TYPE_DOUBLE:
90           d = dbus_message_iter_get_double (&iter);
91           printf ("double:%f\n", d);
92           break;
93
94         case DBUS_TYPE_BYTE:
95           byte = dbus_message_iter_get_byte (&iter);
96           printf ("byte:%d\n", byte);
97           break;
98
99         case DBUS_TYPE_BOOLEAN:
100           boolean = dbus_message_iter_get_boolean (&iter);
101           printf ("boolean:%s\n", boolean ? "true" : "false");
102           break;
103
104         default:
105           printf ("(unknown arg type %d)\n", type);
106           break;
107         }
108     } while (dbus_message_iter_next (&iter));
109 }
110