From 4f7e958a0b540926507e619d5760672083ca01e2 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Thu, 26 Sep 2013 10:49:00 -0700 Subject: [PATCH] Profiler uses loggers This allows us to leave profiler calls in production code --- .../jetbrains/jet/utils/PrintingLogger.java | 87 +++++++++++++++++++ .../src/org/jetbrains/jet/utils/Profiler.java | 66 +++++++------- 2 files changed, 123 insertions(+), 30 deletions(-) create mode 100644 compiler/util/src/org/jetbrains/jet/utils/PrintingLogger.java diff --git a/compiler/util/src/org/jetbrains/jet/utils/PrintingLogger.java b/compiler/util/src/org/jetbrains/jet/utils/PrintingLogger.java new file mode 100644 index 00000000000..99e1014e2c9 --- /dev/null +++ b/compiler/util/src/org/jetbrains/jet/utils/PrintingLogger.java @@ -0,0 +1,87 @@ +/* + * Copyright 2010-2013 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 org.jetbrains.jet.utils; + +import com.intellij.openapi.diagnostic.Logger; +import org.apache.log4j.Level; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.io.PrintStream; + +@SuppressWarnings("UseOfSystemOutOrSystemErr") +public class PrintingLogger extends Logger { + + public static final Logger SYSTEM_OUT = new PrintingLogger(System.out); + public static final Logger SYSTEM_ERR = new PrintingLogger(System.err); + + private final PrintStream out; + + public PrintingLogger(@NotNull PrintStream out) { + this.out = out; + } + + @Override + public boolean isDebugEnabled() { + return true; + } + + @Override + public void debug(@NonNls String message) { + out.println(message); + } + + @Override + public void debug(@Nullable Throwable t) { + //noinspection ConstantConditions + t.printStackTrace(out); + } + + @Override + public void debug(@NonNls String message, @Nullable Throwable t) { + debug(message); + debug(t); + } + + @Override + public void info(@NonNls String message) { + debug(message); + } + + @Override + public void info(@NonNls String message, @Nullable Throwable t) { + debug(message, t); + } + + @Override + public void warn(@NonNls String message, @Nullable Throwable t) { + debug(message, t); + } + + @Override + public void error(@NonNls String message, @Nullable Throwable t, @NonNls @NotNull String... details) { + debug(message, t); + for (String detail : details) { + debug(detail); + } + } + + @Override + public void setLevel(Level level) { + } +} diff --git a/compiler/util/src/org/jetbrains/jet/utils/Profiler.java b/compiler/util/src/org/jetbrains/jet/utils/Profiler.java index 0c4a4f73084..c3fcf0465c2 100644 --- a/compiler/util/src/org/jetbrains/jet/utils/Profiler.java +++ b/compiler/util/src/org/jetbrains/jet/utils/Profiler.java @@ -16,8 +16,8 @@ package org.jetbrains.jet.utils; +import com.intellij.openapi.diagnostic.Logger; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.io.PrintStream; import java.util.Arrays; @@ -37,12 +37,18 @@ public class Profiler { @NotNull public static Profiler create(@NotNull String name) { - return create(name, null); + //noinspection UseOfSystemOutOrSystemErr + return create(name, System.out); } @NotNull - public static Profiler create(@NotNull String name, @Nullable PrintStream out) { - Profiler profiler = new Profiler(name, out); + public static Profiler create(@NotNull String name, @NotNull PrintStream out) { + return create(name, new PrintingLogger(out)); + } + + @NotNull + public static Profiler create(@NotNull String name, @NotNull Logger log) { + Profiler profiler = new Profiler(name, log); PROFILERS.get().push(profiler); return profiler; } @@ -56,16 +62,16 @@ public class Profiler { } private final String name; - private final PrintStream out; + private final Logger log; private long start = Long.MAX_VALUE; private long cumulative = 0; private boolean paused = true; private StackTraceElement[] stackTrace; private boolean mute; - private Profiler(@NotNull String name, @Nullable PrintStream out) { + private Profiler(@NotNull String name, @NotNull Logger log) { this.name = name; - this.out = out == null ? System.out : out; + this.log = log; } public Profiler recordStackTrace(int depth) { @@ -96,7 +102,7 @@ public class Profiler { } public Profiler printStackTrace() { - if (stackTrace != null) { + if (stackTrace != null && log.isDebugEnabled()) { OUT_LOCK.lock(); try { for (StackTraceElement element : stackTrace) { @@ -131,13 +137,15 @@ public class Profiler { paused = true; cumulative = 0; - OUT_LOCK.lock(); - try { - println(name, " took ", format(result)); - printStackTrace(); - } - finally { - OUT_LOCK.unlock(); + if (log.isDebugEnabled()) { + OUT_LOCK.lock(); + try { + println(name, " took ", format(result)); + printStackTrace(); + } + finally { + OUT_LOCK.unlock(); + } } return this; @@ -162,18 +170,17 @@ public class Profiler { } public Profiler println(Object message) { - if (!mute) { - out.println(message); + if (!mute && log.isDebugEnabled()) { + log.debug(String.valueOf(message)); } return this; } public Profiler println(Object a, Object b) { - if (!mute) { + if (!mute && log.isDebugEnabled()) { OUT_LOCK.lock(); try { - out.print(a); - out.println(b); + log.debug(String.valueOf(a) + b); } finally { OUT_LOCK.unlock(); @@ -183,12 +190,10 @@ public class Profiler { } public Profiler println(Object a, Object b, Object c) { - if (!mute) { + if (!mute && log.isDebugEnabled()) { OUT_LOCK.lock(); try { - out.print(a); - out.print(b); - out.println(c); + log.debug(String.valueOf(a) + b + c); } finally { OUT_LOCK.unlock(); @@ -198,16 +203,17 @@ public class Profiler { } public Profiler println(Object a, Object b, Object c, Object... rest) { - if (!mute) { + if (!mute && log.isDebugEnabled()) { OUT_LOCK.lock(); try { - out.print(a); - out.print(b); - out.print(c); + StringBuilder sb = new StringBuilder(); + sb.append(a); + sb.append(b); + sb.append(c); for (Object o : rest) { - out.print(o); + sb.append(o); } - out.println(); + log.debug(sb.toString()); } finally { OUT_LOCK.unlock();