ProgressLogger.java 2.45 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.crx.sling.server.impl.jmx;

import com.day.crx.sling.server.impl.jmx.Profiler;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ProgressLogger
extends Thread {
    static Logger log = LoggerFactory.getLogger(ProgressLogger.class);
    private static Thread backgroundThread;

    public static synchronized void startBackgroundThread() {
        ProgressLogger.log("Start progress logger");
        if (backgroundThread != null && backgroundThread.isAlive()) {
            return;
        }
        backgroundThread = new Thread(){

            @Override
            public void run() {
                int i = 0;
                do {
                    try {
                        Profiler prof = new Profiler();
                        prof.interval = 10;
                        prof.sumClasses = true;
                        prof.startCollecting();
                        this.setName("ProgressLogger " + i + " min");
                        for (int s = 0; s < 600; ++s) {
                            try {
                                Thread.sleep(1000);
                                continue;
                            }
                            catch (InterruptedException e) {
                                ProgressLogger.log("Interrupted");
                            }
                        }
                        ProgressLogger.log(prof.getTop(3));
                        prof = null;
                        ProgressLogger.printStackTraces();
                    }
                    catch (Throwable t) {
                        ProgressLogger.log.error("Error in logger", t);
                    }
                    ++i;
                } while (true);
            }
        };
        backgroundThread.setDaemon(true);
        backgroundThread.start();
    }

    static void printStackTraces() {
        ProgressLogger.log("Stack Traces:");
        Map<Thread, StackTraceElement[]> m = Thread.getAllStackTraces();
        for (Map.Entry<Thread, StackTraceElement[]> e : m.entrySet()) {
            ProgressLogger.log(e.getKey().toString());
            for (StackTraceElement s : e.getValue()) {
                ProgressLogger.log("  " + s);
            }
        }
    }

    static void log(String message) {
        log.info(message);
    }

}