1/*
2 * Copyright (C) 2015 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 com.android.camera.debug;
18
19import com.android.camera.debug.Log.Tag;
20import com.google.common.annotations.VisibleForTesting;
21
22import javax.annotation.ParametersAreNonnullByDefault;
23
24/**
25 * Set of commonly used loggers.
26 */
27@ParametersAreNonnullByDefault
28public class Loggers {
29    /**
30     * This creates a factory that will eat all log input.
31     */
32    public static Logger.Factory noOpFactory() {
33        return NoOpLoggerFactory.instance();
34    }
35
36    /**
37     * This creates a factory that will use the standard android static log
38     * methods.
39     */
40    public static Logger.Factory tagFactory() {
41        return TagLoggerFactory.instance();
42    }
43
44    /**
45     * Creates a logger factory which always returns the given logger.
46     */
47    public static Logger.Factory factoryFor(final Logger logger) {
48        return new Logger.Factory() {
49            @Override
50            public Logger create(Tag tag) {
51                return logger;
52            }
53        };
54    }
55
56    /**
57     * Creates loggers that eat all input and does nothing.
58     */
59    private static class NoOpLoggerFactory implements Logger.Factory {
60        private static class Singleton {
61            private static final NoOpLoggerFactory INSTANCE = new NoOpLoggerFactory();
62        }
63
64        public static NoOpLoggerFactory instance() {
65            return Singleton.INSTANCE;
66        }
67
68        private final NoOpLogger mNoOpLogger;
69
70        public NoOpLoggerFactory() {
71            mNoOpLogger = new NoOpLogger();
72        }
73
74        @Override
75        public Logger create(Tag tag) {
76            return mNoOpLogger;
77        }
78    }
79
80    /**
81     * Creates loggers that use tag objects to write to standard android log
82     * output.
83     */
84    private static class TagLoggerFactory implements Logger.Factory {
85        private static class Singleton {
86            private static final TagLoggerFactory INSTANCE = new TagLoggerFactory();
87        }
88
89        public static TagLoggerFactory instance() {
90            return Singleton.INSTANCE;
91        }
92
93        @Override
94        public Logger create(Tag tag) {
95            return new TagLogger(tag);
96        }
97    }
98
99    /**
100     * NoOp logger eats all input messages and does not display them.
101     */
102    private static class NoOpLogger implements Logger {
103        @Override
104        public void d(String msg) {
105        }
106
107        @Override
108        public void d(String msg, Throwable tr) {
109        }
110
111        @Override
112        public void e(String msg) {
113        }
114
115        @Override
116        public void e(String msg, Throwable tr) {
117        }
118
119        @Override
120        public void i(String msg) {
121        }
122
123        @Override
124        public void i(String msg, Throwable tr) {
125        }
126
127        @Override
128        public void v(String msg) {
129        }
130
131        @Override
132        public void v(String msg, Throwable tr) {
133        }
134
135        @Override
136        public void w(String msg) {
137        }
138
139        @Override
140        public void w(String msg, Throwable tr) {
141        }
142    }
143
144    /**
145     * TagLogger logger writes to the standard static log output with the given
146     * tag object.
147     */
148    private static class TagLogger implements Logger {
149        private final Log.Tag mTag;
150
151        public TagLogger(Log.Tag tag) {
152            mTag = tag;
153        }
154
155        @Override
156        public void d(String msg) {
157            Log.d(mTag, msg);
158        }
159
160        @Override
161        public void d(String msg, Throwable tr) {
162            Log.d(mTag, msg, tr);
163        }
164
165        @Override
166        public void e(String msg) {
167            Log.e(mTag, msg);
168        }
169
170        @Override
171        public void e(String msg, Throwable tr) {
172            Log.e(mTag, msg, tr);
173        }
174
175        @Override
176        public void i(String msg) {
177            Log.i(mTag, msg);
178        }
179
180        @Override
181        public void i(String msg, Throwable tr) {
182            Log.i(mTag, msg, tr);
183        }
184
185        @Override
186        public void v(String msg) {
187            Log.v(mTag, msg);
188        }
189
190        @Override
191        public void v(String msg, Throwable tr) {
192            Log.v(mTag, msg, tr);
193        }
194
195        @Override
196        public void w(String msg) {
197            Log.w(mTag, msg);
198        }
199
200        @Override
201        public void w(String msg, Throwable tr) {
202            Log.w(mTag, msg, tr);
203        }
204    }
205}
206