Browse Source

Set GLib log handler

DarthSim 2 years ago
parent
commit
a9dda9ca4e
4 changed files with 70 additions and 0 deletions
  1. 23 0
      gliblog/gliblog.c
  2. 38 0
      gliblog/gliblog.go
  3. 6 0
      gliblog/gliblog.h
  4. 3 0
      main.go

+ 23 - 0
gliblog/gliblog.c

@@ -0,0 +1,23 @@
+#include "gliblog.h"
+
+static GLogLevelFlags all_levels =
+  G_LOG_FLAG_RECURSION |
+  G_LOG_FLAG_FATAL |
+  G_LOG_LEVEL_ERROR |
+  G_LOG_LEVEL_CRITICAL |
+  G_LOG_LEVEL_WARNING |
+  G_LOG_LEVEL_MESSAGE |
+  G_LOG_LEVEL_INFO;
+
+void
+log_handler(const gchar *log_domain, GLogLevelFlags log_level,
+  const gchar *message, gpointer user_data) {
+
+  logGLib((char *)log_domain, log_level, (char *)message);
+}
+
+void
+glib_log_configure() {
+  g_log_set_handler (NULL, all_levels, log_handler, NULL);
+  g_log_set_handler ("VIPS",  all_levels, log_handler, NULL);
+}

+ 38 - 0
gliblog/gliblog.go

@@ -0,0 +1,38 @@
+package gliblog
+
+/*
+#cgo pkg-config: glib-2.0
+#include "gliblog.h"
+*/
+import "C"
+import log "github.com/sirupsen/logrus"
+
+//export logGLib
+func logGLib(cdomain *C.char, logLevel C.GLogLevelFlags, cstr *C.char) {
+	str := C.GoString(cstr)
+
+	var domain string
+	if cdomain != nil {
+		domain = C.GoString(cdomain)
+	}
+	if len(domain) == 0 {
+		domain = "GLib"
+	}
+
+	entry := log.WithField("source", domain)
+
+	switch logLevel {
+	case C.G_LOG_LEVEL_DEBUG:
+		entry.Debug(str)
+	case C.G_LOG_LEVEL_INFO, C.G_LOG_LEVEL_MESSAGE:
+		entry.Info(str)
+	case C.G_LOG_LEVEL_WARNING:
+		entry.Warn(str)
+	default:
+		entry.Error(str)
+	}
+}
+
+func Init() {
+	C.glib_log_configure()
+}

+ 6 - 0
gliblog/gliblog.h

@@ -0,0 +1,6 @@
+#include <glib.h>
+
+void glib_log_configure();
+
+// from Go
+void logGLib (char *domain, GLogLevelFlags log_level, char *message);

+ 3 - 0
main.go

@@ -14,6 +14,7 @@ import (
 
 	"github.com/imgproxy/imgproxy/v3/config"
 	"github.com/imgproxy/imgproxy/v3/errorreport"
+	"github.com/imgproxy/imgproxy/v3/gliblog"
 	"github.com/imgproxy/imgproxy/v3/imagedata"
 	"github.com/imgproxy/imgproxy/v3/logger"
 	"github.com/imgproxy/imgproxy/v3/memory"
@@ -30,6 +31,8 @@ func initialize() error {
 		return err
 	}
 
+	gliblog.Init()
+
 	maxprocs.Set(maxprocs.Logger(log.Debugf))
 
 	if err := config.Configure(); err != nil {