1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
/*
* Copyright (c) 2015 Balabit
* Copyright (c) 2014 Gergely Nagy <algernon@balabit.hu>
* Copyright (c) 2015 Balazs Scheidler <balazs.scheidler@balabit.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "python-value-pairs.h"
#include "python-helpers.h"
#include "messages.h"
/** Value pairs **/
static void
add_long_to_dict(PyObject *dict, const gchar *name, long num)
{
gchar buf[256];
PyObject *pyobject_to_add = PyLong_FromLong(num);
if (!pyobject_to_add)
{
msg_error("Error while constructing python object",
evt_tag_str("exception", _py_format_exception_text(buf, sizeof(buf))));
_py_finish_exception_handling();
return;
}
PyDict_SetItemString(dict, name, pyobject_to_add);
Py_DECREF(pyobject_to_add);
}
static void
add_string_to_dict(PyObject *dict, const gchar *name, const char *value, gsize value_len)
{
gchar buf[256];
PyObject *pyobject_to_add = PyBytes_FromStringAndSize(value, value_len);
if (!pyobject_to_add)
{
msg_error("Error while constructing python object",
evt_tag_str("exception", _py_format_exception_text(buf, sizeof(buf))));
_py_finish_exception_handling();
return;
}
PyDict_SetItemString(dict, name, pyobject_to_add);
Py_DECREF(pyobject_to_add);
}
static gboolean
python_worker_vp_add_one(const gchar *name,
TypeHint type, const gchar *value, gsize value_len,
gpointer user_data)
{
const LogTemplateOptions *template_options = (const LogTemplateOptions *)((gpointer *)user_data)[0];
PyObject *dict = (PyObject *)((gpointer *)user_data)[1];
gboolean need_drop = FALSE;
gboolean fallback = template_options->on_error & ON_ERROR_FALLBACK_TO_STRING;
switch (type)
{
case TYPE_HINT_INT32:
case TYPE_HINT_INT64:
{
gint64 i;
if (type_cast_to_int64(value, &i, NULL))
{
add_long_to_dict(dict, name, i);
}
else
{
need_drop = type_cast_drop_helper(template_options->on_error,
value, "int");
if (fallback)
{
add_string_to_dict(dict, name, value, value_len);
}
}
break;
}
case TYPE_HINT_STRING:
add_string_to_dict(dict, name, value, value_len);
break;
default:
need_drop = type_cast_drop_helper(template_options->on_error,
value, "<unknown>");
break;
}
return need_drop;
}
/** Main code **/
gboolean
py_value_pairs_apply(ValuePairs *vp, const LogTemplateOptions *template_options, guint32 seq_num, LogMessage *msg,
PyObject **dict)
{
gpointer args[2];
gboolean vp_ok;
*dict = PyDict_New();
args[0] = (gpointer) template_options;
args[1] = *dict;
vp_ok = value_pairs_foreach(vp, python_worker_vp_add_one,
msg, seq_num, LTZ_LOCAL, template_options,
args);
if (!vp_ok)
{
Py_DECREF(*dict);
*dict = NULL;
}
return vp_ok;
}
|