1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.io;
18
19public final class EventLogger {
20
21    /**
22     * Hook for customizing how events are reported.
23     */
24    private static volatile Reporter REPORTER = new DefaultReporter();
25
26    /**
27     * Used to replace default Reporter for logging events. Must be non-null.
28     */
29    public static void setReporter(Reporter reporter) {
30        if (reporter == null) {
31            throw new NullPointerException("reporter == null");
32        }
33        REPORTER = reporter;
34    }
35
36    /**
37     * Returns non-null Reporter.
38     */
39    public static Reporter getReporter() {
40        return REPORTER;
41    }
42
43    /**
44     * Interface to allow customization of reporting behavior.
45     */
46    public static interface Reporter {
47        public void report (int code, Object... list);
48    }
49
50    /**
51     * Default Reporter which reports events to the log.
52     */
53    private static final class DefaultReporter implements Reporter {
54        @Override
55        public void report (int code, Object... list) {
56            StringBuilder sb = new StringBuilder();
57            sb.append(code);
58            for (Object o : list) {
59                sb.append(",");
60                sb.append(o.toString());
61            }
62            System.out.println(sb);
63        }
64    }
65
66    public static void writeEvent(int code, Object... list) {
67        getReporter().report(code, list);
68    }
69}
70