2 * Copyright 2000-2013 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.idea;
18 import com.intellij.diagnostic.LogMessageEx;
19 import com.intellij.openapi.application.ApplicationManager;
20 import com.intellij.openapi.application.ex.ApplicationInfoEx;
21 import com.intellij.openapi.application.impl.ApplicationImpl;
22 import com.intellij.openapi.application.impl.ApplicationInfoImpl;
23 import com.intellij.openapi.command.CommandProcessor;
24 import com.intellij.openapi.diagnostic.ApplicationInfoProvider;
25 import com.intellij.openapi.diagnostic.Attachment;
26 import com.intellij.openapi.diagnostic.IdeaLoggingEvent;
27 import com.intellij.openapi.diagnostic.Logger;
28 import com.intellij.openapi.progress.ProcessCanceledException;
29 import com.intellij.openapi.util.text.StringUtil;
30 import com.intellij.psi.impl.DebugUtil;
31 import org.apache.log4j.Level;
32 import org.jetbrains.annotations.NonNls;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.io.InputStreamReader;
39 import java.io.LineNumberReader;
44 @SuppressWarnings({"HardCodedStringLiteral"})
45 public class IdeaLogger extends Logger {
46 private static ApplicationInfoProvider ourApplicationInfoProvider = getIdeaInfoProvider();
48 public static String ourLastActionId = "";
50 /** If not null - it means that errors occurred and it is the first of them. */
51 public static Exception ourErrorsOccurred;
53 public static String getOurCompilationTimestamp() {
54 return ourCompilationTimestamp;
57 private static String ourCompilationTimestamp;
59 @NonNls private static final String COMPILATION_TIMESTAMP_RESOURCE_NAME = "/.compilation-timestamp";
62 InputStream stream = Logger.class.getResourceAsStream(COMPILATION_TIMESTAMP_RESOURCE_NAME);
65 LineNumberReader reader = new LineNumberReader(new InputStreamReader(stream));
67 String s = reader.readLine();
69 ourCompilationTimestamp = s.trim();
76 catch (IOException ignored) { }
80 private final org.apache.log4j.Logger myLogger;
82 IdeaLogger(org.apache.log4j.Logger logger) {
87 public void error(Object message) {
88 if (message instanceof IdeaLoggingEvent) {
89 myLogger.error(message);
97 public void error(@NonNls String message, Attachment... attachments) {
98 myLogger.error(LogMessageEx.createEvent(message, DebugUtil.currentStackTrace(), attachments));
102 public boolean isDebugEnabled() {
103 return myLogger.isDebugEnabled();
107 public void debug(String message) {
108 myLogger.debug(message);
112 public void debug(Throwable t) {
113 myLogger.debug("", t);
117 public void debug(@NonNls String message, Throwable t) {
118 myLogger.debug(message, t);
122 public void error(String message, @Nullable Throwable t, @NotNull String... details) {
123 if (t instanceof ProcessCanceledException) {
124 myLogger.error(new Throwable("Do not log ProcessCanceledException").initCause(t));
125 throw (ProcessCanceledException)t;
128 if (t != null && t.getClass().getName().contains("ReparsedSuccessfullyException")) {
129 myLogger.error(new Throwable("Do not log ReparsedSuccessfullyException").initCause(t));
130 throw (RuntimeException)t;
133 String detailString = StringUtil.join(details, "\n");
135 if (ourErrorsOccurred == null) {
136 String s = message != null && !message.isEmpty() ? "Error message is '" + message + "'" : "";
137 String mess = "Logger errors occurred. See IDEA logs for details. " + s;
138 ourErrorsOccurred = new Exception(mess + (!detailString.isEmpty() ? "\nDetails: " + detailString : ""), t);
141 myLogger.error(message + (!detailString.isEmpty() ? "\nDetails: " + detailString : ""), t);
145 private void logErrorHeader() {
146 final String info = ourApplicationInfoProvider.getInfo();
149 myLogger.error(info);
152 if (ourCompilationTimestamp != null) {
153 myLogger.error("Internal version. Compiled " + ourCompilationTimestamp);
156 myLogger.error("JDK: " + System.getProperties().getProperty("java.version", "unknown"));
157 myLogger.error("VM: " + System.getProperties().getProperty("java.vm.name", "unknown"));
158 myLogger.error("Vendor: " + System.getProperties().getProperty("java.vendor", "unknown"));
159 myLogger.error("OS: " + System.getProperties().getProperty("os.name", "unknown"));
161 ApplicationImpl application = (ApplicationImpl)ApplicationManager.getApplication();
162 if (application != null && application.isComponentsCreated()) {
163 final String lastPreformedActionId = ourLastActionId;
164 if (lastPreformedActionId != null) {
165 myLogger.error("Last Action: " + lastPreformedActionId);
168 CommandProcessor commandProcessor = CommandProcessor.getInstance();
169 if (commandProcessor != null) {
170 final String currentCommandName = commandProcessor.getCurrentCommandName();
171 if (currentCommandName != null) {
172 myLogger.error("Current Command: " + currentCommandName);
179 public void info(String message) {
180 myLogger.info(message);
184 public void info(String message, @Nullable Throwable t) {
185 myLogger.info(message, t);
189 public void warn(@NonNls String message, @Nullable Throwable t) {
190 myLogger.warn(message, t);
193 public static void setApplicationInfoProvider(ApplicationInfoProvider aProvider) {
194 ourApplicationInfoProvider = aProvider;
197 private static ApplicationInfoProvider getIdeaInfoProvider() {
198 return new ApplicationInfoProvider() {
200 public String getInfo() {
201 final ApplicationInfoEx info = ApplicationInfoImpl.getShadowInstance();
202 return info.getFullApplicationName() + " " + "Build #" + info.getBuild().asString();
208 public void setLevel(Level level) {
209 myLogger.setLevel(level);