1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: DataPolice.java 2221 2012-01-25 21:32:57Z dmsmith $
21   */
22  package org.crosswire.jsword.book;
23  
24  import java.util.logging.Level;
25  
26  import org.crosswire.common.util.Logger;
27  import org.crosswire.jsword.passage.Key;
28  
29  /**
30   * When we can't convert some source data then the user doesn't really care and
31   * just wants it to work, but it would be good to have some way to get the
32   * problems fixed, so as a start point we report them through this class.
33   * 
34   * @see gnu.lgpl.License for license details.<br>
35   *      The copyright to this program is held by it's authors.
36   * @author Joe Walker [joe at eireneh dot com]
37   */
38  public final class DataPolice {
39      /**
40       * Prevent instantiation
41       */
42      private DataPolice() {
43      }
44  
45      /**
46       * Set the current level at which to report problems. Problems at too fine
47       * grain a level might be filtered by default.
48       * 
49       * @param lev
50       *            the level to set
51       */
52      public static void setReportingLevel(Level lev) {
53          DataPolice.level = lev;
54      }
55  
56      /**
57       * Set the level at which to show problems.
58       * 
59       * @param lev
60       *            the level to set
61       */
62      public static void setLevel(Level lev) {
63          log.setLevel(lev);
64      }
65  
66      /**
67       * Report a message against the current book and key.
68       * 
69       * @param lev
70       *            the level at which to report
71       * @param message
72       *            the police report.
73       */
74      public static void report(Book book, Key key, Level lev, String message) {
75          StringBuilder buf = new StringBuilder();
76          BookMetaData bmd = book.getBookMetaData();
77          if (bmd != null) {
78              buf.append(bmd.getInitials());
79          }
80          if (bmd != null && key != null) {
81              buf.append(':');
82              log.debug(bmd.getInitials() + ':' + key.getName());
83          }
84          if (key != null) {
85              buf.append(key.getName());
86          }
87          buf.append(": ");
88          buf.append(message);
89          log.log(lev, buf.toString());
90      }
91  
92      /**
93       * Report a message against the current book and key.
94       * 
95       * @param message
96       *            the police report.
97       */
98      public static void report(Book book, Key key, String message) {
99          report(book, key, level, message);
100     }
101 
102     /**
103      * The level at which to do logging. Default is FINE.
104      */
105     private static Level level = Level.FINE;
106 
107     /**
108      * The log stream
109      */
110     private static final Logger log = Logger.getLogger(DataPolice.class, false);
111 }
112