diff options
author | Andris Zeila <andris.zeila@zabbix.com> | 2019-10-01 10:34:19 +0300 |
---|---|---|
committer | Andris Zeila <andris.zeila@zabbix.com> | 2019-10-01 10:34:57 +0300 |
commit | ece5d29e4bc4825132b86d2d35f481845a6269d2 (patch) | |
tree | 241b470c87f6efe941bb090735c7a37537625e87 | |
parent | a2653044be13b230bb3b0b73d50b3870965c1b25 (diff) |
.......PS. [ZBXNEXT-5386] changed script logging from zabbix_log function to Zabbix.Log
-rw-r--r-- | src/libs/zbxembed/Makefile.am | 1 | ||||
-rw-r--r-- | src/libs/zbxembed/embed.c | 13 | ||||
-rw-r--r-- | src/libs/zbxembed/zabbix.c | 110 | ||||
-rw-r--r-- | src/libs/zbxembed/zabbix.h | 25 |
4 files changed, 139 insertions, 10 deletions
diff --git a/src/libs/zbxembed/Makefile.am b/src/libs/zbxembed/Makefile.am index 1d6c6bd0e6a..0d26e31bc8d 100644 --- a/src/libs/zbxembed/Makefile.am +++ b/src/libs/zbxembed/Makefile.am @@ -5,6 +5,7 @@ noinst_LIBRARIES = libzbxembed.a libzbxembed_a_SOURCES = \ embed.c embed.h \ httprequest.c httprequest.h \ + zabbix.c zabbix.h \ duktape.c duktape.h \ duk_config.h diff --git a/src/libs/zbxembed/embed.c b/src/libs/zbxembed/embed.c index d3e87c5c5cf..f9a5491b741 100644 --- a/src/libs/zbxembed/embed.c +++ b/src/libs/zbxembed/embed.c @@ -23,6 +23,7 @@ #include "zbxembed.h" #include "embed.h" #include "httprequest.h" +#include "zabbix.h" #include "duktape.h" @@ -120,13 +121,6 @@ static void es_free(void *udata, void *ptr) } } -static duk_ret_t es_log(duk_context *ctx) -{ - zabbix_log(duk_to_int(ctx, 0), "%s", duk_to_string(ctx, 1)); - - return 0; -} - /****************************************************************************** * * * Function: zbx_es_check_timeout * @@ -207,9 +201,8 @@ int zbx_es_init_env(zbx_es_t *es, char **error) goto out; } - /* initialize logger */ - duk_push_c_function(es->env->ctx, es_log, 2); - duk_put_global_string(es->env->ctx, "zabbix_log"); + /* initialize Zabbix object */ + zbx_es_init_zabbix(es, error); /* remove Duktape object */ duk_push_global_object(es->env->ctx); diff --git a/src/libs/zbxembed/zabbix.c b/src/libs/zbxembed/zabbix.c new file mode 100644 index 00000000000..21791f12b45 --- /dev/null +++ b/src/libs/zbxembed/zabbix.c @@ -0,0 +1,110 @@ +/* +** Zabbix +** Copyright (C) 2001-2019 Zabbix SIA +** +** 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 2 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 envied 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. +**/ + +#include "common.h" +#include "log.h" +#include "zbxjson.h" +#include "zbxembed.h" +#include "embed.h" +#include "duktape.h" +#include "zabbix.h" + +/****************************************************************************** + * * + * Function: es_zabbix_dtor * + * * + * Purpose: Curlzabbix destructor * + * * + ******************************************************************************/ +duk_ret_t es_zabbix_dtor(duk_context *ctx) +{ + ZBX_UNUSED(ctx); + return 0; +} + +/****************************************************************************** + * * + * Function: es_zabbix_ctor * + * * + * Purpose: Curlzabbix constructor * + * * + ******************************************************************************/ +static duk_ret_t es_zabbix_ctor(duk_context *ctx) +{ + if (!duk_is_constructor_call(ctx)) + return DUK_RET_TYPE_ERROR; + + duk_push_this(ctx); + + duk_push_c_function(ctx, es_zabbix_dtor, 1); + duk_set_finalizer(ctx, -2); + return 0; +} + +/****************************************************************************** + * * + * Function: es_zabbix_status * + * * + * Purpose: Curlzabbix.Status method * + * * + ******************************************************************************/ +duk_ret_t es_zabbix_log(duk_context *ctx) +{ + zabbix_log(duk_to_int(ctx, 0), "%s", duk_to_string(ctx, 1)); + return 0; +} + +static const duk_function_list_entry zabbix_methods[] = { + {"Log", es_zabbix_log, 2}, + {NULL, NULL, 0} +}; + +static int es_zabbix_create_object(duk_context *ctx) +{ + duk_push_c_function(ctx, es_zabbix_ctor, 0); + duk_push_object(ctx); + + duk_put_function_list(ctx, -1, zabbix_methods); + + if (1 != duk_put_prop_string(ctx, -2, "prototype")) + return FAIL; + + duk_new(ctx, 0); + duk_put_global_string(ctx, "Zabbix"); + + return SUCCEED; +} + +int zbx_es_init_zabbix(zbx_es_t *es, char **error) +{ + if (0 != setjmp(es->env->loc)) + { + *error = zbx_strdup(*error, es->env->error); + return FAIL; + } + + if (FAIL == es_zabbix_create_object(es->env->ctx)) + { + *error = zbx_strdup(*error, duk_safe_to_string(es->env->ctx, -1)); + duk_pop(es->env->ctx); + return FAIL; + } + + return SUCCEED; +} diff --git a/src/libs/zbxembed/zabbix.h b/src/libs/zbxembed/zabbix.h new file mode 100644 index 00000000000..ff2bd8d1961 --- /dev/null +++ b/src/libs/zbxembed/zabbix.h @@ -0,0 +1,25 @@ +/* +** Zabbix +** Copyright (C) 2001-2019 Zabbix SIA +** +** 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 2 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. +**/ + +#ifndef ZABBIX_ZABBIX_H +#define ZABBIX_ZABBIX_H + +int zbx_es_init_zabbix(zbx_es_t *es, char **error); + +#endif |