The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
| 17 | package android.util; |
| 18 | |
Chris Wren | d09bf82 | 2016-12-02 17:43:23 -0500 | [diff] [blame] | 19 | import android.annotation.SystemApi; |
Artur Satayev | df43959 | 2019-12-10 17:47:53 +0000 | [diff] [blame^] | 20 | import android.compat.annotation.UnsupportedAppUsage; |
Chris Wren | d09bf82 | 2016-12-02 17:43:23 -0500 | [diff] [blame] | 21 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 22 | import java.io.BufferedReader; |
| 23 | import java.io.FileReader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import java.io.IOException; |
| 25 | import java.io.UnsupportedEncodingException; |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 26 | import java.nio.BufferUnderflowException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import java.nio.ByteBuffer; |
| 28 | import java.nio.ByteOrder; |
Rubin Xu | 75431fb | 2016-01-07 21:12:14 +0000 | [diff] [blame] | 29 | import java.util.Arrays; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import java.util.Collection; |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 31 | import java.util.HashMap; |
| 32 | import java.util.regex.Matcher; |
| 33 | import java.util.regex.Pattern; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 36 | * Access to the system diagnostic event record. System diagnostic events are |
| 37 | * used to record certain system-level events (such as garbage collection, |
| 38 | * activity manager state, system watchdogs, and other low level activity), |
| 39 | * which may be automatically collected and analyzed during system development. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | * |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 41 | * <p>This is <b>not</b> the main "logcat" debugging log ({@link android.util.Log})! |
| 42 | * These diagnostic events are for system integrators, not application authors. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | * |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 44 | * <p>Events use integer tag codes corresponding to /system/etc/event-log-tags. |
| 45 | * They carry a payload of one or more int, long, or String values. The |
| 46 | * event-log-tags file defines the payload contents for each type code. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | public class EventLog { |
Jesse Wilson | a0f8bc5 | 2011-02-24 10:44:33 -0800 | [diff] [blame] | 49 | /** @hide */ public EventLog() {} |
| 50 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 51 | private static final String TAG = "EventLog"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 53 | private static final String TAGS_FILE = "/system/etc/event-log-tags"; |
| 54 | private static final String COMMENT_PATTERN = "^\\s*(#.*)?$"; |
| 55 | private static final String TAG_PATTERN = "^\\s*(\\d+)\\s+(\\w+)\\s*(\\(.*\\))?\\s*$"; |
| 56 | private static HashMap<String, Integer> sTagCodes = null; |
| 57 | private static HashMap<Integer, String> sTagNames = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | |
Neil Fuller | bf0dc0f | 2015-11-24 18:19:06 +0000 | [diff] [blame] | 59 | /** A previously logged event read from the logs. Instances are thread safe. */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | public static final class Event { |
| 61 | private final ByteBuffer mBuffer; |
Chris Wren | d9f3d9e | 2016-12-16 14:08:34 -0500 | [diff] [blame] | 62 | private Exception mLastWtf; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | |
Mark Salyzyn | 747802f | 2014-04-25 13:25:18 -0700 | [diff] [blame] | 64 | // Layout of event log entry received from Android logger. |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 65 | // see system/core/include/log/log.h |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | private static final int LENGTH_OFFSET = 0; |
Mark Salyzyn | 747802f | 2014-04-25 13:25:18 -0700 | [diff] [blame] | 67 | private static final int HEADER_SIZE_OFFSET = 2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | private static final int PROCESS_OFFSET = 4; |
| 69 | private static final int THREAD_OFFSET = 8; |
| 70 | private static final int SECONDS_OFFSET = 12; |
| 71 | private static final int NANOSECONDS_OFFSET = 16; |
Chris Wren | 4d6b54d | 2017-04-27 16:56:54 -0400 | [diff] [blame] | 72 | private static final int UID_OFFSET = 24; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | |
Mark Salyzyn | 747802f | 2014-04-25 13:25:18 -0700 | [diff] [blame] | 74 | // Layout for event log v1 format, v2 and v3 use HEADER_SIZE_OFFSET |
| 75 | private static final int V1_PAYLOAD_START = 20; |
| 76 | private static final int DATA_OFFSET = 4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 78 | // Value types |
| 79 | private static final byte INT_TYPE = 0; |
| 80 | private static final byte LONG_TYPE = 1; |
| 81 | private static final byte STRING_TYPE = 2; |
| 82 | private static final byte LIST_TYPE = 3; |
Jeff Brown | e7e9cce | 2015-04-28 13:26:48 -0700 | [diff] [blame] | 83 | private static final byte FLOAT_TYPE = 4; |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 84 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | /** @param data containing event, read from the system */ |
Mathew Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 86 | @UnsupportedAppUsage |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 87 | /*package*/ Event(byte[] data) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | mBuffer = ByteBuffer.wrap(data); |
| 89 | mBuffer.order(ByteOrder.nativeOrder()); |
| 90 | } |
| 91 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 92 | /** @return the process ID which wrote the log entry */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | public int getProcessId() { |
| 94 | return mBuffer.getInt(PROCESS_OFFSET); |
| 95 | } |
| 96 | |
Chris Wren | 4d6b54d | 2017-04-27 16:56:54 -0400 | [diff] [blame] | 97 | /** |
| 98 | * @return the UID which wrote the log entry |
| 99 | * @hide |
| 100 | */ |
| 101 | @SystemApi |
| 102 | public int getUid() { |
| 103 | try { |
| 104 | return mBuffer.getInt(UID_OFFSET); |
| 105 | } catch (IndexOutOfBoundsException e) { |
| 106 | // buffer won't contain the UID if the caller doesn't have permission. |
| 107 | return -1; |
| 108 | } |
| 109 | } |
| 110 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 111 | /** @return the thread ID which wrote the log entry */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | public int getThreadId() { |
| 113 | return mBuffer.getInt(THREAD_OFFSET); |
| 114 | } |
| 115 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 116 | /** @return the wall clock time when the entry was written */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | public long getTimeNanos() { |
| 118 | return mBuffer.getInt(SECONDS_OFFSET) * 1000000000l |
| 119 | + mBuffer.getInt(NANOSECONDS_OFFSET); |
| 120 | } |
| 121 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 122 | /** @return the type tag code of the entry */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | public int getTag() { |
Mark Salyzyn | 747802f | 2014-04-25 13:25:18 -0700 | [diff] [blame] | 124 | int offset = mBuffer.getShort(HEADER_SIZE_OFFSET); |
| 125 | if (offset == 0) { |
| 126 | offset = V1_PAYLOAD_START; |
| 127 | } |
| 128 | return mBuffer.getInt(offset); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Jeff Brown | e7e9cce | 2015-04-28 13:26:48 -0700 | [diff] [blame] | 131 | /** @return one of Integer, Long, Float, String, null, or Object[] of same. */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | public synchronized Object getData() { |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 133 | try { |
Mark Salyzyn | 747802f | 2014-04-25 13:25:18 -0700 | [diff] [blame] | 134 | int offset = mBuffer.getShort(HEADER_SIZE_OFFSET); |
| 135 | if (offset == 0) { |
| 136 | offset = V1_PAYLOAD_START; |
| 137 | } |
| 138 | mBuffer.limit(offset + mBuffer.getShort(LENGTH_OFFSET)); |
Chris Wren | d9f3d9e | 2016-12-16 14:08:34 -0500 | [diff] [blame] | 139 | if ((offset + DATA_OFFSET) >= mBuffer.limit()) { |
| 140 | // no payload |
| 141 | return null; |
| 142 | } |
Mark Salyzyn | 747802f | 2014-04-25 13:25:18 -0700 | [diff] [blame] | 143 | mBuffer.position(offset + DATA_OFFSET); // Just after the tag. |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 144 | return decodeObject(); |
| 145 | } catch (IllegalArgumentException e) { |
| 146 | Log.wtf(TAG, "Illegal entry payload: tag=" + getTag(), e); |
Chris Wren | d9f3d9e | 2016-12-16 14:08:34 -0500 | [diff] [blame] | 147 | mLastWtf = e; |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 148 | return null; |
| 149 | } catch (BufferUnderflowException e) { |
| 150 | Log.wtf(TAG, "Truncated entry payload: tag=" + getTag(), e); |
Chris Wren | d9f3d9e | 2016-12-16 14:08:34 -0500 | [diff] [blame] | 151 | mLastWtf = e; |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 152 | return null; |
| 153 | } |
Jim Miller | 3063687 | 2009-06-08 18:59:49 -0700 | [diff] [blame] | 154 | } |
| 155 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | /** @return the loggable item at the current position in mBuffer. */ |
| 157 | private Object decodeObject() { |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 158 | byte type = mBuffer.get(); |
| 159 | switch (type) { |
| 160 | case INT_TYPE: |
Jeff Brown | e7e9cce | 2015-04-28 13:26:48 -0700 | [diff] [blame] | 161 | return mBuffer.getInt(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 163 | case LONG_TYPE: |
Jeff Brown | e7e9cce | 2015-04-28 13:26:48 -0700 | [diff] [blame] | 164 | return mBuffer.getLong(); |
| 165 | |
| 166 | case FLOAT_TYPE: |
| 167 | return mBuffer.getFloat(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 169 | case STRING_TYPE: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | try { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | int length = mBuffer.getInt(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | int start = mBuffer.position(); |
| 173 | mBuffer.position(start + length); |
| 174 | return new String(mBuffer.array(), start, length, "UTF-8"); |
| 175 | } catch (UnsupportedEncodingException e) { |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 176 | Log.wtf(TAG, "UTF-8 is not supported", e); |
Chris Wren | d9f3d9e | 2016-12-16 14:08:34 -0500 | [diff] [blame] | 177 | mLastWtf = e; |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 178 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 181 | case LIST_TYPE: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | int length = mBuffer.get(); |
Dan Egnor | 6916089 | 2010-01-06 23:12:57 -0800 | [diff] [blame] | 183 | if (length < 0) length += 256; // treat as signed byte |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | Object[] array = new Object[length]; |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 185 | for (int i = 0; i < length; ++i) array[i] = decodeObject(); |
| 186 | return array; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | |
| 188 | default: |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 189 | throw new IllegalArgumentException("Unknown entry type: " + type); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | } |
| 191 | } |
Rubin Xu | 75431fb | 2016-01-07 21:12:14 +0000 | [diff] [blame] | 192 | |
| 193 | /** @hide */ |
| 194 | public static Event fromBytes(byte[] data) { |
| 195 | return new Event(data); |
| 196 | } |
| 197 | |
| 198 | /** @hide */ |
| 199 | public byte[] getBytes() { |
| 200 | byte[] bytes = mBuffer.array(); |
| 201 | return Arrays.copyOf(bytes, bytes.length); |
| 202 | } |
Chris Wren | d9f3d9e | 2016-12-16 14:08:34 -0500 | [diff] [blame] | 203 | |
| 204 | /** |
| 205 | * Retreive the last WTF error generated by this object. |
| 206 | * @hide |
| 207 | */ |
| 208 | //VisibleForTesting |
| 209 | public Exception getLastError() { |
| 210 | return mLastWtf; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Clear the error state for this object. |
| 215 | * @hide |
| 216 | */ |
| 217 | //VisibleForTesting |
| 218 | public void clearError() { |
| 219 | mLastWtf = null; |
| 220 | } |
Pavel Grafov | 4ce59d4 | 2017-02-25 19:45:43 +0000 | [diff] [blame] | 221 | |
| 222 | /** |
| 223 | * @hide |
| 224 | */ |
| 225 | @Override |
| 226 | public boolean equals(Object o) { |
| 227 | // Not using ByteBuffer.equals since it takes buffer position into account and we |
| 228 | // always use absolute positions here. |
| 229 | if (this == o) return true; |
| 230 | if (o == null || getClass() != o.getClass()) return false; |
| 231 | Event other = (Event) o; |
| 232 | return Arrays.equals(mBuffer.array(), other.mBuffer.array()); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * @hide |
| 237 | */ |
| 238 | @Override |
| 239 | public int hashCode() { |
| 240 | // Not using ByteBuffer.hashCode since it takes buffer position into account and we |
| 241 | // always use absolute positions here. |
| 242 | return Arrays.hashCode(mBuffer.array()); |
| 243 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | // We assume that the native methods deal with any concurrency issues. |
| 247 | |
| 248 | /** |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 249 | * Record an event log message. |
| 250 | * @param tag The event type tag code |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | * @param value A value to log |
| 252 | * @return The number of bytes written |
| 253 | */ |
| 254 | public static native int writeEvent(int tag, int value); |
| 255 | |
| 256 | /** |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 257 | * Record an event log message. |
| 258 | * @param tag The event type tag code |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | * @param value A value to log |
| 260 | * @return The number of bytes written |
| 261 | */ |
| 262 | public static native int writeEvent(int tag, long value); |
| 263 | |
| 264 | /** |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 265 | * Record an event log message. |
| 266 | * @param tag The event type tag code |
Jeff Brown | e7e9cce | 2015-04-28 13:26:48 -0700 | [diff] [blame] | 267 | * @param value A value to log |
| 268 | * @return The number of bytes written |
| 269 | */ |
| 270 | public static native int writeEvent(int tag, float value); |
| 271 | |
| 272 | /** |
| 273 | * Record an event log message. |
| 274 | * @param tag The event type tag code |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | * @param str A value to log |
| 276 | * @return The number of bytes written |
| 277 | */ |
| 278 | public static native int writeEvent(int tag, String str); |
| 279 | |
| 280 | /** |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 281 | * Record an event log message. |
| 282 | * @param tag The event type tag code |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | * @param list A list of values to log |
| 284 | * @return The number of bytes written |
| 285 | */ |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 286 | public static native int writeEvent(int tag, Object... list); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | |
| 288 | /** |
| 289 | * Read events from the log, filtered by type. |
| 290 | * @param tags to search for |
| 291 | * @param output container to add events into |
| 292 | * @throws IOException if something goes wrong reading events |
| 293 | */ |
| 294 | public static native void readEvents(int[] tags, Collection<Event> output) |
| 295 | throws IOException; |
Jim Miller | 95ff240 | 2009-07-01 18:03:41 -0700 | [diff] [blame] | 296 | |
| 297 | /** |
Chris Wren | d09bf82 | 2016-12-02 17:43:23 -0500 | [diff] [blame] | 298 | * Read events from the log, filtered by type, blocking until logs are about to be overwritten. |
| 299 | * @param tags to search for |
| 300 | * @param timestamp timestamp allow logs before this time to be overwritten. |
| 301 | * @param output container to add events into |
| 302 | * @throws IOException if something goes wrong reading events |
| 303 | * @hide |
| 304 | */ |
| 305 | @SystemApi |
| 306 | public static native void readEventsOnWrapping(int[] tags, long timestamp, |
| 307 | Collection<Event> output) |
| 308 | throws IOException; |
| 309 | |
| 310 | /** |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 311 | * Get the name associated with an event type tag code. |
| 312 | * @param tag code to look up |
| 313 | * @return the name of the tag, or null if no tag has that number |
Jim Miller | 95ff240 | 2009-07-01 18:03:41 -0700 | [diff] [blame] | 314 | */ |
Dan Egnor | 62136d3 | 2010-01-05 19:09:08 -0800 | [diff] [blame] | 315 | public static String getTagName(int tag) { |
| 316 | readTagsFile(); |
| 317 | return sTagNames.get(tag); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Get the event type tag code associated with an event name. |
| 322 | * @param name of event to look up |
| 323 | * @return the tag code, or -1 if no tag has that name |
| 324 | */ |
| 325 | public static int getTagCode(String name) { |
| 326 | readTagsFile(); |
| 327 | Integer code = sTagCodes.get(name); |
| 328 | return code != null ? code : -1; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Read TAGS_FILE, populating sTagCodes and sTagNames, if not already done. |
| 333 | */ |
| 334 | private static synchronized void readTagsFile() { |
| 335 | if (sTagCodes != null && sTagNames != null) return; |
| 336 | |
| 337 | sTagCodes = new HashMap<String, Integer>(); |
| 338 | sTagNames = new HashMap<Integer, String>(); |
| 339 | |
| 340 | Pattern comment = Pattern.compile(COMMENT_PATTERN); |
| 341 | Pattern tag = Pattern.compile(TAG_PATTERN); |
| 342 | BufferedReader reader = null; |
| 343 | String line; |
| 344 | |
| 345 | try { |
| 346 | reader = new BufferedReader(new FileReader(TAGS_FILE), 256); |
| 347 | while ((line = reader.readLine()) != null) { |
| 348 | if (comment.matcher(line).matches()) continue; |
| 349 | |
| 350 | Matcher m = tag.matcher(line); |
| 351 | if (!m.matches()) { |
| 352 | Log.wtf(TAG, "Bad entry in " + TAGS_FILE + ": " + line); |
| 353 | continue; |
| 354 | } |
| 355 | |
| 356 | try { |
| 357 | int num = Integer.parseInt(m.group(1)); |
| 358 | String name = m.group(2); |
| 359 | sTagCodes.put(name, num); |
| 360 | sTagNames.put(num, name); |
| 361 | } catch (NumberFormatException e) { |
| 362 | Log.wtf(TAG, "Error in " + TAGS_FILE + ": " + line, e); |
| 363 | } |
| 364 | } |
| 365 | } catch (IOException e) { |
| 366 | Log.wtf(TAG, "Error reading " + TAGS_FILE, e); |
| 367 | // Leave the maps existing but unpopulated |
| 368 | } finally { |
| 369 | try { if (reader != null) reader.close(); } catch (IOException e) {} |
| 370 | } |
| 371 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | } |