Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.util.Log;
import java.util.Collection;
import java.util.Iterator;

public class Main {
    public static void log(Object message) {
        StackTraceElement element = new Throwable().getStackTrace()[1];
        print(element, message, null);
    }

    public static void log(Object message, Throwable error) {
        StackTraceElement element = new Throwable().getStackTrace()[1];
        print(element, message, error);
    }

    private static void print(StackTraceElement element, Object message, Throwable error) {
        String className = element.getClassName();
        className = className.substring(className.lastIndexOf(".") + 1);
        String tag = className + '.' + element.getMethodName() + '(' + element.getFileName() + ':'
                + element.getLineNumber() + ')';
        String text = toString(message);

        if (error != null) {
            Log.e("[KakaCache]", tag + "\n\t" + text, error);
        } else {
            Log.e("[KakaCache]", tag + "\n\t" + text);
        }
    }

    private static String toString(Object message) {
        if (message == null) {
            return "[null]";
        }
        if (message instanceof Throwable) {
            return Log.getStackTraceString((Throwable) message);
        }
        if (message instanceof Collection) {
            return toString((Collection) message);
        }
        return String.valueOf(message);
    }

    private static String toString(Collection message) {
        Iterator it = message.iterator();
        if (!it.hasNext())
            return "[]";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (;;) {
            Object e = it.next();
            sb.append(e);
            if (!it.hasNext())
                return sb.append(']').toString();
            sb.append(',').append('\n').append(' ');
        }
    }
}