From 773467790bd066ec6b9693732d165d7fde9f18f7 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sun, 22 Feb 2009 23:18:43 +0100 Subject: [PATCH] java plugin: Add support for `log' callbacks. --- bindings/java/org/collectd/api/Collectd.java | 16 +++-- .../org/collectd/api/CollectdLogInterface.java | 27 +++++++++ src/collectd-java.pod | 45 ++++++++++++++ src/java.c | 68 +++++++++++++++++++++- 4 files changed, 150 insertions(+), 6 deletions(-) create mode 100644 bindings/java/org/collectd/api/CollectdLogInterface.java diff --git a/bindings/java/org/collectd/api/Collectd.java b/bindings/java/org/collectd/api/Collectd.java index 3950b220..eb96969a 100644 --- a/bindings/java/org/collectd/api/Collectd.java +++ b/bindings/java/org/collectd/api/Collectd.java @@ -28,11 +28,11 @@ package org.collectd.api; */ public class Collectd { - private static final int LOG_ERR = 3; - private static final int LOG_WARNING = 4; - private static final int LOG_NOTICE = 5; - private static final int LOG_INFO = 6; - private static final int LOG_DEBUG = 7; + public static final int LOG_ERR = 3; + public static final int LOG_WARNING = 4; + public static final int LOG_NOTICE = 5; + public static final int LOG_INFO = 6; + public static final int LOG_DEBUG = 7; /** * Java representation of collectd/src/plugin.h:plugin_register_config @@ -71,6 +71,12 @@ public class Collectd CollectdShutdownInterface object); /** + * Java representation of collectd/src/plugin.h:plugin_register_log + */ + native public static int registerLog (String name, + CollectdLogInterface object); + + /** * Java representation of collectd/src/plugin.h:plugin_dispatch_values */ native public static int dispatchValues (ValueList vl); diff --git a/bindings/java/org/collectd/api/CollectdLogInterface.java b/bindings/java/org/collectd/api/CollectdLogInterface.java new file mode 100644 index 00000000..ab874c5d --- /dev/null +++ b/bindings/java/org/collectd/api/CollectdLogInterface.java @@ -0,0 +1,27 @@ +/* + * collectd/java - org/collectd/api/CollectdLogInterface.java + * Copyright (C) 2009 Florian octo Forster + * + * 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; only version 2 of the License is applicable. + * + * 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 St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Authors: + * Florian octo Forster + */ + +package org.collectd.api; + +public interface CollectdLogInterface +{ + public void log (int severity, String message); +} diff --git a/src/collectd-java.pod b/src/collectd-java.pod index c4f4ef7f..14e542d8 100644 --- a/src/collectd-java.pod +++ b/src/collectd-java.pod @@ -181,6 +181,42 @@ the destructor to clean up behind the object but use this function instead. To signal success, this method has to return zero. Anything else will be considered an error condition and cause an appropriate message to be logged. +=head2 log callback + +Interface: B + +Signature: I B (I severity, I message) + +This callback can be used to receive log messages from the daemon. + +The argument I is one of: + +=over 4 + +=item * + +org.collectd.api.Collectd.LOG_ERR + +=item * + +org.collectd.api.Collectd.LOG_WARNING + +=item * + +org.collectd.api.Collectd.LOG_NOTICE + +=item * + +org.collectd.api.Collectd.LOG_INFO + +=item * + +org.collectd.api.Collectd.LOG_DEBUG + +=back + +The function does not return any value. + =head2 Example This short example demonstrates how to register a read callback with the @@ -271,6 +307,15 @@ Registers the B function of I with the daemon. Returns zero upon success and non-zero when an error occurred. +=head2 registerLog + +Signature: I B (I name, +I object); + +Registers the B function of I with the daemon. + +Returns zero upon success and non-zero when an error occurred. + =head2 dispatchValues Signature: I B (I) diff --git a/src/java.c b/src/java.c index 39311c8b..fc332e50 100644 --- a/src/java.c +++ b/src/java.c @@ -66,6 +66,7 @@ typedef struct java_plugin_config_s java_plugin_config_t; #define CB_TYPE_WRITE 4 #define CB_TYPE_FLUSH 5 #define CB_TYPE_SHUTDOWN 6 +#define CB_TYPE_LOG 7 struct cjni_callback_info_s /* {{{ */ { char *name; @@ -115,6 +116,7 @@ static int cjni_read (user_data_t *user_data); static int cjni_write (const data_set_t *ds, const value_list_t *vl, user_data_t *ud); static int cjni_flush (int timeout, const char *identifier, user_data_t *ud); +static void cjni_log (int severity, const char *message, user_data_t *ud); /* * C to Java conversion functions @@ -1279,6 +1281,29 @@ static jint JNICALL cjni_api_register_shutdown (JNIEnv *jvm_env, /* {{{ */ CB_TYPE_SHUTDOWN)); } /* }}} jint cjni_api_register_shutdown */ +static jint JNICALL cjni_api_register_log (JNIEnv *jvm_env, /* {{{ */ + jobject this, jobject o_name, jobject o_log) +{ + user_data_t ud; + cjni_callback_info_t *cbi; + + cbi = cjni_callback_info_create (jvm_env, o_name, o_log, CB_TYPE_LOG); + if (cbi == NULL) + return (-1); + + DEBUG ("java plugin: Registering new log callback: %s", cbi->name); + + memset (&ud, 0, sizeof (ud)); + ud.data = (void *) cbi; + ud.free_func = cjni_callback_info_destroy; + + plugin_register_log (cbi->name, cjni_log, &ud); + + (*jvm_env)->DeleteLocalRef (jvm_env, o_log); + + return (0); +} /* }}} jint cjni_api_register_log */ + static void JNICALL cjni_api_log (JNIEnv *jvm_env, /* {{{ */ jobject this, jint severity, jobject o_message) { @@ -1337,6 +1362,10 @@ static JNINativeMethod jni_api_functions[] = /* {{{ */ "(Ljava/lang/String;Lorg/collectd/api/CollectdShutdownInterface;)I", cjni_api_register_shutdown }, + { "registerLog", + "(Ljava/lang/String;Lorg/collectd/api/CollectdLogInterface;)I", + cjni_api_register_log }, + { "log", "(ILjava/lang/String;)V", cjni_api_log }, @@ -1390,6 +1419,11 @@ static cjni_callback_info_t *cjni_callback_info_create (JNIEnv *jvm_env, /* {{{ method_signature = "()I"; break; + case CB_TYPE_LOG: + method_name = "log"; + method_signature = "(ILjava/lang/String;)V"; + break; + default: ERROR ("java plugin: cjni_callback_info_create: Unknown type: %#x", type); @@ -1941,7 +1975,7 @@ static int cjni_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */ return (status); } /* }}} int cjni_write */ -/* Call the CB_TYPE_WRITE callback pointed to by the `user_data_t' pointer. */ +/* Call the CB_TYPE_FLUSH callback pointed to by the `user_data_t' pointer. */ static int cjni_flush (int timeout, const char *identifier, /* {{{ */ user_data_t *ud) { @@ -1994,6 +2028,38 @@ static int cjni_flush (int timeout, const char *identifier, /* {{{ */ return (status); } /* }}} int cjni_flush */ +/* Call the CB_TYPE_LOG callback pointed to by the `user_data_t' pointer. */ +static void cjni_log (int severity, const char *message, /* {{{ */ + user_data_t *ud) +{ + JNIEnv *jvm_env; + cjni_callback_info_t *cbi; + jobject o_message; + + if (jvm == NULL) + return; + + if ((ud == NULL) || (ud->data == NULL)) + return; + + jvm_env = cjni_thread_attach (); + if (jvm_env == NULL) + return; + + cbi = (cjni_callback_info_t *) ud->data; + + o_message = (*jvm_env)->NewStringUTF (jvm_env, message); + if (o_message == NULL) + return; + + (*jvm_env)->CallVoidMethod (jvm_env, + cbi->object, cbi->method, (jint) severity, o_message); + + (*jvm_env)->DeleteLocalRef (jvm_env, o_message); + + cjni_thread_detach (); +} /* }}} int cjni_log */ + /* Iterate over `java_classes_list' and create one object of each class. This * will trigger the object's constructors, to the objects can register callback * methods. */ -- 2.11.0