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.async.MainThread;
20
21import java.util.Arrays;
22import java.util.Collections;
23import java.util.List;
24import java.util.Objects;
25
26/**
27 * Helpful logging extensions for including more detailed info about
28 * the objects and threads involved in the logging.
29 */
30public class LogUtil {
31    /**
32     * Prefixes a message with with a hashcode tag of the object,
33     * and a [ui] tag if the method was executed on the main thread.
34     */
35    public static String addTags(Object object, String msg) {
36        return hashCodeTag(object) + mainThreadTag() + " " + msg;
37    }
38
39    /**
40     * Prefixes a message with the bracketed tags specified in the
41     * tag list, along with a hashcode tag of the object, and a
42     * [ui] tag if the method was executed on the main thread.
43     */
44    public static String addTags(Object object, String msg, String tagList) {
45        return hashCodeTag(object) + mainThreadTag() + formatTags(tagList) + " " + msg;
46    }
47
48    private static String formatTags(String tagList) {
49        // Split on common "divider" characters:
50        // * All whitespace, except spaces: \x00-\x1F
51        // * () Characters: \x28-\x29
52        // * , Character: \x2C
53        // * / Character: \x2F
54        // * ;<=>? Characters: \x3B-\x3F
55        // * [\] Characters: \x5B-\x5D
56        // * {|} Characters: \x7B-\x7D
57        List<String> tags = Arrays.asList(tagList.split("[\\x00-\\x1F\\x28-\\x29\\x2C\\x2F"
58              + "\\x3B-\\x3F\\x5B-\\x5D\\x7B-\\x7D]"));
59        Collections.sort(tags);
60        String result = "";
61        // Convert all non-empty entries to tags.
62        for (String tag : tags) {
63            String trimmed = tag.trim();
64            if(trimmed.length() > 0) {
65                result += "[" + trimmed + "]";
66            }
67        }
68        return result;
69    }
70
71    private static String hashCodeTag(Object object) {
72        final String tag;
73        if (object == null) {
74            tag = "null";
75        } else {
76            tag = Integer.toHexString(Objects.hashCode(object));
77        }
78        return String.format("[%-9s]", "@" + tag);
79    }
80
81    private static String mainThreadTag() {
82        return MainThread.isMainThread() ? "[ui]" : "";
83    }
84}
85