Ultra Simple Output Class

Lately I’ve been messing around with Flash on devices and in other “new frontiers”. It’s fun, but sometimes getting debugging working is hit or miss. To address this, I wrote an ultra simple class for tracing to a text field. It’s nothing fancy, but that’s the point – it’s simple (~15 lines of code), tiny (~0.5kb), handy (at least for me), and works well, so I thought I’d share.

To use it, simply drop Out.as into your class directory. Set an output text field when your app starts up:

Out.textField = outFld;

You can also specify a maximum number of characters to retain, so that old traces will be deleted as new ones are added.

Out.maxLength = 1000;

From anywhere in your application you can call Out.trace(value). This will trace the value normally, append it to the end of your text field, and scroll the text field to the end so that the latest trace is always displayed.

Out.trace("hello");
Out.trace("count: ",count); // supports multiple params

Finally, if you want to do something more custom, you can override Out.handler to use your own function.

function log(...rest:Array):Boolean {
  trace("logging: "+rest.join(","));
  // ... etc.
  // only write to text field if first param (ex. priority) is > 1:
  return rest[0] > 1; // returning false prevents it being written to Out.textField.
}
Out.handler = log;

You can download the Out class here. It is licensed under the MIT license.

Grant Skinner

The "g" in gskinner. Also the "skinner".

@gskinner

8 Comments

  1. Nice class, i can see its potential for growth and extension. for less typing, i like Out.put(“msg”) instead of Out.trace(“msg”), what do you think?

  2. talking about small AND useful, even copyright comment has more lines of code 😀

  3. Nice, what I like about the way you designed this is that it links to an existing TextField you create however you want, rather than forcing on you some pre-built output display; and the custom handler goes well with this, too.

  4. Hi grant.

    I’m Korean developer.

    How do you think global package method?

    package

    {

    public function output( …$rest: Array ): void

    {

    // do what you do

    }

    }

    Like above.

    If you use this, you dont need to import anything and you can call output at every scopes.

    And code is shotter.

    I using global method called by “echo”(you may guess what I used to)

    Since I use global method, I dont feel need to anymore.

  5. Hi Can someone explain this to me:

    public static function trace(…rest:Array):void {

    “…rest” what does that signify, why the …

  6. @steve: it has been introduced in AS3, this is the help reference page that explains it: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/statements.html#…_%28rest%29_parameter

  7. It is well explained, short but concise. Gives me nice insights.

  8. This is a very nice class. Thanks!

    A nice enhancement anyone can throw on is to toggle the textfield’s visibility with an obscure combination of key presses like o+u+t. might not be so cool on mobile though.

Comments are closed.