tags:

views:

411

answers:

5

In Flash/Flex, is it possible to capture the result of 'trace' in code?

So, for example, if one part of the code calls trace("foo"), I'd like to automatically capture the string "foo" and pass it to some other function.

Edit: I'm not interested in trying to use trace instead of a proper logging framework… I want to write a plugin for FlexUnit, so when a test fails it can say something like: "Test blah failed. Here is the output: ... traced text ...".

Edit 2: I only want to capture the results of trace. Or, in other words, even though my code uses a proper logging framework, I want to handle gracefully code that's still using trace for logging.

A: 

If you want to write traces to a log, you can just use the Debug version of Flash Player and tell it to log traces.

Anon.
Unfortunately that only logs to a file… Which isn't quite what I want. I actually want to capture the logged messages in ActionScript.
David Wolever
+1  A: 

As far as I know it's impossible to do it externally, google brings up no results. Have you considered creating a variable for the output and then adding that to the log, eg:

var outputtext = "text";

trace(outputtext);

// log outputtext here

Disregard if it isn't feasible, but I can't think of any other way.

However you can do it internally, if it's just for development purposes: http://broadcast.artificialcolors.com/index.php?c=1&more=1&pb=1&tb=1&title=logging_flash_trace_output_to_a_text_fil

citricsquid
That is, of course, a possibility (I've actually got my logger setup so, when I'm developing, it just 'trace's the text)… But I'd like to capture *all* trace'd text - especially the "stupid debugging stuff" that someone left in there by mistake.
David Wolever
A: 

I have a Debug.write method that sends the passed messages over a LocalConnection which I use that instead of trace. My requirement is to be able to capture the debug statements even when the SWF is running out of the authoring environment, but you can use this method to capture the trace messages.

Amarghosh
I can see how you could use `Debug.write(...)` to capture messages… But how does that relate to capturing messages written with `trace(...)`?
David Wolever
You are gonna have to replace `trace` with `Debug.write`. The only way to capture trace messages is to use a debug player and edit your mm.cfg file (in your OS's user directory) - that is not really what you are after, you need to do it in your swf itself. So, you are gonna have to replace your traces
Amarghosh
A: 

As far as I understood you don't want to use logging, which is of course the right way to do it.

So, you can simply create a Static class with method trace, and call this method from anywhere in the application, that's how you will get all traces to one place, then could do what ever you want with the trace string before printing it to console.

Another way is to create bubbling trace event and dispatch it whenever you want to trace message, then add listener to STAGE for it and catch all events... Hope its help

FLEXpert
I realize that there are a bunch of different ways to get debug messages out of an application… But, for the moment, I'm only interested in getting `trace`'d messages. I'm writing test code (specifically, a FlexUnit Listener), and I'd like it if it would plug in nicely to *any* code (even code which uses `trace`).
David Wolever
A: 

I would suggest looking through the source for the swiz framework. They use the flex internal logLogger app-wide and use best practices in a good majority of their code.

jeremym
I'm not sure how that relates to my question…
David Wolever
David, it addresses your question to a tee. The logger framework for flex is a very powerful and dynamic tool (to not only use in test / locally as many here are proposing) but to monitor server calls, errors, faults, internal app failures, and how your users are generally using your application.Here's a simple post for you:http://www.colettas.org/?p=153I mentioned swiz because it is the only public example of logger use that I know of 'off the top'. We use it extensively at my place of employment, but that code is very private and government regulated.
jeremym
Funny... I just started reading that post. It pretty much says exactly what you're asking in the 2nd paragraph:"The Flex logging framework is a much more powerful way to log what’s going on in your application, like the trace() statement but much more. "
jeremym
and, if you're hip to using the debugger of flash player as most here are proposing: http://www.flexafterdark.com/docs/Flex-Logging Best of luck.
jeremym