~ubuntu-branches/ubuntu/wily/syslog-ng/wily-proposed

« back to all changes in this revision

Viewing changes to modules/afmongodb/libmongo-client/examples/bson-inspect.c

  • Committer: Package Import Robot
  • Author(s): Gergely Nagy, Gergely Nagy
  • Date: 2013-11-04 15:27:37 UTC
  • mfrom: (1.3.12)
  • Revision ID: package-import@ubuntu.com-20131104152737-mqh6eqtna2xk97jq
Tags: 3.5.1-1
[ Gergely Nagy <algernon@madhouse-project.org> ]
* New upstream release.
  + Support auto-loading modules (Closes: #650814)
  + The SMTP module is available in syslog-ng-mod-smtp (Closes: #722746)
  + New modules: amqp, geoip, stomp, redis and smtp.
  + Multi-line input support (indented multiline and regexp-based)
  + Template type hinting for the MongoDB destination and $(format-json)
  + Support for unit suffixes in the configuration file
  + New filters, template functions and other miscellaneous changes
* New (team) maintainer, Laszlo Boszormenyi, Attila Szalay and myself
  added to Uploaders.
* Ship /var/lib/syslog-ng in the syslog-ng-core package, instead of
  creating it in the init script. Thanks Michael Biebl
  <biebl@debian.org> for the report & assistance. (Closes: #699942, #719910)
* Use dh-systemd for proper systemd-related maintainer scripts. Based on
  a patch by Michael Biebl <biebl@debian.org>. (Closes: #713982,
  #690067)
* Do not wait for syslog-ng to settle down during installation / update.
  This also fixes installing via debootstrap and a fake
  start-stop-daemon. (Closes: #714254)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* bson-inspect.c - BSON inspector, example application.
2
 
 * Copyright 2011 Gergely Nagy <algernon@balabit.hu>
 
2
 * Copyright 2011, 2012 Gergely Nagy <algernon@balabit.hu>
3
3
 *
4
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
5
 * you may not use this file except in compliance with the License.
49
49
  while (bson_cursor_next (c))
50
50
    {
51
51
      if (!first)
52
 
        {
53
 
          printf (", ");
54
 
          if (verbose)
55
 
            printf ("\n");
56
 
        }
 
52
        {
 
53
          printf (", ");
 
54
          if (verbose)
 
55
            printf ("\n");
 
56
        }
57
57
      first = FALSE;
58
58
      if (verbose)
59
 
        {
60
 
          _indent (ilevel, verbose);
61
 
          printf ("/* type='%s'; */\n",
62
 
                  bson_cursor_type_as_string (c) + 10);
63
 
        }
 
59
        {
 
60
          _indent (ilevel, verbose);
 
61
          printf ("/* type='%s'; */\n",
 
62
                  bson_cursor_type_as_string (c) + 10);
 
63
        }
64
64
      _indent (ilevel, verbose);
65
65
      if (!as_array)
66
 
        {
67
 
          printf ("\"%s\" : ", bson_cursor_key (c));
68
 
        }
 
66
        {
 
67
          printf ("\"%s\" : ", bson_cursor_key (c));
 
68
        }
69
69
      switch (bson_cursor_type (c))
70
 
        {
71
 
        case BSON_TYPE_DOUBLE:
72
 
          {
73
 
            gdouble d;
74
 
            bson_cursor_get_double (c, &d);
75
 
            printf ("%f", d);
76
 
            break;
77
 
          }
78
 
        case BSON_TYPE_STRING:
79
 
          {
80
 
            const gchar *s;
81
 
            gchar *s2;
82
 
            bson_cursor_get_string (c, &s);
83
 
            s2 = g_strescape (s, NULL);
84
 
            printf ("\"%s\"", s2);
85
 
            g_free (s2);
86
 
            break;
87
 
          }
88
 
        case BSON_TYPE_OID:
89
 
          {
90
 
            const guint8 *oid;
91
 
            gint j;
92
 
            bson_cursor_get_oid (c, &oid);
93
 
            printf ("ObjectId( \"");
94
 
            for (j = 0; j < 12; j++)
95
 
              printf ("%02x", oid[j]);
96
 
            printf ("\" )");
97
 
            break;
98
 
          }
99
 
        case BSON_TYPE_BOOLEAN:
100
 
          {
101
 
            gboolean b;
102
 
            bson_cursor_get_boolean (c, &b);
103
 
            printf ((b) ? "true" : "false");
104
 
            break;
105
 
          }
106
 
        case BSON_TYPE_REGEXP:
107
 
          {
108
 
            const gchar *r, *o;
109
 
            gchar *r2, *o2;
110
 
            bson_cursor_get_regex (c, &r, &o);
111
 
            r2 = g_strescape (r, NULL);
112
 
            o2 = g_strescape (o, NULL);
113
 
            printf ("Regex(\"/%s/%s\")", r2, o2);
114
 
            g_free (r2);
115
 
            g_free (o2);
116
 
            break;
117
 
          }
118
 
        case BSON_TYPE_NULL:
119
 
          {
120
 
            printf ("null");
121
 
            break;
122
 
          }
123
 
        case BSON_TYPE_JS_CODE:
124
 
          {
125
 
            const gchar *js;
126
 
            gchar *js2;
127
 
            bson_cursor_get_javascript (c, &js);
128
 
            js2 = g_strescape (js, NULL);
129
 
            printf ("%s", js2);
130
 
            g_free (js2);
131
 
            break;
132
 
          }
133
 
        case BSON_TYPE_SYMBOL:
134
 
          {
135
 
            const gchar *s;
136
 
            gchar *s2;
137
 
            bson_cursor_get_symbol (c, &s);
138
 
            s2 = g_strescape (s, NULL);
139
 
            printf ("%s", s2);
140
 
            g_free (s2);
141
 
            break;
142
 
          }
143
 
        case BSON_TYPE_INT32:
144
 
          {
145
 
            gint32 l32;
146
 
            bson_cursor_get_int32 (c, &l32);
147
 
            printf ("%d", l32);
148
 
            break;
149
 
          }
150
 
        case BSON_TYPE_INT64:
151
 
          {
152
 
            gint64 l64;
153
 
            bson_cursor_get_int64 (c, &l64);
154
 
            printf ("%" G_GINT64_FORMAT, l64);
155
 
            break;
156
 
          }
157
 
        case BSON_TYPE_DOCUMENT:
158
 
          {
159
 
            bson *sd;
160
 
            bson_cursor_get_document (c, &sd);
161
 
            printf ("{ ");
162
 
            if (verbose)
163
 
              printf ("/* size='%d' */\n", bson_size (sd));
164
 
            bson_dump (sd, ilevel + 1, verbose, FALSE);
165
 
            if (verbose)
166
 
              {
167
 
                printf ("\n");
168
 
                _indent (ilevel, verbose);
169
 
                printf ("}");
170
 
              }
171
 
            else
172
 
              printf (" }");
173
 
            bson_free (sd);
174
 
            break;
175
 
          }
176
 
        case BSON_TYPE_ARRAY:
177
 
          {
178
 
            bson *sa;
179
 
 
180
 
            bson_cursor_get_array (c, &sa);
181
 
            printf ("[ ");
182
 
            if (verbose)
183
 
              printf ("/* size='%d' */\n", bson_size (sa));
184
 
            bson_dump (sa, ilevel + 1, verbose, TRUE);
185
 
            if (verbose)
186
 
              {
187
 
                printf ("\n");
188
 
                _indent (ilevel, verbose);
189
 
                printf ("]");
190
 
              }
191
 
            else
192
 
              printf (" ]");
193
 
            bson_free (sa);
194
 
            break;
195
 
          }
196
 
        case BSON_TYPE_BINARY:
197
 
          {
198
 
            const guint8 *data;
199
 
            gint32 size;
200
 
            bson_binary_subtype t;
201
 
            gchar *b64;
202
 
 
203
 
            bson_cursor_get_binary (c, &t, &data, &size);
204
 
            b64 = g_base64_encode (data, size);
205
 
            printf ("{ ");
206
 
            if (verbose)
207
 
              {
208
 
                printf ("/* size='%d' */\n", size);
209
 
                _indent (ilevel + 1, verbose);
210
 
              }
211
 
            printf ("\"$binary\" : \"%s\",", b64);
212
 
            if (verbose)
213
 
              {
214
 
                printf ("\n");
215
 
                _indent (ilevel + 1, verbose);
216
 
              }
217
 
            else
218
 
              printf (" ");
219
 
            printf ("\"$type\" : \"%02d\"", t);
220
 
            if (verbose)
221
 
              {
222
 
                printf ("\n");
223
 
                _indent (ilevel, verbose);
224
 
              }
225
 
            else
226
 
              printf (" ");
227
 
            printf ("}");
228
 
            g_free (b64);
229
 
            break;
230
 
          }
231
 
        case BSON_TYPE_JS_CODE_W_SCOPE:
232
 
        case BSON_TYPE_UNDEFINED:
233
 
        case BSON_TYPE_UTC_DATETIME:
234
 
        case BSON_TYPE_DBPOINTER:
235
 
        case BSON_TYPE_TIMESTAMP:
236
 
        case BSON_TYPE_MIN:
237
 
        case BSON_TYPE_MAX:
238
 
        default:
239
 
          printf ("\"<unimplemented>\"");
240
 
          break;
241
 
        }
 
70
        {
 
71
        case BSON_TYPE_DOUBLE:
 
72
          {
 
73
            gdouble d;
 
74
            bson_cursor_get_double (c, &d);
 
75
            printf ("%f", d);
 
76
            break;
 
77
          }
 
78
        case BSON_TYPE_STRING:
 
79
          {
 
80
            const gchar *s;
 
81
            gchar *s2;
 
82
            bson_cursor_get_string (c, &s);
 
83
            s2 = g_strescape (s, NULL);
 
84
            printf ("\"%s\"", s2);
 
85
            g_free (s2);
 
86
            break;
 
87
          }
 
88
        case BSON_TYPE_OID:
 
89
          {
 
90
            const guint8 *oid;
 
91
            gint j;
 
92
            bson_cursor_get_oid (c, &oid);
 
93
            printf ("ObjectId( \"");
 
94
            for (j = 0; j < 12; j++)
 
95
              printf ("%02x", oid[j]);
 
96
            printf ("\" )");
 
97
            break;
 
98
          }
 
99
        case BSON_TYPE_BOOLEAN:
 
100
          {
 
101
            gboolean b;
 
102
            bson_cursor_get_boolean (c, &b);
 
103
            printf ((b) ? "true" : "false");
 
104
            break;
 
105
          }
 
106
        case BSON_TYPE_REGEXP:
 
107
          {
 
108
            const gchar *r, *o;
 
109
            gchar *r2, *o2;
 
110
            bson_cursor_get_regex (c, &r, &o);
 
111
            r2 = g_strescape (r, NULL);
 
112
            o2 = g_strescape (o, NULL);
 
113
            printf ("Regex(\"/%s/%s\")", r2, o2);
 
114
            g_free (r2);
 
115
            g_free (o2);
 
116
            break;
 
117
          }
 
118
        case BSON_TYPE_NULL:
 
119
          {
 
120
            printf ("null");
 
121
            break;
 
122
          }
 
123
        case BSON_TYPE_JS_CODE:
 
124
          {
 
125
            const gchar *js;
 
126
            gchar *js2;
 
127
            bson_cursor_get_javascript (c, &js);
 
128
            js2 = g_strescape (js, NULL);
 
129
            printf ("%s", js2);
 
130
            g_free (js2);
 
131
            break;
 
132
          }
 
133
        case BSON_TYPE_SYMBOL:
 
134
          {
 
135
            const gchar *s;
 
136
            gchar *s2;
 
137
            bson_cursor_get_symbol (c, &s);
 
138
            s2 = g_strescape (s, NULL);
 
139
            printf ("%s", s2);
 
140
            g_free (s2);
 
141
            break;
 
142
          }
 
143
        case BSON_TYPE_INT32:
 
144
          {
 
145
            gint32 l32;
 
146
            bson_cursor_get_int32 (c, &l32);
 
147
            printf ("%d", l32);
 
148
            break;
 
149
          }
 
150
        case BSON_TYPE_INT64:
 
151
          {
 
152
            gint64 l64;
 
153
            bson_cursor_get_int64 (c, &l64);
 
154
            printf ("%" G_GINT64_FORMAT, l64);
 
155
            break;
 
156
          }
 
157
        case BSON_TYPE_DOCUMENT:
 
158
          {
 
159
            bson *sd;
 
160
            bson_cursor_get_document (c, &sd);
 
161
            printf ("{ ");
 
162
            if (verbose)
 
163
              printf ("/* size='%d' */\n", bson_size (sd));
 
164
            bson_dump (sd, ilevel + 1, verbose, FALSE);
 
165
            if (verbose)
 
166
              {
 
167
                printf ("\n");
 
168
                _indent (ilevel, verbose);
 
169
                printf ("}");
 
170
              }
 
171
            else
 
172
              printf (" }");
 
173
            bson_free (sd);
 
174
            break;
 
175
          }
 
176
        case BSON_TYPE_ARRAY:
 
177
          {
 
178
            bson *sa;
 
179
 
 
180
            bson_cursor_get_array (c, &sa);
 
181
            printf ("[ ");
 
182
            if (verbose)
 
183
              printf ("/* size='%d' */\n", bson_size (sa));
 
184
            bson_dump (sa, ilevel + 1, verbose, TRUE);
 
185
            if (verbose)
 
186
              {
 
187
                printf ("\n");
 
188
                _indent (ilevel, verbose);
 
189
                printf ("]");
 
190
              }
 
191
            else
 
192
              printf (" ]");
 
193
            bson_free (sa);
 
194
            break;
 
195
          }
 
196
        case BSON_TYPE_BINARY:
 
197
          {
 
198
            const guint8 *data;
 
199
            gint32 size;
 
200
            bson_binary_subtype t;
 
201
            gchar *b64;
 
202
 
 
203
            bson_cursor_get_binary (c, &t, &data, &size);
 
204
            b64 = g_base64_encode (data, size);
 
205
            printf ("{ ");
 
206
            if (verbose)
 
207
              {
 
208
                printf ("/* size='%d' */\n", size);
 
209
                _indent (ilevel + 1, verbose);
 
210
              }
 
211
            printf ("\"$binary\" : \"%s\",", b64);
 
212
            if (verbose)
 
213
              {
 
214
                printf ("\n");
 
215
                _indent (ilevel + 1, verbose);
 
216
              }
 
217
            else
 
218
              printf (" ");
 
219
            printf ("\"$type\" : \"%02d\"", t);
 
220
            if (verbose)
 
221
              {
 
222
                printf ("\n");
 
223
                _indent (ilevel, verbose);
 
224
              }
 
225
            else
 
226
              printf (" ");
 
227
            printf ("}");
 
228
            g_free (b64);
 
229
            break;
 
230
          }
 
231
        case BSON_TYPE_JS_CODE_W_SCOPE:
 
232
        case BSON_TYPE_UNDEFINED:
 
233
        case BSON_TYPE_UTC_DATETIME:
 
234
        case BSON_TYPE_DBPOINTER:
 
235
        case BSON_TYPE_TIMESTAMP:
 
236
        case BSON_TYPE_MIN:
 
237
        case BSON_TYPE_MAX:
 
238
        default:
 
239
          printf ("\"<unimplemented>\"");
 
240
          break;
 
241
        }
242
242
    }
243
243
  bson_cursor_free (c);
244
244
}
259
259
  GOptionEntry entries[] =
260
260
    {
261
261
      { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
262
 
        "Be verbose", NULL },
 
262
        "Be verbose", NULL },
263
263
      { NULL, 0, 0, 0, NULL, NULL, NULL }
264
264
    };
265
265
 
290
290
  if (fd == -1)
291
291
    {
292
292
      fprintf (stderr, "Error opening file '%s': %s\n",
293
 
               argv[1], strerror (errno));
 
293
               argv[1], strerror (errno));
294
294
      exit (1);
295
295
    }
296
296
  if (fstat (fd, &st) != 0)
297
297
    {
298
298
      fprintf (stderr, "Error fstat()ing file '%s': %s\n",
299
 
               argv[1], strerror (errno));
 
299
               argv[1], strerror (errno));
300
300
      close (fd);
301
301
      exit (1);
302
302
    }
305
305
  if (data == MAP_FAILED)
306
306
    {
307
307
      fprintf (stderr, "Error mmap()ing file '%s': %s\n",
308
 
               argv[1], strerror (errno));
 
308
               argv[1], strerror (errno));
309
309
      close (fd);
310
310
      exit (1);
311
311
    }
313
313
  while (offs < st.st_size)
314
314
    {
315
315
      b = bson_new_from_data ((const guint8 *)(data + offs),
316
 
                              bson_stream_doc_size (data, offs) - 1);
 
316
                              bson_stream_doc_size (data, offs) - 1);
317
317
      bson_finish (b);
318
318
      offs += bson_size (b);
319
319
 
320
320
      if (verbose)
321
 
        printf ("/* Document #%" G_GUINT64_FORMAT "; size='%d' */\n", i,
322
 
                bson_size (b));
 
321
        printf ("/* Document #%" G_GUINT64_FORMAT "; size='%d' */\n", i,
 
322
                bson_size (b));
323
323
      printf ("{ ");
324
324
      if (verbose)
325
 
        printf ("\n");
 
325
        printf ("\n");
326
326
      bson_dump (b, 1, verbose, FALSE);
327
327
      if (verbose)
328
 
        printf ("\n}\n");
 
328
        printf ("\n}\n");
329
329
      else
330
 
        printf (" }\n");
 
330
        printf (" }\n");
331
331
      if (verbose)
332
 
        printf ("\n");
 
332
        printf ("\n");
333
333
 
334
334
      bson_free (b);
335
335
      i++;