Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.Locale;
import android.util.Log;

public class Main {
    private static final String LOG_PREFIX = "[StackOverflow]";

    private static void log(int priority, String tag, String message, Throwable exception) {
        if (message == null || message.length() == 0) {
            if (exception != null) {
                message = Log.getStackTraceString(exception);
            } else {
                // Swallow message if it's null and there's no throwable.
                return;
            }
        } else if (exception != null) {
            message += "\n" + Log.getStackTraceString(exception);
        }

        if (message.length() < 4000) {
            Log.println(priority, getCaller(tag), message);
        } else {
            // It's rare that the message will be this large, so we're ok
            // with the perf hit of splitting
            // and calling Log.println N times. It's possible but unlikely
            // that a single line will be
            // longer than 4000 characters: we're explicitly ignoring this
            // case here.
            String[] lines = message.split("\n");
            for (String line : lines) {
                Log.println(priority, getCaller(tag), line);
            }
        }
    }

    private static String getCaller(String tag) {
        String caller = "<unknown>";
        StackTraceElement[] trace = new Throwable().fillInStackTrace().getStackTrace();
        if (trace.length < 6) {
            return caller;
        }
        // Walk up the stack looking for the first caller outside of LogUtils.
        // It will be at least 5 frames up, so start there.
        for (int i = 5; i < trace.length; i++) {
            // Class<?> clazz = trace[i].getClass();
            String clazzName = trace[i].getClassName();
            if (!clazzName.contains("Log")) {
                String callingClass = clazzName;
                callingClass = callingClass.substring(callingClass.lastIndexOf('.') + 1);
                callingClass = callingClass.substring(callingClass.lastIndexOf('$') + 1);
                caller = callingClass + "." + trace[i].getMethodName();
                break;
            }
        }
        return String.format(Locale.US, "[%d/%s] %s %s: %s", Thread.currentThread().getId(),
                Thread.currentThread().getName(), LOG_PREFIX, tag, caller);
    }
}