From c8dce9ce407b2f8248d669df196a4bf0f9523723 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Thu, 27 Dec 2018 23:09:55 +0100
Subject: Updated the logging features of the Python bindings.

---
 plugins/pychrysalide/core/logs.c   | 139 ++++++++++++-------------------------
 plugins/pychrysalide/core/logs.h   |   7 +-
 plugins/pychrysalide/core/module.c |   2 +-
 plugins/pychrysalide/helpers.c     |  20 +++---
 plugins/pychrysalide/helpers.h     |  11 +--
 plugins/pychrysalide/plugin.c      |  56 +++++++++++++++
 plugins/pychrysalide/pychrysa.c    |  33 ++++++++-
 plugins/pychrysalide/pychrysa.h    |   4 ++
 8 files changed, 155 insertions(+), 117 deletions(-)

diff --git a/plugins/pychrysalide/core/logs.c b/plugins/pychrysalide/core/logs.c
index 38084c4..f2158f9 100644
--- a/plugins/pychrysalide/core/logs.c
+++ b/plugins/pychrysalide/core/logs.c
@@ -33,6 +33,7 @@
 
 #include "../access.h"
 #include "../helpers.h"
+#include "../pychrysa.h"
 
 
 
@@ -46,13 +47,13 @@ static PyObject *py_logs_set_verbosity(PyObject *, PyObject *);
 static PyObject *py_logs_log_message(PyObject *, PyObject *);
 
 /* Définit les constantes pour les types de message. */
-static bool define_python_log_constants(PyTypeObject *);
+static bool define_python_log_constants(PyObject *);
 
 
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : self = classe assurant le lien avec l'éditeur de messages.   *
+*  Paramètres  : self = objet Python concerné par l'appel.                    *
 *                args = arguments fournis à l'appel.                          *
 *                                                                             *
 *  Description : Fournit la verbosité des messages système.                   *
@@ -79,7 +80,7 @@ static PyObject *py_logs_get_verbosity(PyObject *self, PyObject *args)
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : self = classe assurant le lien avec l'éditeur de messages.   *
+*  Paramètres  : self = objet Python concerné par l'appel.                    *
 *                args = arguments fournis à l'appel.                          *
 *                                                                             *
 *  Description : Définit la verbosité des messages système.                   *
@@ -110,7 +111,7 @@ static PyObject *py_logs_set_verbosity(PyObject *self, PyObject *args)
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : self = classe assurant le lien avec l'éditeur de messages.   *
+*  Paramètres  : self = objet Python concerné par l'appel.                    *
 *                args = arguments fournis à l'appel.                          *
 *                                                                             *
 *  Description : Affiche un message dans le journal des messages système.     *
@@ -138,7 +139,7 @@ static PyObject *py_logs_log_message(PyObject *self, PyObject *args)
         case LMT_BAD_BINARY:
         case LMT_ERROR:
         case LMT_EXT_ERROR:
-            log_simple_message(type, msg);
+            log_pychrysalide_simple_message(type, msg);
             result = Py_None;
             Py_INCREF(result);
             break;
@@ -158,66 +159,7 @@ static PyObject *py_logs_log_message(PyObject *self, PyObject *args)
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : -                                                            *
-*                                                                             *
-*  Description : Fournit un accès à une définition de type à diffuser.        *
-*                                                                             *
-*  Retour      : Définition d'objet pour Python.                              *
-*                                                                             *
-*  Remarques   : -                                                            *
-*                                                                             *
-******************************************************************************/
-
-PyTypeObject *get_python_logs_type(void)
-{
-    static PyMethodDef py_logs_methods[] = {
-        {
-            "get_verbosity", py_logs_get_verbosity,
-            METH_NOARGS | METH_STATIC,
-            "get_verbosity(, /)\n--\n\nGet the log verbosity."
-        },
-        {
-            "set_verbosity", py_logs_set_verbosity,
-            METH_VARARGS | METH_STATIC,
-            "set_verbosity(, /)\n--\n\nSet the log verbosity."
-        },
-        {
-            "log_message", py_logs_log_message,
-            METH_VARARGS | METH_STATIC,
-            "log_message(type, msg, /)\n--\n\nDisplay a message in the log window, if any."
-        },
-        { NULL }
-
-    };
-
-    static PyGetSetDef py_logs_getseters[] = {
-        { NULL }
-    };
-
-    static PyTypeObject py_logs_type = {
-
-        PyVarObject_HEAD_INIT(NULL, 0)
-
-        .tp_name = "pychrysalide.core.logs",
-        .tp_basicsize = sizeof(PyObject) + 80,
-
-        .tp_flags = Py_TPFLAGS_DEFAULT,
-
-        .tp_doc = "Access to the core log facilities",
-
-        .tp_methods = py_logs_methods,
-        .tp_getset  = py_logs_getseters
-
-    };
-
-    return &py_logs_type;
-
-}
-
-
-/******************************************************************************
-*                                                                             *
-*  Paramètres  : obj_type = type dont le dictionnaire est à compléter.        *
+*  Paramètres  : dict = dictionnaire de module à compléter.                   *
 *                                                                             *
 *  Description : Définit les constantes pour les types de message.            *
 *                                                                             *
@@ -227,19 +169,19 @@ PyTypeObject *get_python_logs_type(void)
 *                                                                             *
 ******************************************************************************/
 
-static bool define_python_log_constants(PyTypeObject *obj_type)
+static bool define_python_log_constants(PyObject *dict)
 {
     bool result;                            /* Bilan à retourner           */
 
     result = true;
 
-    result &= PyDict_AddULongMacro(obj_type, LMT_INFO);
-    result &= PyDict_AddULongMacro(obj_type, LMT_PROCESS);
-    result &= PyDict_AddULongMacro(obj_type, LMT_WARNING);
-    result &= PyDict_AddULongMacro(obj_type, LMT_BAD_BINARY);
-    result &= PyDict_AddULongMacro(obj_type, LMT_ERROR);
-    result &= PyDict_AddULongMacro(obj_type, LMT_EXT_ERROR);
-    result &= PyDict_AddULongMacro(obj_type, LMT_COUNT);
+    result &= PyModDict_AddULongMacro(dict, LMT_INFO);
+    result &= PyModDict_AddULongMacro(dict, LMT_PROCESS);
+    result &= PyModDict_AddULongMacro(dict, LMT_WARNING);
+    result &= PyModDict_AddULongMacro(dict, LMT_BAD_BINARY);
+    result &= PyModDict_AddULongMacro(dict, LMT_ERROR);
+    result &= PyModDict_AddULongMacro(dict, LMT_EXT_ERROR);
+    result &= PyModDict_AddULongMacro(dict, LMT_COUNT);
 
     return result;
 
@@ -248,9 +190,9 @@ static bool define_python_log_constants(PyTypeObject *obj_type)
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : module = module dont la définition est à compléter.          *
+*  Paramètres  : -                                                            *
 *                                                                             *
-*  Description : Prend en charge l'objet 'pychrysalide.gui.panels.LogPanel'.  *
+*  Description : Définit une extension du module 'core' à compléter.          *
 *                                                                             *
 *  Retour      : Bilan de l'opération.                                        *
 *                                                                             *
@@ -258,34 +200,43 @@ static bool define_python_log_constants(PyTypeObject *obj_type)
 *                                                                             *
 ******************************************************************************/
 
-bool ensure_python_logs_is_registered(void)
+bool populate_core_module_with_logs(void)
 {
-    PyTypeObject *type;                     /* Type Python pour 'logs'     */
+    bool result;                            /* Bilan à retourner           */
     PyObject *module;                       /* Module à recompléter        */
-    int ret;                                /* Bilan d'un appel            */
+    PyObject *dict;                         /* Dictionnaire dudit module   */
 
-    type = get_python_logs_type();
+    static PyMethodDef py_queue_methods[] = {
 
-    if (!PyType_HasFeature(type, Py_TPFLAGS_READY))
-    {
-        type->tp_new = PyType_GenericNew;
-
-        if (PyType_Ready(type) != 0)
-            return false;
-
-        if (!define_python_log_constants(type))
-            return false;
+        {
+            "get_verbosity", py_logs_get_verbosity,
+            METH_NOARGS,
+            "get_verbosity(, /)\n--\n\nGet the log verbosity."
+        },
+        {
+            "set_verbosity", py_logs_set_verbosity,
+            METH_VARARGS,
+            "set_verbosity(, /)\n--\n\nSet the log verbosity."
+        },
+        {
+            "log_message", py_logs_log_message,
+            METH_VARARGS,
+            "log_message(type, msg, /)\n--\n\nDisplay a message in the log window, if any."
+        },
+        { NULL }
 
-        module = get_access_to_python_module("pychrysalide.core");
+    };
 
-        Py_INCREF(type);
-        ret = PyModule_AddObject(module, "logs", (PyObject *)type);
+    module = get_access_to_python_module("pychrysalide.core");
 
-        if (ret != 0)
-            return false;
+    result = register_python_module_methods(module, py_queue_methods);
 
+    if (result)
+    {
+        dict = PyModule_GetDict(module);
+        result = define_python_log_constants(dict);
     }
 
-    return true;
+    return result;
 
 }
diff --git a/plugins/pychrysalide/core/logs.h b/plugins/pychrysalide/core/logs.h
index 3165897..ed27808 100644
--- a/plugins/pychrysalide/core/logs.h
+++ b/plugins/pychrysalide/core/logs.h
@@ -31,11 +31,8 @@
 
 
 
-/* Fournit un accès à une définition de type à diffuser. */
-PyTypeObject *get_python_logs_type(void);
-
-/* Prend en charge l'objet 'pychrysalide.core.logs'. */
-bool ensure_python_logs_is_registered(void);
+/* Définit une extension du module 'core' à compléter. */
+bool populate_core_module_with_logs(void);
 
 
 
diff --git a/plugins/pychrysalide/core/module.c b/plugins/pychrysalide/core/module.c
index 900bf83..c786ad6 100644
--- a/plugins/pychrysalide/core/module.c
+++ b/plugins/pychrysalide/core/module.c
@@ -94,7 +94,7 @@ bool populate_core_module(void)
 
     if (result) result = ensure_python_demanglers_is_registered();
     if (result) result = populate_core_module_with_global();
-    if (result) result = ensure_python_logs_is_registered();
+    if (result) result = populate_core_module_with_logs();
     if (result) result = ensure_python_params_is_registered();
     if (result) result = populate_core_module_with_queue();
 
diff --git a/plugins/pychrysalide/helpers.c b/plugins/pychrysalide/helpers.c
index 9237aba..ad62de4 100644
--- a/plugins/pychrysalide/helpers.c
+++ b/plugins/pychrysalide/helpers.c
@@ -208,9 +208,9 @@ PyObject *run_python_method(PyObject *module, const char *method, PyObject *args
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : obj_type = type dont le dictionnaire est à compléter.        *
-*                key      = désignation de la constante à intégrer.           *
-*                value    = valeur de la constante à intégrer.                *
+*  Paramètres  : dict  = dictionnaire à compléter.                            *
+*                key   = désignation de la constante à intégrer.              *
+*                value = valeur de la constante à intégrer.                   *
 *                                                                             *
 *  Description : Ajoute une constante au dictionnaire d'un type Python donné. *
 *                                                                             *
@@ -220,7 +220,7 @@ PyObject *run_python_method(PyObject *module, const char *method, PyObject *args
 *                                                                             *
 ******************************************************************************/
 
-bool PyDict_AddULongConstant(PyTypeObject *obj_type, const char *key, unsigned long value)
+bool PyDict_AddULongConstant(PyObject *dict, const char *key, unsigned long value)
 {
     bool result;                            /* Bilan à retourner           */
     PyObject *item;                         /* Nouvel élément à insérer    */
@@ -228,7 +228,7 @@ bool PyDict_AddULongConstant(PyTypeObject *obj_type, const char *key, unsigned l
 
     item = build_constval_from_c_code(key, value);
 
-    ret = PyDict_SetItemString(obj_type->tp_dict, key, item);
+    ret = PyDict_SetItemString(dict, key, item);
     result = (ret != -1);
 
     Py_DECREF(item);
@@ -240,9 +240,9 @@ bool PyDict_AddULongConstant(PyTypeObject *obj_type, const char *key, unsigned l
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : obj_type = type dont le dictionnaire est à compléter.        *
-*                key      = désignation de la constante à intégrer.           *
-*                value    = valeur de la constante à intégrer.                *
+*  Paramètres  : dict  = dictionnaire à compléter.                            *
+*                key   = désignation de la constante à intégrer.              *
+*                value = valeur de la constante à intégrer.                   *
 *                                                                             *
 *  Description : Ajoute une constante au dictionnaire d'un type Python donné. *
 *                                                                             *
@@ -252,7 +252,7 @@ bool PyDict_AddULongConstant(PyTypeObject *obj_type, const char *key, unsigned l
 *                                                                             *
 ******************************************************************************/
 
-bool PyDict_AddStringConstant(PyTypeObject *obj_type, const char *key, const char *value)
+bool PyDict_AddStringConstant(PyObject *dict, const char *key, const char *value)
 {
     bool result;                            /* Bilan à retourner           */
     PyObject *item;                         /* Nouvel élément à insérer    */
@@ -260,7 +260,7 @@ bool PyDict_AddStringConstant(PyTypeObject *obj_type, const char *key, const cha
 
     item = PyUnicode_FromString(value);
 
-    ret = PyDict_SetItemString(obj_type->tp_dict, key, item);
+    ret = PyDict_SetItemString(dict, key, item);
     result = (ret != -1);
 
     Py_DECREF(item);
diff --git a/plugins/pychrysalide/helpers.h b/plugins/pychrysalide/helpers.h
index c31c7ad..e080ca4 100644
--- a/plugins/pychrysalide/helpers.h
+++ b/plugins/pychrysalide/helpers.h
@@ -47,14 +47,17 @@ PyObject *_run_python_method(PyObject *, PyObject *);
 PyObject *run_python_method(PyObject *, const char *, PyObject *);
 
 /* Ajoute une constante au dictionnaire d'un type Python donné. */
-bool PyDict_AddULongConstant(PyTypeObject *, const char *, unsigned long);
+bool PyDict_AddULongConstant(PyObject *, const char *, unsigned long);
 
 /* Ajoute une constante au dictionnaire d'un type Python donné. */
-bool PyDict_AddStringConstant(PyTypeObject *, const char *, const char *);
+bool PyDict_AddStringConstant(PyObject *, const char *, const char *);
 
 
-#define PyDict_AddULongMacro(tp, c) PyDict_AddULongConstant(tp, #c, c)
-#define PyDict_AddStringMacro(tp, c) PyDict_AddStringConstant(tp, #c, c)
+#define PyDict_AddULongMacro(tp, c) PyDict_AddULongConstant(tp->tp_dict, #c, c)
+#define PyDict_AddStringMacro(tp, c) PyDict_AddStringConstant(tp->tp_dict, #c, c)
+
+#define PyModDict_AddULongMacro(d, c) PyDict_AddULongConstant(d, #c, c)
+#define PyModDict_AddStringMacro(d, c) PyDict_AddStringConstant(d, #c, c)
 
 
 
diff --git a/plugins/pychrysalide/plugin.c b/plugins/pychrysalide/plugin.c
index f232548..a693d7b 100644
--- a/plugins/pychrysalide/plugin.c
+++ b/plugins/pychrysalide/plugin.c
@@ -104,6 +104,9 @@ static void g_python_plugin_process_disass(const GPythonPlugin *, PluginAction,
 /* ------------------------- MODULE PYTHON POUR LES SCRIPTS ------------------------- */
 
 
+/* Affiche un message dans le journal des messages système. */
+static PyObject *py_plugin_module_log_message(PyObject *, PyObject *);
+
 /* Définit les constantes pour les greffons en Python. */
 static bool py_plugin_module_define_constants(PyTypeObject *);
 
@@ -895,6 +898,54 @@ static void g_python_plugin_process_disass(const GPythonPlugin *plugin, PluginAc
 
 /******************************************************************************
 *                                                                             *
+*  Paramètres  : self = objet Python concerné par l'appel.                    *
+*                args = arguments fournis à l'appel.                          *
+*                                                                             *
+*  Description : Affiche un message dans le journal des messages système.     *
+*                                                                             *
+*  Retour      : Rien en équivalent Python.                                   *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static PyObject *py_plugin_module_log_message(PyObject *self, PyObject *args)
+{
+    PyObject *result;                       /* Bilan à retourner           */
+    unsigned long type;                     /* Espèce du message           */
+    const char *msg;                        /* Contenu du message          */
+
+    if (!PyArg_ParseTuple(args, "ks", &type, &msg))
+        return NULL;
+
+    switch (type)
+    {
+        case LMT_INFO:
+        case LMT_PROCESS:
+        case LMT_WARNING:
+        case LMT_BAD_BINARY:
+        case LMT_ERROR:
+        case LMT_EXT_ERROR:
+            /*g_plugin_module_*/log_simple_message(/*G_PLUGIN_MODULE(pygobject_get(self)), */type, msg);
+            result = Py_None;
+            Py_INCREF(result);
+            break;
+
+        default:
+            PyErr_SetString(PyExc_ValueError,
+                            _("Invalid type of message"));
+            result = NULL;
+            break;
+
+    }
+
+    return result;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
 *  Paramètres  : obj_type = type dont le dictionnaire est à compléter.        *
 *                                                                             *
 *  Description : Définit les constantes pour les greffons en Python.          *
@@ -962,6 +1013,11 @@ static bool py_plugin_module_define_constants(PyTypeObject *obj_type)
 PyTypeObject *get_python_plugin_module_type(void)
 {
     static PyMethodDef py_plugin_module_methods[] = {
+        {
+            "log_message", py_plugin_module_log_message,
+            METH_VARARGS,
+            "log_message(type, msg, /)\n--\n\nDisplay a message in the log window, if any."
+        },
         { NULL }
     };
 
diff --git a/plugins/pychrysalide/pychrysa.c b/plugins/pychrysalide/pychrysa.c
index ff36dab..8849981 100644
--- a/plugins/pychrysalide/pychrysa.c
+++ b/plugins/pychrysalide/pychrysa.c
@@ -67,8 +67,11 @@ DEFINE_CHRYSALIDE_CONTAINER_PLUGIN("PyChrysalide", "Provides bindings to Python"
                                    EMPTY_PG_LIST(.required), AL(PGA_PLUGIN_INIT, PGA_PLUGIN_EXIT, PGA_NATIVE_LOADED));
 
 
+/* Conservation d'une référence au greffon pour les messages */
+static GPluginModule *_this = NULL;
+
 /* Note la nature du chargement */
-static bool _standalone = true;
+#define _standalone (_this == NULL)
 
 /* Réceptacle pour le chargement forcé */
 static PyObject *_chrysalide_module = NULL;
@@ -566,6 +569,8 @@ G_MODULE_EXPORT bool chrysalide_plugin_init(GPluginModule *plugin)
     DIR *dir;                               /* Répertoire à parcourir      */
     int ret;                                /* Bilan de préparatifs        */
 
+    _this = plugin;
+
     /* Définition des zones d'influence */
 
     dir = opendir(PLUGINS_DATA_DIR G_DIR_SEPARATOR_S "python");
@@ -585,8 +590,6 @@ G_MODULE_EXPORT bool chrysalide_plugin_init(GPluginModule *plugin)
 
     /* Chargement du module pour Python */
 
-    _standalone = false;
-
     ret = PyImport_AppendInittab("pychrysalide", &PyInit_pychrysalide);
 
     if (ret == -1)
@@ -695,3 +698,27 @@ PyThreadState *get_pychrysalide_main_tstate(void)
     return result;
 
 }
+
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : msg = message à faire apparaître à l'écran.                  *
+*                                                                             *
+*  Description : Présente dans le journal un message simple.                  *
+*                                                                             *
+*  Retour      : -                                                            *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+void log_pychrysalide_simple_message(LogMessageType type, const char *msg)
+{
+    if (_this != NULL)
+        g_plugin_module_log_simple_message(_this, type, msg);
+
+    else
+        log_simple_message(type, msg);
+
+}
diff --git a/plugins/pychrysalide/pychrysa.h b/plugins/pychrysalide/pychrysa.h
index 0f844e6..74f699a 100644
--- a/plugins/pychrysalide/pychrysa.h
+++ b/plugins/pychrysalide/pychrysa.h
@@ -36,6 +36,7 @@
 
 
 #include <plugins/plugin.h>
+#include <plugins/plugin-int.h>
 
 
 
@@ -54,6 +55,9 @@ G_MODULE_EXPORT void chrysalide_plugin_on_native_loaded(GPluginModule *, PluginA
 /* Fournit les informations du thread principal. */
 PyThreadState *get_pychrysalide_main_tstate(void);
 
+/* Présente dans le journal un message simple. */
+void log_pychrysalide_simple_message(LogMessageType, const char *);
+
 
 
 #endif  /* _PLUGINS_PYCHRYSALIDE_PYCHRYSA_H */
-- 
cgit v0.11.2-87-g4458