*** empty log message ***
[IRC.git] / Robust / Transactions / jcarderdstm2version / src / com / enea / jcarder / util / logging / AppendableHandler.java
1 /*
2  * JCarder -- cards Java programs to keep threads disentangled
3  *
4  * Copyright (C) 2006-2007 Enea AB
5  * Copyright (C) 2007 Ulrik Svensson
6  * Copyright (C) 2007 Joel Rosdahl
7  *
8  * This program is made available under the GNU GPL version 2, with a special
9  * exception for linking with JUnit. See the accompanying file LICENSE.txt for
10  * details.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.
15  */
16
17 package com.enea.jcarder.util.logging;
18
19 import java.io.IOException;
20
21 import com.enea.jcarder.util.logging.Logger.Level;
22
23 /**
24  * This class acts as a log handler for classes that that implement the
25  * Appendable interface.
26  *
27  * The Appendable does not need to be thread-safe; AppendableHandler
28  * synchronizes calls to Appendable's methods.
29  */
30 public class AppendableHandler implements Handler {
31     private final Appendable mDestination;
32     private final Level mLevel;
33     private final String mMessageFormat;
34
35     /**
36      * Constructor.
37      *
38      * The default log level is FINEST and the default message format is
39      * "{level}: {message}\n"
40      *
41      * @param destination
42      *            Destination of the log messages.
43      */
44     public AppendableHandler(Appendable destination) {
45         this(destination, Logger.Level.FINEST);
46     }
47
48     /**
49      * Constructor.
50      *
51      * The default message format is "{level}: {message}\n"
52      *
53      * @param destination
54      *            Destination of the log messages.
55      * @param logLevel
56      *            Log level.
57      */
58     public AppendableHandler(Appendable destination, Logger.Level logLevel) {
59         this(destination, logLevel, "{level}: {message}\n");
60     }
61
62     /**
63      * Constructor.
64      *
65      * Substrings like {keyword} are expanded in the message format string.
66      *
67      * Currently supported keywords:
68      *
69      * - {message} -- the message
70      * - {level} -- the log level
71      *
72      * @param destination Destination of the log messages.
73      * @param logLevel Log level.
74      * @param messageFormat Message format.
75      */
76     public AppendableHandler(Appendable destination,
77                              Logger.Level logLevel,
78                              String messageFormat) {
79         mDestination = destination;
80         mLevel = logLevel;
81         mMessageFormat = messageFormat;
82     }
83
84
85     public void publish(Level level, String message) {
86         if (level.compareTo(mLevel) <= 0) {
87             try {
88                 String formattedMessage = mMessageFormat
89                     .replace("{level}", level.toString())
90                     .replace("{message}", message);
91                 synchronized (mDestination) {
92                     mDestination.append(formattedMessage);
93                 }
94             } catch (IOException e) {
95                 // Ignore.
96             }
97         }
98     }
99 }