Skip to any available Digital Media Help Center chapter of Programming Flex 3: |
Chapter 18: Application Debugging
One of the strengths of Flex is its modern debugging capabilities. Debugging client-side code in web applications has traditionally been cumbersome. The Flash Debug Player, provided with Flex, allows developers the ability to debug applications in the same way they have been accustomed to with other modern development platforms.
In this chapter, we will cover runtime errors, debugging applications using FDB, debugging applications using the Flex Builder debugger, remote debugging, and tracing and logging.
Section 18.1: The Flash Debug Player
The Flash Debug Player is at the core of the debugging capabilities provided to Flex. The Debug Player provides several benefits specific to developers and is required for most types of debugging you will need to do. The browser plug-in and standalone editions of the Debug Player are included in the free SDK in the /runtimes/player folder, and in the <Path to Flex Builder 3>/Player folder if you are using Flex Builder 3. Also, if you installed Flex Builder, the Debug Player browser plug-in is typically installed during the install process. You can always check to ensure that you have the latest edition of the Debug Player by visiting http://www.adobe.com/support/flashplayer/downloads.html.
If you are unsure whether you have the Debug Player installed within a browser, you can build a simple application to check the version of the player installed. Here's an example:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Label text="Flash Player Version: {flash.system.Capabilities.version},
Debug Player: {flash.system.Capabilities.isDebugger}"/>
</mx:Application>
When you open the application in your browser, you should be
presented with the version of Flash Player and whether it is the debug
edition. flash.system.Capabilities is a
class that Flash Player provides that allows you to retrieve information
about the runtime environment in which an application is executing. In
this example, we are checking the isDebugger property, which should return
true if you have the Debug Player
installed. You can also use this property to enable additional debugging
type features of an application only when they are running within a Debug
Player.
The Debug Player provides several additional capabilities on top of the traditional player, as we'll see in the next section. This functionality allows a developer access to the runtime behavior of a running application and is required for some debugging tools, including the command-line debugger and the Flex Builder debugger.
Warning: Running an application in the Debug Player, especially when it is being debugged, will impact runtime performance and can even impact application behavior. You should install the Debug Player only for debugging purposes and never in a production environment.
Runtime Errors
Flash Player 9 supports runtime type checking and exceptions capabilities that developers have become accustomed to in any modern runtime. Runtime errors when identified during the development process can help a great deal when debugging applications. Runtime errors are not presented to users with the non-Debug Player installed, but for development and testing purposes, you should have the Flash Debug Player installed (as should any team members who are involved in application testing). The Debug Player will display runtime errors by presenting you with a dialog as errors occur in your application. You may wonder why such errors are not presented to the user with the regular Flash Player. This is because Adobe silently hides such errors from regular users to minimize their impact on the application experience. The runtime errors still occur, but rather than interrupt the user with a dialog and halt the applications, Flash Player attempts to silently continue code execution. This does not guarantee that an application will always continue to run; some exceptions are fatal and will cause an application to halt. Because of this, it is not advisable to deploy any application that contains runtime exceptions that are not handled. This also does not guarantee that an application will respond as expected when a nonfatal exception occurs. In general, it is a good practice to properly handle exceptions to prevent unexpected results.
Note: A good practice on a team is to have all testers and developers install the Flash Debug Player. Doing so will allow you to catch runtime errors earlier in your development process.
If you execute this code in the Debug Player, you will receive a runtime error:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize=
"initializeHandler()">
<mx:Script>
<![CDATA[
private function initializeHandler():void
{
var loader:Loader = new Loader();
loader.load(new URLRequest("foo"));
}
]]>
</mx:Script>
</mx:Application>
The runtime error you'll receive is IOErrorEvent, as the example doesn't handle
such an exception. Figure 18-1 shows the dialog that results when running Debug Player.
You can find a list of runtime error codes, along with descriptions, in the Flex Language Reference documentation, under Appendixes→Run-Time Errors.
This excerpt is from Programming Flex 3. If you want to try your hand at developing rich Internet applications with Adobe's Flex 3, and already have experience with frameworks such as .NET or Java, this is the ideal book to get you started. Programming Flex 3 gives you a solid understanding of Flex 3's core concepts, and valuable insight into how, why, and when to use specific Flex features. Learn to get the most from this amazing and sophisticated technology.
The Debugging API
Although runtime errors are useful, often you will require more than just runtime errors to identify bugs. For such cases, the Flash Debug Player exposes an API for debuggers to interact with an application at runtime. This includes the ability to set breakpoints, step through code, set and retrieve variables at runtime, as well as other debugging-related tasks. Adobe provides two debuggers. One is the free FDB command-line debugger provided by the Flex SDK, and the other is the integrated GUI debugger that is part of Flex Builder. The debuggers communicate with the Flash Debug Player through a TCP socket connection. Typically, this happens on the same machine, but it is possible to also do remote debugging whereby one machine is the client and the other is running the debugger.
To allow the Debug Player to initiate a debug session, the application must be compiled with the debug data included within the SWF.
To do this, you need to set the compiler flag to true.
mxmlc -debug=true main.mxml
This flag generates a debug-enabled SWF. Although you may not
experience any side effects from using a debug-enabled SWF for
production, this is strongly discouraged because the -debug compiler flag produces larger SWF files
and exposes the internals of an application. If a user has the Debug
Player installed, he could inspect the internals of your application and
even change client-side variable values. Later in this chapter, we will
discuss how to use debug-enabled SWF files using various debuggers
available today.
Using Show Redraw Regions
Even with modern hardware, you can run into rendering performance bottlenecks with graphics-intensive applications. Isolating such bottlenecks can be challenging, especially considering all the variables involved in how Flash Player renders content. For this reason, the Debug Player exposes an option called Show Redraw Regions.
When this option is enabled, the player will highlight areas of a
running application that are being redrawn, which can help you in
identifying graphical regions of an application that may be drawing
inefficiently. You enable this option by selecting the Show Redraw
Regions option from the Debug Player's context menu or through . To enable this option
through ActionScript you can call the method of the flash.profiler
package. This method works only with the Player and doesn't require a
debug-enabled SWF. Here's a simple example that will allow you to
experiment with how this feature works:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
initialize="flash.profiler.showRedrawRegions(true)">
<mx:HSlider width="100%"/>
</mx:Application>
Compile and run this example in the Debug Player. Drag the slider to see how the Debug Player highlights what areas are being redrawn. This is especially helpful with Flex applications because the Flex framework provides a lot of functionality that you don't need to implement and often may not even know how it is implemented. Using the Show Redraw Regions feature can help you identify rendering bottlenecks in Flash Player.
Note: By default, the showRedrawRegions() method will highlight
regions by drawing a blue rectangle outline around the regions being
redrawn and the player context menu will use a red outline. However,
sometimes you might find the default color difficult to identify. If
so, you can specify your own color values by passing in a color value
for the second parameter of the showRedrawRegions() method.
Section 18.2: Using FDB
As part of the Flex SDK, Adobe includes FDB, a free command-line debugger. This debugger is fully featured, although usually you will opt to use the Flex Builder debugger if available. With that said, it is great to have access to a free debugging tool included as part of the SDK. This allows developers who do not want to purchase Flex Builder access to a fully featured debugger. We won't be covering FDB in depth, but we will discuss the basics and some of the possible benefits it has to offer.
You launch FDB from the command line as you would any other
command-line application. Once it is started, you will be prompted with
the FDB prompt (fdb). You can type
help at the prompt for a list of
available commands.
The starting point for a debug session with FDB is to launch an
.swf compiled with debugging enabled
in the Debug Player and establish a connection with FDB. You do this by
first executing the run command at the
FDB prompt. Once the command is executed, FDB will confirm that it is
waiting for the player to connect. To connect Flash Player to FDB, open a
debug-enabled .swf with the Debug
Player. When a debug-
.swf is opened, the player will
attempt to auto-connect to the local debugger, if available. If you open
an application without the debugger listening for a connection from the
player, the Flash Debug Player will prompt you to select the debugger you
want to use. Although typically a user will be running the application and
the debugger on the same machine, which means you may never receive the
prompt requesting you to select the debugger, it is possible to initiate a
remote debugging session. Remote debugging allows you to execute an application on a
machine that is separate from the debugger, allowing you to debug problems
that are reproducible on only certain machines, or even debug across
platforms wherein a Mac OS X machine executes an application with the
debugger running on a Windows machine. For this purpose, all debugging
communication occurs through TCP on port 7935. We cover remote debugging
later in this chapter.
|
Once a connection is established, you can set breakpoints or instruct the debugger to continue execution of the application. To continue execution you can issue the command. Application trace messages are shown in the debugger as they are encountered.
Breakpoints can be set using several methods. The most typical
method of setting a breakpoint is to specify the class and line number for
the breakpoint, which you can achieve by issuing the break command. For example, if you wanted to
insert a breakpoint in line 56 of the class MainApp, you would input the command break MainApp.mxml:56. If you want a breakpoint
when a button is pressed but you are not sure of the method that will be
called when the event occurs, you could enter break button. This will return a list of
methods that begin with the word button. From the
list, you should see buttonPressed and
the location of the method. With that information, you could set the
breakpoint by calling break
buttonPressed. Once you do that, you will need to call the
command continue, which will tell the
debugger to allow the player to continue executing the application. There
are other methods of setting breakpoints with FDB, which you can find by
issuing the command help
breakpoint.
When an application is executing, the debugger will inform you when
it encounters a breakpoint. Once a breakpoint is encountered, you have
several options. You can issue the continue command, which will continue execution
of the application, step through the application using the step command, and set the value of a variable
using the set command. When done
debugging, you can exit FDB to end the debugging session, which will
automatically end the active connection with the Debug Player, or you can
execute the kill command.
As discussed, FDB makes it easy to search for methods on which you
want to set breakpoints. Some of the other nice features of FDB that Flex
Builder's debugger doesn't support are the ability to set conditional
breakpoints using the condition
command, and the ability to review a list of all the loaded types and
functions with the info functions
command.
FDB can be a powerful tool and is worth exploring, but as you will see in the next section, Flex Builder provides a more practical method of application debugging, which you will likely opt for over FDB.
Section 18.3: Debugging with Flex Builder
One of the best selling points of Flex Builder is the integrated GUI debugger. FDB is free, but for day-to-day debugging, Flex Builder's debugger makes it much easier to debug applications.
A default Flex Builder installation (see Figure 18-2) will configure the tasks needed to get you up and running for debugging applications. To debug an application you are working on, you just need to select Run→Debug from the main menu (or press F11). This will compile the application if it is not already compiled, launch it within the browser, and connect the application to the debugger. The first time you debug an application you will be prompted to switch perspectives to the debugging perspective in Flex Builder, which usually is recommended.
When in the debugging perspective, you will have access to the currently running application. Often you will set a breakpoint to stop the application at a point during the execution process in which you are interested. You can set breakpoints by double-clicking the left margin of a source line or by using the keyboard shortcut Ctrl-Shift-B. You can do this during a debug session or even before a session is started in the development perspective. You can navigate through a list of breakpoints by using the Breakpoints panel (see Figure 18-3). The Breakpoints panel contains a list of all breakpoints currently set and lets you navigate directly to a breakpoint or disable a breakpoint. This panel can also be very useful if you aren't sure what breakpoints you have set in your application as it allows you to view all breakpoints and manage them from one central location.
When Flash Player encounters a breakpoint, as in Figure 18-4, execution will halt, the debugger will gain focus, and the call stack will be displayed. You then will have the option of having the application continue execution, step through code, set variables, or evaluate expressions.
Note: Breakpoints can be set on both ActionScript and MXML code. Breakpoints on MXML code are valid only when ActionScript event handlers exist. In cases where you set a breakpoint on an invalid MXML line, Flex Builder will attempt to find the nearest valid breakpoint. It does so by scanning the next 10 lines of execution for points where a breakpoint would be valid, and automatically moving the breakpoint to that location.
To help speed up the debugging process, Flex Builder offers the following default keyboard shortcuts for stepping through code:
- Stepping into (F5)
- Stepping over (F6)
- Step return (F7)
- Resume execution (F8)
This excerpt is from Programming Flex 3. If you want to try your hand at developing rich Internet applications with Adobe's Flex 3, and already have experience with frameworks such as .NET or Java, this is the ideal book to get you started. Programming Flex 3 gives you a solid understanding of Flex 3's core concepts, and valuable insight into how, why, and when to use specific Flex features. Learn to get the most from this amazing and sophisticated technology.
While stepping through code, the debugger will have control over the player. At times, this may cause the player and browser to seem unresponsive. This is normal as you step through code. To suspend the debugger from having control until the next breakpoint, you have to tell the debugger to resume execution by clicking on the Play button or by using the F8 keyboard shortcut.
While debugging, you also have the ability to review values of variables. The Variables panel (see Figure 18-5) will list all object instances as well as their child properties and values. You can also review the values of variables right within the code editor by hovering above a variable within the code. This will display a tool tip with the current value of the variable. To change a value, click the Value column for the field that you wish to change, as shown in Figure 18-6, or right-click the property you wish to change and select Change Value.
Changing variables will often result in other variables changing as well. To give you insight into the changes that occur as a result of changing a variable, the Variables panel will highlight in yellow the affected variables in the panel.
Note: When debugging an application, you also have the ability to set breakpoints in and step through Flex framework code. Stepping through Flex framework code will happen automatically when encountered. To set breakpoints within the framework, you will need to open the class file. An easy way to do this is to use the shortcut Ctrl-Shift-T within Flex Builder and select the appropriate type. Once the class file is opened, you can set breakpoints as you would normally.
To end a debug session you can either close Flash Player or your browser, or click the red square within the Debug panel. Caution should be taken when closing an active debug session by closing the browser when it is executing within a browser, as sessions that are in a halted state can cause a web browser to close unexpectedly.
This book does not fully cover all the functionality possible with the debugger within Flex Builder. For full coverage, you can review the documentation provided with Flex Builder.
Section 18.4: Remote Debugging
When attempting to isolate a bug in an application, it is possible that you will encounter a case where a bug is reproducible on only a specific machine. For such a case, you can use the Flash Debug Player's remote debugging feature. This feature can also be useful if you would want to use the Flex Builder debugger on a Windows or Mac-based machine while the application is executed under Linux, which does not have a native version of Flex Builder available.
As mentioned earlier, debugging occurs over a TCP connection on port 7935. Such a connection is typically established on the same machine transparently, but with remote debugging it is established from one machine to another. One machine will typically be running the debugger and can be referred to as the server, while the other will be running the application and can be referred to as the client machine. Once a connection is established between the client and the server, all of the features of using a debugger function in the same manner. It's important to remember that in a typical workstation, the client machine may not be configured properly for remote debugging. Remember that the Flash Debug Player is required for a debug session, so you will need to ensure that it is installed on the client machine. It is also important to keep in mind that the client machine will need to be executing a debug-enabled SWF in the same manner as we discussed earlier in the chapter.
To initiate a debug session, follow these steps:
- Initialize the debugger on the server machine.
- With FDB: one benefit of using FDB when initializing a remote debugging session is that
the steps for initializing FDB are exactly the same as those for
initializing a local session. You initialize FDB by launching FDB
and executing the
runcommand. - With Flex Builder's Debugger: initializing a remote debugging session with Flex Builder is more involved. Flex Builder's debugger doesn't formally support remote debugging, although it is possible. We will cover remote debugging with Flex Builder in the next section.
- With FDB: one benefit of using FDB when initializing a remote debugging session is that
the steps for initializing FDB are exactly the same as those for
initializing a local session. You initialize FDB by launching FDB
and executing the
- Initialize the debug-enabled SWF on the client machine and ensure that the Debug Player is installed.
- Once initialized, the player will prompt you for a debugger, because the debugger is not running on the client machine. Input the IP address of the server running the debugger and select Connect.
- If the server is running the debugger and listening for a connection, the client and server will connect. Note here that you will also need to ensure that the server is not blocking port 7935.
Once a connection has been established, debugging can be performed in the manner discussed earlier in this chapter.
Establishing a Remote Debugging Session with the Flex Builder Debugger
As mentioned earlier, debugging sessions—both local and remote—are established over a TCP connection on port 7935. Flex Builder uses the same connection to establish debug sessions. Although by default Flex Builder does not expose remote debugging capabilities, it still is possible to do so.
To initiate a remote debug session with Flex Builder, follow these steps:
- Compiled a debug-enabled .swf.
- Copy the debug-enabled .swf to the remote client machine.
- On the machine that will run the Flex Builder debugger (server), create an empty HTML file (typically within your project's bin-debug folder). Since no content exists in an empty file, the Flex Builder debugger will launch a session that is empty and just wait for a remote connection.
- Ensure that no firewall is actively blocking port 7935 on the client machine.
- Edit the debug configuration within Flex Builder (select Run→Run).
- Select the target debug configuration.
- Uncheck the “Use defaults” checkbox.
- Click the Browse button for the debug file path.
- Select the blank HTML page created in step 3.
- Click Debug in Flex Builder. You should see “Launching {application name}” and a progress bar in the bottom status bar of Flex Builder (at this point, Flex Builder is waiting for a remote connection, in the same manner FDB does).
- Open the debug-enabled .swf on the remote machine and, when prompted for a remote host address, input the host address of the machine with the Flex Builder debugger.
|
Section 18.5: Logging Using trace() Within an Application
Although not an advanced debugging technique, at one time or another
a developer will find a need to trace (also referred to as
log) messages from within an application. For this
purpose, Flash Player exposes a global trace() method. You can log messages from
anywhere within an application simply by calling the trace() method and passing any parameter of type
string:
trace("application initialized");
Trace messages are typically displayed by attaching the debugger to an application. With an active FDB debug session, trace messages will be displayed in the console. With Flex Builder, launching a debug session will automatically do this and trace messages will be shown in the Console panel in the debugging perspective (see ).
One of the great benefits of using the trace statement in this
manner is the ability to receive the messages in real time while an
application is running. The trace()
method also supports passing in arrays or multiple arguments (rest-style
arguments). This can be very useful in dumping data for informational
purposes—for example, if you wanted to be able to easily track what
children are currently in the Display Child list.
Example 18-1 contains
two children. When you click the buttonOne button, the function is called and
an array of children is displayed in the output window.
Example 18-1. Calling the clickHandler() function
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
private function clickHandler():void
{
trace("Childrent: "+this.getChildren());
}
]]>
</mx:Script>
<mx:Button id="buttonOne" click="clickHandler()" label="Dump Data"/>
<mx:Label id="labelTwo"/>
</mx:Application>
As with other debugging methods we have seen thus far, using the
trace() function requires the Flash
Debug Player. Although often you will just use the Flex Builder Debugger
to output trace messages, with the debug version of the player you have
the option of outputting the trace messages to a file. You may want to use
this feature if you are having a hard time isolating user-reported bugs by
having the user configure his machine to log all trace calls to a file and
allow you to review the logfiles at a later time for clues on what
sequence of events may have caused the bug. This also is useful for when a
tester isolates a bug in an application and provides corresponding log
.
By default, installing the Debug Player does not enable trace logging. You will need to configure the player to enable logging. The configuration filename is mm.cfg. Under Windows XP it is located at C:\Documents and Settings\<user_name>, on Windows Vista it is located at c:\users\<user_name>, on Linux it is located at /home/<user_name>, and under Mac OS X it is located at /Library/Application Support/Macromedia/. For other operating systems and a full list, consult the Flash Player .
Note: As of this writing, the Flash Player documentation relating to debugger configuration was available at http://livedocs.adobe.com/flex/3/html/help.html?content=logging_04.html. The paths to both mm.cfg and can change with different versions of Flash Player. It is advisable that you review the latest documentation on where such files are located according to the player version you are using if you are having difficulty making the examples in this section work.
First review your operating-system-specific path for an existing
mm.cfg. If none already exists, you
will need to create one in the location appropriate for your operating
system. The configuration file is a plain-text file that supports several
options. Most important, you will be interested in the ErrorReportingEnable, MaxWarnings, and configuration
properties.
A basic mm.cfg file that enables the trace output includes the following:
TraceOutputFileEnable=1
Once the configuration file is updated and saved to the proper location with the filename mm.cfg, the Flash Debug Player will log trace messages to flashlog.txt in the folder specific to your operating system. Under Windows XP, it will be located at , under Windows Vista it is at C:\Users\<username>\AppData\Roaming\Macromedia\Flash Player\Logs, under Mac OS X it is at /Users/<username>/Library/Preferences/Macromedia/Flash Player/Logs/, and under Linux it is at /home/<username>/.macromedia/Flash_Player/Logs/.
Along with enabling trace logging, the Debug Flash Player can also
log all runtime errors to the same logfile. You can enable error reporting
using ErrorReportingEnable:
TraceOutputFileEnable=1 ErrorReportingEnable=1
This excerpt is from Programming Flex 3. If you want to try your hand at developing rich Internet applications with Adobe's Flex 3, and already have experience with frameworks such as .NET or Java, this is the ideal book to get you started. Programming Flex 3 gives you a solid understanding of Flex 3's core concepts, and valuable insight into how, why, and when to use specific Flex features. Learn to get the most from this amazing and sophisticated technology.
Section 18.6: The Logging Framework
The trace() statement can be a
powerful method of logging, but if you have been exposed to logging in the
past, you likely used some sort of logging framework or built your own.
Flex includes a logging framework that offers several benefits over using
the trace() statement alone.
The logging framework consists of two main components: the logger and the target. The logger is used by an application to configure the logging framework and to send messages that are output via a target.
A target is used to specify where log messages are output.
They can be output to any mechanism that Flash Player
supports. The logging framework includes a , which inherits from
LineFormattedTarget and AbstractTarget and implements the ILoggingTarget interface.
TraceTarget internally sends
messages via the global trace()
function. This will often be the target you use. Here's an example using
the logging framework with TraceTarget:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
initialize="initializeHandler()">
<mx:Script>
<![CDATA[
import mx.logging.Log;
import mx.logging.targets.TraceTarget;
private var _target:TraceTarget;
private function initializeHandler():void
{
_target = new TraceTarget();
_target.includeTime = true;
_target.includeLevel = true;
_target.includeCategory = true;
Log.addTarget(_target);
}
private function sendToLog():void
{
Log.getLogger("com.oreilly.programmingflex.MainClass").info("Log
Message");
}
]]>
</mx:Script>
<mx:Button click="sendToLog()" label="Log Message"/>
</mx:Application>
In this example, clicking on a button will send a message in the
same manner as calling trace() would.
The main distinction to just using trace() is the ability to configure the target
to include extra information, define a category for a message, and have
different levels of errors.
Note: The Flex framework internally uses the logging framework within
the mx.rpc.* package with the
WebService, RemoteObject, and HTTPService components. This allows you to
retrieve details of the communication between the Flex client and the
server. We will cover debugging remote data communication later in this
chapter.
A target can support extra functionality. In the preceding example, the date, category, and level were enabled. This will instruct the target to include the time, category of message, and level with the messages. The built-in targets support other properties that you may want to explore.
Specifying the Logging Options
A log message must define two values: the level of the message,
which we discussed, and the category. The category is required to define
the origins of a message and, in return, allow you to filter what is
displayed by the logging framework. In the preceding example, the
category was com.oreilly.programmingflex.MainClass. It is a
good idea to specify a category based on the package and class, as this
will allow you to easily filter and identify the origins of logged
messages.
The built-in targets support the ability to filter the messages so
that only messages you are interested in are displayed. This is useful
in cases where you're interested only in log messages that are within a
certain package, and it's achieved via the filters property of the target. The filters property
accepts an array of categories. A category filter can be any text value,
but it is recommended that you follow package-naming conventions. You
may also specify an * (wildcard)
filter—for example, the following category filter of com.oreilly.* will instruct the target to
output all messages in the com.oreilly package and within its
subpackages:
_target.filters = ["com.oreilly.*"];
You also can define multiple filters as well as redefine the filters at any time. Setting the level is achieved in a similar manner.
The default logging level is ALL, but you can define another level by
setting the level property:
_target.level = LogEventLevel.FATAL;
The logger supports sending several levels of messages with the
debug(), info(), warn(), error(), and fatal() methods. Alternatively, you can call
the log() method of the logger and
pass in a log level. You can find the different levels with the constant
values LogEventLevel.FATAL,
LogEventLevel.ERROR, LogEventLevel.WARN, LogEventLevel.INFO, and LogEventLevel.DEBUG. This can be useful if you
want to output all messages during development and debugging, but limit
what is output in a production environment. When you set a log level,
all messages in that level and above are logged. For example, setting
the level to WARN will log all
messages with that level as well as messages with a FATAL or ERROR level.
|
Defining a Custom Target
If the built-in targets are not sufficient, you can define your
own. To define your own target you need to implement the ILoggingTarget interface. For convenience, the logging framework includes the
AbstractTarget class, which already implements a default set of behaviors that
you can easily subclass to define your own target. Example 18-2 is a custom target that will send a message to a remote server via the Socket class rather than via trace().
Example 18-2. Custom target sending a message to a remote server via the Socket class
package com.oreilly.programmingflex.logging.targets
{
import flash.net.Socket;
import mx.logging.LogEvent;
import mx.logging.AbstractTarget;
public class SocketTarget extends AbstractTarget
{
private var _host:String;
private var _port:int;
private var _socket:Socket;
public function SocketTarget(host:String = "localhost",port:int = 18080)
{
_host = host;
_port = port;
//This example omits the error handling. For production you will
//need to handle errors when creating the socket and when sending
//messages
_socket = new Socket(host,port);
super();
}
override public function logEvent(event:LogEvent):void
{
_socket.writeUTF(event.message);
_socket.flush();
}
}
}
Example 18-3 is updated to use the new SocketTarget.
Example 18-3. Example 18-2 updated to use the new SocketTarget
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
initialize="initializeHandler()">
<mx:Script>
<![CDATA[
import com.oreilly.programmingflex.logging.targets.SocketTarget;
import mx.logging.Log;
private var _target:SocketTarget;
private function initializeHandler():void
{
_target = new SocketTarget();
Log.addTarget(_target);
}
private function sendToLog():void
{
Log.getLogger("com.oreilly.programmingflex.MainClass").info("Log
Message");
}
]]>
</mx:Script>
<mx:Button click="sendToLog()" label="Log Message"/>
</mx:Application>
With Flex's built-in logging framework, you will be able to log messages, easily change options so that you can more easily debug an application, and integrate the framework within your application.
Section 18.7: Debugging Remote Data
Although you can use the debugger to inspect data after Flex has received it and before Flex sends it, you may want to find out more details regarding what data is being sent and received. You can achieve this by using the logging framework or a data inspector.
Debugging with the Flex Logging Framework
The WebService, HTTPService, and RemoteObject components use the Flex logging
framework, which can greatly assist debugging applications. Messages
are automatically logged to the Flex logging framework, so you won't
need to enable the components to explicitly begin logging. Messages that
are logged are within the mx.messaging.* filter. Example 18-4 is an HTTPService call with a TraceTarget that will show only log messages
related to the server calls.
Example 18-4. HTTPService call with a TraceTarget that shows log messages related to server calls
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
initialize="initializeHandler()">
<mx:Script>
<![CDATA[
import mx.logging.Log;
import mx.logging.targets.TraceTarget;
private var _target:TraceTarget;
private function initializeHandler():void
{
_target = new TraceTarget();
_target.includeTime = true;
_target.includeLevel = true;
_target.includeCategory = true;
_target.filters = ["mx.messaging.*"];
Log.addTarget(_target);
}
private function sendToLog():void
{
Log.getLogger("com.oreilly.programmingflex.Logging").
info("Log Message");
}
]]>
</mx:Script>
<mx:Button click="sendToLog()" label="Log Message"/>
<mx:Button click="service.send();" label="Send HTTPService"/>
<mx:HTTPService id="service" url="http://www.w3c.org"/>
</mx:Application>
This example will log messages from the HTTPService but not from the button click
handler, which can be very useful when you are working with a larger
application and you are interested in viewing only the log information
from the mx.rpc components. The
server component logs useful information on both the data that is being
sent and received, as well as the information that can be used for
profiling messaging performance. For the WebService component, this can be especially
useful in gauging Flex's performance in terms of serializing and
deserializing SOAP messages.
This excerpt is from Programming Flex 3. If you want to try your hand at developing rich Internet applications with Adobe's Flex 3, and already have experience with frameworks such as .NET or Java, this is the ideal book to get you started. Programming Flex 3 gives you a solid understanding of Flex 3's core concepts, and valuable insight into how, why, and when to use specific Flex features. Learn to get the most from this amazing and sophisticated technology.
Debugging Using a Data Inspector
When debugging network programming code, using a data inspector (packet sniffing tools or a proxy) is invaluable. With Flex, these tools can also be very useful. Adobe does not provide such a built-in tool, but many tools exist that work with Flex. If you are already comfortable with a tool, you can continue to use that tool.
Some common network debugging tools include the following:
- Charles
- This cross-platform proxy tool for debugging RPC communication also supports AMF3 (http://www.charlesproxy.com/).
- ServiceCapture
- This cross-platform proxy tool for debugging RPC communication supports AMF3 as well (http://kevinlangdon.com/serviceCapture).
- Wireshark (similar to Ethereal)
- This is a feature-complete packet sniffer that is capable of inspecting all traffic for both real-time applications as well as RPC (http://www.wireshark.org).
- Fiddler
- This is a quick HTTP proxy debugger that is free. It supports RPC debugging, but does not support AMF3 (http://www.fiddlertool.com).
Section 18.8: Summary
In many ways, a development platform is only as good as the debugging capabilities available to the developer. In this chapter, we covered many of the methods you can use to debug Flex applications.
Skip to any available Digital Media Help Center chapter of Programming Flex 3: |
Copyright © 2009 O'Reilly Media, Inc.





