Source Code Cross Referenced for Logger.java in  » Database-JDBC-Connection-Pool » jTDS » net » sourceforge » jtds » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database JDBC Connection Pool » jTDS » net.sourceforge.jtds.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.