001package org.javasimon.utils;
002
003import org.javasimon.Counter;
004import org.javasimon.CounterSample;
005import org.javasimon.Simon;
006import org.javasimon.Split;
007import org.javasimon.Stopwatch;
008import org.javasimon.StopwatchSample;
009import org.javasimon.callback.CallbackSkeleton;
010
011/**
012 * SystemDebugCallback just prints operations on the standard output, warning is sent to error output.
013 *
014 * @author <a href="mailto:virgo47@gmail.com">Richard "Virgo" Richter</a>
015 */
016public final class SystemDebugCallback extends CallbackSkeleton {
017        public static final String DEBUG_PREFIX = "SIMON DEBUG: ";
018
019        @Override
020        public void onStopwatchStart(Split split) {
021                out("Start split: " + split);
022        }
023
024        @Override
025        public void onStopwatchStop(Split split, StopwatchSample sample) {
026                out("Stopwatch stop (" + split + "): " + sample.simonToString());
027        }
028
029        @Override
030        public void onStopwatchAdd(Stopwatch stopwatch, Split split, StopwatchSample sample) {
031                out("Stopwatch add (" + split + "): " + sample.simonToString());
032        }
033
034        @Override
035        public void onCounterDecrease(Counter counter, long dec, CounterSample sample) {
036                out("Counter decrease: " + sample.simonToString());
037        }
038
039        @Override
040        public void onCounterIncrease(Counter counter, long inc, CounterSample sample) {
041                out("Counter increase: " + sample.simonToString());
042        }
043
044        @Override
045        public void onCounterSet(Counter counter, long val, CounterSample sample) {
046                out("Counter set: " + sample.simonToString());
047        }
048
049        @Override
050        public void onSimonCreated(Simon simon) {
051                out("Simon created: " + simon);
052        }
053
054        @Override
055        public void onSimonDestroyed(Simon simon) {
056                out("Simon destroyed: " + simon);
057        }
058
059        @Override
060        public void onManagerClear() {
061                out("Manager clear");
062        }
063
064        @Override
065        public void onManagerMessage(String message) {
066                out("Simon message: " + message);
067        }
068
069        private void out(String message) {
070                System.out.println(DEBUG_PREFIX + message);
071        }
072
073        /**
074         * Warning and stack trace are print out to the error output. Either cause or warning
075         * (or both) should be provided otherwise the method does nothing.
076         * <p/>
077         * {@inheritDoc}
078         */
079        @Override
080        public void onManagerWarning(String warning, Exception cause) {
081                if (warning != null) {
082                        System.err.println(DEBUG_PREFIX + "Simon warning: " + warning);
083                }
084                if (cause != null) {
085                        System.err.print(DEBUG_PREFIX);
086                        cause.printStackTrace();
087                }
088        }
089}