Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14792 manas 1
/**
2
 * Copyright 2010-present Facebook.
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
 
17
package com.facebook.internal;
18
 
19
import android.util.Log;
20
import com.facebook.LoggingBehavior;
21
import com.facebook.Settings;
22
 
23
import java.util.HashMap;
24
import java.util.Map;
25
 
26
/**
27
 * com.facebook.internal is solely for the use of other packages within the Facebook SDK for Android. Use of
28
 * any of the classes in this package is unsupported, and they may be modified or removed without warning at
29
 * any time.
30
 */
31
public class Logger {
32
    public static final String LOG_TAG_BASE = "FacebookSDK.";
33
    private static final HashMap<String, String> stringsToReplace = new HashMap<String, String>();
34
 
35
    private final LoggingBehavior behavior;
36
    private final String tag;
37
    private StringBuilder contents;
38
    private int priority = Log.DEBUG;
39
 
40
    // Note that the mapping of replaced strings is never emptied, so it should be used only for things that
41
    // are not expected to be too numerous, such as access tokens.
42
    public synchronized static void registerStringToReplace(String original, String replace) {
43
        stringsToReplace.put(original, replace);
44
    }
45
 
46
    public synchronized static void registerAccessToken(String accessToken) {
47
        if (Settings.isLoggingBehaviorEnabled(LoggingBehavior.INCLUDE_ACCESS_TOKENS) == false) {
48
            registerStringToReplace(accessToken, "ACCESS_TOKEN_REMOVED");
49
        }
50
    }
51
 
52
    public static void log(LoggingBehavior behavior, String tag, String string) {
53
        log(behavior, Log.DEBUG, tag, string);
54
    }
55
 
56
    public static void log(LoggingBehavior behavior, String tag, String format, Object... args) {
57
        if (Settings.isLoggingBehaviorEnabled(behavior)) {
58
            String string = String.format(format, args);
59
            log(behavior, Log.DEBUG, tag, string);
60
        }
61
    }
62
 
63
    public static void log(LoggingBehavior behavior, int priority, String tag, String string) {
64
        if (Settings.isLoggingBehaviorEnabled(behavior)) {
65
            string = replaceStrings(string);
66
            if (tag.startsWith(LOG_TAG_BASE) == false) {
67
                tag = LOG_TAG_BASE + tag;
68
            }
69
            Log.println(priority, tag, string);
70
 
71
            // Developer errors warrant special treatment by printing out a stack trace, to make both more noticeable,
72
            // and let the source of the problem be more easily pinpointed.
73
            if (behavior == LoggingBehavior.DEVELOPER_ERRORS) {
74
                (new Exception()).printStackTrace();
75
            }
76
        }
77
    }
78
 
79
    private synchronized static String replaceStrings(String string) {
80
        for (Map.Entry<String, String> entry : stringsToReplace.entrySet()) {
81
            string = string.replace(entry.getKey(), entry.getValue());
82
        }
83
        return string;
84
    }
85
 
86
    public Logger(LoggingBehavior behavior, String tag) {
87
        Validate.notNullOrEmpty(tag, "tag");
88
 
89
        this.behavior = behavior;
90
        this.tag = LOG_TAG_BASE + tag;
91
        this.contents = new StringBuilder();
92
    }
93
 
94
    public int getPriority() {
95
        return priority;
96
    }
97
 
98
    public void setPriority(int value) {
99
        Validate.oneOf(value, "value", Log.ASSERT, Log.DEBUG, Log.ERROR, Log.INFO, Log.VERBOSE, Log.WARN);
100
 
101
        priority = value;
102
    }
103
 
104
    public String getContents() {
105
        return replaceStrings(contents.toString());
106
    }
107
 
108
    // Writes the accumulated contents, then clears contents to start again.
109
    public void log() {
110
        logString(contents.toString());
111
        contents = new StringBuilder();
112
    }
113
 
114
    // Immediately logs a string, ignoring any accumulated contents, which are left unchanged.
115
    public void logString(String string) {
116
        log(behavior, priority, tag, string);
117
    }
118
 
119
    public void append(StringBuilder stringBuilder) {
120
        if (shouldLog()) {
121
            contents.append(stringBuilder);
122
        }
123
    }
124
 
125
    public void append(String string) {
126
        if (shouldLog()) {
127
            contents.append(string);
128
        }
129
    }
130
 
131
    public void append(String format, Object... args) {
132
        if (shouldLog()) {
133
            contents.append(String.format(format, args));
134
        }
135
    }
136
 
137
    public void appendKeyValue(String key, Object value) {
138
        append("  %s:\t%s\n", key, value);
139
    }
140
 
141
    private boolean shouldLog() {
142
        return Settings.isLoggingBehaviorEnabled(behavior);
143
    }
144
}