From 39626f45d96d624a970f2a0f4825e4f6bb5cc817 Mon Sep 17 00:00:00 2001 From: Olivier Dion <olivier.dion@savoirfairelinux.com> Date: Fri, 15 Oct 2021 10:29:23 -0400 Subject: [PATCH] agent/bindings/logger: Add Jami's logger foreign function bindings Change-Id: Ib70fdc0739a0ab7216be851b35f55a1ecb72a9a2 --- test/agent/jami/logger.scm | 27 +++++++++++++++ test/agent/src/bindings/bindings.cpp | 12 +++++-- test/agent/src/bindings/logger.h | 51 ++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 test/agent/jami/logger.scm create mode 100644 test/agent/src/bindings/logger.h diff --git a/test/agent/jami/logger.scm b/test/agent/jami/logger.scm new file mode 100644 index 0000000000..5cf40c06dc --- /dev/null +++ b/test/agent/jami/logger.scm @@ -0,0 +1,27 @@ +(define-module (jami logger) + #:use-module ((jami logger bindings) #:prefix ffi:) + #:export (debug + info + warning + error)) + +(define-syntax-rule (logging% lvl fmt args ...) + (let* ((source-location (current-source-location)) + (filename (or (assq-ref source-location 'filename) "<guile>")) + (line (or (assq-ref source-location 'line) -1))) + (ffi:log lvl + filename + (+ line 1) + (format #f fmt args ...)))) + +(define-syntax-rule (debug fmt args ...) + (logging% ffi:LOG_DEBUG fmt args ...)) + +(define-syntax-rule (info fmt args ...) + (logging% ffi:LOG_INFO fmt args ...)) + +(define-syntax-rule (warning fmt args ...) + (logging% ffi:LOG_WARNING fmt args ...)) + +(define-syntax-rule (error fmt args ...) + (logging% ffi:LOG_ERR fmt args ...)) diff --git a/test/agent/src/bindings/bindings.cpp b/test/agent/src/bindings/bindings.cpp index 7ef7f28c7c..6876234f53 100644 --- a/test/agent/src/bindings/bindings.cpp +++ b/test/agent/src/bindings/bindings.cpp @@ -24,15 +24,21 @@ /* Include module's bindings here */ #include "bindings/account.h" #include "bindings/call.h" +#include "bindings/logger.h" #include "bindings/signal.h" void install_scheme_primitives() { /* Define modules here */ - scm_c_define_module("jami account", install_account_primitives, NULL); - scm_c_define_module("jami call", install_call_primitives, NULL); - scm_c_define_module("jami signal", install_signal_primitives, NULL); + auto load_module = [](auto name, auto init){ + scm_c_define_module(name, init, NULL); + }; + + load_module("jami account", install_account_primitives); + load_module("jami call", install_call_primitives); + load_module("jami logger bindings", install_logger_primitives); + load_module("jami signal", install_signal_primitives); } /* diff --git a/test/agent/src/bindings/logger.h b/test/agent/src/bindings/logger.h new file mode 100644 index 0000000000..a7637a1a61 --- /dev/null +++ b/test/agent/src/bindings/logger.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2021 Savoir-faire Linux Inc. + * + * Author: Olivier Dion <olivier.dion@savoirfairelinux.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#pragma once + +/* Jami */ +#include "logger.h" + +/* Agent */ +#include "utils.h" + +static SCM log_binding(SCM log_lvl_int, SCM file_str, SCM line_int, SCM text_str) +{ + const std::string file = from_guile(file_str); + const std::string text = from_guile(text_str); + + jami::Logger::log(from_guile(log_lvl_int), + file.c_str(), + from_guile(line_int), + false, "[GUILE] %s\n", text.c_str()); + + return SCM_UNDEFINED; +} + +static void +install_logger_primitives(void *) +{ + define_primitive("log", 4, 0, 0, (void*) log_binding); + + DEFINE_INT(LOG_DEBUG); + DEFINE_INT(LOG_INFO); + DEFINE_INT(LOG_WARNING); + DEFINE_INT(LOG_ERR); +} -- GitLab