1 /*
2  ******************************************************************************
3  * Copyright (C) 2004, International Business Machines Corporation and        *
4  * others. All Rights Reserved.                                               *
5  ******************************************************************************
6  *
7  * in shell:  (such as .cldrrc)
8  *   export CWDEBUG="-DCLDR_DTD_CACHE=/tmp/cldrdtd/"
9  *   export CWDEFS="-DCLDR_DTD_CACHE_DEBUG=y ${CWDEBUG}"
10  *
11  *
12  * in code:
13  *   docBuilder.setEntityResolver(new CachingEntityResolver());
14  *
15  */
16 
17 package org.unicode.cldr.util;
18 
19 import java.io.IOException;
20 import java.io.PrintWriter;
21 import java.util.Locale;
22 
23 import org.unicode.cldr.draft.FileUtilities;
24 
25 public class Log {
26     static private PrintWriter log;
27 
logln(int test, String message)28     public static void logln(int test, String message) {
29         if (log != null && test != 0) log.println(message);
30     }
31 
logln(boolean test, String message)32     public static void logln(boolean test, String message) {
33         if (log != null && test) log.println(message);
34     }
35 
logln(Object message)36     public static void logln(Object message) {
37         if (log != null) log.println(message);
38     }
39 
40     /**
41      * @return Returns the log.
42      */
getLog()43     public static PrintWriter getLog() {
44         return log;
45     }
46 
47     /**
48      * @param newlog
49      *            The log to set.
50      */
setLog(PrintWriter newlog)51     public static void setLog(PrintWriter newlog) {
52         log = newlog;
53     }
54 
55     /**
56      */
close()57     public static void close() {
58         if (log != null) log.close();
59     }
60 
setLog(String dir, String file)61     public static void setLog(String dir, String file) throws IOException {
62         log = FileUtilities.openUTF8Writer(dir, file);
63         log.print('\uFEFF');
64     }
65 
setLog(String file)66     public static void setLog(String file) throws IOException {
67         log = FileUtilities.openUTF8Writer(null, file);
68         log.print('\uFEFF');
69     }
70 
setLogNoBOM(String file)71     public static void setLogNoBOM(String file) throws IOException {
72         log = FileUtilities.openUTF8Writer(null, file);
73     }
74 
setLogNoBOM(String dir, String file)75     public static void setLogNoBOM(String dir, String file) throws IOException {
76         log = FileUtilities.openUTF8Writer(dir, file);
77     }
78 
println()79     public static void println() {
80         log.println();
81     }
82 
println(String string)83     public static void println(String string) {
84         log.println(string);
85     }
86 
print(String string)87     public static void print(String string) {
88         log.print(string);
89     }
90 
91     /**
92      * format a line and print, in 80 character pieces. A bit dumb right now: doesn't handle strings.
93      *
94      * @param format
95      * @param args
96      */
formatln(String format, Object... args)97     public static void formatln(String format, Object... args) {
98         String value = String.format(Locale.ENGLISH, format, args);
99         if (value.length() <= 80) {
100             log.println(value);
101             return;
102         }
103         // if it is too long, see if there is a comment
104         int commentLocation = value.lastIndexOf("//");
105         String comment = "";
106         if (commentLocation > 0) {
107             comment = value.substring(commentLocation);
108             value = value.substring(0, commentLocation);
109         }
110         while (value.length() > 80) {
111             int lastSpace = value.lastIndexOf(' ', 80);
112             if (lastSpace == -1) {
113                 log.println(value);
114                 break;
115             }
116             log.println(value.substring(0, lastSpace));
117             value = value.substring(lastSpace);
118         }
119         if (value.length() + comment.length() < 79) {
120             log.println(value + " " + comment);
121             return;
122         }
123         log.println(value);
124         log.println("    " + comment);
125     }
126 }
127