/*
 * 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.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.testFramework;

import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.xml.DOMConfigurator;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressWarnings({"CallToPrintStackTrace", "UseOfSystemOutOrSystemErr"})
public class TestLoggerFactory implements Logger.Factory {
  private static final String SYSTEM_MACRO = "$SYSTEM_DIR$";
  private static final String APPLICATION_MACRO = "$APPLICATION_DIR$";
  private static final String LOG_DIR_MACRO = "$LOG_DIR$";
  private static final String LOG_DIR = "testlog";

  private boolean myInitialized = false;

  private TestLoggerFactory() { }

  @Override
  public synchronized Logger getLoggerInstance(final String name) {
    if (!myInitialized) {
      init();
    }

    return new TestLogger(org.apache.log4j.Logger.getLogger(name));
  }

  private void init() {
    try {
      File logXmlFile = new File(PathManager.getHomePath(), "test-log.xml");
      if (!logXmlFile.exists()) {
        logXmlFile = new File(PathManager.getBinPath(), "log.xml");
      }
      if (!logXmlFile.exists()) {
        return;
      }

      final String logDir = getTestLogDir();
      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, LOG_DIR_MACRO, StringUtil.replace(logDir, "\\", "\\\\"));

      final File logDirFile = new File(logDir);
      if (!logDirFile.mkdirs() && !logDirFile.exists()) {
        throw new IOException("Unable to create log dir: " + logDirFile);
      }

      System.setProperty("log4j.defaultInitOverride", "true");
      final DOMConfigurator domConfigurator = new DOMConfigurator();
      try {
        domConfigurator.doConfigure(new StringReader(text), LogManager.getLoggerRepository());
      }
      catch (ClassCastException e) {
        // shit :-E
        System.err.println("log.xml content:\n" + text);
        throw e;
      }

      myInitialized = true;
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static String getTestLogDir() {
    return PathManager.getSystemPath() + "/" + LOG_DIR;
  }

  public static void dumpLogToStdout(@NotNull String testStartMarker) throws IOException {
    File ideaLog = new File(getTestLogDir(), "idea.log");
    if (ideaLog.exists()) {
      String logText = FileUtil.loadFile(ideaLog);
      Pattern logStart = Pattern.compile("[0-9\\-, :\\[\\]]+(DEBUG|INFO|ERROR) - ");
      System.out.println("\n\nIdea Log:");
      for (String line : StringUtil.splitByLines(logText.substring(Math.max(0, logText.lastIndexOf(testStartMarker))))) {
        Matcher matcher = logStart.matcher(line);
        int lineStart = matcher.lookingAt() ? matcher.end() : 0;
        System.out.println(line.substring(lineStart));
      }
    }
  }

}
@@ -631,16 +631,56 @@
 ework;%0A%0A
+import com.intellij.openapi.Disposable;%0A
 import c
@@ -720,24 +720,24 @@
 athManager;%0A
-
 import com.i
@@ -767,24 +767,67 @@
 tic.Logger;%0A
+import com.intellij.openapi.util.Disposer;%0A
 import com.i
@@ -856,24 +856,24 @@
 o.FileUtil;%0A
-
 import com.i
@@ -906,24 +906,55 @@
 StringUtil;%0A
+import org.apache.log4j.Level;%0A
 import org.a
@@ -4012,9 +4012,426 @@
  %7D%0A  %7D%0A%0A
+  public static void enableDebugLogging(@NotNull Disposable parentDisposable, String... categories) %7B%0A    for (String category : categories) %7B%0A      final Logger logger = Logger.getInstance(category);%0A      logger.setLevel(Level.DEBUG);%0A      Disposer.register(parentDisposable, new Disposable() %7B%0A        @Override%0A        public void dispose() %7B%0A          logger.setLevel(Level.INFO);%0A        %7D%0A      %7D);%0A    %7D%0A  %7D%0A
 %7D

Move enable debug logging for certain categories to a common place