Re: g_log_structured() and introspection



Hey,

On Sat, 2016-08-13 at 01:49 -0400, Ray Strode wrote:

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)});

Seems reasonable to me. pygobject, gjs people: how does that look to
you?

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

Yeah, the conversion from each a{sv} value to a GLogField value would
be the least satisfying part: I guess we'd do something like:
  if it's a string: use the string directly
  if it's a byte array ('ay'): use the byte array with its length
  if it's anything else: g_variant_print() and hope it looks reasonable

Philip

Attachment: signature.asc
Description: This is a digitally signed message part



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