1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package installer;
23 import java.io.PrintStream;
24 import java.io.FileOutputStream;
25 
26 import java.util.Date;
27 import java.text.DateFormat;
28 import java.text.SimpleDateFormat;
29 
30 
31 //import java.io.PrintWriter;
32 public class LogStream extends PrintStream
33 {
34     static final private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z: ");
35 
getTimeStamp()36     private String getTimeStamp()
37     {
38          String timeStamp = formatter.format( new Date() );
39          return timeStamp;
40     }
LogStream( String logFileName )41     public LogStream( String logFileName ) throws java.io.FileNotFoundException
42     {
43         super( new FileOutputStream( logFileName ) );
44     }
println(String x)45     public  void println(String x)
46     {
47         super.println( getTimeStamp() + x );
48     }
main(String[] args)49     public static void main(String[] args)
50     {
51         if ( args.length > 0 )
52         {
53             try
54             {
55                 LogStream log = new LogStream( args[0] );
56                 System.setErr(log);
57                 System.setOut(log);
58                 System.out.println("Test from logger from out");
59                 System.err.println("Test from logger from err");
60                 System.out.println("finised test from out");
61                 System.err.println("finised test from err");
62             }
63             catch( java.io.FileNotFoundException fe )
64             {
65                 System.err.println("Error creating logStream: " + fe );
66                 fe.printStackTrace();
67             }
68         }
69         else
70         {
71             System.err.println("specify log file java LogStream [logfile]");
72             System.exit(1);
73         }
74     }
75 }
76