root/coldbox/trunk/system/services/DebuggerService.cfc @ 2103

Revision 2103, 10.3 kB (checked in by lmajano, 4 years ago)

First run of refactorings

  • Property svn:executable set to *
Line 
1<!-----------------------------------------------------------------------
2********************************************************************************
3Copyright 2005-2008 ColdBox Framework by Luis Majano and Ortus Solutions, Corp
4www.coldboxframework.com | www.luismajano.com | www.ortussolutions.com
5********************************************************************************
6
7Author      :   Luis Majano
8Date        :   January 18, 2007
9Description :
10        This cfc takes care of debugging settings.
11
12Modification History:
1301/18/2007 - Created
14----------------------------------------------------------------------->
15<cfcomponent name="debuggerService" output="false" hint="The coldbox debugger service" extends="coldbox.system.services.BaseService">
16
17<!------------------------------------------- CONSTRUCTOR ------------------------------------------->
18
19        <cffunction name="init" access="public" output="false" returntype="DebuggerService" hint="Constructor">
20                <cfargument name="controller" type="any" required="true">
21                <cfscript>
22                        /* Set Controller */
23                        setController(arguments.controller);
24                        /* set the unique cookie name */
25                        setCookieName("coldbox_debugmode_#controller.getAppHash()#");
26                        /* Create persistent profilers */
27                        setProfilers(arrayNew(1));
28                        return this;
29                </cfscript>
30        </cffunction>
31
32<!------------------------------------------- PUBLIC ------------------------------------------->
33       
34        <!--- timerStart --->
35        <cffunction name="timerStart" output="false" access="public" returntype="string" hint="Start an internal code timer and get a hash of the timer storage">
36                <cfargument name="label" type="string" required="true" hint="The timer label to record"/>
37                <cfscript>
38                        var labelHash = 0;
39                        var timerInfo = 0;
40                        /* Verify Debug Mode */
41                        if( getDebugMode() ){
42                                /* Check if DebugTimers Query is set, else create it for this request */
43                                if ( not structKeyExists(request,"DebugTimers") ){
44                                        request.DebugTimers = QueryNew("Id,Method,Time,Timestamp,RC");
45                                }
46                                /* Create Timer Hash */
47                                labelHash = hash(arguments.label);
48                                /* Create timer Info */
49                                timerInfo = structnew();
50                                timerInfo.stime = getTickCount();
51                                timerInfo.label = arguments.label;
52                                /* Persist in request */
53                                request[labelHash] = timerInfo;
54                        }
55                        return labelHash;
56                </cfscript>
57        </cffunction>
58       
59        <!--- timerEnd --->
60        <cffunction name="timerEnd" output="false" access="public" returntype="void" hint="End an internal code timer">
61                <cfargument name="labelHash" type="string" required="true" default="" hint="The timer label hash to stop"/>
62                <cfscript>
63                        var timerInfo = 0;
64                        /* Verify Debug Mode and timer label exists, else do nothing. */
65                        if( getDebugMode() and structKeyExists(request,arguments.labelHash) ){
66                                /* Get Timer Info */
67                                timerInfo = request[arguments.labelHash];
68                                /* Save timer */
69                                QueryAddRow(request.DebugTimers,1);
70                                QuerySetCell(request.DebugTimers, "Id", createUUID());
71                                QuerySetCell(request.DebugTimers, "Method", timerInfo.label);
72                                QuerySetCell(request.DebugTimers, "Time", getTickCount() - timerInfo.stime);
73                                QuerySetCell(request.DebugTimers, "Timestamp", now());
74                                /* Request Context SnapShot */
75                                if ( not findnocase("rendering",timerInfo.label) ){
76                                        /* Save Collection */
77                                        QuerySetCell(request.DebugTimers, "RC", htmlEditFormat(controller.getRequestService().getContext().getCollection().toString()) );
78                                }
79                                else{
80                                        QuerySetCell(request.DebugTimers, "RC", '');
81                                }
82                                /* Cleanup */
83                                structDelete(request,arguments.labelHash);
84                        }
85                </cfscript>
86        </cffunction>
87       
88        <!--- Get the debug mode flag --->
89        <cffunction name="getDebugMode" access="public" hint="I Get the current user's debugmode" returntype="boolean"  output="false">
90                <cfif structKeyExists(cookie,getCookieName())>
91                        <cfif isBoolean(cookie[getCookieName()])>
92                                <cfreturn cookie[getCookieName()]>
93                        <cfelse>
94                                <cfset structDelete(cookie, getCookieName())>
95                        </cfif>
96                </cfif>
97                <!--- Return default of false. --->
98                <cfreturn false>
99        </cffunction>
100
101        <!--- Set the debug mode flag --->
102        <cffunction name="setDebugMode" access="public" hint="I set the current user's debugmode" returntype="void"  output="false">
103                <cfargument name="mode" type="boolean" required="true" >
104                <cfif arguments.mode>
105                        <cfcookie name="#getCookieName()#" value="true">
106                <cfelseif structKeyExists(cookie,getCookieName())>
107                        <cfcookie name="#getCookieName()#" value="false" expires="#now()#">
108                </cfif>
109        </cffunction>
110
111        <!--- render the debug log --->
112        <cffunction name="renderDebugLog" access="public" hint="Return the debug log." output="false" returntype="Any">
113                <cfset var RenderedDebugging = "">
114                <cfset var Event = controller.getRequestService().getContext()>
115
116                <!--- Set local Variables --->
117                <cfset var itemTypes = controller.getColdboxOCM().getItemTypes()>
118                <cfset var cacheMetadata = "">
119                <cfset var cacheKeyList = "">
120                <cfset var cacheKeyIndex = 1>
121
122                <!--- Setup Local Variables --->
123                <cfset var debugStartTime = GetTickCount()>
124                <cfset var RequestCollection = Event.getCollection()>
125
126                <!--- Debug Rendering Type --->
127                <cfset var renderType = "main">
128
129                <!--- JVM Data --->
130                <cfset var JVMRuntime = controller.getColdboxOCM().getJavaRuntime().getRuntime()>
131                <cfset var JVMFreeMemory = JVMRuntime.freeMemory()/1024>
132                <cfset var JVMTotalMemory = JVMRuntime.totalMemory()/1024>
133                <cfset var JVMMaxMemory = JVMRuntime.maxMemory()/1024>
134
135                <!--- Render debuglog --->
136                <cfsavecontent variable="RenderedDebugging"><cfinclude template="../includes/Debug.cfm"></cfsavecontent>
137                <cfreturn RenderedDebugging>
138        </cffunction>
139
140        <!--- Render the cache panel --->
141        <cffunction name="renderCachePanel" access="public" hint="Renders the caching panel." output="false" returntype="Any">
142                <cfset var event = controller.getRequestService().getContext()>
143                <cfset var RenderedDebugging = "">
144
145                <!--- Set local Variables --->
146                <cfset var itemTypes = controller.getColdboxOCM().getItemTypes()>
147                <cfset var cacheMetadata = controller.getColdboxOCM().getpool_metadata()>
148                <cfset var cacheKeyList = listSort(structKeyList(cacheMetaData),"textnocase")>
149                <cfset var cacheKeyIndex = 1>
150
151                <!--- Setup Local Variables --->
152                <cfset var RequestCollection = Event.getCollection()>
153
154                <!--- JVM Data --->
155                <cfset var JVMRuntime = controller.getColdboxOCM().getJavaRuntime().getRuntime()>
156                <cfset var JVMFreeMemory = JVMRuntime.freeMemory()/1024>
157                <cfset var JVMTotalMemory = JVMRuntime.totalMemory()/1024>
158                <cfset var JVMMaxMemory = JVMRuntime.maxMemory()/1024>
159
160                <!--- Debug Rendering Type --->
161                <cfset var renderType = "CachePanel">
162
163                <!--- Generate Debugging --->
164                <cfsavecontent variable="RenderedDebugging"><cfinclude template="/coldbox/system/includes/panels/CachePanel.cfm"></cfsavecontent>
165                <cfreturn RenderedDebugging>
166        </cffunction>
167       
168        <!--- Render Cache Dumpver --->
169        <cffunction name="renderCacheDumper" access="public" hint="Renders the caching key value dumper." output="false" returntype="Any">
170                <cfset var event = controller.getRequestService().getContext()>
171                <cfset var cachekey = URLDecode(event.getValue('key',''))>
172                <cfset var cacheValue = controller.getColdboxOCM().get(cachekey)>
173                <cfset var dumperContents = "">
174               
175                <cfif isSimpleValue(cacheValue)>
176                        <cfsavecontent variable="dumperContents"><cfoutput><strong>#cachekey#</strong> = #cacheValue#</cfoutput></cfsavecontent>
177                <cfelse>
178                        <cfsavecontent variable="dumperContents"><cfdump var="#cacheValue#" label="#cachekey#"></cfsavecontent>
179                </cfif>
180               
181                <cfreturn dumperContents>
182        </cffunction>
183       
184        <!--- Render Profilers --->
185        <cffunction name="renderProfiler" access="public" hint="Renders the execution profilers." output="false" returntype="Any">
186                <cfset var profilerContents = "">
187                <cfset var profilers = getProfilers()>
188                <cfset var profilersCount = ArrayLen(profilers)>
189                <cfset var x = 1>
190                <cfset var refLocal = structnew()>
191               
192                <cfsavecontent variable="profilerContents"><cfinclude template="/coldbox/system/includes/panels/ProfilerPanel.cfm"></cfsavecontent>
193                               
194                <cfreturn profilerContents>
195        </cffunction>
196       
197        <!--- Get set the cookie name --->
198        <cffunction name="getcookieName" access="public" output="false" returntype="string" hint="Get cookieName">
199                <cfreturn instance.cookieName/>
200        </cffunction>
201        <cffunction name="setcookieName" access="public" output="false" returntype="void" hint="Set cookieName">
202                <cfargument name="cookieName" type="string" required="true"/>
203                <cfset instance.cookieName = arguments.cookieName/>
204        </cffunction>
205       
206        <!--- Configuration Bean --->
207        <cffunction name="getdebuggerConfigBean" access="public" output="false" returntype="coldbox.system.beans.DebuggerConfigBean" hint="Get debuggerConfigBean">
208                <cfreturn instance.debuggerConfigBean/>
209        </cffunction>   
210        <cffunction name="setdebuggerConfigBean" access="public" output="false" returntype="void" hint="Set debuggerConfigBean">
211                <cfargument name="debuggerConfigBean" type="coldbox.system.beans.DebuggerConfigBean" required="true"/>
212                <cfset instance.debuggerConfigBean = arguments.debuggerConfigBean/>
213        </cffunction>
214       
215        <!--- Persistent Profilers --->
216        <cffunction name="getProfilers" access="public" output="false" returntype="array" hint="Get Profilers">
217                <cfreturn instance.Profilers/>
218        </cffunction>
219        <cffunction name="setProfilers" access="public" output="false" returntype="void" hint="Set Profilers">
220                <cfargument name="Profilers" type="array" required="true"/>
221                <cfset instance.Profilers = arguments.Profilers/>
222        </cffunction>
223       
224        <!--- Push a profiler --->
225        <cffunction name="pushProfiler" access="public" returntype="void" hint="Push a profiler record" output="false" >
226                <cfargument name="profilerRecord" required="true" type="query" hint="The profiler query for this request">
227                <cfscript>
228                        var newRecord = structnew();
229                       
230                        /* Size Check */
231                        if( ArrayLen(getProfilers()) gte getDebuggerConfigBean().getmaxPersistentRequestProfilers() ){
232                                popProfiler();
233                        }
234                        /* Append the new profiler */
235                        newRecord.datetime = now();
236                        newRecord.ip = cgi.REMOTE_ADDR;
237                        newRecord.timers = arguments.profilerRecord;
238                       
239                        ArrayAppend(getProfilers(),newRecord);
240                </cfscript>
241               
242        </cffunction>
243       
244        <!--- Pop a profiler --->
245        <cffunction name="popProfiler" access="public" returntype="void" hint="Pop a profiler record" output="false" >
246                <cfscript>
247                        /* Delete eldest Entry */
248                        ArrayDeleteAt(getProfilers(),1);
249                </cfscript>
250        </cffunction>
251       
252<!------------------------------------------- PRIVATE ------------------------------------------->
253
254
255</cfcomponent>
Note: See TracBrowser for help on using the browser.