1/*
2 * Copyright (C) 2011 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.ddmuilib.logcat;
18
19import com.android.ddmlib.Log.LogLevel;
20
21/**
22 * Model a single log message output from {@code logcat -v long}.
23 * A logcat message has a {@link LogLevel}, the pid (process id) of the process
24 * generating the message, the time at which the message was generated, and
25 * the tag and message itself.
26 */
27public final class LogCatMessage {
28    private final LogLevel mLogLevel;
29    private final String mPid;
30    private final String mTid;
31    private final String mAppName;
32    private final String mTag;
33    private final String mTime;
34    private final String mMessage;
35
36    /**
37     * Construct an immutable log message object.
38     */
39    public LogCatMessage(LogLevel logLevel, String pid, String tid, String appName,
40            String tag, String time, String msg) {
41        mLogLevel = logLevel;
42        mPid = pid;
43        mAppName = appName;
44        mTag = tag;
45        mTime = time;
46        mMessage = msg;
47
48        long tidValue;
49        try {
50            // Thread id's may be in hex on some platforms.
51            // Decode and store them in radix 10.
52            tidValue = Long.decode(tid.trim());
53        } catch (NumberFormatException e) {
54            tidValue = -1;
55        }
56
57        mTid = Long.toString(tidValue);
58    }
59
60    public LogLevel getLogLevel() {
61        return mLogLevel;
62    }
63
64    public String getPid() {
65        return mPid;
66    }
67
68    public String getTid() {
69        return mTid;
70    }
71
72    public String getAppName() {
73        return mAppName;
74    }
75
76    public String getTag() {
77        return mTag;
78    }
79
80    public String getTime() {
81        return mTime;
82    }
83
84    public String getMessage() {
85        return mMessage;
86    }
87
88    @Override
89    public String toString() {
90        return mTime + ": "
91                + mLogLevel.getPriorityLetter() + "/"
92                + mTag + "("
93                + mPid + "): "
94                + mMessage;
95    }
96}
97