summaryrefslogtreecommitdiff
path: root/java/src/Ice/LoggerI.java
blob: 6a75f702898b01d3ea0e0e78eddec0723bd3df15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// **********************************************************************
//
// Copyright (c) 2003-2012 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

package Ice;

public class LoggerI implements Logger
{
    public 
    LoggerI(String prefix, String file)
    {
        if(prefix.length() > 0)
        {
            _prefix = prefix + ": ";
        }

        _lineSeparator = System.getProperty("line.separator");
        _date = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT);
        _time = new java.text.SimpleDateFormat(" HH:mm:ss:SSS");

        if(file.length() != 0)
        {
            _file = file;
            try
            {
                _out = new java.io.FileOutputStream(new java.io.File(_file), true);
            }
            catch(java.io.FileNotFoundException ex)
            {
                throw new InitializationException("FileLogger: cannot open " + _file);
            }
        }
    }

    public void
    print(String message)
    {
        StringBuilder s = new StringBuilder(256);
        s.append(message);
        write(s, false);
    }

    public void
    trace(String category, String message)
    {
        StringBuilder s = new StringBuilder(256);
        s.append("-- ");
        s.append(_date.format(new java.util.Date()));
        s.append(_time.format(new java.util.Date()));
        s.append(' ');
        s.append(_prefix);
        s.append(category);
        s.append(": ");
        s.append(message);
        write(s, true);
    }

    public void
    warning(String message)
    {
        StringBuilder s = new StringBuilder(256);
        s.append("-! ");
        s.append(_date.format(new java.util.Date()));
        s.append(_time.format(new java.util.Date()));
        s.append(' ');
        s.append(_prefix);
        s.append("warning: ");
        s.append(Thread.currentThread().getName());
        s.append(": ");
        s.append(message);
        write(s, true);
    }

    public void
    error(String message)
    {
        StringBuilder s = new StringBuilder(256);
        s.append("!! ");
        s.append(_date.format(new java.util.Date()));
        s.append(_time.format(new java.util.Date()));
        s.append(' ');
        s.append(_prefix);
        s.append("error: ");
        s.append(Thread.currentThread().getName());
        s.append(": ");
        s.append(message);
        write(s, true);
    }

    public Logger
    cloneWithPrefix(String prefix)
    {
        return new LoggerI(prefix, _file);
    }

    private void
    write(StringBuilder message, boolean indent)
    {
        if(indent)
        {
            int idx = 0;
            while((idx = message.indexOf("\n", idx)) != -1)
            {
                message.insert(idx + 1, "   ");
                ++idx;
            }
        }
        message.append(_lineSeparator);

        if(_out == null)
        {
            System.err.print(message.toString());
        }
        else
        {
            try
            {
                _out.write(message.toString().getBytes());
            }
            catch(java.io.IOException ex)
            {
            }
        }
    }

    String _prefix = "";
    String _file = "";
    String _lineSeparator;
    java.text.DateFormat _date;
    java.text.SimpleDateFormat _time;
    java.io.FileOutputStream _out = null;
}