Profiler uses loggers

This allows us to leave profiler calls in production code
This commit is contained in:
Andrey Breslav
2013-09-26 10:49:00 -07:00
parent 51d54661a1
commit 4f7e958a0b
2 changed files with 123 additions and 30 deletions
@@ -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) {
}
}
@@ -16,8 +16,8 @@
package org.jetbrains.jet.utils; package org.jetbrains.jet.utils;
import com.intellij.openapi.diagnostic.Logger;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.Arrays; import java.util.Arrays;
@@ -37,12 +37,18 @@ public class Profiler {
@NotNull @NotNull
public static Profiler create(@NotNull String name) { public static Profiler create(@NotNull String name) {
return create(name, null); //noinspection UseOfSystemOutOrSystemErr
return create(name, System.out);
} }
@NotNull @NotNull
public static Profiler create(@NotNull String name, @Nullable PrintStream out) { public static Profiler create(@NotNull String name, @NotNull PrintStream out) {
Profiler profiler = new Profiler(name, 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); PROFILERS.get().push(profiler);
return profiler; return profiler;
} }
@@ -56,16 +62,16 @@ public class Profiler {
} }
private final String name; private final String name;
private final PrintStream out; private final Logger log;
private long start = Long.MAX_VALUE; private long start = Long.MAX_VALUE;
private long cumulative = 0; private long cumulative = 0;
private boolean paused = true; private boolean paused = true;
private StackTraceElement[] stackTrace; private StackTraceElement[] stackTrace;
private boolean mute; private boolean mute;
private Profiler(@NotNull String name, @Nullable PrintStream out) { private Profiler(@NotNull String name, @NotNull Logger log) {
this.name = name; this.name = name;
this.out = out == null ? System.out : out; this.log = log;
} }
public Profiler recordStackTrace(int depth) { public Profiler recordStackTrace(int depth) {
@@ -96,7 +102,7 @@ public class Profiler {
} }
public Profiler printStackTrace() { public Profiler printStackTrace() {
if (stackTrace != null) { if (stackTrace != null && log.isDebugEnabled()) {
OUT_LOCK.lock(); OUT_LOCK.lock();
try { try {
for (StackTraceElement element : stackTrace) { for (StackTraceElement element : stackTrace) {
@@ -131,13 +137,15 @@ public class Profiler {
paused = true; paused = true;
cumulative = 0; cumulative = 0;
OUT_LOCK.lock(); if (log.isDebugEnabled()) {
try { OUT_LOCK.lock();
println(name, " took ", format(result)); try {
printStackTrace(); println(name, " took ", format(result));
} printStackTrace();
finally { }
OUT_LOCK.unlock(); finally {
OUT_LOCK.unlock();
}
} }
return this; return this;
@@ -162,18 +170,17 @@ public class Profiler {
} }
public Profiler println(Object message) { public Profiler println(Object message) {
if (!mute) { if (!mute && log.isDebugEnabled()) {
out.println(message); log.debug(String.valueOf(message));
} }
return this; return this;
} }
public Profiler println(Object a, Object b) { public Profiler println(Object a, Object b) {
if (!mute) { if (!mute && log.isDebugEnabled()) {
OUT_LOCK.lock(); OUT_LOCK.lock();
try { try {
out.print(a); log.debug(String.valueOf(a) + b);
out.println(b);
} }
finally { finally {
OUT_LOCK.unlock(); OUT_LOCK.unlock();
@@ -183,12 +190,10 @@ public class Profiler {
} }
public Profiler println(Object a, Object b, Object c) { public Profiler println(Object a, Object b, Object c) {
if (!mute) { if (!mute && log.isDebugEnabled()) {
OUT_LOCK.lock(); OUT_LOCK.lock();
try { try {
out.print(a); log.debug(String.valueOf(a) + b + c);
out.print(b);
out.println(c);
} }
finally { finally {
OUT_LOCK.unlock(); OUT_LOCK.unlock();
@@ -198,16 +203,17 @@ public class Profiler {
} }
public Profiler println(Object a, Object b, Object c, Object... rest) { public Profiler println(Object a, Object b, Object c, Object... rest) {
if (!mute) { if (!mute && log.isDebugEnabled()) {
OUT_LOCK.lock(); OUT_LOCK.lock();
try { try {
out.print(a); StringBuilder sb = new StringBuilder();
out.print(b); sb.append(a);
out.print(c); sb.append(b);
sb.append(c);
for (Object o : rest) { for (Object o : rest) {
out.print(o); sb.append(o);
} }
out.println(); log.debug(sb.toString());
} }
finally { finally {
OUT_LOCK.unlock(); OUT_LOCK.unlock();