1 | /* |
2 | Copyright (C) 2005 MySQL AB |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of version 2 of the GNU General Public License as |
6 | published by the Free Software Foundation. |
7 | |
8 | There are special exceptions to the terms and conditions of the GPL |
9 | as it is applied to this software. View the full text of the |
10 | exception in file EXCEPTIONS-CONNECTOR-J in the directory of this |
11 | software distribution. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 | */ |
22 | |
23 | package com.mysql.jdbc.log; |
24 | |
25 | import com.mysql.jdbc.Util; |
26 | import com.mysql.jdbc.profiler.ProfilerEvent; |
27 | |
28 | public class LogUtils { |
29 | |
30 | public static final String CALLER_INFORMATION_NOT_AVAILABLE = "Caller information not available"; |
31 | |
32 | private static final String LINE_SEPARATOR = System |
33 | .getProperty("line.separator"); |
34 | |
35 | private static final int LINE_SEPARATOR_LENGTH = LINE_SEPARATOR.length(); |
36 | |
37 | public static Object expandProfilerEventIfNecessary( |
38 | Object possibleProfilerEvent) { |
39 | |
40 | if (possibleProfilerEvent instanceof ProfilerEvent) { |
41 | StringBuffer msgBuf = new StringBuffer(); |
42 | |
43 | ProfilerEvent evt = (ProfilerEvent) possibleProfilerEvent; |
44 | |
45 | Throwable locationException = evt.getEventCreationPoint(); |
46 | |
47 | if (locationException == null) { |
48 | locationException = new Throwable(); |
49 | } |
50 | |
51 | msgBuf.append("Profiler Event: ["); |
52 | |
53 | boolean appendLocationInfo = false; |
54 | |
55 | switch (evt.getEventType()) { |
56 | case ProfilerEvent.TYPE_EXECUTE: |
57 | msgBuf.append("EXECUTE"); |
58 | |
59 | break; |
60 | |
61 | case ProfilerEvent.TYPE_FETCH: |
62 | msgBuf.append("FETCH"); |
63 | |
64 | break; |
65 | |
66 | case ProfilerEvent.TYPE_OBJECT_CREATION: |
67 | msgBuf.append("CONSTRUCT"); |
68 | |
69 | break; |
70 | |
71 | case ProfilerEvent.TYPE_PREPARE: |
72 | msgBuf.append("PREPARE"); |
73 | |
74 | break; |
75 | |
76 | case ProfilerEvent.TYPE_QUERY: |
77 | msgBuf.append("QUERY"); |
78 | |
79 | break; |
80 | |
81 | case ProfilerEvent.TYPE_WARN: |
82 | msgBuf.append("WARN"); |
83 | appendLocationInfo = true; |
84 | |
85 | break; |
86 | |
87 | default: |
88 | msgBuf.append("UNKNOWN"); |
89 | } |
90 | |
91 | msgBuf.append("] "); |
92 | msgBuf.append(findCallingClassAndMethod(locationException)); |
93 | msgBuf.append(" duration: "); |
94 | msgBuf.append(evt.getEventDurationMillis()); |
95 | msgBuf.append(" ms, connection-id: "); |
96 | msgBuf.append(evt.getConnectionId()); |
97 | msgBuf.append(", statement-id: "); |
98 | msgBuf.append(evt.getStatementId()); |
99 | msgBuf.append(", resultset-id: "); |
100 | msgBuf.append(evt.getResultSetId()); |
101 | |
102 | String evtMessage = evt.getMessage(); |
103 | |
104 | if (evtMessage != null) { |
105 | msgBuf.append(", message: "); |
106 | msgBuf.append(evtMessage); |
107 | } |
108 | |
109 | if (appendLocationInfo) { |
110 | msgBuf |
111 | .append("\n\nFull stack trace of location where event occurred:\n\n"); |
112 | msgBuf.append(Util.stackTraceToString(locationException)); |
113 | msgBuf.append("\n"); |
114 | } |
115 | |
116 | return msgBuf; |
117 | } |
118 | |
119 | return possibleProfilerEvent; |
120 | |
121 | } |
122 | |
123 | public static String findCallingClassAndMethod(Throwable t) { |
124 | String stackTraceAsString = Util.stackTraceToString(t); |
125 | |
126 | String callingClassAndMethod = CALLER_INFORMATION_NOT_AVAILABLE; |
127 | |
128 | int endInternalMethods = stackTraceAsString |
129 | .lastIndexOf("com.mysql.jdbc"); |
130 | |
131 | if (endInternalMethods != -1) { |
132 | int endOfLine = -1; |
133 | int compliancePackage = stackTraceAsString.indexOf( |
134 | "com.mysql.jdbc.compliance", endInternalMethods); |
135 | |
136 | if (compliancePackage != -1) { |
137 | endOfLine = compliancePackage - LINE_SEPARATOR_LENGTH; |
138 | } else { |
139 | endOfLine = stackTraceAsString.indexOf(LINE_SEPARATOR, |
140 | endInternalMethods); |
141 | } |
142 | |
143 | if (endOfLine != -1) { |
144 | int nextEndOfLine = stackTraceAsString.indexOf(LINE_SEPARATOR, |
145 | endOfLine + LINE_SEPARATOR_LENGTH); |
146 | |
147 | if (nextEndOfLine != -1) { |
148 | callingClassAndMethod = stackTraceAsString.substring( |
149 | endOfLine + LINE_SEPARATOR_LENGTH, nextEndOfLine); |
150 | } else { |
151 | callingClassAndMethod = stackTraceAsString |
152 | .substring(endOfLine + LINE_SEPARATOR_LENGTH); |
153 | } |
154 | } |
155 | } |
156 | |
157 | if (!callingClassAndMethod.startsWith("\tat ") && |
158 | !callingClassAndMethod.startsWith("at ")) { |
159 | return "at " + callingClassAndMethod; |
160 | } |
161 | |
162 | return callingClassAndMethod; |
163 | } |
164 | } |