View Javadoc
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  package org.fourthline.cling.android;
17  
18  import android.util.Log;
19  
20  import java.io.PrintWriter;
21  import java.io.StringWriter;
22  import java.util.logging.Formatter;
23  import java.util.logging.Handler;
24  import java.util.logging.Level;
25  import java.util.logging.LogRecord;
26  
27  /*
28  Taken from: http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob_plain;f=core/java/com/android/internal/logging/AndroidHandler.java;hb=c2ad241504fcaa12d4579d3b0b4038d1ca8d08c9
29   */
30  public class FixedAndroidLogHandler extends Handler {
31      /**
32       * Holds the formatter for all Android log handlers.
33       */
34      private static final Formatter THE_FORMATTER = new Formatter() {
35          @Override
36          public String format(LogRecord r) {
37              Throwable thrown = r.getThrown();
38              if (thrown != null) {
39                  StringWriter sw = new StringWriter();
40                  PrintWriter pw = new PrintWriter(sw);
41                  sw.write(r.getMessage());
42                  sw.write("\n");
43                  thrown.printStackTrace(pw);
44                  pw.flush();
45                  return sw.toString();
46              } else {
47                  return r.getMessage();
48              }
49          }
50      };
51  
52      /**
53       * Constructs a new instance of the Android log handler.
54       */
55      public FixedAndroidLogHandler() {
56          setFormatter(THE_FORMATTER);
57      }
58  
59      @Override
60      public void close() {
61          // No need to close, but must implement abstract method.
62      }
63  
64      @Override
65      public void flush() {
66          // No need to flush, but must implement abstract method.
67      }
68  
69      @Override
70      public void publish(LogRecord record) {
71          try {
72              int level = getAndroidLevel(record.getLevel());
73              String tag = record.getLoggerName();
74  
75              if (tag == null) {
76                  // Anonymous logger.
77                  tag = "null";
78              } else {
79                  // Tags must be <= 23 characters.
80                  int length = tag.length();
81                  if (length > 23) {
82                      // Most loggers use the full class name. Try dropping the
83                      // package.
84                      int lastPeriod = tag.lastIndexOf(".");
85                      if (length - lastPeriod - 1 <= 23) {
86                          tag = tag.substring(lastPeriod + 1);
87                      } else {
88                          // Use last 23 chars.
89                          tag = tag.substring(tag.length() - 23);
90                      }
91                  }
92              }
93  
94              /* ############################################################################################
95  
96              Instead of using the perfectly fine java.util.logging API for setting the
97              loggable levels, this call relies on a totally obscure "local.prop" file which you have to place on
98              your device. By default, if you do not have that file and if you do not execute some magic
99              "setprop" commands on your device, only INFO/WARN/ERROR is loggable. So whatever you do with
100             java.util.logging.Logger.setLevel(...) doesn't have any effect. The debug messages might arrive
101             here but they are dropped because you _also_ have to set the Android internal logging level with
102             the aforementioned magic switches.
103 
104             Also, consider that you have to understand how a JUL logger name is mapped to the "tag" of
105             the Android log. Basically, the whole cutting and cropping procedure above is what you have to
106             memorize if you want to log with JUL and configure Android for debug output.
107 
108             I actually admire the pure evil of this setup, even Mr. Ceki can learn something!
109 
110             Commenting out these lines makes it all work as expected:
111 
112             if (!Log.isLoggable(tag, level)) {
113                 return;
114             }
115 
116             ############################################################################################### */
117 
118             String message = getFormatter().format(record);
119             Log.println(level, tag, message);
120         } catch (RuntimeException e) {
121             Log.e("AndroidHandler", "Error logging message.", e);
122         }
123     }
124 
125     /**
126      * Converts a {@link java.util.logging.Logger} logging level into an Android one.
127      *
128      * @param level The {@link java.util.logging.Logger} logging level.
129      *
130      * @return The resulting Android logging level.
131      */
132     static int getAndroidLevel(Level level) {
133         int value = level.intValue();
134         if (value >= 1000) { // SEVERE
135             return Log.ERROR;
136         } else if (value >= 900) { // WARNING
137             return Log.WARN;
138         } else if (value >= 800) { // INFO
139             return Log.INFO;
140         } else {
141             return Log.DEBUG;
142         }
143     }
144 
145 }