/src/cpw/mods/fml/relauncher/FMLRelaunchLog.java

https://bitbucket.org/agaricusb/mcpc-1.4.5 · Java · 228 lines · 187 code · 28 blank · 13 comment · 6 complexity · 0224189df983aefc7f8d6c5eeb831005 MD5 · raw file

  1. package cpw.mods.fml.relauncher;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.PrintStream;
  6. import java.util.concurrent.Executors;
  7. import java.util.concurrent.LinkedBlockingQueue;
  8. import java.util.logging.ConsoleHandler;
  9. import java.util.logging.FileHandler;
  10. import java.util.logging.Handler;
  11. import java.util.logging.Level;
  12. import java.util.logging.LogManager;
  13. import java.util.logging.LogRecord;
  14. import java.util.logging.Logger;
  15. public class FMLRelaunchLog
  16. {
  17. private static class ConsoleLogWrapper extends Handler
  18. {
  19. @Override
  20. public void publish(LogRecord record)
  21. {
  22. boolean currInt = Thread.interrupted();
  23. try
  24. {
  25. ConsoleLogThread.recordQueue.put(record);
  26. }
  27. catch (InterruptedException e)
  28. {
  29. e.printStackTrace(errCache);
  30. }
  31. if (currInt)
  32. {
  33. Thread.currentThread().interrupt();
  34. }
  35. }
  36. @Override
  37. public void flush()
  38. {
  39. }
  40. @Override
  41. public void close() throws SecurityException
  42. {
  43. }
  44. }
  45. private static class ConsoleLogThread implements Runnable
  46. {
  47. static ConsoleHandler wrappedHandler = new ConsoleHandler();
  48. static LinkedBlockingQueue<LogRecord> recordQueue = new LinkedBlockingQueue<LogRecord>();
  49. @Override
  50. public void run()
  51. {
  52. do
  53. {
  54. LogRecord lr;
  55. try
  56. {
  57. lr = recordQueue.take();
  58. wrappedHandler.publish(lr);
  59. }
  60. catch (InterruptedException e)
  61. {
  62. e.printStackTrace(errCache);
  63. Thread.interrupted();
  64. // Stupid
  65. }
  66. }
  67. while (true);
  68. }
  69. }
  70. private static class LoggingOutStream extends ByteArrayOutputStream
  71. {
  72. private Logger log;
  73. private StringBuilder currentMessage;
  74. public LoggingOutStream(Logger log)
  75. {
  76. this.log = log;
  77. this.currentMessage = new StringBuilder();
  78. }
  79. @Override
  80. public void flush() throws IOException
  81. {
  82. String record;
  83. synchronized(FMLRelaunchLog.class)
  84. {
  85. super.flush();
  86. record = this.toString();
  87. super.reset();
  88. currentMessage.append(record.replace(FMLLogFormatter.LINE_SEPARATOR, "\n"));
  89. if (currentMessage.lastIndexOf("\n")>=0)
  90. {
  91. // Are we longer than just the line separator?
  92. if (currentMessage.length()>1)
  93. {
  94. // Trim the line separator
  95. currentMessage.setLength(currentMessage.length()-1);
  96. log.log(Level.INFO, currentMessage.toString());
  97. }
  98. currentMessage.setLength(0);
  99. }
  100. }
  101. }
  102. }
  103. /**
  104. * Our special logger for logging issues to. We copy various assets from the
  105. * Minecraft logger to acheive a similar appearance.
  106. */
  107. public static FMLRelaunchLog log = new FMLRelaunchLog();
  108. static File minecraftHome;
  109. private static boolean configured;
  110. private static Thread consoleLogThread;
  111. private static PrintStream errCache;
  112. private Logger myLog;
  113. private FMLRelaunchLog()
  114. {
  115. }
  116. /**
  117. * Configure the FML logger
  118. */
  119. private static void configureLogging()
  120. {
  121. LogManager.getLogManager().reset();
  122. Logger globalLogger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
  123. globalLogger.setLevel(Level.OFF);
  124. log.myLog = Logger.getLogger("ForgeModLoader");
  125. Logger stdOut = Logger.getLogger("STDOUT");
  126. stdOut.setParent(log.myLog);
  127. Logger stdErr = Logger.getLogger("STDERR");
  128. stdErr.setParent(log.myLog);
  129. FMLLogFormatter formatter = new FMLLogFormatter();
  130. // Console handler captures the normal stderr before it gets replaced
  131. log.myLog.setUseParentHandlers(false);
  132. log.myLog.addHandler(new ConsoleLogWrapper());
  133. consoleLogThread = new Thread(new ConsoleLogThread());
  134. consoleLogThread.start();
  135. ConsoleLogThread.wrappedHandler.setLevel(Level.parse(System.getProperty("fml.log.level","INFO")));
  136. ConsoleLogThread.wrappedHandler.setFormatter(formatter);
  137. log.myLog.setLevel(Level.ALL);
  138. try
  139. {
  140. File logPath = new File(minecraftHome, FMLRelauncher.logFileNamePattern);
  141. FileHandler fileHandler = new FileHandler(logPath.getPath(), 0, 3);
  142. fileHandler.setFormatter(formatter);
  143. fileHandler.setLevel(Level.ALL);
  144. log.myLog.addHandler(fileHandler);
  145. }
  146. catch (Exception e)
  147. {
  148. }
  149. // Set system out to a log stream
  150. errCache = System.err;
  151. System.setOut(new PrintStream(new LoggingOutStream(stdOut), true));
  152. System.setErr(new PrintStream(new LoggingOutStream(stdErr), true));
  153. // Reset global logging to shut up other logging sources (thanks guava!)
  154. configured = true;
  155. }
  156. public static void log(Level level, String format, Object... data)
  157. {
  158. if (!configured)
  159. {
  160. configureLogging();
  161. }
  162. log.myLog.log(level, String.format(format, data));
  163. }
  164. public static void log(Level level, Throwable ex, String format, Object... data)
  165. {
  166. if (!configured)
  167. {
  168. configureLogging();
  169. }
  170. log.myLog.log(level, String.format(format, data), ex);
  171. }
  172. public static void severe(String format, Object... data)
  173. {
  174. log(Level.SEVERE, format, data);
  175. }
  176. public static void warning(String format, Object... data)
  177. {
  178. log(Level.WARNING, format, data);
  179. }
  180. public static void info(String format, Object... data)
  181. {
  182. log(Level.INFO, format, data);
  183. }
  184. public static void fine(String format, Object... data)
  185. {
  186. log(Level.FINE, format, data);
  187. }
  188. public static void finer(String format, Object... data)
  189. {
  190. log(Level.FINER, format, data);
  191. }
  192. public static void finest(String format, Object... data)
  193. {
  194. log(Level.FINEST, format, data);
  195. }
  196. public Logger getLogger()
  197. {
  198. return myLog;
  199. }
  200. }