After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 771598 - overrides/GLib: Add log_structured() - wrapper for g_log_variant()
overrides/GLib: Add log_structured() - wrapper for g_log_variant()
Status: RESOLVED FIXED
Product: gjs
Classification: Bindings
Component: general
unspecified
Other All
: Normal normal
: ---
Assigned To: gjs-maint
gjs-maint
Depends on:
Blocks: 770717
 
 
Reported: 2016-09-17 18:07 UTC by Jonh Wendell
Modified: 2016-10-03 22:52 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
overrides/GLib: Add log_structured() - wrapper for g_log_variant() (1.46 KB, patch)
2016-09-17 18:07 UTC, Jonh Wendell
none Details | Review
overrides/GLib: Add log_structured() - wrapper for g_log_variant() (1.62 KB, patch)
2016-10-03 22:05 UTC, Jonh Wendell
committed Details | Review

Description Jonh Wendell 2016-09-17 18:07:50 UTC
This simplifies JS code for doing structured log:

GLib.log_structured('teste',
                    GLib.LogLevelFlags.LEVEL_WARNING,
                    {'MESSAGE': 'Just a test',
                     'A_FIELD': 'A value'});

Instead of having to create the variant variables by hand.
Comment 1 Jonh Wendell 2016-09-17 18:07:55 UTC
Created attachment 335770 [details] [review]
overrides/GLib: Add log_structured() - wrapper for g_log_variant()
Comment 2 Ray Strode [halfline] 2016-09-20 18:42:19 UTC
Review of attachment 335770 [details] [review]:

seems fine to me. you'll need to also add a dependency in configure.ac on glib 2.50.0.

::: modules/overrides/GLib.js
@@ +249,3 @@
+    let fields = {};
+    for (let key in stringFields) {
+        fields[key] = new GLib.Variant('s', stringFields[key]);

you could potentially 

throw new TypeError('field is not a string') if typeof stringsFields[key] != 'string'

but maybe the new GLib.Variant call already does that for us ?

@@ +293,3 @@
     };
+
+    this.log_structured = _log_structured;

all the other definitions in _init use assigned anonymous functions.  I think your way is cleaner, but it's also not as consistent.
Comment 3 Jonh Wendell 2016-10-03 22:05:52 UTC
Created attachment 336843 [details] [review]
overrides/GLib: Add log_structured() - wrapper for g_log_variant()

This simplifies JS code for doing structured log:

GLib.log_structured('teste',
                    GLib.LogLevelFlags.LEVEL_WARNING,
                    {'MESSAGE': 'Just a test',
                     'A_FIELD': 'A value'});

Instead of having to create the variant variables by hand.
Comment 4 Jonh Wendell 2016-10-03 22:09:20 UTC
(In reply to Ray Strode [halfline] from comment #2)
> ::: modules/overrides/GLib.js
> @@ +249,3 @@
> +    let fields = {};
> +    for (let key in stringFields) {
> +        fields[key] = new GLib.Variant('s', stringFields[key]);
> 
> you could potentially 
> 
> throw new TypeError('field is not a string') if typeof stringsFields[key] !=
> 'string'
> 
> but maybe the new GLib.Variant call already does that for us ?

Yep, it raises an error with a stack trace. Example when I used a number:

(gjs:26292): Gjs-WARNING **: JS ERROR: Error: Expected type utf8 for Argument 'string' but got type 'number'
_pack_variant@resource:///org/gnome/gjs/modules/overrides/GLib.js:104
@resource:///org/gnome/gjs/modules/overrides/GLib.js:261
@resource:///org/gnome/gjs/modules/overrides/GLib.js:289
@test2.js:7
Comment 5 Cosimo Cecchi 2016-10-03 22:40:34 UTC
Review of attachment 336843 [details] [review]:

Looks good to me.
Comment 6 Jonh Wendell 2016-10-03 22:52:36 UTC
Attachment 336843 [details] pushed as f934ac9 - overrides/GLib: Add log_structured() - wrapper for g_log_variant()