1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
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.ide.eclipse.adt;
18 
19 import org.eclipse.core.resources.IProject;
20 
21 import java.io.OutputStream;
22 import java.io.PrintStream;
23 import java.util.Calendar;
24 
25 /**
26  * Custom PrintStream allowing to precede the message with a tag containing data/project info.
27  *
28  * Additionally, a prefix can be set (and removed) at runtime.
29  *
30  * Only {@link #println()} is supported.
31  */
32 public class AndroidPrintStream extends PrintStream {
33     private IProject mProject;
34     private String mPrefix;
35 
36     /**
37      * Default constructor with project and output stream.
38      * The project is used to get the project name for the output tag.
39      *
40      * @param project The Project
41      * @param prefix A prefix to be printed before the actual message. Can be null
42      * @param stream The Stream
43      */
AndroidPrintStream(IProject project, String prefix, OutputStream stream)44     public AndroidPrintStream(IProject project, String prefix, OutputStream stream) {
45         super(stream);
46         mProject = project;
47     }
48 
49     /**
50      * Updates the value of the prefix.
51      * @param prefix
52      */
setPrefix(String prefix)53     public void setPrefix(String prefix) {
54         mPrefix = prefix;
55     }
56 
57     @Override
println(String message)58     public void println(String message) {
59         // write the date/project tag first.
60         String tag = getMessageTag(mProject != null ? mProject.getName() : null);
61 
62         print(tag);
63         print(" "); //$NON-NLS-1$
64         if (mPrefix != null) {
65             print(mPrefix);
66             print(" "); //$NON-NLS-1$
67         }
68 
69         // then write the regular message
70         super.println(message);
71     }
72 
73     /**
74      * Creates a string containing the current date/time, and the tag.
75      * The tag does not end with a whitespace.
76      * @param tag The tag associated to the message. Can be null
77      * @return The dateTag
78      */
getMessageTag(String tag)79     public static String getMessageTag(String tag) {
80         Calendar c = Calendar.getInstance();
81 
82         if (tag == null) {
83             return String.format(Messages.Console_Date_Tag, c);
84         }
85 
86         return String.format(Messages.Console_Data_Project_Tag, c, tag);
87     }
88 
89 }
90