1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package stats;
28 
29 import share.LogWriter;
30 
31 import java.io.PrintWriter;
32 import java.text.DecimalFormat;
33 import java.util.Calendar;
34 import java.util.GregorianCalendar;
35 
36 public class SimpleLogWriter extends PrintWriter implements LogWriter {
37 
38     boolean m_bLogging = false;
39     share.DescEntry entry = null;
40     share.Watcher ow = null;
41 
42     public SimpleLogWriter() {
43         super(System.out);
44         Calendar cal = new GregorianCalendar();
45         DecimalFormat dfmt = new DecimalFormat("00");
46         super.println("LOG> Log started " +
47             dfmt.format(cal.get(Calendar.DAY_OF_MONTH)) + "." +
48             dfmt.format(cal.get(Calendar.MONTH)) + "." +
49             dfmt.format(cal.get(Calendar.YEAR)) + " - " +
50             dfmt.format(cal.get(Calendar.HOUR_OF_DAY)) + ":" +
51             dfmt.format(cal.get(Calendar.MINUTE)) + ":" +
52             dfmt.format(cal.get(Calendar.SECOND)));
53         super.flush();
54     }
55 
56     public boolean initialize(share.DescEntry _entry, boolean _bLogging) {
57         m_bLogging = _bLogging;
58         entry = _entry;
59 
60         return true;
61     }
62 
63     public void println(String msg) {
64         if ((ow == null) && (entry != null))
65         {
66             this.ow = (share.Watcher) entry.UserDefinedParams.get("Watcher");
67             if (this.ow != null)
68             {
69                 this.ow.ping();
70             }
71         }
72         else
73         {
74             if (ow != null)
75             {
76                 this.ow.ping();
77             }
78             else
79             {
80                 // special case: ow == null && entry == null
81                 System.out.println(msg);
82             }
83         }
84 
85         if (m_bLogging) {
86             super.println("LOG> " + msg);
87             super.flush();
88         }
89     // else
90     // {
91     //     super.println(" ++ " + msg);
92     //     super.flush();
93     // }
94     }
95 
96     public boolean summary(share.DescEntry entry) {
97         return true;
98     }
99 
100     public Object getWatcher() {
101         return this.ow;
102     }
103 
104     public void setWatcher(Object watcher)
105     {
106         if (watcher != null)
107         {
108             entry.UserDefinedParams.put("Watcher", (share.Watcher) watcher);
109         }
110     }
111 }
112