early es6 test class for Dragonfly
This commit is contained in:
		
							
								
								
									
										234
									
								
								Dragonfly.js
									
									
									
									
									
								
							
							
						
						
									
										234
									
								
								Dragonfly.js
									
									
									
									
									
								
							@@ -1,3 +1,4 @@
 | 
			
		||||
"use strict";
 | 
			
		||||
var util = require( "util" );
 | 
			
		||||
 | 
			
		||||
/*{{{ Private methods */
 | 
			
		||||
@@ -22,129 +23,148 @@ function logDate( date )
 | 
			
		||||
}
 | 
			
		||||
/*}}}*/
 | 
			
		||||
// Logger
 | 
			
		||||
function Dragonfly( logHandler )
 | 
			
		||||
class Dragonfly
 | 
			
		||||
{
 | 
			
		||||
	this.currentSphere = Dragonfly.defaultSphere;
 | 
			
		||||
	this.Visibility = Dragonfly.Visibility;
 | 
			
		||||
	this.Spheres = Dragonfly.Spheres;
 | 
			
		||||
 | 
			
		||||
	// Bind prototype functions
 | 
			
		||||
	for( var i in Dragonfly.prototype )
 | 
			
		||||
	// Static properties
 | 
			
		||||
	static get defaultSphere()
 | 
			
		||||
	{
 | 
			
		||||
		Dragonfly[i] = this[i].bind( this );
 | 
			
		||||
		return this.__dsphere;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	static set defaultSphere( v )
 | 
			
		||||
	{
 | 
			
		||||
		return this.__dsphere = Math.floor( v + 0 );
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var cluster = require( "cluster" );
 | 
			
		||||
	if( cluster.isMaster )
 | 
			
		||||
	static get Spheres()
 | 
			
		||||
	{
 | 
			
		||||
		this.logHandler = logHandler || { write: console.log };
 | 
			
		||||
		this.messageBus = function( msg )
 | 
			
		||||
		return {
 | 
			
		||||
			// Debug
 | 
			
		||||
			THERMO: 30
 | 
			
		||||
			// Inspect
 | 
			
		||||
			, STRATO: 20
 | 
			
		||||
			// Production
 | 
			
		||||
			, HYDRO: 10
 | 
			
		||||
			, LITHO: 0
 | 
			
		||||
		};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	static get Visibility()
 | 
			
		||||
	{
 | 
			
		||||
		return {
 | 
			
		||||
			VISIBLE: 9
 | 
			
		||||
			, VH8: 8, VH7: 7, VH6: 6
 | 
			
		||||
			, HIDDEN: 5
 | 
			
		||||
			, HU4: 4, HU3: 3, HU2: 2
 | 
			
		||||
			, UNSEEN: 1
 | 
			
		||||
		};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	constructor( logHandler )
 | 
			
		||||
	{
 | 
			
		||||
		this.currentSphere = Dragonfly.defaultSphere;
 | 
			
		||||
		this.Visibility = Dragonfly.Visibility;
 | 
			
		||||
		this.Spheres = Dragonfly.Spheres;
 | 
			
		||||
 | 
			
		||||
		// Bind prototype functions
 | 
			
		||||
		for( var i in Dragonfly.prototype )
 | 
			
		||||
		{
 | 
			
		||||
			if( msg.cmd == "dragonLog" )
 | 
			
		||||
			Dragonfly[i] = this[i].bind( this );
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		var cluster = require( "cluster" );
 | 
			
		||||
		if( cluster.isMaster )
 | 
			
		||||
		{
 | 
			
		||||
			this.logHandler = logHandler || { write: console.log };
 | 
			
		||||
			this.messageBus = function( msg )
 | 
			
		||||
			{
 | 
			
		||||
				this.logHandler.write( msg.data );
 | 
			
		||||
			}
 | 
			
		||||
		}.bind( this );
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		this.logHandler = { write: function( e ) { process.send({ cmd: "dragonLog", data: e }); } };
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var cluster = require("cluster");
 | 
			
		||||
	var tag = cluster.isMaster ? "M" : "S";
 | 
			
		||||
	this.ptag = "[ " + tag + ":" + process.pid + " ] ";
 | 
			
		||||
 | 
			
		||||
	this.Info( "Dragonfly ready.", Dragonfly.Visibility.VISIBLE );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Dragonfly.prototype.Debug = function( mesg, visibility )
 | 
			
		||||
{
 | 
			
		||||
	this.Log( mesg, Dragonfly.Spheres.THERMO, visibility );
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
Dragonfly.prototype.Info = function( mesg, visibility )
 | 
			
		||||
{
 | 
			
		||||
	this.Log( mesg, Dragonfly.Spheres.STRATO, visibility );
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
Dragonfly.prototype.Warning = function( mesg, visibility )
 | 
			
		||||
{
 | 
			
		||||
	this.Log( mesg, Dragonfly.Spheres.HYDRO, visibility );
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
Dragonfly.prototype.Error = function( mesg, visibility )
 | 
			
		||||
{
 | 
			
		||||
	this.Log( mesg, Dragonfly.Spheres.LITHO, visibility );
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
Dragonfly.prototype.Log = function( mesg, sphere, visibility )
 | 
			
		||||
{
 | 
			
		||||
	if( isNaN( sphere ) ) sphere = Dragonfly.Spheres.LITHO;
 | 
			
		||||
 | 
			
		||||
	visibility = Number( visibility );
 | 
			
		||||
	isNaN( visibility ) && ( visibility = 0 );
 | 
			
		||||
 | 
			
		||||
	sphere += visibility;
 | 
			
		||||
 | 
			
		||||
	var write = true;
 | 
			
		||||
	if( this.currentSphere < sphere )
 | 
			
		||||
	{
 | 
			
		||||
		write = ( this.currentSphere % 10 < sphere % 10 );
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if( write )
 | 
			
		||||
	{
 | 
			
		||||
		typeof( mesg ) == "function"
 | 
			
		||||
			? mesg( this.writeLine.bind( this ) )
 | 
			
		||||
			: this.writeLine( mesg )
 | 
			
		||||
			;
 | 
			
		||||
	}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
Dragonfly.prototype.writeLine = function ()
 | 
			
		||||
{
 | 
			
		||||
	for( var i in arguments )
 | 
			
		||||
	{
 | 
			
		||||
		if( typeof( arguments[i] ) == "string" )
 | 
			
		||||
		{
 | 
			
		||||
			this.__log( arguments[i] );
 | 
			
		||||
				if( msg.cmd == "dragonLog" )
 | 
			
		||||
				{
 | 
			
		||||
					this.logHandler.write( msg.data );
 | 
			
		||||
				}
 | 
			
		||||
			}.bind( this );
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			var lines = util.inspect( arguments[i] ).split("\n");
 | 
			
		||||
			for( var j in lines )
 | 
			
		||||
			this.logHandler = { write: function( e ) { process.send({ cmd: "dragonLog", data: e }); } };
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		var cluster = require("cluster");
 | 
			
		||||
		var tag = cluster.isMaster ? "M" : "S";
 | 
			
		||||
		this.ptag = "[ " + tag + ":" + process.pid + " ] ";
 | 
			
		||||
 | 
			
		||||
		this.Info( "Dragonfly ready.", Dragonfly.Visibility.VISIBLE );
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	Debug( mesg, visibility )
 | 
			
		||||
	{
 | 
			
		||||
		this.Log( mesg, Dragonfly.Spheres.THERMO, visibility );
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	Info( mesg, visibility )
 | 
			
		||||
	{
 | 
			
		||||
		this.Log( mesg, Dragonfly.Spheres.STRATO, visibility );
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	Warning( mesg, visibility )
 | 
			
		||||
	{
 | 
			
		||||
		this.Log( mesg, Dragonfly.Spheres.HYDRO, visibility );
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	Error( mesg, visibility )
 | 
			
		||||
	{
 | 
			
		||||
		this.Log( mesg, Dragonfly.Spheres.LITHO, visibility );
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	Log( mesg, sphere, visibility )
 | 
			
		||||
	{
 | 
			
		||||
		if( isNaN( sphere ) ) sphere = Dragonfly.Spheres.LITHO;
 | 
			
		||||
 | 
			
		||||
		visibility = Number( visibility );
 | 
			
		||||
		isNaN( visibility ) && ( visibility = 0 );
 | 
			
		||||
 | 
			
		||||
		sphere += visibility;
 | 
			
		||||
 | 
			
		||||
		var write = true;
 | 
			
		||||
		if( this.currentSphere < sphere )
 | 
			
		||||
		{
 | 
			
		||||
			write = ( this.currentSphere % 10 < sphere % 10 );
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if( write )
 | 
			
		||||
		{
 | 
			
		||||
			typeof( mesg ) == "function"
 | 
			
		||||
				? mesg( this.writeLine.bind( this ) )
 | 
			
		||||
				: this.writeLine( mesg )
 | 
			
		||||
				;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	writeLine ()
 | 
			
		||||
	{
 | 
			
		||||
		for( var i in arguments )
 | 
			
		||||
		{
 | 
			
		||||
			if( typeof( arguments[i] ) == "string" )
 | 
			
		||||
			{
 | 
			
		||||
				this.__log( lines[j] );
 | 
			
		||||
				this.__log( arguments[i] );
 | 
			
		||||
			}
 | 
			
		||||
			else
 | 
			
		||||
			{
 | 
			
		||||
				var lines = util.inspect( arguments[i] ).split("\n");
 | 
			
		||||
				for( var j in lines )
 | 
			
		||||
				{
 | 
			
		||||
					this.__log( lines[j] );
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
Dragonfly.prototype.__log = function ( line )
 | 
			
		||||
{
 | 
			
		||||
	this.logHandler.write( this.ptag + logDate( new Date() ) + util.format( line ) + "\n" );
 | 
			
		||||
};
 | 
			
		||||
	__log ( line )
 | 
			
		||||
	{
 | 
			
		||||
		this.logHandler.write( this.ptag + logDate( new Date() ) + util.format( line ) + "\n" );
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Static properties
 | 
			
		||||
Dragonfly.defaultSphere = 10;
 | 
			
		||||
 | 
			
		||||
Dragonfly.Spheres = {
 | 
			
		||||
	// Debug
 | 
			
		||||
	THERMO: 30
 | 
			
		||||
	// Inspect
 | 
			
		||||
	, STRATO: 20
 | 
			
		||||
	// Production
 | 
			
		||||
	, HYDRO: 10
 | 
			
		||||
	, LITHO: 0
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
Dragonfly.Visibility = {
 | 
			
		||||
	VISIBLE: 9
 | 
			
		||||
	, VH8: 8, VH7: 7, VH6: 6
 | 
			
		||||
	, HIDDEN: 5
 | 
			
		||||
	, HU4: 4, HU3: 3, HU2: 2
 | 
			
		||||
	, UNSEEN: 1
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module.exports = Dragonfly;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user