/*
 * This file is part of libbluray
 * Copyright (C) 2012  Petri Hintukainen <phintuka@users.sourceforge.net>
 *
 * 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/>.
 */

package org.videolan;

import java.io.PrintStream;

public class Logger {
    static {
        String prop;

        use_throw = false;
        use_trace = false;

        // capture stdout and stderr from on-disc applets
        // (those produce useful debug information sometimes)
        System.setOut(createCapture(System.out, false));
        System.setErr(createCapture(System.err, true));

        prop = System.getProperty("debug.unimplemented.throw");
        if (prop != null && prop.equalsIgnoreCase("YES")) {
            use_throw = true;
        }
        prop = System.getProperty("debug.trace");
        if (prop == null || !prop.equalsIgnoreCase("NO")) {
            use_trace = true;
        }
    }

    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();
    }

    private static PrintStream createCapture(final PrintStream printStream, final boolean error) {
        return new PrintStream(printStream) {
            public void print(final String string) {
                Logger.log(error, string);
            }
            public void println(final String string) {
                Logger.log(error, string);
            }
        };
    }

    public static Logger getLogger(String name) {
        return new Logger(name);
    }

    private Logger(String name) {
        this.name = name;
    }

    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(false, name, msg);
        }
    }

    public void info(String msg) {
        log(false, name, "INFO: " + msg);
    }

    public void warning(String msg) {
        log(false, name, "WARNING: " + msg);
    }

    public void error(String msg) {
        log(true, name, "ERROR: " + msg);
    }

    public void unimplemented() {
        unimplemented(null);
    }

    public static String dumpStack() {
        String dump = "";
        StackTraceElement e[] = new Exception("Stack trace").getStackTrace();
        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(true, "UNIMPLEMENTED: " + location + "\n" + dumpStack());

        if (use_throw) {
            throw new Error("Not implemented: " + location);
        }
    }

    public static void unimplemented(String cls, String func) {
        if (cls == null)
            cls = "<?>";

        if (func == null)
            func = "";
        else
            func = "." + func + "()";

        String location = cls + func;

        log(true, "UNIMPLEMENTED: " + location + "\n" + dumpStack());

        if (use_throw) {
            throw new Error("Not implemented: " + location);
        }
    }

    private String name;
    private static boolean use_trace;
    private static boolean use_throw;
}

@@ -3780,34 +3780,8 @@
 ) %7B%0A
-        String dump = %22%22;%0A
     
@@ -3911,22 +3911,103 @@
     
-dump = %22%5Ct%22 + 
+StringBuffer dump = new StringBuffer();%0A            dump.append(%22%5Ct%22);%0A            dump.append(
 e%5B2%5D
@@ -4009,32 +4009,33 @@
 (e%5B2%5D.toString()
+)
 ;%0A            fo
@@ -4058,32 +4058,34 @@
 %3C e.length; i++)
+ %7B
 %0A               
@@ -4093,21 +4093,53 @@
 dump
- += %22%5Cn%5Ct%22 + 
+.append(%22%5Cn%5Ct%22);%0A                dump.append(
 e%5Bi%5D
@@ -4149,16 +4149,17 @@
 String()
+)
 ;%0A      
@@ -4152,32 +4152,36 @@
 ing());%0A        
+  
+  
 %7D%0A        return
@@ -4170,24 +4170,28 @@
   %7D%0A        
+ 
+   
 return dump;
@@ -4189,16 +4189,56 @@
 urn dump
+.toString();%0A        %7D%0A        return %22%22
 ;%0A    %7D%0A

Use StringBuffer