BotanSS/Dragonfly.js

246 lines
4.2 KiB
JavaScript
Raw Normal View History

2015-10-19 10:50:17 +00:00
"use strict";
2022-04-04 05:56:36 +00:00
const util = require( "util" );
const cluster = require( "cluster" );
2014-10-10 08:38:06 +00:00
/*{{{ Private methods */
// Months
var mon = [ '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12' ];
function padZero( num )
{
if( num < 10 ) return "0" + String( num );
return String( num );
}
function logDate( date )
{
return "[ " + date.getFullYear()
+ "-" + mon[ date.getMonth() ]
+ "-" + padZero( date.getDate() )
+ " " + padZero( date.getHours() )
+ ":" + padZero( date.getMinutes() )
+ ":" + padZero( date.getSeconds() )
+ " ] ";
}
2022-04-04 05:56:36 +00:00
function test_startsWithAny( line, conds )
{
if( conds === undefined )
return true;
for( let cond of conds )
{
if( line.startsWith( cond ) )
{
return true;
}
}
return false;
}
function test_endsWithAny( line, conds )
{
if( conds === undefined )
return true;
for( let cond of conds )
{
if( line.endsWith( cond ) )
{
return true;
}
}
return false;
}
function test_containsAll( line, conds )
{
if( conds === undefined )
return true;
for( let cond of conds )
{
if( !~line.indexOf( cond ) )
{
return false;
}
}
return true;
}
2014-10-10 08:38:06 +00:00
/*}}}*/
// Logger
2015-10-19 10:50:17 +00:00
class Dragonfly
2014-10-10 08:38:06 +00:00
{
2015-10-19 10:50:17 +00:00
// Static properties
static get defaultSphere()
2014-10-10 08:38:06 +00:00
{
2015-10-19 10:50:17 +00:00
return this.__dsphere;
2014-10-10 08:38:06 +00:00
}
2015-10-19 10:50:17 +00:00
static set defaultSphere( v )
2015-01-09 11:01:47 +00:00
{
2015-10-19 10:50:17 +00:00
return this.__dsphere = Math.floor( v + 0 );
2015-01-09 11:01:47 +00:00
}
2015-10-19 10:50:17 +00:00
static get Spheres()
2015-01-09 11:01:47 +00:00
{
2015-10-19 10:50:17 +00:00
return {
// Debug
THERMO: 30
// Inspect
, STRATO: 20
// Production
, HYDRO: 10
, LITHO: 0
};
2015-01-09 11:01:47 +00:00
}
2015-10-19 10:50:17 +00:00
static get Visibility()
{
return {
VISIBLE: 9
, VH8: 8, VH7: 7, VH6: 6
, HIDDEN: 5
, HU4: 4, HU3: 3, HU2: 2
, UNSEEN: 1
};
}
2014-10-10 08:38:06 +00:00
2015-10-19 10:50:17 +00:00
constructor( logHandler )
{
this.currentSphere = Dragonfly.defaultSphere;
this.Visibility = Dragonfly.Visibility;
this.Spheres = Dragonfly.Spheres;
2022-04-04 05:56:36 +00:00
this._configured = false;
2014-10-10 08:38:06 +00:00
2015-10-19 10:50:17 +00:00
// Bind prototype functions
for( var i in Dragonfly.prototype )
{
Dragonfly[i] = this[i].bind( this );
}
2014-10-10 08:38:06 +00:00
2015-10-19 10:50:17 +00:00
if( cluster.isMaster )
{
this.logHandler = logHandler || { write: console.log };
this.messageBus = function( msg )
{
if( msg.cmd == "dragonLog" )
{
this.logHandler.write( msg.data );
}
}.bind( this );
}
else
{
2022-04-04 05:56:36 +00:00
// Send to master to prevent multiple process competing to write into the handler
2015-10-19 10:50:17 +00:00
this.logHandler = { write: function( e ) { process.send({ cmd: "dragonLog", data: e }); } };
}
2014-10-10 08:38:06 +00:00
2015-10-19 10:50:17 +00:00
var tag = cluster.isMaster ? "M" : "S";
this.ptag = "[ " + tag + ":" + process.pid + " ] ";
2014-10-10 08:38:06 +00:00
2022-04-03 12:52:44 +00:00
// this.Info( "Dragonfly ready.", Dragonfly.Visibility.VISIBLE );
2015-10-19 10:50:17 +00:00
}
2014-10-10 08:38:06 +00:00
2015-10-19 10:50:17 +00:00
Debug( mesg, visibility )
{
this.Log( mesg, Dragonfly.Spheres.THERMO, visibility );
}
2014-10-10 08:38:06 +00:00
2015-10-19 10:50:17 +00:00
Info( mesg, visibility )
{
this.Log( mesg, Dragonfly.Spheres.STRATO, visibility );
}
2014-10-10 08:38:06 +00:00
2015-10-19 10:50:17 +00:00
Warning( mesg, visibility )
2014-10-10 08:38:06 +00:00
{
2015-10-19 10:50:17 +00:00
this.Log( mesg, Dragonfly.Spheres.HYDRO, visibility );
2014-10-10 08:38:06 +00:00
}
2015-10-19 10:50:17 +00:00
Error( mesg, visibility )
2015-02-06 04:39:32 +00:00
{
2015-10-19 10:50:17 +00:00
this.Log( mesg, Dragonfly.Spheres.LITHO, visibility );
2015-02-06 04:39:32 +00:00
}
2014-10-10 08:38:06 +00:00
2015-10-19 10:50:17 +00:00
Log( mesg, sphere, visibility )
2014-10-10 08:38:06 +00:00
{
2015-10-19 10:50:17 +00:00
if( isNaN( sphere ) ) sphere = Dragonfly.Spheres.LITHO;
visibility = Number( visibility );
isNaN( visibility ) && ( visibility = 0 );
sphere += visibility;
var write = true;
if( this.currentSphere < sphere )
2014-10-10 08:38:06 +00:00
{
2015-10-19 10:50:17 +00:00
write = ( this.currentSphere % 10 < sphere % 10 );
2014-10-10 08:38:06 +00:00
}
2015-10-19 10:50:17 +00:00
if( write )
2014-10-10 08:38:06 +00:00
{
2015-10-19 10:50:17 +00:00
typeof( mesg ) == "function"
? mesg( this.writeLine.bind( this ) )
: this.writeLine( mesg )
;
}
}
2022-04-04 05:56:36 +00:00
writeLine()
2015-10-19 10:50:17 +00:00
{
2022-04-04 05:56:36 +00:00
for( let x of arguments )
2015-10-19 10:50:17 +00:00
{
2022-04-04 05:56:36 +00:00
if( typeof( x ) == "string" )
2015-10-19 10:50:17 +00:00
{
2022-04-04 05:56:36 +00:00
if( this._exclude( x ) )
return;
this.__log( x );
2015-10-19 10:50:17 +00:00
}
else
2014-10-10 08:38:06 +00:00
{
2022-04-04 05:56:36 +00:00
var lines = util.inspect( x );
if( this._exclude( lines ) )
return;
for( let line of lines.split("\n") )
2015-10-19 10:50:17 +00:00
{
2022-04-04 05:56:36 +00:00
this.__log( line );
2015-10-19 10:50:17 +00:00
}
2014-10-10 08:38:06 +00:00
}
}
}
2022-04-04 05:56:36 +00:00
_exclude( line )
{
if( !this._configured )
this._configure();
for( let ex of this._excludes )
{
if( test_startsWithAny( line, ex[ "^" ] )
&& test_endsWithAny( line, ex[ "$" ] )
&& test_containsAll( line, ex[ "~" ] ) )
{
return true;
}
}
return false;
}
_configure()
{
this._configured = true;
var cl = global.botanLoader;
this._excludes = cl.load( "config.all" ).log.exclude;
}
2015-10-19 10:50:17 +00:00
__log ( line )
{
2022-04-04 05:56:36 +00:00
this.logHandler.write( this.ptag + logDate( new Date() ) + line + "\n" );
2015-10-19 10:50:17 +00:00
}
}
2014-10-10 08:38:06 +00:00
2015-01-09 11:01:47 +00:00
Dragonfly.defaultSphere = 10;
module.exports = Dragonfly;