开发者

Show ActionScript console output in the actual GUI

my question is simple, yet I couldn't find any answer of it in the net, maybe it's impossible to do...

The thing is that I have an ActionScript 3.0 application, and wanted to include a little one-line size textbox which showed all the trace() calls and such, which are shown in the console.

Has anyone got any idea of how can it be done? I would really appreciate it, as I have a full project with traces on it that I'd like to show, and it's now when I'm finishing that I'm realising I don't know how to do it :P

Of course not everything is lost, as I could just do my own class that sho开发者_JS百科wed there the messages, but it would be cleanier, and quicker not to have to replace all the trace() calls for my new class and method.

Regards and thanks in advance :)


I just did this last week.

There are logging frameworks for Flex out there. A shame, though, that Flex's logging only works in Debug mode. If you search SO for Flex logging you'll find various suggestions. None of them are amazing, IMO.

Finally I rolled my own by just creating a Log class with a static function that acts as a proxy for trace.

Something like:

public static myTrace(... args) : void { ... }

Then you just forward the args to trace but also to whatever other destination you want (e.g. an array of strings + dates) that you can then display in the log window.

Incidentally, I also used SwfAddress to trigger the log window whenever a certain parameter is added to the URL. Very handy.

Oh, what the heck.. here's the class. It just keeps the last 100 strings and there's also a "dump" function that you can invoke if you want to send the data to your server or just quickly print the entire history.

public class Log
{
    public static var lines : ArrayList = new ArrayList();
    public static const MAX_LINES : int = 100;

    private static function logLine(line : String) : void
    {
        while (lines.length > MAX_LINES)
            lines.removeItemAt(0);
        lines.addItem({"line" : line, "time" : new Date()});
    }

    public static function logDump() : String
    {
        var ret : String = "";
        for each (var entry : Object in lines.source)
        {
            ret = (entry.time as Date).toUTCString() + " " + entry.line + "\n" + ret;
        }
        return ret;
    }

    public static function debug(...args) : void
    {
        trace(args);

        var line : String = "";
        for (var i : int = 0; i < args.length; i++)
            if (args[i] != null)
                line += args[i].toString();
        logLine(line);
    }
}


Alternatively, you can use the ASDebugger

http://labs.flexperiments.nl/asdebugger-20-a-real-time-debugger-and-editor/

ASDebugger.debug( 'shallala' );
ASDebugger.debug_prop( variable );

Try to avoid using the debug display object option. The debugger can crash for complex objects (especially in flex)

You can probably do a simple replacement of 'trace(' to 'ASDebugger.debug('

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜