EventLogTags.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
1/*
2 * Copyright (C) 2008 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 android.util;
18
19import android.util.Log;
20
21import java.io.BufferedReader;
22import java.io.FileReader;
23import java.io.IOException;
24import java.util.HashMap;
25import java.util.regex.Matcher;
26import java.util.regex.Pattern;
27
28/** Parsed representation of /etc/event-log-tags. */
29public class EventLogTags {
30    private final static String TAG = "EventLogTags";
31
32    private final static String TAGS_FILE = "/etc/event-log-tags";
33
34    public static class Description {
35        public final int mTag;
36        public final String mName;
37        // TODO: Parse parameter descriptions when anyone has a use for them.
38
39        Description(int tag, String name) {
40            mTag = tag;
41            mName = name;
42        }
43    }
44
45    private final static Pattern COMMENT_PATTERN = Pattern.compile(
46            "^\\s*(#.*)?$");
47
48    private final static Pattern TAG_PATTERN = Pattern.compile(
49            "^\\s*(\\d+)\\s+(\\w+)\\s*(\\(.*\\))?\\s*$");
50
51    private final HashMap<String, Description> mNameMap =
52            new HashMap<String, Description>();
53
54    private final HashMap<Integer, Description> mTagMap =
55            new HashMap<Integer, Description>();
56
57    public EventLogTags() throws IOException {
58        this(new BufferedReader(new FileReader(TAGS_FILE), 256));
59    }
60
61    public EventLogTags(BufferedReader input) throws IOException {
62        String line;
63        while ((line = input.readLine()) != null) {
64            Matcher m = COMMENT_PATTERN.matcher(line);
65            if (m.matches()) continue;
66
67            m = TAG_PATTERN.matcher(line);
68            if (m.matches()) {
69                try {
70                    int tag = Integer.parseInt(m.group(1));
71                    Description d = new Description(tag, m.group(2));
72                    mNameMap.put(d.mName, d);
73                    mTagMap.put(d.mTag, d);
74                } catch (NumberFormatException e) {
75                    Log.e(TAG, "Error in event log tags entry: " + line, e);
76                }
77            } else {
78                Log.e(TAG, "Can't parse event log tags entry: " + line);
79            }
80        }
81    }
82
83    public Description get(String name) {
84        return mNameMap.get(name);
85    }
86
87    public Description get(int tag) {
88        return mTagMap.get(tag);
89    }
90}
91