📄 logwriter.java
字号:
/* Derby - Class org.apache.derby.client.am.LogWriter Copyright (c) 2001, 2005 The Apache Software Foundation or its licensors, where applicable. 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.apache.derby.client.am;import org.apache.derby.jdbc.ClientDataSource;public class LogWriter { protected java.io.PrintWriter printWriter_; protected int traceLevel_; private boolean driverConfigurationHasBeenWrittenToJdbc1Stream_ = false; private boolean driverConfigurationHasBeenWrittenToJdbc2Stream_ = false; // It is assumed that this constructor is never called when logWriter is null. public LogWriter(java.io.PrintWriter printWriter, int traceLevel) { printWriter_ = printWriter; traceLevel_ = traceLevel; } final protected boolean loggingEnabled(int traceLevel) { // It is an invariant that the printWriter is never null, so remove the return printWriter_ != null && (traceLevel & traceLevel_) != 0; } final protected boolean traceSuspended() { return org.apache.derby.client.am.Configuration.traceSuspended__; } // When garbage collector doesn't kick in in time // to close file descriptors, "Too many open files" // exception may occur (currently found on Linux). // To minimize the chance of this problem happening, // the print writer needs to be closed (after this // DNC log writer is closed) when each connection has // its own trace file (i.e. traceDirectory is specified). public boolean printWriterNeedsToBeClosed_; void close() { if (printWriterNeedsToBeClosed_) { printWriter_.close(); printWriterNeedsToBeClosed_ = false; } // printWriter_ = null; // help GC. } // --------------------------------------------------------------------------- public void dncprintln(String s) { synchronized (printWriter_) { printWriter_.println("[derby] " + s); printWriter_.flush(); } } private void dncprint(String s) { synchronized (printWriter_) { printWriter_.print("[derby] " + s); printWriter_.flush(); } } private void dncprintln(String header, String s) { synchronized (printWriter_) { printWriter_.println("[derby]" + header + " " + s); printWriter_.flush(); } } private void dncprint(String header, String s) { synchronized (printWriter_) { printWriter_.print("[derby]" + header + " " + s); printWriter_.flush(); } } // ------------------------ tracepoint api ----------------------------------- public void tracepoint(String component, int tracepoint, String message) { if (traceSuspended()) { return; } dncprintln(component, "[time:" + System.currentTimeMillis() + "]" + "[thread:" + Thread.currentThread().getName() + "]" + "[tracepoint:" + tracepoint + "]" + message); } public void tracepoint(String component, int tracepoint, String classContext, String methodContext) { if (traceSuspended()) { return; } String staticContextTracepointRecord = component + "[time:" + System.currentTimeMillis() + "]" + "[thread:" + Thread.currentThread().getName() + "]" + "[tracepoint:" + tracepoint + "]" + "[" + classContext + "." + methodContext + "]"; dncprintln(staticContextTracepointRecord); } public void tracepoint(String component, int tracepoint, Object instance, String classContext, String methodContext) { if (traceSuspended()) { return; } String instanceContextTracepointRecord = component + "[time:" + System.currentTimeMillis() + "]" + "[thread:" + Thread.currentThread().getName() + "]" + "[tracepoint:" + tracepoint + "]" + "[" + classContext + "@" + Integer.toHexString(instance.hashCode()) + "." + methodContext + "]"; dncprintln(instanceContextTracepointRecord); } public void tracepoint(String component, int tracepoint, String classContext, String methodContext, java.util.Map memory) { if (traceSuspended()) { return; } String staticContextTracepointRecord = component + "[time:" + System.currentTimeMillis() + "]" + "[thread:" + Thread.currentThread().getName() + "]" + "[tracepoint:" + tracepoint + "]" + "[" + classContext + "." + methodContext + "]"; dncprintln(staticContextTracepointRecord + getMemoryMapDisplay(memory)); } public void tracepoint(String component, int tracepoint, Object instance, String classContext, String methodContext, java.util.Map memory) { if (traceSuspended()) { return; } String instanceContextTracepointRecord = component + "[time:" + System.currentTimeMillis() + "]" + "[thread:" + Thread.currentThread().getName() + "]" + "[tracepoint:" + tracepoint + "]" + "[" + classContext + "@" + Integer.toHexString(instance.hashCode()) + "." + methodContext + "]"; dncprintln(instanceContextTracepointRecord + getMemoryMapDisplay(memory)); } private String getMemoryMapDisplay(java.util.Map memory) { return memory.toString(); // need to loop thru all keys in the map and print values } // ------------- API entry and exit trace methods ---------------------------- // Entry and exit are be traced separately because input arguments need // to be traced before any potential exception can occur. // Exit tracing is only performed on methods that return values. // Entry tracing is only performed on methods that update state, // so entry tracing is not performed on simple getter methods. // We could decide in the future to restrict entry tracing only to methods with input arguments. private void traceExternalMethod(Object instance, String className, String methodName) { if (traceSuspended()) { return; } dncprint(buildExternalMethodHeader(instance, className), methodName); } private void traceExternalDeprecatedMethod(Object instance, String className, String methodName) { if (traceSuspended()) { return; } dncprint(buildExternalMethodHeader(instance, className), "Deprecated " + methodName); } private String buildExternalMethodHeader(Object instance, String className) { return "[Time:" + System.currentTimeMillis() + "]" + "[Thread:" + Thread.currentThread().getName() + "]" + "[" + className + "@" + Integer.toHexString(instance.hashCode()) + "]"; } private String getClassNameOfInstanceIfTraced(Object instance) { if (instance == null) // this prevents NPE from instance.getClass() used below { return null; } else if (instance instanceof Connection && loggingEnabled(ClientDataSource.TRACE_CONNECTION_CALLS)) { return "Connection"; } else if (instance instanceof ResultSet && loggingEnabled(ClientDataSource.TRACE_RESULT_SET_CALLS)) { return "ResultSet"; } else if (instance instanceof CallableStatement && loggingEnabled(ClientDataSource.TRACE_STATEMENT_CALLS)) { return "CallableStatement"; } else if (instance instanceof PreparedStatement && loggingEnabled(ClientDataSource.TRACE_STATEMENT_CALLS)) { return "PreparedStatement"; } else if (instance instanceof Statement && loggingEnabled(ClientDataSource.TRACE_STATEMENT_CALLS)) { return "Statement"; } // Not yet externalizing Blob tracing, except for trace_all else if (instance instanceof Blob && loggingEnabled(ClientDataSource.TRACE_ALL)) // add a trace level for lobs !! { return "Blob"; } // Not yet externalizing Clob tracing, except for trace_all else if (instance instanceof Clob && loggingEnabled(ClientDataSource.TRACE_ALL)) // add a trace level for bobs !! { return "Clob"; } // Not yet externalizing dbmd catalog call tracing, except for trace_all else if (instance instanceof DatabaseMetaData && loggingEnabled(ClientDataSource.TRACE_ALL)) // add a trace level for dbmd ?? { return "DatabaseMetaData"; } // we don't use instanceof javax.transaction.XAResource to avoid dependency on j2ee.jar else if (loggingEnabled(ClientDataSource.TRACE_XA_CALLS) && instance.getClass().getName().startsWith("org.apache.derby.client.net.NetXAResource")) { return "NetXAResource"; } else if (loggingEnabled(ClientDataSource.TRACE_ALL) && instance.getClass().getName().equals("org.apache.derby.client.ClientPooledConnection")) { return "ClientPooledConnection"; } else if (loggingEnabled(ClientDataSource.TRACE_ALL) && instance.getClass().getName().equals("org.apache.derby.jdbc.ClientConnectionPoolDataSource")) { return "ClientConnectionPoolDataSource"; } else if (loggingEnabled(ClientDataSource.TRACE_ALL) && instance.getClass().getName().equals("org.apache.derby.client.ClientXAConnection")) { return "ClientXAConnection"; } else if (loggingEnabled(ClientDataSource.TRACE_ALL) && instance.getClass().getName().equals("org.apache.derby.jdbc.ClientDataSource")) { return "ClientDataSource"; } else if (loggingEnabled(ClientDataSource.TRACE_ALL) && instance.getClass().getName().equals("org.apache.derby.jdbc.ClientXADataSource")) { return "ClientXADataSource"; } else { return instance.getClass().getName(); } } // --------------------------- method exit tracing -------------------------- public void traceExit(Object instance, String methodName, Object returnValue) { if (traceSuspended()) { return; } String className = getClassNameOfInstanceIfTraced(instance); if (className == null) { return; } synchronized (printWriter_) { traceExternalMethod(instance, className, methodName); printWriter_.println(" () returned " + returnValue); printWriter_.flush(); } } public void traceDeprecatedExit(Object instance, String methodName, Object returnValue) { if (traceSuspended()) { return; } String className = getClassNameOfInstanceIfTraced(instance); if (className == null) { return; } synchronized (printWriter_) { traceExternalDeprecatedMethod(instance, className, methodName); printWriter_.println(" () returned " + returnValue); printWriter_.flush(); } } public void traceExit(Object instance, String methodName, ResultSet resultSet) { if (traceSuspended()) { return; } String returnValue = (resultSet == null) ? "ResultSet@null" : "ResultSet@" + Integer.toHexString(resultSet.hashCode()); traceExit(instance, methodName, returnValue); } public void traceExit(Object instance, String methodName, CallableStatement returnValue) { if (traceSuspended()) { return; } traceExit(instance, methodName, "CallableStatement@" + Integer.toHexString(returnValue.hashCode())); } public void traceExit(Object instance, String methodName, PreparedStatement returnValue) { if (traceSuspended()) { return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -