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