package jetbrains.mps.tool.environment;
/*Generated by MPS */
import java.io.File;
import jetbrains.mps.core.tool.environment.util.PathManager;
import jetbrains.mps.util.FileUtil;
import jetbrains.mps.util.StringUtil;
import org.apache.log4j.xml.DOMConfigurator;
import java.io.StringReader;
import org.apache.log4j.LogManager;
/**
* need this class to read log.xml configuration file even when there is no idea platform
*/
public final class Log4jInitializer {
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$";
public void init() {
System.setProperty("log4j.defaultInitOverride", "true");
File logXmlFile = new File(PathManager.getHomePath() + "/bin/log.xml");
if (!(logXmlFile.exists())) {
System.err.println("log.xml file does not exist! Path: [$home/bin/log.xml]");
return;
}
String text = FileUtil.read(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(PathManager.getLogPath(), "\\", "\\\\"));
File file = new File(PathManager.getLogPath());
if (!(file.mkdirs()) && !(file.exists())) {
System.err.println("Cannot create log directory: " + file);
}
new DOMConfigurator().doConfigure(new StringReader(text), LogManager.getLoggerRepository());
LogManager.getLogger("").info("Log4j has been initialized successfully");
}
}