Source code: com/lilacsoftware/orca/applets/Debug.java
1 package com.lilacsoftware.orca.applets;
2 import java.io.*;
3 import java.util.*;
4
5 /**
6 * This class implements a simple Debug Tool.
7 *
8 * @author E.B from JavaZOOM
9 * @date 07/10/2001
10 *
11 * Homepage : http://www.javazoom.net
12 *
13 *-----------------------------------------------------------------------
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 *----------------------------------------------------------------------
28 */
29
30 public class Debug
31 {
32 private static Debug _instance = null;
33 private PrintWriter _out = null;
34 private int _loglevel = 0;
35
36 private Debug()
37 {}
38
39 /**
40 * Returns Debug instance.
41 */
42 public synchronized static Debug getInstance()
43 {
44 if (_instance == null)
45 {
46 _instance = new Debug();
47 }
48 return _instance;
49 }
50
51 /**
52 * Inits log file.
53 */
54 public synchronized void init(String filename, int level)
55 {
56 _loglevel = level;
57
58 try
59 {
60 _out = new PrintWriter(new FileOutputStream(filename));
61 } catch (IOException ioe)
62 {
63 System.err.println("Cannot open log file : "+ioe.getMessage());
64 }
65 }
66
67 /**
68 * Closes log file.
69 */
70 public synchronized void close()
71 {
72 if (_out != null)
73 {
74 try
75 {
76 _out.flush();
77 _out.close();
78 } catch (Exception ioe)
79 {
80 System.err.println("Cannot close log file : "+ioe.getMessage());
81 }
82 }
83 }
84
85 /**
86 * Sets log level.
87 */
88 public void setLogLevel(int level)
89 {
90 _loglevel = level;
91 }
92
93 /**
94 * Sends message to log file.
95 */
96 public synchronized void log(int level, String msg)
97 {
98 if (_loglevel >= level)
99 {
100 if (_out != null)
101 {
102 _out.println(msg);
103 _out.flush();
104 }
105 else
106 {
107 System.out.println(msg);
108 }
109 }
110 }
111 }