blob: f55a31fcc986f056bd57fc11bd8346f920f179d2 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 com.android.internal.logging;
18
19import android.util.Log;
Dianne Hackborn8c841092013-06-24 13:46:13 -070020import com.android.internal.util.FastPrintWriter;
Jesse Wilson3183c782009-10-22 16:21:11 -070021import dalvik.system.DalvikLogging;
22import dalvik.system.DalvikLogHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import java.io.PrintWriter;
25import java.io.StringWriter;
Jesse Wilson3183c782009-10-22 16:21:11 -070026import java.util.logging.Formatter;
27import java.util.logging.Handler;
28import java.util.logging.Level;
29import java.util.logging.LogRecord;
30import java.util.logging.Logger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
32/**
33 * Implements a {@link java.util.logging.Logger} handler that writes to the Android log. The
34 * implementation is rather straightforward. The name of the logger serves as
35 * the log tag. Only the log levels need to be converted appropriately. For
36 * this purpose, the following mapping is being used:
37 *
38 * <table>
39 * <tr>
40 * <th>logger level</th>
41 * <th>Android level</th>
42 * </tr>
43 * <tr>
44 * <td>
45 * SEVERE
46 * </td>
47 * <td>
48 * ERROR
49 * </td>
50 * </tr>
51 * <tr>
52 * <td>
53 * WARNING
54 * </td>
55 * <td>
56 * WARN
57 * </td>
58 * </tr>
59 * <tr>
60 * <td>
61 * INFO
62 * </td>
63 * <td>
64 * INFO
65 * </td>
66 * </tr>
67 * <tr>
68 * <td>
69 * CONFIG
70 * </td>
71 * <td>
72 * DEBUG
73 * </td>
74 * </tr>
75 * <tr>
76 * <td>
77 * FINE, FINER, FINEST
78 * </td>
79 * <td>
80 * VERBOSE
81 * </td>
82 * </tr>
83 * </table>
84 */
Jesse Wilson3183c782009-10-22 16:21:11 -070085public class AndroidHandler extends Handler implements DalvikLogHandler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 /**
87 * Holds the formatter for all Android log handlers.
88 */
89 private static final Formatter THE_FORMATTER = new Formatter() {
90 @Override
91 public String format(LogRecord r) {
92 Throwable thrown = r.getThrown();
93 if (thrown != null) {
94 StringWriter sw = new StringWriter();
Dianne Hackborn8c841092013-06-24 13:46:13 -070095 PrintWriter pw = new FastPrintWriter(sw, false, 256);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 sw.write(r.getMessage());
97 sw.write("\n");
98 thrown.printStackTrace(pw);
99 pw.flush();
100 return sw.toString();
101 } else {
102 return r.getMessage();
103 }
104 }
105 };
106
107 /**
108 * Constructs a new instance of the Android log handler.
109 */
110 public AndroidHandler() {
111 setFormatter(THE_FORMATTER);
112 }
113
114 @Override
115 public void close() {
116 // No need to close, but must implement abstract method.
117 }
118
119 @Override
120 public void flush() {
121 // No need to flush, but must implement abstract method.
122 }
123
124 @Override
125 public void publish(LogRecord record) {
Jesse Wilson3183c782009-10-22 16:21:11 -0700126 int level = getAndroidLevel(record.getLevel());
127 String tag = DalvikLogging.loggerNameToTag(record.getLoggerName());
128 if (!Log.isLoggable(tag, level)) {
129 return;
130 }
131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 String message = getFormatter().format(record);
134 Log.println(level, tag, message);
135 } catch (RuntimeException e) {
136 Log.e("AndroidHandler", "Error logging message.", e);
137 }
138 }
Bob Leeef0996f2009-03-24 20:06:19 -0700139
Jesse Wilson3183c782009-10-22 16:21:11 -0700140 public void publish(Logger source, String tag, Level level, String message) {
141 // TODO: avoid ducking into native 2x; we aren't saving any formatter calls
142 int priority = getAndroidLevel(level);
143 if (!Log.isLoggable(tag, priority)) {
144 return;
145 }
146
147 try {
148 Log.println(priority, tag, message);
149 } catch (RuntimeException e) {
150 Log.e("AndroidHandler", "Error logging message.", e);
151 }
152 }
153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 /**
155 * Converts a {@link java.util.logging.Logger} logging level into an Android one.
Jesse Wilson3183c782009-10-22 16:21:11 -0700156 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 * @param level The {@link java.util.logging.Logger} logging level.
Jesse Wilson3183c782009-10-22 16:21:11 -0700158 *
159 * @return The resulting Android logging level.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 */
Bob Leeef0996f2009-03-24 20:06:19 -0700161 static int getAndroidLevel(Level level) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 int value = level.intValue();
Bob Leeef0996f2009-03-24 20:06:19 -0700163 if (value >= 1000) { // SEVERE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 return Log.ERROR;
Bob Leeef0996f2009-03-24 20:06:19 -0700165 } else if (value >= 900) { // WARNING
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 return Log.WARN;
Bob Leeef0996f2009-03-24 20:06:19 -0700167 } else if (value >= 800) { // INFO
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 return Log.INFO;
Bob Leeef0996f2009-03-24 20:06:19 -0700169 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 return Log.DEBUG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 }
172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173}