| 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 | private static final String LINE_SEPARATOR = System |
| 31 | .getProperty("line.separator"); |
| 32 | |
| 33 | private static final int LINE_SEPARATOR_LENGTH = LINE_SEPARATOR.length(); |
| 34 | |
| 35 | public static Object expandProfilerEventIfNecessary( |
| 36 | Object possibleProfilerEvent) { |
| 37 | |
| 38 | if (possibleProfilerEvent instanceof ProfilerEvent) { |
| 39 | StringBuffer msgBuf = new StringBuffer(); |
| 40 | |
| 41 | ProfilerEvent evt = (ProfilerEvent) possibleProfilerEvent; |
| 42 | |
| 43 | Throwable locationException = evt.getEventCreationPoint(); |
| 44 | |
| 45 | if (locationException == null) { |
| 46 | locationException = new Throwable(); |
| 47 | } |
| 48 | |
| 49 | msgBuf.append("Profiler Event: ["); |
| 50 | |
| 51 | boolean appendLocationInfo = false; |
| 52 | |
| 53 | switch (evt.getEventType()) { |
| 54 | case ProfilerEvent.TYPE_EXECUTE: |
| 55 | msgBuf.append("EXECUTE"); |
| 56 | |
| 57 | break; |
| 58 | |
| 59 | case ProfilerEvent.TYPE_FETCH: |
| 60 | msgBuf.append("FETCH"); |
| 61 | |
| 62 | break; |
| 63 | |
| 64 | case ProfilerEvent.TYPE_OBJECT_CREATION: |
| 65 | msgBuf.append("CONSTRUCT"); |
| 66 | |
| 67 | break; |
| 68 | |
| 69 | case ProfilerEvent.TYPE_PREPARE: |
| 70 | msgBuf.append("PREPARE"); |
| 71 | |
| 72 | break; |
| 73 | |
| 74 | case ProfilerEvent.TYPE_QUERY: |
| 75 | msgBuf.append("QUERY"); |
| 76 | |
| 77 | break; |
| 78 | |
| 79 | case ProfilerEvent.TYPE_WARN: |
| 80 | msgBuf.append("WARN"); |
| 81 | appendLocationInfo = true; |
| 82 | |
| 83 | break; |
| 84 | |
| 85 | default: |
| 86 | msgBuf.append("UNKNOWN"); |
| 87 | } |
| 88 | |
| 89 | msgBuf.append("] "); |
| 90 | msgBuf.append(findCallingClassAndMethod(locationException)); |
| 91 | msgBuf.append(" duration: "); |
| 92 | msgBuf.append(evt.getEventDurationMillis()); |
| 93 | msgBuf.append(" ms, connection-id: "); |
| 94 | msgBuf.append(evt.getConnectionId()); |
| 95 | msgBuf.append(", statement-id: "); |
| 96 | msgBuf.append(evt.getStatementId()); |
| 97 | msgBuf.append(", resultset-id: "); |
| 98 | msgBuf.append(evt.getResultSetId()); |
| 99 | |
| 100 | String evtMessage = evt.getMessage(); |
| 101 | |
| 102 | if (evtMessage != null) { |
| 103 | msgBuf.append(", message: "); |
| 104 | msgBuf.append(evtMessage); |
| 105 | } |
| 106 | |
| 107 | if (appendLocationInfo) { |
| 108 | msgBuf |
| 109 | .append("\n\nFull stack trace of location where event occurred:\n\n"); |
| 110 | msgBuf.append(Util.stackTraceToString(locationException)); |
| 111 | msgBuf.append("\n"); |
| 112 | } |
| 113 | |
| 114 | return msgBuf; |
| 115 | } |
| 116 | |
| 117 | return possibleProfilerEvent; |
| 118 | |
| 119 | } |
| 120 | |
| 121 | public static String findCallingClassAndMethod(Throwable t) { |
| 122 | String stackTraceAsString = Util.stackTraceToString(t); |
| 123 | |
| 124 | String callingClassAndMethod = "Caller information not available"; |
| 125 | |
| 126 | int endInternalMethods = stackTraceAsString |
| 127 | .lastIndexOf("com.mysql.jdbc"); |
| 128 | |
| 129 | if (endInternalMethods != -1) { |
| 130 | int endOfLine = -1; |
| 131 | int compliancePackage = stackTraceAsString.indexOf( |
| 132 | "com.mysql.jdbc.compliance", endInternalMethods); |
| 133 | |
| 134 | if (compliancePackage != -1) { |
| 135 | endOfLine = compliancePackage - LINE_SEPARATOR_LENGTH; |
| 136 | } else { |
| 137 | endOfLine = stackTraceAsString.indexOf(LINE_SEPARATOR, |
| 138 | endInternalMethods); |
| 139 | } |
| 140 | |
| 141 | if (endOfLine != -1) { |
| 142 | int nextEndOfLine = stackTraceAsString.indexOf(LINE_SEPARATOR, |
| 143 | endOfLine + LINE_SEPARATOR_LENGTH); |
| 144 | |
| 145 | if (nextEndOfLine != -1) { |
| 146 | callingClassAndMethod = stackTraceAsString.substring( |
| 147 | endOfLine + LINE_SEPARATOR_LENGTH, nextEndOfLine); |
| 148 | } else { |
| 149 | callingClassAndMethod = stackTraceAsString |
| 150 | .substring(endOfLine + LINE_SEPARATOR_LENGTH); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if (!callingClassAndMethod.startsWith("\tat ") && |
| 156 | !callingClassAndMethod.startsWith("at ")) { |
| 157 | return "at " + callingClassAndMethod; |
| 158 | } |
| 159 | |
| 160 | return callingClassAndMethod; |
| 161 | } |
| 162 | } |