From cae534e044a2fac6656452d9cdb430d04fb9f318 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Sat, 9 Jun 2012 16:20:06 +0400 Subject: [PATCH] Cleanup --- bin/log.xml | 19 ++--- .../src/com/intellij/idea/LoggerFactory.java | 85 +++++-------------- 2 files changed, 26 insertions(+), 78 deletions(-) diff --git a/bin/log.xml b/bin/log.xml index a85a744b1c38..d8fca779f651 100644 --- a/bin/log.xml +++ b/bin/log.xml @@ -1,6 +1,6 @@ - + @@ -29,9 +29,9 @@ - - - + + + @@ -52,19 +52,10 @@ - - - + diff --git a/platform/platform-impl/src/com/intellij/idea/LoggerFactory.java b/platform/platform-impl/src/com/intellij/idea/LoggerFactory.java index 11d6a1843479..d7a8cc8ce70d 100644 --- a/platform/platform-impl/src/com/intellij/idea/LoggerFactory.java +++ b/platform/platform-impl/src/com/intellij/idea/LoggerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,53 +25,42 @@ import org.apache.log4j.xml.DOMConfigurator; import java.io.File; import java.io.StringReader; -@SuppressWarnings({"HardCodedStringLiteral"}) +@SuppressWarnings({"CallToPrintStackTrace", "UseOfSystemOutOrSystemErr"}) public class LoggerFactory implements Logger.Factory { private static final String SYSTEM_MACRO = "$SYSTEM_DIR$"; private static final String APPLICATION_MACRO = "$APPLICATION_DIR$"; - private static final String COMMENT_LINE_FOR_TEST_MODE_MACRO = "$COMMENT_LINE_FOR_TEST_MODE$"; - private static final String LOGDIR_MACRO = "$LOG_DIR$"; + private static final String LOG_DIR_MACRO = "$LOG_DIR$"; private boolean myInitialized = false; private static final LoggerFactory ourInstance = new LoggerFactory(); - public static final String LOG_DIR = "log"; public static LoggerFactory getInstance() { return ourInstance; } - private LoggerFactory() { - } + private LoggerFactory() { } - public Logger getLoggerInstance(String name) { - synchronized (this) { - try { - if (!isInitialized()) { - init(); - } + @Override + public synchronized Logger getLoggerInstance(String name) { + try { + if (!myInitialized) { + init(); } - catch (Exception e) { - e.printStackTrace(); - } - - return new IdeaLogger(org.apache.log4j.Logger.getLogger(name)); } + catch (Exception e) { + e.printStackTrace(); + } + + return new IdeaLogger(org.apache.log4j.Logger.getLogger(name)); } private void init() { try { - /* - //debug code. Don't delete. - ClassLoader classLoader = Logger.class.getClassLoader(); - if (!(classLoader.getClass().getName().startsWith("com.intellij"))) { - System.err.println("Logger shouldn't be used outside the PluginManager"); - Thread.dumpStack(); - } - */ - System.setProperty("log4j.defaultInitOverride", "true"); - File logXmlFile = FileUtil.findFirstThatExist(PathManager.getHomePath() + "/bin/log.xml", PathManager.getHomePath() + "/community/bin/log.xml"); + + File logXmlFile = FileUtil.findFirstThatExist(PathManager.getHomePath() + "/bin/log.xml", + PathManager.getHomePath() + "/community/bin/log.xml"); if (logXmlFile == null) { throw new RuntimeException("log.xml file does not exist! Path: [ $home/bin/log.xml]"); } @@ -79,14 +68,12 @@ public class LoggerFactory implements Logger.Factory { String text = FileUtil.loadFile(logXmlFile); text = StringUtil.replace(text, SYSTEM_MACRO, StringUtil.replace(PathManager.getSystemPath(), "\\", "\\\\")); text = StringUtil.replace(text, APPLICATION_MACRO, StringUtil.replace(PathManager.getHomePath(), "\\", "\\\\")); - text = StringUtil.replace(text, LOGDIR_MACRO, StringUtil.replace(PathManager.getLogPath(), "\\", "\\\\")); - - if ("true".equals(System.getProperty("idea.test.test_mode"))) { - text = commentTestModeLines(text); - } + text = StringUtil.replace(text, LOG_DIR_MACRO, StringUtil.replace(PathManager.getLogPath(), "\\", "\\\\")); File file = new File(PathManager.getLogPath()); - file.mkdirs(); + if (!file.mkdirs() && !file.exists()) { + System.err.println("Cannot create log directory: " + file); + } new DOMConfigurator().doConfigure(new StringReader(text), LogManager.getLoggerRepository()); @@ -96,34 +83,4 @@ public class LoggerFactory implements Logger.Factory { e.printStackTrace(); } } - - private boolean isInitialized() { - return myInitialized; - } - - private static String commentTestModeLines(String text) { - String result = text; - int index = text.indexOf(COMMENT_LINE_FOR_TEST_MODE_MACRO); - if (index != -1) { - String str1 = result.substring(0, index); - String str2 = result.substring(index); - int firstLineChar = Math.max(str1.lastIndexOf('\n'), str1.lastIndexOf('\r')); - int lastLineChar = str2.indexOf('\n'); - int lastLineChar2 = str2.indexOf('\r'); - if (lastLineChar == -1) { - lastLineChar = lastLineChar2; - } - else if (lastLineChar2 != -1) { - lastLineChar = Math.min(lastLineChar, lastLineChar2); - } - if (firstLineChar != -1) { - str1 = str1.substring(0, firstLineChar); - } - if (lastLineChar != -1) { - str2 = str2.substring(lastLineChar); - } - result = commentTestModeLines(str1) + commentTestModeLines(str2); - } - return result; - } }