blob: 12f6a4f9badbd18a5b3774d664d88c5c54fba99e [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;
Jesse Wilson3183c782009-10-22 16:21:11 -070020import dalvik.system.DalvikLogging;
21import dalvik.system.DalvikLogHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import java.io.PrintWriter;
24import java.io.StringWriter;
Jesse Wilson3183c782009-10-22 16:21:11 -070025import java.util.logging.Formatter;
26import java.util.logging.Handler;
27import java.util.logging.Level;
28import java.util.logging.LogRecord;
29import java.util.logging.Logger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
31/**
32 * Implements a {@link java.util.logging.Logger} handler that writes to the Android log. The
33 * implementation is rather straightforward. The name of the logger serves as
34 * the log tag. Only the log levels need to be converted appropriately. For
35 * this purpose, the following mapping is being used:
36 *
37 * <table>
38 * <tr>
39 * <th>logger level</th>
40 * <th>Android level</th>
41 * </tr>
42 * <tr>
43 * <td>
44 * SEVERE
45 * </td>
46 * <td>
47 * ERROR
48 * </td>
49 * </tr>
50 * <tr>
51 * <td>
52 * WARNING
53 * </td>
54 * <td>
55 * WARN
56 * </td>
57 * </tr>
58 * <tr>
59 * <td>
60 * INFO
61 * </td>
62 * <td>
63 * INFO
64 * </td>
65 * </tr>
66 * <tr>
67 * <td>
68 * CONFIG
69 * </td>
70 * <td>
71 * DEBUG
72 * </td>
73 * </tr>
74 * <tr>
75 * <td>
76 * FINE, FINER, FINEST
77 * </td>
78 * <td>
79 * VERBOSE
80 * </td>
81 * </tr>
82 * </table>
83 */
Jesse Wilson3183c782009-10-22 16:21:11 -070084public class AndroidHandler extends Handler implements DalvikLogHandler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 /**
86 * Holds the formatter for all Android log handlers.
87 */
88 private static final Formatter THE_FORMATTER = new Formatter() {
89 @Override
90 public String format(LogRecord r) {
91 Throwable thrown = r.getThrown();
92 if (thrown != null) {
93 StringWriter sw = new StringWriter();
94 PrintWriter pw = new PrintWriter(sw);
95 sw.write(r.getMessage());
96 sw.write("\n");
97 thrown.printStackTrace(pw);
98 pw.flush();
99 return sw.toString();
100 } else {
101 return r.getMessage();
102 }
103 }
104 };
105
106 /**
107 * Constructs a new instance of the Android log handler.
108 */
109 public AndroidHandler() {
110 setFormatter(THE_FORMATTER);
111 }
112
113 @Override
114 public void close() {
115 // No need to close, but must implement abstract method.
116 }
117
118 @Override
119 public void flush() {
120 // No need to flush, but must implement abstract method.
121 }
122
123 @Override
124 public void publish(LogRecord record) {
Jesse Wilson3183c782009-10-22 16:21:11 -0700125 int level = getAndroidLevel(record.getLevel());
126 String tag = DalvikLogging.loggerNameToTag(record.getLoggerName());
127 if (!Log.isLoggable(tag, level)) {
128 return;
129 }
130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 String message = getFormatter().format(record);
133 Log.println(level, tag, message);
134 } catch (RuntimeException e) {
135 Log.e("AndroidHandler", "Error logging message.", e);
136 }
137 }
Bob Leeef0996f2009-03-24 20:06:19 -0700138
Jesse Wilson3183c782009-10-22 16:21:11 -0700139 public void publish(Logger source, String tag, Level level, String message) {
140 // TODO: avoid ducking into native 2x; we aren't saving any formatter calls
141 int priority = getAndroidLevel(level);
142 if (!Log.isLoggable(tag, priority)) {
143 return;
144 }
145
146 try {
147 Log.println(priority, tag, message);
148 } catch (RuntimeException e) {
149 Log.e("AndroidHandler", "Error logging message.", e);
150 }
151 }
152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 /**
154 * Converts a {@link java.util.logging.Logger} logging level into an Android one.
Jesse Wilson3183c782009-10-22 16:21:11 -0700155 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 * @param level The {@link java.util.logging.Logger} logging level.
Jesse Wilson3183c782009-10-22 16:21:11 -0700157 *
158 * @return The resulting Android logging level.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 */
Bob Leeef0996f2009-03-24 20:06:19 -0700160 static int getAndroidLevel(Level level) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 int value = level.intValue();
Bob Leeef0996f2009-03-24 20:06:19 -0700162 if (value >= 1000) { // SEVERE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 return Log.ERROR;
Bob Leeef0996f2009-03-24 20:06:19 -0700164 } else if (value >= 900) { // WARNING
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 return Log.WARN;
Bob Leeef0996f2009-03-24 20:06:19 -0700166 } else if (value >= 800) { // INFO
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 return Log.INFO;
Bob Leeef0996f2009-03-24 20:06:19 -0700168 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 return Log.DEBUG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 }
171 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172}