• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 com.android.monkeyrunner;
17 
18 import com.google.common.collect.Maps;
19 
20 import java.io.ByteArrayOutputStream;
21 import java.io.PrintWriter;
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24 import java.util.Map;
25 import java.util.logging.Formatter;
26 import java.util.logging.Level;
27 import java.util.logging.LogRecord;
28 
29 /*
30  * Custom Logging Formatter for MonkeyRunner that generates all log
31  * messages on a single line.
32  */
33 public class MonkeyFormatter extends Formatter {
34     public static final Formatter DEFAULT_INSTANCE = new MonkeyFormatter();
35 
36     private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyMMdd HH:mm:ss.SSS");
37 
38     private static Map<Level, String> LEVEL_TO_STRING_CACHE = Maps.newHashMap();
39 
levelToString(Level level)40     private static final String levelToString(Level level) {
41         String levelName = LEVEL_TO_STRING_CACHE.get(level);
42         if (levelName == null) {
43             levelName = level.getName().substring(0, 1);
44             LEVEL_TO_STRING_CACHE.put(level, levelName);
45         }
46         return levelName;
47     }
48 
getHeader(LogRecord record)49     private static String getHeader(LogRecord record) {
50         StringBuilder sb = new StringBuilder();
51 
52         sb.append(FORMAT.format(new Date(record.getMillis()))).append(":");
53         sb.append(levelToString(record.getLevel())).append(" ");
54 
55         sb.append("[").append(Thread.currentThread().getName()).append("] ");
56 
57         String loggerName = record.getLoggerName();
58         if (loggerName != null) {
59             sb.append("[").append(loggerName).append("]");
60         }
61         return sb.toString();
62     }
63 
64     private class PrintWriterWithHeader extends PrintWriter {
65         private final ByteArrayOutputStream out;
66         private final String header;
67 
PrintWriterWithHeader(String header)68         public PrintWriterWithHeader(String header) {
69             this(header, new ByteArrayOutputStream());
70         }
71 
PrintWriterWithHeader(String header, ByteArrayOutputStream out)72         public PrintWriterWithHeader(String header, ByteArrayOutputStream out) {
73             super(out, true);
74             this.header = header;
75             this.out = out;
76         }
77 
78         @Override
println(Object x)79         public void println(Object x) {
80             print(header);
81             super.println(x);
82         }
83 
84         @Override
println(String x)85         public void println(String x) {
86             print(header);
87             super.println(x);
88         }
89 
90         @Override
toString()91         public String toString() {
92             return out.toString();
93         }
94     }
95 
96     @Override
format(LogRecord record)97     public String format(LogRecord record) {
98         Throwable thrown = record.getThrown();
99         String header = getHeader(record);
100 
101         StringBuilder sb = new StringBuilder();
102         sb.append(header);
103         sb.append(" ").append(formatMessage(record));
104         sb.append("\n");
105 
106         // Print the exception here if we caught it
107         if (thrown != null) {
108 
109             PrintWriter pw = new PrintWriterWithHeader(header);
110             thrown.printStackTrace(pw);
111             sb.append(pw.toString());
112         }
113 
114         return sb.toString();
115     }
116 }
117