Skip to content

Instantly share code, notes, and snippets.

@sixones
Created April 20, 2009 19:05
Show Gist options
  • Save sixones/98671 to your computer and use it in GitHub Desktop.
Save sixones/98671 to your computer and use it in GitHub Desktop.
/**
* The MIT License
*
* Copyright (c) 2009 Adam Livesley
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
import flash.external.ExternalInterface;
/**
* Flex 3 / Actionscript wrapper for the Firebug console, allows messages
* to be outputted through the Firebug console.
*/
public class FirebugConsole
{
protected var _firebugAvailable:Boolean = false;
protected var _firebugVersion:String = "";
protected var _previouslyChecked:Boolean = false;
public static const INFO:String = "info";
public static const WARN:String = "warn";
public static const ERROR:String = "error";
public static const LOG:String = "log";
public function FirebugConsole()
{
this.checkForFirebug();
}
/**
* Create's an info message on the Firebug console.
* @see FirebugConsole.sendToFirebug
*/
public function info(message:String):void
{
this.sendToFirebug(message, FirebugConsole.INFO);
}
/**
* Create's a warning message on the Firebug console.
* @see FirebugConsole.sendToFirebug
*/
public function warn(message:String):void
{
this.sendToFirebug(message, FirebugConsole.WARN);
}
/**
* Create's an error message on the Firebug console.
* @see FirebugConsole.sendToFirebug
*/
public function error(message:String):void
{
this.sendToFirebug(message, FirebugConsole.ERROR);
}
/**
* Create's a log message on the Firebug console.
* @see FirebugConsole.sendToFirebug
*/
public function log(message:String):void
{
this.sendToFirebug(message, FirebugConsole.LOG);
}
/**
* Send's a message to the Firebug console, selects the javascript
* function to call based on the specified type.
*/
public function sendToFirebug(message:String, type:String = "log"):void
{
if (!this._previouslyChecked)
{
this.checkForFirebug();
}
if (this._firebugAvailable)
{
ExternalInterface.call("console." + type, message);
}
}
/**
* Check's if Firebug is available or not within the current
* executed context. Once this method is called the
* <code>previouslyChecked</code> property will return true.
* The <code>firebugVersion</code> property is also populated
* during the check for Firebug.
*/
public function checkForFirebug():Boolean
{
this._firebugAvailable = false;
if (ExternalInterface.available)
{
try
{
this._firebugVersion = ExternalInterface.call("console.firebug");
}
catch (e:Error)
{
this._firebugVersion = "";
}
catch (e:SecurityError)
{
this._firebugVersion = "";
}
finally
{
if (this._firebugVersion != "")
{
this._firebugAvailable = true;
}
}
}
this._previouslyChecked = true;
return this._firebugAvailable;
}
/**
* Gets the current Firebug version, if Firebug is
* not available an empty string will be returned.
*/
public function get firebugVersion():String
{
return this._firebugVersion;
}
/**
* Specifies whether or not Firebug is available in
* the current runtime context.
*/
public function get firebugAvailable():Boolean
{
return this._firebugAvailable;
}
/**
* Specifies whether or not the existance of firebug has been
* checked yet or not.
*/
public function get previouslyChecked():Boolean
{
return this._previouslyChecked;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment