[libbluray-devel] Redirect BD-J logging to libbluray logging system

hpi1 git at videolan.org
Wed Mar 13 10:09:57 CET 2013


libbluray | branch: master | hpi1 <hpi1 at anonymous.org> | Wed Mar 13 10:37:24 2013 +0200| [dd3a0af7b877a726ab3e380d68bc8f62756f5e9b] | committer: hpi1

Redirect BD-J logging to libbluray logging system

> http://git.videolan.org/gitweb.cgi/libbluray.git/?a=commit;h=dd3a0af7b877a726ab3e380d68bc8f62756f5e9b
---

 src/Makefile.am                                 |    2 +
 src/libbluray/bdj/java/org/videolan/Logger.java |   74 ++++++++++++++----
 src/libbluray/bdj/native/org_videolan_Logger.c  |   92 +++++++++++++++++++++++
 src/libbluray/bdj/native/org_videolan_Logger.h  |   27 +++++++
 src/libbluray/bdj/native/register_native.c      |    6 ++
 5 files changed, 187 insertions(+), 14 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index f1ee9f5..3cc4ef5 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -126,6 +126,8 @@ libbluray_la_SOURCES += \
 	libbluray/bdj/native/java_awt_BDFontMetrics.c \
 	libbluray/bdj/native/org_videolan_Libbluray.h \
 	libbluray/bdj/native/org_videolan_Libbluray.c \
+	libbluray/bdj/native/org_videolan_Logger.h \
+	libbluray/bdj/native/org_videolan_Logger.c \
 	libbluray/bdj/native/register_native.h \
 	libbluray/bdj/native/register_native.c
 
diff --git a/src/libbluray/bdj/java/org/videolan/Logger.java b/src/libbluray/bdj/java/org/videolan/Logger.java
index a457ec4..12b8437 100644
--- a/src/libbluray/bdj/java/org/videolan/Logger.java
+++ b/src/libbluray/bdj/java/org/videolan/Logger.java
@@ -36,6 +36,33 @@ public class Logger {
         }
     }
 
+    private static class Location {
+        public int line = 0;
+        public String file = "?";
+        public String cls = "?";
+        public String func = "?";
+        Location() {
+        }
+        Location(StackTraceElement e) {
+            line = e.getLineNumber();
+            file = e.getFileName();
+            cls = e.getClassName();
+            func = e.getMethodName();
+            if (file == null) {
+                file = "<unknown.java>";
+            }
+        }
+    }
+    private static Location getLocation(int depth) {
+        StackTraceElement e[] = new Exception("Stack trace").getStackTrace();
+        if (e != null && e.length > 1) {
+            for (int i = depth; i < e.length; i++)
+                if (!e[i].getClassName().startsWith("org.videolan.Logger"))
+                    return new Location(e[i]);
+        }
+        return new Location();
+    }
+
     public static Logger getLogger(String name) {
         return new Logger(name);
     }
@@ -44,45 +71,57 @@ public class Logger {
         this.name = name;
     }
 
-    private static void log(String msg) {
-        System.out.println(msg);
+    private static native void logN(boolean error, String file, int line, String msg);
+
+    private static void log(boolean error, String cls, String msg) {
+        logN(error, cls, 0, msg);
+    }
+
+    private static void log(boolean error, String msg) {
+        Location l = getLocation(3);
+        logN(error, l.file + ":" + l.cls + "." + l.func, l.line, msg);
     }
 
     public void trace(String msg) {
         if (use_trace) {
-            log("BD-J: " + name + ": " + msg);
+            log(false, name, msg);
         }
     }
 
     public void info(String msg) {
-        log("BD-J: " + name + ": " + msg);
+        log(false, name, "INFO: " + msg);
     }
 
     public void warning(String msg) {
-        log("BD-J WARNING: " + name + ": " + msg);
+        log(false, name, "WARNING: " + msg);
     }
 
     public void error(String msg) {
-        log("BD-J ERROR: " + name + ": " + msg);
+        log(true, name, "ERROR: " + msg);
     }
 
     public void unimplemented() {
         unimplemented(null);
     }
 
-    public static void dumpStack() {
+    public static String dumpStack() {
+        String dump = "";
         StackTraceElement e[] = new Exception("Stack trace").getStackTrace();
-        for (int i = 2; i < e.length; i++)
-            log("    " + e[i].toString());
+        if (e != null && e.length > 1) {
+            dump = "\t" + e[2].toString();
+            for (int i = 3; i < e.length; i++)
+                dump += "\n\t" + e[i].toString();
+        }
+        return dump;
     }
 
     public void unimplemented(String func) {
         String location = name;
         if (func != null) {
             location = location + "." + func + "()";
-            log("BD-J: Not implemented: " + location);
         }
-        dumpStack();
+
+        log(true, "UNIMPLEMENTED: " + location + "\n" + dumpStack());
 
         if (use_throw) {
             throw new Error("Not implemented: " + location);
@@ -90,10 +129,17 @@ public class Logger {
     }
 
     public static void unimplemented(String cls, String func) {
-        String location = cls + "." + func + "()";
-        log("BD-J: Not implemented: " + location);
+        if (cls == null)
+            cls = "<?>";
+
+        if (func == null)
+            func = "";
+        else
+            func = "." + func + "()";
+
+        String location = cls + func;
 
-        dumpStack();
+        log(true, "UNIMPLEMENTED: " + location + "\n" + dumpStack());
 
         if (use_throw) {
             throw new Error("Not implemented: " + location);
diff --git a/src/libbluray/bdj/native/org_videolan_Logger.c b/src/libbluray/bdj/native/org_videolan_Logger.c
new file mode 100644
index 0000000..06a84e1
--- /dev/null
+++ b/src/libbluray/bdj/native/org_videolan_Logger.c
@@ -0,0 +1,92 @@
+/*
+ * This file is part of libbluray
+ * Copyright (C) 2012  libbluray
+ *
+ * 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, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#include <jni.h>
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "util/logging.h"
+
+#include <stdint.h>
+
+#include "org_videolan_Logger.h"
+
+/* Disable some warnings */
+#if defined __GNUC__
+#pragma GCC diagnostic ignored "-Wunused-parameter"
+#endif
+
+#ifdef __cplusplus
+#define CPP_EXTERN extern
+#else
+#define CPP_EXTERN
+#endif
+
+JNIEXPORT void JNICALL
+Java_org_videolan_Logger_logN(JNIEnv *env, jclass cls, jboolean error, jstring jfile, jint line, jstring string)
+{
+    const char *msg, *file;
+    jsize length;
+    uint32_t mask = DBG_BDJ;
+
+    length = (*env)->GetStringLength(env, string);
+    if (length <= 0) {
+        return;
+    }
+
+    msg = (*env)->GetStringUTFChars(env, string, NULL);
+    if (!msg) {
+        return;
+    }
+
+    file = (*env)->GetStringUTFChars(env, jfile, NULL);
+
+    if (error) {
+        mask |= DBG_CRIT;
+    }
+
+    bd_debug(file ? file : "JVM", line, mask, "%s\n", msg);
+
+    if (file) {
+        (*env)->ReleaseStringUTFChars(env, jfile, file);
+    }
+    (*env)->ReleaseStringUTFChars(env, string, msg);
+}
+
+#define CC (char*)             /* cast a literal from (const char*) */
+#define VC (void*)(uintptr_t)  /* cast function pointer to void* */
+#if defined __GNUC__
+#pragma GCC diagnostic ignored "-Wcast-qual"
+#endif
+
+BD_PRIVATE CPP_EXTERN const JNINativeMethod
+Java_org_videolan_Logger_methods[] =
+{ /* AUTOMATICALLY GENERATED */
+    {
+        CC("logN"),
+        CC("(ZLjava/lang/String;ILjava/lang/String;)V"),
+        VC(Java_org_videolan_Logger_logN),
+    },
+};
+
+BD_PRIVATE CPP_EXTERN const int
+Java_org_videolan_Logger_methods_count =
+     sizeof(Java_org_videolan_Logger_methods)/sizeof(Java_org_videolan_Logger_methods[0]);
diff --git a/src/libbluray/bdj/native/org_videolan_Logger.h b/src/libbluray/bdj/native/org_videolan_Logger.h
new file mode 100644
index 0000000..5c24efb
--- /dev/null
+++ b/src/libbluray/bdj/native/org_videolan_Logger.h
@@ -0,0 +1,27 @@
+/* DO NOT EDIT THIS FILE - it is machine generated */
+#include <jni.h>
+/* Header for class java_awt_BDGraphics */
+
+#ifndef WIN32
+#undef JNIEXPORT
+#define JNIEXPORT static
+#endif
+
+#ifndef _Included_java_awt_BDGraphics
+#define _Included_java_awt_BDGraphics
+#ifdef __cplusplus
+extern "C" {
+#endif
+/*
+ * Class:     org_videolan_Logger
+ * Method:    logN
+ * Signature: (ZLjava/lang/String;ILjava/lang/String;)V
+
+ */
+JNIEXPORT void JNICALL Java_org_videolan_Logger_logN
+  (JNIEnv *, jclass, jboolean, jstring, jint, jstring);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/src/libbluray/bdj/native/register_native.c b/src/libbluray/bdj/native/register_native.c
index 8a188c4..5803d56 100644
--- a/src/libbluray/bdj/native/register_native.c
+++ b/src/libbluray/bdj/native/register_native.c
@@ -23,14 +23,20 @@
 
 int bdj_register_native_methods(JNIEnv *env)
 {
+    extern const JNINativeMethod Java_org_videolan_Logger_methods[];
     extern const JNINativeMethod Java_org_videolan_Libbluray_methods[];
     extern const JNINativeMethod Java_java_awt_BDGraphics_methods[];
     extern const JNINativeMethod Java_java_awt_BDFontMetrics_methods[];
+    extern const int Java_org_videolan_Logger_methods_count;
     extern const int Java_org_videolan_Libbluray_methods_count;
     extern const int Java_java_awt_BDGraphics_methods_count;
     extern const int Java_java_awt_BDFontMetrics_methods_count;
 
     return
+      bdj_register_methods(env, "org/videolan/Logger",
+                           Java_org_videolan_Logger_methods,
+                           Java_org_videolan_Logger_methods_count)
+      *
       bdj_register_methods(env, "org/videolan/Libbluray",
                            Java_org_videolan_Libbluray_methods,
                            Java_org_videolan_Libbluray_methods_count)



More information about the libbluray-devel mailing list