001: /*
002: Copyright (C) 2005-2007 MySQL AB
003:
004: This program is free software; you can redistribute it and/or modify
005: it under the terms of version 2 of the GNU General Public License as
006: published by the Free Software Foundation.
007:
008: There are special exceptions to the terms and conditions of the GPL
009: as it is applied to this software. View the full text of the
010: exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
011: software distribution.
012:
013: This program is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: GNU General Public License for more details.
017:
018: You should have received a copy of the GNU General Public License
019: along with this program; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: */
022:
023: package com.mysql.jdbc.log;
024:
025: import com.mysql.jdbc.Util;
026: import com.mysql.jdbc.profiler.ProfilerEvent;
027:
028: public class LogUtils {
029:
030: public static final String CALLER_INFORMATION_NOT_AVAILABLE = "Caller information not available";
031:
032: private static final String LINE_SEPARATOR = System
033: .getProperty("line.separator");
034:
035: private static final int LINE_SEPARATOR_LENGTH = LINE_SEPARATOR
036: .length();
037:
038: public static Object expandProfilerEventIfNecessary(
039: Object possibleProfilerEvent) {
040:
041: if (possibleProfilerEvent instanceof ProfilerEvent) {
042: StringBuffer msgBuf = new StringBuffer();
043:
044: ProfilerEvent evt = (ProfilerEvent) possibleProfilerEvent;
045:
046: Throwable locationException = evt.getEventCreationPoint();
047:
048: if (locationException == null) {
049: locationException = new Throwable();
050: }
051:
052: msgBuf.append("Profiler Event: [");
053:
054: boolean appendLocationInfo = false;
055:
056: switch (evt.getEventType()) {
057: case ProfilerEvent.TYPE_EXECUTE:
058: msgBuf.append("EXECUTE");
059:
060: break;
061:
062: case ProfilerEvent.TYPE_FETCH:
063: msgBuf.append("FETCH");
064:
065: break;
066:
067: case ProfilerEvent.TYPE_OBJECT_CREATION:
068: msgBuf.append("CONSTRUCT");
069:
070: break;
071:
072: case ProfilerEvent.TYPE_PREPARE:
073: msgBuf.append("PREPARE");
074:
075: break;
076:
077: case ProfilerEvent.TYPE_QUERY:
078: msgBuf.append("QUERY");
079:
080: break;
081:
082: case ProfilerEvent.TYPE_WARN:
083: msgBuf.append("WARN");
084: appendLocationInfo = true;
085:
086: break;
087:
088: case ProfilerEvent.TYPE_SLOW_QUERY:
089: msgBuf.append("SLOW QUERY");
090: appendLocationInfo = false;
091:
092: break;
093:
094: default:
095: msgBuf.append("UNKNOWN");
096: }
097:
098: msgBuf.append("] ");
099: msgBuf.append(findCallingClassAndMethod(locationException));
100: msgBuf.append(" duration: ");
101: msgBuf.append(evt.getEventDuration());
102: msgBuf.append(" ");
103: msgBuf.append(evt.getDurationUnits());
104: msgBuf.append(", connection-id: ");
105: msgBuf.append(evt.getConnectionId());
106: msgBuf.append(", statement-id: ");
107: msgBuf.append(evt.getStatementId());
108: msgBuf.append(", resultset-id: ");
109: msgBuf.append(evt.getResultSetId());
110:
111: String evtMessage = evt.getMessage();
112:
113: if (evtMessage != null) {
114: msgBuf.append(", message: ");
115: msgBuf.append(evtMessage);
116: }
117:
118: if (appendLocationInfo) {
119: msgBuf
120: .append("\n\nFull stack trace of location where event occurred:\n\n");
121: msgBuf.append(Util
122: .stackTraceToString(locationException));
123: msgBuf.append("\n");
124: }
125:
126: return msgBuf;
127: }
128:
129: return possibleProfilerEvent;
130: }
131:
132: public static String findCallingClassAndMethod(Throwable t) {
133: String stackTraceAsString = Util.stackTraceToString(t);
134:
135: String callingClassAndMethod = CALLER_INFORMATION_NOT_AVAILABLE;
136:
137: int endInternalMethods = stackTraceAsString
138: .lastIndexOf("com.mysql.jdbc");
139:
140: if (endInternalMethods != -1) {
141: int endOfLine = -1;
142: int compliancePackage = stackTraceAsString.indexOf(
143: "com.mysql.jdbc.compliance", endInternalMethods);
144:
145: if (compliancePackage != -1) {
146: endOfLine = compliancePackage - LINE_SEPARATOR_LENGTH;
147: } else {
148: endOfLine = stackTraceAsString.indexOf(LINE_SEPARATOR,
149: endInternalMethods);
150: }
151:
152: if (endOfLine != -1) {
153: int nextEndOfLine = stackTraceAsString.indexOf(
154: LINE_SEPARATOR, endOfLine
155: + LINE_SEPARATOR_LENGTH);
156:
157: if (nextEndOfLine != -1) {
158: callingClassAndMethod = stackTraceAsString
159: .substring(endOfLine
160: + LINE_SEPARATOR_LENGTH,
161: nextEndOfLine);
162: } else {
163: callingClassAndMethod = stackTraceAsString
164: .substring(endOfLine
165: + LINE_SEPARATOR_LENGTH);
166: }
167: }
168: }
169:
170: if (!callingClassAndMethod.startsWith("\tat ")
171: && !callingClassAndMethod.startsWith("at ")) {
172: return "at " + callingClassAndMethod;
173: }
174:
175: return callingClassAndMethod;
176: }
177: }
|