2005-01-30 Havoc Pennington <hp@redhat.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   const char *destination;
48   int message_type;
49   int count;
50
51   message_type = dbus_message_get_type (message);
52   sender = dbus_message_get_sender (message);
53   destination = dbus_message_get_destination (message);
54
55   printf ("%s sender=%s -> dest=%s",
56           type_to_name (message_type),
57           sender ? sender : "(null sender)",
58           destination ? destination : "(null destination)");
59          
60   switch (message_type)
61     {
62     case DBUS_MESSAGE_TYPE_METHOD_CALL:
63     case DBUS_MESSAGE_TYPE_SIGNAL:
64       printf (" interface=%s; member=%s\n",
65               dbus_message_get_interface (message),
66               dbus_message_get_member (message));
67       break;
68       
69     case DBUS_MESSAGE_TYPE_METHOD_RETURN:
70       printf ("\n");
71       break;
72
73     case DBUS_MESSAGE_TYPE_ERROR:
74       printf (" error_name=%s\n",
75               dbus_message_get_error_name (message));
76       break;
77
78     default:
79       printf ("\n");
80       break;
81     }
82
83   dbus_message_iter_init (message, &iter);
84   count = 0;
85   
86   do
87     {
88       int type = dbus_message_iter_get_arg_type (&iter);
89       const char *str;
90       dbus_uint32_t uint32;
91       dbus_int32_t int32;
92       double d;
93       unsigned char byte;
94       dbus_bool_t boolean;
95
96       if (type == DBUS_TYPE_INVALID)
97         break;
98
99       switch (type)
100         {
101         case DBUS_TYPE_STRING:
102           dbus_message_iter_get_basic (&iter, &str);
103           printf (" %d string \"%s\"\n", count, str);
104           break;
105
106         case DBUS_TYPE_INT32:
107           dbus_message_iter_get_basic (&iter, &int32);
108           printf (" %d int32 %d\n", count, int32);
109           break;
110
111         case DBUS_TYPE_UINT32:
112           dbus_message_iter_get_basic (&iter, &uint32);
113           printf (" %d uint32 %u\n", count, uint32);
114           break;
115
116         case DBUS_TYPE_DOUBLE:
117           dbus_message_iter_get_basic (&iter, &d);
118           printf (" %d double %g\n", count, d);
119           break;
120
121         case DBUS_TYPE_BYTE:
122           dbus_message_iter_get_basic (&iter, &byte);
123           printf (" %d byte %d\n", count, byte);
124           break;
125
126         case DBUS_TYPE_BOOLEAN:
127           dbus_message_iter_get_basic (&iter, &boolean);
128           printf (" %d boolean %s\n", count, boolean ? "true" : "false");
129           break;
130
131         default:
132           printf (" (dbus-monitor too dumb to decipher arg type '%c')\n", type);
133           break;
134         }
135       
136       count += 1;
137     } while (dbus_message_iter_next (&iter));
138 }
139