• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.cts;
18 
19 import java.io.BufferedWriter;
20 import java.io.File;
21 import java.io.FileWriter;
22 import java.io.IOException;
23 import java.io.PrintStream;
24 
25 /**
26  * Utility class to help the CTS logging
27  */
28 public class Log {
29     private static final String INFO_PREFIX = "\nCTS_INFO >>> ";
30     private static final String ERROR_PREFIX = "\nCTS_ERROR >>> ";
31     private static final String DEBUG_PREFIX = "\nCTS_DEBUG >>> ";
32     private static final String LOG_FNAME_PREFIX = "log_";
33     private static final String LOG_FNAME_SURFIX = "_.txt";
34 
35     private static PrintStream mOut = System.err;
36 
37     private static boolean TRACE = true;
38     private static BufferedWriter mTraceOutput = null;
39 
40     private static boolean LOG = true;
41     private static BufferedWriter mLogOutput = null;
42     private static String mLogFileName;
43 
44     /**
45      * Print the message to the information stream without adding prefix.
46      *
47      * @param msg The message to be printed.
48      */
println(final String msg)49     public static void println(final String msg) {
50         log(INFO_PREFIX + msg);
51         mOut.println(msg);
52     }
53 
54     /**
55      * Add the message to the information stream.
56      *
57      * @param msg the message to be added to the information stream.
58      */
i(final String msg)59     public static void i(final String msg) {
60         log(INFO_PREFIX + msg);
61 
62         mOut.println(INFO_PREFIX + msg);
63     }
64 
65     /**
66      * Add the message to the error message stream.
67      * @param msg The message to be added to the error message stream.
68      * @param e The exception.
69      */
e(final String msg, Exception e)70     public static void e(final String msg, Exception e) {
71         log(ERROR_PREFIX + msg);
72 
73         if (!HostConfig.DEBUG) {
74             CUIOutputStream.println(ERROR_PREFIX + msg);
75             if (e != null) {
76                 CUIOutputStream.println(e.getMessage());
77             }
78             return;
79         }
80 
81         mOut.println(ERROR_PREFIX + msg);
82         if (e != null) {
83             e.printStackTrace();
84         }
85     }
86 
87     /**
88      * Add the message to the debugging stream.
89      *
90      * @param msg The message to be added to the debugging stream.
91      */
d(final String msg)92     public static void d(final String msg) {
93         log(DEBUG_PREFIX + System.currentTimeMillis() + " " + msg);
94 
95         if (HostConfig.DEBUG) {
96             mOut.println(DEBUG_PREFIX + msg);
97         }
98     }
99 
100     /**
101      * Set the output stream.
102      *
103      * @param out The output stream.
104      */
setOutput(PrintStream out)105     public static void setOutput(PrintStream out) {
106         if (out != null) {
107             mOut = out;
108         }
109     }
110 
111     /**
112      * Reset the output stream.
113      */
resetOutput()114     public static void resetOutput() {
115         mOut = System.out;
116     }
117 
118     /**
119      * Initialize the log stream.
120      *
121      * @param path The path to add the log file.
122      */
initLog(String path)123     public static void initLog(String path) {
124         mLogFileName = path + File.separator + LOG_FNAME_PREFIX
125             + HostUtils.getFormattedTimeString(System.currentTimeMillis(), "_", ".", ".")
126             + LOG_FNAME_SURFIX;
127         try {
128             if (mLogOutput == null) {
129                 mLogOutput = new BufferedWriter(new FileWriter(mLogFileName));
130             }
131         } catch (IOException e) {
132             e.printStackTrace();
133         }
134     }
135 
136     /**
137      * Close the log stream.
138      */
closeLog()139     public static void closeLog() {
140         if (mLogOutput != null) {
141             try {
142                 mLogOutput.close();
143                 mLogOutput = null;
144             } catch (IOException e) {
145                 e.printStackTrace();
146             }
147         }
148     }
149 
150     /**
151      * Log the message.
152      *
153      * @param msg The message to be logged.
154      */
log(String msg)155     public static void log(String msg) {
156         if (LOG && (mLogOutput != null)) {
157             try {
158                 if (msg != null) {
159                     mLogOutput.write(msg + "\n");
160                     mLogOutput.flush();
161                 }
162             } catch (IOException e) {
163                 e.printStackTrace();
164             }
165         }
166     }
167 
168     /**
169      * Add the message to the trace stream.
170      *
171      * @param msg The message to be added to the trace stream.
172      */
t(String msg)173     public static void t(String msg) {
174         if (TRACE) {
175             try {
176                 if (mTraceOutput == null) {
177                     mTraceOutput = new BufferedWriter(new FileWriter("debug.txt"));
178                 }
179                 if (msg != null) {
180                     mTraceOutput.write(msg + "\n");
181                     mTraceOutput.flush();
182                 }
183             } catch (IOException e) {
184                 e.printStackTrace();
185             }
186         }
187     }
188 
189     /**
190      * Close the trace stream.
191      */
closeTrace()192     public static void closeTrace() {
193         if (mTraceOutput != null) {
194             try {
195                 mTraceOutput.close();
196                 mTraceOutput = null;
197             } catch (IOException e) {
198                 e.printStackTrace();
199             }
200         }
201     }
202 }
203