提交 45f10244 authored 作者: Thomas Mueller's avatar Thomas Mueller

Enabling the trace mechanism by creating a specially named file is no longer supported.

上级 d7b57e07
...@@ -97,6 +97,7 @@ public class Trace { ...@@ -97,6 +97,7 @@ public class Trace {
private TraceWriter traceWriter; private TraceWriter traceWriter;
private String module; private String module;
private String lineSeparator; private String lineSeparator;
private int level = TraceSystem.PARENT;
Trace(TraceWriter traceWriter, String module) { Trace(TraceWriter traceWriter, String module) {
this.traceWriter = traceWriter; this.traceWriter = traceWriter;
...@@ -104,13 +105,30 @@ public class Trace { ...@@ -104,13 +105,30 @@ public class Trace {
this.lineSeparator = SysProperties.LINE_SEPARATOR; this.lineSeparator = SysProperties.LINE_SEPARATOR;
} }
/**
* Set the trace level of this component. This setting overrides the parent
* trace level.
*
* @param level the new level
*/
public void setLevel(int level) {
this.level = level;
}
private boolean isEnabled(int level) {
if (this.level == TraceSystem.PARENT) {
return traceWriter.isEnabled(level);
}
return level <= this.level;
}
/** /**
* Check if the trace level is equal or higher than INFO. * Check if the trace level is equal or higher than INFO.
* *
* @return true if it is * @return true if it is
*/ */
public boolean isInfoEnabled() { public boolean isInfoEnabled() {
return traceWriter.isEnabled(TraceSystem.INFO); return isEnabled(TraceSystem.INFO);
} }
/** /**
...@@ -119,7 +137,7 @@ public class Trace { ...@@ -119,7 +137,7 @@ public class Trace {
* @return true if it is * @return true if it is
*/ */
public boolean isDebugEnabled() { public boolean isDebugEnabled() {
return traceWriter.isEnabled(TraceSystem.DEBUG); return isEnabled(TraceSystem.DEBUG);
} }
/** /**
...@@ -128,7 +146,9 @@ public class Trace { ...@@ -128,7 +146,9 @@ public class Trace {
* @param s the message * @param s the message
*/ */
public void error(String s) { public void error(String s) {
traceWriter.write(TraceSystem.ERROR, module, s, null); if (isEnabled(TraceSystem.ERROR)) {
traceWriter.write(TraceSystem.ERROR, module, s, null);
}
} }
/** /**
...@@ -138,7 +158,9 @@ public class Trace { ...@@ -138,7 +158,9 @@ public class Trace {
* @param t the exception * @param t the exception
*/ */
public void error(String s, Throwable t) { public void error(String s, Throwable t) {
traceWriter.write(TraceSystem.ERROR, module, s, t); if (isEnabled(TraceSystem.ERROR)) {
traceWriter.write(TraceSystem.ERROR, module, s, t);
}
} }
/** /**
...@@ -147,7 +169,9 @@ public class Trace { ...@@ -147,7 +169,9 @@ public class Trace {
* @param s the message * @param s the message
*/ */
public void info(String s) { public void info(String s) {
traceWriter.write(TraceSystem.INFO, module, s, null); if (isEnabled(TraceSystem.INFO)) {
traceWriter.write(TraceSystem.INFO, module, s, null);
}
} }
/** /**
...@@ -157,16 +181,9 @@ public class Trace { ...@@ -157,16 +181,9 @@ public class Trace {
* @param t the exception * @param t the exception
*/ */
public void info(String s, Throwable t) { public void info(String s, Throwable t) {
traceWriter.write(TraceSystem.INFO, module, s, t); if (isEnabled(TraceSystem.INFO)) {
} traceWriter.write(TraceSystem.INFO, module, s, t);
}
/**
* Write Java source code with trace level DEBUG to the trace system.
*
* @param java the source code
*/
public void debugCode(String java) {
traceWriter.write(TraceSystem.DEBUG, module, lineSeparator + "/**/" + java, null);
} }
/** /**
...@@ -175,7 +192,9 @@ public class Trace { ...@@ -175,7 +192,9 @@ public class Trace {
* @param java the source code * @param java the source code
*/ */
public void infoCode(String java) { public void infoCode(String java) {
traceWriter.write(TraceSystem.INFO, module, lineSeparator + "/**/" + java, null); if (isEnabled(TraceSystem.INFO)) {
traceWriter.write(TraceSystem.INFO, module, lineSeparator + "/**/" + java, null);
}
} }
/** /**
...@@ -187,6 +206,9 @@ public class Trace { ...@@ -187,6 +206,9 @@ public class Trace {
* @param time the time it took to run the statement in ms * @param time the time it took to run the statement in ms
*/ */
public void infoSQL(String sql, String params, int count, long time) { public void infoSQL(String sql, String params, int count, long time) {
if (!isEnabled(TraceSystem.INFO)) {
return;
}
StringBuffer buff = new StringBuffer(sql.length() + params.length() + 20); StringBuffer buff = new StringBuffer(sql.length() + params.length() + 20);
buff.append(lineSeparator); buff.append(lineSeparator);
buff.append("/*SQL"); buff.append("/*SQL");
...@@ -229,7 +251,9 @@ public class Trace { ...@@ -229,7 +251,9 @@ public class Trace {
* @param s the message * @param s the message
*/ */
public void debug(String s) { public void debug(String s) {
traceWriter.write(TraceSystem.DEBUG, module, s, null); if (isEnabled(TraceSystem.DEBUG)) {
traceWriter.write(TraceSystem.DEBUG, module, s, null);
}
} }
/** /**
...@@ -239,7 +263,20 @@ public class Trace { ...@@ -239,7 +263,20 @@ public class Trace {
* @param t the exception * @param t the exception
*/ */
public void debug(String s, Throwable t) { public void debug(String s, Throwable t) {
traceWriter.write(TraceSystem.DEBUG, module, s, t); if (isEnabled(TraceSystem.DEBUG)) {
traceWriter.write(TraceSystem.DEBUG, module, s, t);
}
}
/**
* Write Java source code with trace level DEBUG to the trace system.
*
* @param java the source code
*/
public void debugCode(String java) {
if (isEnabled(TraceSystem.DEBUG)) {
traceWriter.write(TraceSystem.DEBUG, module, lineSeparator + "/**/" + java, null);
}
} }
} }
...@@ -31,6 +31,11 @@ import org.h2.util.SmallLRUCache; ...@@ -31,6 +31,11 @@ import org.h2.util.SmallLRUCache;
*/ */
public class TraceSystem implements TraceWriter { public class TraceSystem implements TraceWriter {
/**
* The parent trace level should be used.
*/
public static final int PARENT = -1;
/** /**
* This trace level means nothing should be written. * This trace level means nothing should be written.
*/ */
...@@ -74,21 +79,19 @@ public class TraceSystem implements TraceWriter { ...@@ -74,21 +79,19 @@ public class TraceSystem implements TraceWriter {
*/ */
private static final int DEFAULT_MAX_FILE_SIZE = 64 * 1024 * 1024; private static final int DEFAULT_MAX_FILE_SIZE = 64 * 1024 * 1024;
private static final int CHECK_FILE_TIME = 4000;
private static final int CHECK_SIZE_EACH_WRITES = 128; private static final int CHECK_SIZE_EACH_WRITES = 128;
private int levelSystemOut = DEFAULT_TRACE_LEVEL_SYSTEM_OUT; private int levelSystemOut = DEFAULT_TRACE_LEVEL_SYSTEM_OUT;
private int levelFile = DEFAULT_TRACE_LEVEL_FILE; private int levelFile = DEFAULT_TRACE_LEVEL_FILE;
private int level;
private int maxFileSize = DEFAULT_MAX_FILE_SIZE; private int maxFileSize = DEFAULT_MAX_FILE_SIZE;
private String fileName; private String fileName;
private long lastCheck;
private SmallLRUCache traces; private SmallLRUCache traces;
private SimpleDateFormat dateFormat; private SimpleDateFormat dateFormat;
private Writer fileWriter; private Writer fileWriter;
private PrintWriter printWriter; private PrintWriter printWriter;
private int checkSize; private int checkSize;
private boolean closed; private boolean closed;
private boolean manualEnabling = true;
private boolean writingErrorLogged; private boolean writingErrorLogged;
private TraceWriter writer = this; private TraceWriter writer = this;
...@@ -100,6 +103,7 @@ public class TraceSystem implements TraceWriter { ...@@ -100,6 +103,7 @@ public class TraceSystem implements TraceWriter {
*/ */
public TraceSystem(String fileName, boolean init) { public TraceSystem(String fileName, boolean init) {
this.fileName = fileName; this.fileName = fileName;
updateLevel();
traces = new SmallLRUCache(100); traces = new SmallLRUCache(100);
dateFormat = new SimpleDateFormat("MM-dd HH:mm:ss "); dateFormat = new SimpleDateFormat("MM-dd HH:mm:ss ");
if (fileName != null && init) { if (fileName != null && init) {
...@@ -111,6 +115,10 @@ public class TraceSystem implements TraceWriter { ...@@ -111,6 +115,10 @@ public class TraceSystem implements TraceWriter {
} }
} }
private void updateLevel() {
level = Math.max(levelSystemOut, levelFile);
}
/** /**
* Write the exception to the driver manager log writer if configured. * Write the exception to the driver manager log writer if configured.
* *
...@@ -123,16 +131,6 @@ public class TraceSystem implements TraceWriter { ...@@ -123,16 +131,6 @@ public class TraceSystem implements TraceWriter {
} }
} }
/**
* Allow to manually enable the trace option by placing a specially named
* file in the right folder.
*
* @param value the new value
*/
public void setManualEnabling(boolean value) {
this.manualEnabling = value;
}
/** /**
* Get or create a trace object for this module. * Get or create a trace object for this module.
* *
...@@ -149,8 +147,7 @@ public class TraceSystem implements TraceWriter { ...@@ -149,8 +147,7 @@ public class TraceSystem implements TraceWriter {
} }
public boolean isEnabled(int level) { public boolean isEnabled(int level) {
int max = Math.max(levelSystemOut, levelFile); return level <= this.level;
return level <= max;
} }
/** /**
...@@ -178,6 +175,7 @@ public class TraceSystem implements TraceWriter { ...@@ -178,6 +175,7 @@ public class TraceSystem implements TraceWriter {
*/ */
public void setLevelSystemOut(int level) { public void setLevelSystemOut(int level) {
levelSystemOut = level; levelSystemOut = level;
updateLevel();
} }
/** /**
...@@ -208,6 +206,7 @@ public class TraceSystem implements TraceWriter { ...@@ -208,6 +206,7 @@ public class TraceSystem implements TraceWriter {
} }
} }
levelFile = level; levelFile = level;
updateLevel();
} }
private String format(String module, String s) { private String format(String module, String s) {
...@@ -224,34 +223,12 @@ public class TraceSystem implements TraceWriter { ...@@ -224,34 +223,12 @@ public class TraceSystem implements TraceWriter {
} }
} }
if (fileName != null) { if (fileName != null) {
if (level > levelFile) {
enableIfRequired();
}
if (level <= levelFile) { if (level <= levelFile) {
writeFile(format(module, s), t); writeFile(format(module, s), t);
} }
} }
} }
private void enableIfRequired() {
if (!manualEnabling) {
return;
}
long time = System.currentTimeMillis();
if (time > lastCheck + CHECK_FILE_TIME) {
String checkFile = fileName + Constants.SUFFIX_TRACE_START_FILE;
lastCheck = time;
if (FileUtils.exists(checkFile)) {
levelFile = DEBUG;
try {
FileUtils.delete(checkFile);
} catch (Exception e) {
// the file may be read only
}
}
}
}
private synchronized void writeFile(String s, Throwable t) { private synchronized void writeFile(String s, Throwable t) {
try { try {
if (checkSize++ >= CHECK_SIZE_EACH_WRITES) { if (checkSize++ >= CHECK_SIZE_EACH_WRITES) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论