001: // jTDS JDBC Driver for Microsoft SQL Server and Sybase
002: // Copyright (C) 2004 The jTDS Project
003: //
004: // This library is free software; you can redistribute it and/or
005: // modify it under the terms of the GNU Lesser General Public
006: // License as published by the Free Software Foundation; either
007: // version 2.1 of the License, or (at your option) any later version.
008: //
009: // This library is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: // Lesser General Public License for more details.
013: //
014: // You should have received a copy of the GNU Lesser General Public
015: // License along with this library; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: //
018: package net.sourceforge.jtds.util;
019:
020: import java.sql.*;
021: import java.io.PrintWriter;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024:
025: /**
026: * Class providing static methods to log diagnostics.
027: * <p>
028: * There are three ways to enable logging:
029: * <ol>
030: * <li>Pass a valid PrintWriter to DriverManager.setLogWriter().
031: * <li>Pass a valid PrintWriter to DataSource.setLogWriter().
032: * <li>For backwards compatibility call Logger.setActive();
033: * </ol>
034: *
035: * @author Mike Hutchinson
036: * @version $Id: Logger.java,v 1.11 2005/04/20 16:49:31 alin_sinpalean Exp $
037: */
038: public class Logger {
039: /** PrintWriter stream set by DataSource. */
040: private static PrintWriter log;
041:
042: /**
043: * Set the logging PrintWriter stream.
044: *
045: * @param out the PrintWriter stream
046: */
047: public static void setLogWriter(PrintWriter out) {
048: log = out;
049: }
050:
051: /**
052: * Get the logging PrintWriter Stream.
053: *
054: * @return the logging stream as a <code>PrintWriter</code>
055: */
056: public static PrintWriter getLogWriter() {
057: return log;
058: }
059:
060: /**
061: * Retrieve the active status of the logger.
062: *
063: * @return <code>boolean</code> true if logging enabled
064: */
065: public static boolean isActive() {
066: return (log != null || DriverManager.getLogWriter() != null);
067: }
068:
069: /**
070: * Print a diagnostic message to the output stream provided by
071: * the DataSource or the DriverManager.
072: *
073: * @param message the diagnostic message to print
074: */
075: public static void println(String message) {
076: if (log != null) {
077: log.println(message);
078: } else {
079: DriverManager.println(message);
080: }
081: }
082:
083: private static final char hex[] = { '0', '1', '2', '3', '4', '5',
084: '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
085:
086: /**
087: * Print a dump of the current input or output network packet.
088: *
089: * @param streamId the owner of this packet
090: * @param in true if this is an input packet
091: * @param pkt the packet data
092: */
093: public static void logPacket(int streamId, boolean in, byte[] pkt) {
094: int len = ((pkt[2] & 0xFF) << 8) | (pkt[3] & 0xFF);
095:
096: StringBuffer line = new StringBuffer(80);
097:
098: line.append("----- Stream #");
099: line.append(streamId);
100: line.append(in ? " read" : " send");
101: line.append((pkt[1] != 0) ? " last " : " ");
102:
103: switch (pkt[0]) {
104: case 1:
105: line.append("Request packet ");
106: break;
107: case 2:
108: line.append("Login packet ");
109: break;
110: case 3:
111: line.append("RPC packet ");
112: break;
113: case 4:
114: line.append("Reply packet ");
115: break;
116: case 6:
117: line.append("Cancel packet ");
118: break;
119: case 14:
120: line.append("XA control packet ");
121: break;
122: case 15:
123: line.append("TDS5 Request packet ");
124: break;
125: case 16:
126: line.append("MS Login packet ");
127: break;
128: case 17:
129: line.append("NTLM Authentication packet ");
130: break;
131: case 18:
132: line.append("MS Prelogin packet ");
133: break;
134: default:
135: line.append("Invalid packet ");
136: break;
137: }
138:
139: println(line.toString());
140: println("");
141: line.setLength(0);
142:
143: for (int i = 0; i < len; i += 16) {
144: if (i < 1000) {
145: line.append(' ');
146: }
147:
148: if (i < 100) {
149: line.append(' ');
150: }
151:
152: if (i < 10) {
153: line.append(' ');
154: }
155:
156: line.append(i);
157: line.append(':').append(' ');
158:
159: int j = 0;
160:
161: for (; j < 16 && i + j < len; j++) {
162: int val = pkt[i + j] & 0xFF;
163:
164: line.append(hex[val >> 4]);
165: line.append(hex[val & 0x0F]);
166: line.append(' ');
167: }
168:
169: for (; j < 16; j++) {
170: line.append(" ");
171: }
172:
173: line.append('|');
174:
175: for (j = 0; j < 16 && i + j < len; j++) {
176: int val = pkt[i + j] & 0xFF;
177:
178: if (val > 31 && val < 127) {
179: line.append((char) val);
180: } else {
181: line.append(' ');
182: }
183: }
184:
185: line.append('|');
186: println(line.toString());
187: line.setLength(0);
188: }
189:
190: println("");
191: }
192:
193: /**
194: * Print an Exception stack trace to the log.
195: *
196: * @param e the exception to log
197: */
198: public static void logException(Exception e) {
199: if (log != null) {
200: e.printStackTrace(log);
201: } else if (DriverManager.getLogWriter() != null) {
202: e.printStackTrace(DriverManager.getLogWriter());
203: }
204: }
205:
206: //
207: // Backward compatbility method
208: //
209: /**
210: * Turn the logging on or off.
211: *
212: * @deprecated Use the JDBC standard mechanisms to enable logging.
213: * @param value true to turn on logging
214: */
215: public static void setActive(boolean value) {
216: if (value && log == null) {
217: try {
218: log = new PrintWriter(new FileOutputStream("log.out"),
219: true);
220: } catch (IOException e) {
221: log = null; // Sorry no logging!
222: }
223: }
224: }
225: }
|