Re: g_log_structured() and introspection



Hey,

During the talk on GLib's new structured logging API at GUADEC today,
it was pointed out that g_log_structured() (and the rest of the
interesting bits of the new structured logging API) are not
introspectable.

I'm not sure what we can do about this. The API is based around
GLogField, which is basically a pointer and a length, and hence is not
introspectable:

    struct _GLogField
    {
      const gchar *key;
      gconstpointer value;
      gssize length;
    };

One idea:

1) g_log_variant (GLogLevelFlags log_level, GVariant *variant);

The variant would have to be of type G_VARIANT_TYPE_VARDICT

In javascript for instance it could look like this:

fields = { 'MESSAGE': new GLib.Variant('s', 'checking for optional
sweep modulation'),
              'GLIB_DOMAIN': new GLib.Variant('s', 'Foo'),
              'FOO_STATE': new GLib.Variant('s', JSON.stringify(fooState)),
              'FOO_AGE': new GLib.Variant('u', 3) };

GLib.log_variant(GLib.LogLevelFlags.level_debug, new
GLib.Variant('a{sv}', fields))

which is a little wordy, but not more awful than how dbus is done.
Then Gjs could ship a string only convenience
api like g_log_structured by doing:

GLib.log_structured = function(logDomain, logLevel, stringFields) = {
   fields = {};
   for (let key in stringFields) {
     fields[key] = new GLib.Variant('s', stringFields[key]);
   }
   fields['GLIB_DOMAIN'] = new GLib.Variant('s', logDomain);

   GLib.log_variant(logLevel, new GLib.Variant('a{sv}', fields);
}

then the javascript would be:

GLib.log_structured(GLib.LogLevelFlags.level_debug,
                             { 'MESSAGE': 'checking for optional sweep
modulation',
                               'FOO_STATE': JSON.stringify(fooState)});

(or it could make it variadic like g_log_structured, but that's less
idiomatic for our brand of javascript)

One complication with g_log_variant, is figuring out how to send the
variant off to journald. If you just use g_variant_print, you'll end
up putting quotes around all the strings, which is probably wrong.
It's probably have to unpack the variant manually, with fall back to
g_variant_print

anyway, just an idea.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]