15596b4c902dcb685928b43678f428746ca5ffd08Angus Kong/*
25596b4c902dcb685928b43678f428746ca5ffd08Angus Kong * Copyright (C) 2014 The Android Open Source Project
35596b4c902dcb685928b43678f428746ca5ffd08Angus Kong *
45596b4c902dcb685928b43678f428746ca5ffd08Angus Kong * Licensed under the Apache License, Version 2.0 (the "License");
55596b4c902dcb685928b43678f428746ca5ffd08Angus Kong * you may not use this file except in compliance with the License.
65596b4c902dcb685928b43678f428746ca5ffd08Angus Kong * You may obtain a copy of the License at
75596b4c902dcb685928b43678f428746ca5ffd08Angus Kong *
85596b4c902dcb685928b43678f428746ca5ffd08Angus Kong *      http://www.apache.org/licenses/LICENSE-2.0
95596b4c902dcb685928b43678f428746ca5ffd08Angus Kong *
105596b4c902dcb685928b43678f428746ca5ffd08Angus Kong * Unless required by applicable law or agreed to in writing, software
115596b4c902dcb685928b43678f428746ca5ffd08Angus Kong * distributed under the License is distributed on an "AS IS" BASIS,
125596b4c902dcb685928b43678f428746ca5ffd08Angus Kong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135596b4c902dcb685928b43678f428746ca5ffd08Angus Kong * See the License for the specific language governing permissions and
145596b4c902dcb685928b43678f428746ca5ffd08Angus Kong * limitations under the License.
155596b4c902dcb685928b43678f428746ca5ffd08Angus Kong */
165596b4c902dcb685928b43678f428746ca5ffd08Angus Kong
175596b4c902dcb685928b43678f428746ca5ffd08Angus Kongpackage com.android.camera.debug;
185596b4c902dcb685928b43678f428746ca5ffd08Angus Kong
195596b4c902dcb685928b43678f428746ca5ffd08Angus Kongpublic class Log {
20af4bcb92d710f2cb74ddc0559397c49b55beb115Alan Newberger    /**
21af4bcb92d710f2cb74ddc0559397c49b55beb115Alan Newberger     * All Camera logging using this class will use this tag prefix.
22af4bcb92d710f2cb74ddc0559397c49b55beb115Alan Newberger     * Additionally, the prefix itself is checked in isLoggable and
23af4bcb92d710f2cb74ddc0559397c49b55beb115Alan Newberger     * serves as an override. So, to toggle all logs allowed by the
24af4bcb92d710f2cb74ddc0559397c49b55beb115Alan Newberger     * current {@link Configuration}, you can set properties:
25af4bcb92d710f2cb74ddc0559397c49b55beb115Alan Newberger     *
26af4bcb92d710f2cb74ddc0559397c49b55beb115Alan Newberger     * adb shell setprop log.tag.CAM_ VERBOSE
27af4bcb92d710f2cb74ddc0559397c49b55beb115Alan Newberger     * adb shell setprop log.tag.CAM_ ""
28af4bcb92d710f2cb74ddc0559397c49b55beb115Alan Newberger     */
295596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    public static final String CAMERA_LOGTAG_PREFIX = "CAM_";
305596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    private static final Log.Tag TAG = new Log.Tag("Log");
315596b4c902dcb685928b43678f428746ca5ffd08Angus Kong
325596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    /**
335596b4c902dcb685928b43678f428746ca5ffd08Angus Kong     * This class restricts the length of the log tag to be less than the
345596b4c902dcb685928b43678f428746ca5ffd08Angus Kong     * framework limit and also prepends the common tag prefix defined by
355596b4c902dcb685928b43678f428746ca5ffd08Angus Kong     * {@code CAMERA_LOGTAG_PREFIX}.
365596b4c902dcb685928b43678f428746ca5ffd08Angus Kong     */
375596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    public static final class Tag {
385596b4c902dcb685928b43678f428746ca5ffd08Angus Kong
395596b4c902dcb685928b43678f428746ca5ffd08Angus Kong        // The length limit from Android framework is 23.
405596b4c902dcb685928b43678f428746ca5ffd08Angus Kong        private static final int MAX_TAG_LEN = 23 - CAMERA_LOGTAG_PREFIX.length();
415596b4c902dcb685928b43678f428746ca5ffd08Angus Kong
425596b4c902dcb685928b43678f428746ca5ffd08Angus Kong        final String mValue;
435596b4c902dcb685928b43678f428746ca5ffd08Angus Kong
445596b4c902dcb685928b43678f428746ca5ffd08Angus Kong        public Tag(String tag) {
455596b4c902dcb685928b43678f428746ca5ffd08Angus Kong            final int lenDiff = tag.length() - MAX_TAG_LEN;
465596b4c902dcb685928b43678f428746ca5ffd08Angus Kong            if (lenDiff > 0) {
475596b4c902dcb685928b43678f428746ca5ffd08Angus Kong                w(TAG, "Tag " + tag + " is " + lenDiff + " chars longer than limit.");
485596b4c902dcb685928b43678f428746ca5ffd08Angus Kong            }
495596b4c902dcb685928b43678f428746ca5ffd08Angus Kong            mValue = CAMERA_LOGTAG_PREFIX + (lenDiff > 0 ? tag.substring(0, MAX_TAG_LEN) : tag);
505596b4c902dcb685928b43678f428746ca5ffd08Angus Kong        }
515596b4c902dcb685928b43678f428746ca5ffd08Angus Kong
525596b4c902dcb685928b43678f428746ca5ffd08Angus Kong        @Override
535596b4c902dcb685928b43678f428746ca5ffd08Angus Kong        public String toString() {
545596b4c902dcb685928b43678f428746ca5ffd08Angus Kong            return mValue;
555596b4c902dcb685928b43678f428746ca5ffd08Angus Kong        }
565596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    }
575596b4c902dcb685928b43678f428746ca5ffd08Angus Kong
585596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    public static void d(Tag tag, String msg) {
59c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger        if (isLoggable(tag, android.util.Log.DEBUG)) {
60c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger            android.util.Log.d(tag.toString(), msg);
61c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger        }
625596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    }
635596b4c902dcb685928b43678f428746ca5ffd08Angus Kong
645596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    public static void d(Tag tag, String msg, Throwable tr) {
65c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger        if (isLoggable(tag, android.util.Log.DEBUG)) {
66c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger            android.util.Log.d(tag.toString(), msg, tr);
67c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger        }
685596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    }
695596b4c902dcb685928b43678f428746ca5ffd08Angus Kong
705596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    public static void e(Tag tag, String msg) {
71c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger        if (isLoggable(tag, android.util.Log.ERROR)) {
72c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger            android.util.Log.e(tag.toString(), msg);
73c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger        }
745596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    }
755596b4c902dcb685928b43678f428746ca5ffd08Angus Kong
765596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    public static void e(Tag tag, String msg, Throwable tr) {
77c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger        if (isLoggable(tag, android.util.Log.ERROR)) {
78c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger            android.util.Log.e(tag.toString(), msg, tr);
79c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger        }
805596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    }
815596b4c902dcb685928b43678f428746ca5ffd08Angus Kong
825596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    public static void i(Tag tag, String msg) {
83c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger        if (isLoggable(tag, android.util.Log.INFO)) {
84c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger            android.util.Log.i(tag.toString(), msg);
85c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger        }
865596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    }
875596b4c902dcb685928b43678f428746ca5ffd08Angus Kong
885596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    public static void i(Tag tag, String msg, Throwable tr) {
89c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger        if (isLoggable(tag, android.util.Log.INFO)) {
90c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger            android.util.Log.i(tag.toString(), msg, tr);
91c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger        }
925596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    }
935596b4c902dcb685928b43678f428746ca5ffd08Angus Kong
945596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    public static void v(Tag tag, String msg) {
95c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger        if (isLoggable(tag, android.util.Log.VERBOSE)) {
96c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger            android.util.Log.v(tag.toString(), msg);
97c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger        }
985596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    }
995596b4c902dcb685928b43678f428746ca5ffd08Angus Kong
1005596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    public static void v(Tag tag, String msg, Throwable tr) {
101c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger        if (isLoggable(tag, android.util.Log.VERBOSE)) {
102c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger            android.util.Log.v(tag.toString(), msg, tr);
103c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger        }
1045596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    }
1055596b4c902dcb685928b43678f428746ca5ffd08Angus Kong
1065596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    public static void w(Tag tag, String msg) {
107c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger        if (isLoggable(tag, android.util.Log.WARN)) {
108c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger            android.util.Log.w(tag.toString(), msg);
109c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger        }
1105596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    }
1115596b4c902dcb685928b43678f428746ca5ffd08Angus Kong
1125596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    public static void w(Tag tag, String msg, Throwable tr) {
113c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger        if (isLoggable(tag, android.util.Log.WARN)) {
114c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger            android.util.Log.w(tag.toString(), msg, tr);
115c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger        }
1165596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    }
1175596b4c902dcb685928b43678f428746ca5ffd08Angus Kong
1185596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    private static boolean isLoggable(Tag tag, int level) {
1195596b4c902dcb685928b43678f428746ca5ffd08Angus Kong        try {
120c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger            if (LogHelper.getOverrideLevel() != 0) {
121c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger                // Override system log level and output. VERBOSE is smaller than
122c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger                // ERROR, so the comparison checks that the override value is smaller
123c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger                // than the desired output level. This applies to all tags.
124c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger                return LogHelper.getOverrideLevel() <= level;
125c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger            } else {
126c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger                // The prefix can be used as an override tag to see all camera logs
127c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger                return android.util.Log.isLoggable(CAMERA_LOGTAG_PREFIX, level)
128c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger                        || android.util.Log.isLoggable(tag.toString(), level);
129c00f209e861dba093eb3c267ddc5078b23928d05Alan Newberger            }
1305596b4c902dcb685928b43678f428746ca5ffd08Angus Kong        } catch (IllegalArgumentException ex) {
1315596b4c902dcb685928b43678f428746ca5ffd08Angus Kong            e(TAG, "Tag too long:" + tag);
1325596b4c902dcb685928b43678f428746ca5ffd08Angus Kong            return false;
1335596b4c902dcb685928b43678f428746ca5ffd08Angus Kong        }
1345596b4c902dcb685928b43678f428746ca5ffd08Angus Kong    }
1355596b4c902dcb685928b43678f428746ca5ffd08Angus Kong}
136