root/trunk/src/core/org/pathvisio/debug/Logger.java @ 2979

Revision 2979, 3.5 KB (checked in by martijn, 9 months ago)

For the sake of uniformity, removed all trailing whitespace in Java code

  • Property svn:eol-style set to native
Line 
1// PathVisio,
2// a tool for data visualization and analysis using Biological Pathways
3// Copyright 2006-2009 BiGCaT Bioinformatics
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17package org.pathvisio.debug;
18
19import java.io.File;
20import java.io.FileNotFoundException;
21import java.io.PrintStream;
22
23/**
24        Logs output to a stream, with the option to filter for types of messages
25
26        log levels:
27                1: Trace
28                2: debug
29                3: info
30                4: warn
31                5: error
32                6: fatal
33*/
34public class Logger
35{
36        private boolean debugEnabled = true;
37        private boolean traceEnabled = false;
38        private boolean infoEnabled = true;
39        private boolean warnEnabled = true;
40        private boolean errorEnabled = true;
41        private boolean fatalEnabled = true;
42
43        private PrintStream s = System.err;
44
45        public PrintStream getStream () { return s; }
46        public void setStream (PrintStream aStream) { s = aStream; }
47
48        /**
49         * if dest is "STDERR" or "STDOUT", the
50         * standard error / output are used.
51         * Otherwise, dest is interpreted as a filename
52         */
53        public void setDest(String dest)
54        {
55                if (dest.equals ("STDERR"))
56                {
57                        s = System.err;
58                }
59                else if (dest.equals("STDOUT"))
60                {
61                        s = System.out;
62                }
63                else
64                {
65                        try
66                        {
67                                s = new PrintStream (new File (dest));
68                        }
69                        catch (FileNotFoundException e)
70                        {
71                                s = System.err;
72                                error ("Could not open log file " + dest + " for writing", e);
73                        }
74                }
75
76        }
77
78        StopWatch logTimer;
79
80        public Logger()
81        {
82                logTimer = new StopWatch();
83                logTimer.start();
84        }
85
86        /**
87                get/set log level to a certain level. The higher the level, the
88                move output. Messages above this level are discarded.
89        */
90
91        public void setLogLevel (boolean debug, boolean trace, boolean info,
92                boolean warn, boolean error, boolean fatal)
93        {
94                debugEnabled = debug;
95                traceEnabled = trace;
96                infoEnabled = info;
97                warnEnabled = warn;
98                errorEnabled = error;
99                fatalEnabled = fatal;
100        }
101
102        private static final String FORMAT_STRING = "[%10.3f] ";
103
104        public void trace (String msg)
105        {
106                if (traceEnabled)
107                {
108                        s.printf (FORMAT_STRING , logTimer.look() / 1000.0f);
109                        s.println ("Trace: " + msg);
110                }
111        }
112
113        public void debug (String msg)
114        {
115                if (debugEnabled)
116                {
117                        s.printf (FORMAT_STRING , logTimer.look() / 1000.0f);
118                        s.println ("Debug: " + msg);
119                }
120        }
121
122        public void info  (String msg)
123        {
124                if (infoEnabled)
125                {
126                        s.printf (FORMAT_STRING , logTimer.look() / 1000.0f);
127                        s.println ("Info:  " + msg);
128                }
129        }
130
131        public void warn  (String msg) { if (warnEnabled) s.println ("Warn:  " + msg); }
132
133        public void warn  (String msg, Throwable e)
134        {
135                if (warnEnabled) {      s.println ("Warn:  " + msg + "\n\t" + e.getMessage());  }
136                if (debugEnabled) {     e.printStackTrace(s); }
137        }
138
139        public void error (String msg) { if (errorEnabled) s.println ("Error: " + msg); }
140        public void error (String msg, Throwable e)
141        {
142                if(errorEnabled) { error(msg + "\n\t" + e.toString() + (e != null ? ": " + e.getMessage() : "")); }
143                if(debugEnabled) { e.printStackTrace(s); }
144        }
145        public void fatal (String msg) { if (fatalEnabled) s.println ("Fatal: " + msg); }
146
147
148        /**
149           Global application logger
150         */
151        public static Logger log = new Logger();
152}
Note: See TracBrowser for help on using the browser.