Permalink
Browse files

Added servert js libraries

  • Loading branch information...
1 parent 256a141 commit d6867ce4141e44a97037b36fedc9baae722bde1f @a727891 committed Feb 3, 2013
Showing with 168 additions and 0 deletions.
  1. +98 −0 Server/lib/Logger.js
  2. +70 −0 Server/lib/class.js
View
@@ -0,0 +1,98 @@
+var Logger = function () {
+
+};
+
+Logger.prototype.debugEnable = false;
+Logger.prototype.TTYEcho = true;
+/*Console text color codes*/
+Logger.prototype.reset = '\033[0m';
+Logger.prototype.bold = '\033[1m';
+Logger.prototype.italic = '\033[3m';
+Logger.prototype.underline = '\033[4m';
+Logger.prototype.blink = '\033[5m';
+Logger.prototype.black = '\033[30m';
+Logger.prototype.red = '\033[31m';
+Logger.prototype.green = '\033[32m';
+Logger.prototype.yellow = '\033[33m';
+Logger.prototype.blue = '\033[34m';
+Logger.prototype.magenta = '\033[35m';
+Logger.prototype.cyan = '\033[36m';
+Logger.prototype.white = '\033[37m';
+
+/*Timestamp constants*/
+Logger.prototype.dateDelim = '/';
+Logger.prototype.timeDelim = ':';
+
+Logger.prototype.DebugEnable = function (boo) {
+ this.info("LOGGER DEBUGGING is now " + (boo ? "ON" : "OFF"));
+ this.debugEnable = boo;
+};
+Logger.prototype.TTYEchoEnable = function (boo) {
+ this.info("LOGGER TTY echo is now " + (boo ? "ON" : "OFF"));
+ this.TTYEcho = boo;
+};
+
+/*log
+ *writes a line to the console with a timestamp;
+ */
+Logger.prototype.log = function (data, hidden) {
+ if (!hidden) //If hidden argument is passed, don't display in console.
+ console.log(this.getTimeStamp() + data);
+ //Log to text file regardless.
+};
+/*getTimeStamp
+ *builds the current time in a string format
+ */
+Logger.prototype.getTimeStamp = function () {
+ var now = new Date(), year, month, day, hour, minute, second;
+ //Set Values
+ year = now.getFullYear().toString().substr(2);
+ month = this.padZero(now.getMonth() + 1);
+ day = this.padZero(now.getDate());
+ hour = this.padZero(now.getHours());
+ minute = this.padZero(now.getMinutes());
+ second = this.padZero(now.getSeconds());
+ //Build time string
+ return year + this.dateDelim + month + this.dateDelim + day + " " +
+ hour + this.timeDelim + minute + this.timeDelim + second
+};
+/*padZero
+ * val - number
+ * returns 0+number if less than 9, number otherwise
+ */
+Logger.prototype.padZero = function (val) {
+ return val <= 9 ? '0' + val : val;
+};
+/*info
+ * writes an info string to the console
+ */
+Logger.prototype.info = function (data) {
+ this.log(this.cyan + "[info ] " + this.reset + data);
+};
+/*debug
+ * writes a debug message to the console if enabled
+ */
+Logger.prototype.debug = function (data) {
+ if (this.debugEnable)
+ this.log(this.green + "[DEBUG] " + this.reset + data);
+};
+/*warn
+ * writes a warning string to the console
+ */
+Logger.prototype.warn = function (data) {
+ this.log(this.yellow + "[Warn ] " + this.reset + data);
+};
+/*Err
+ * writes an error string to the console
+ */
+Logger.prototype.err = function (data) {
+ this.log(this.red + "[ERROR] " + this.reset + data);
+};
+/*tty
+ * writes tty input to the log.
+ */
+Logger.prototype.tty = function (data) {
+ this.log(this.magenta + "[STDIN] " + data + this.reset, !this.TTYEcho);
+};
+/*export the Logger object */
+exports.Logger = Logger;
View
@@ -0,0 +1,70 @@
+/* Simple JavaScript Inheritance
+ * By John Resig http://ejohn.org/
+ * MIT Licensed.
+ */
+// Inspired by base2 and Prototype
+var initializing = false, fnTest = /xyz/.test(function () {
+ xyz;
+}) ? /\b_super\b/ : /.*/;
+
+// The base Class implementation (does nothing)
+Class = function () {
+};
+
+// Create a new Class that inherits from this class
+Class.extend = function (prop) {
+ var _super = this.prototype;
+
+ // Instantiate a base class (but only create the instance,
+ // don't run the init constructor)
+ initializing = true;
+ var prototype = new this();
+ initializing = false;
+
+ // Copy the properties over onto the new prototype
+ for (var name in prop) {
+ // Check if we're overwriting an existing function
+ prototype[name] = typeof prop[name] == "function" &&
+ typeof _super[name] == "function" && fnTest.test(prop[name]) ?
+ (function (name, fn) {
+ return function () {
+ var tmp = this._super;
+
+ // Add a new ._super() method that is the same method
+ // but on the super-class
+ this._super = _super[name];
+
+ // The method only need to be bound temporarily, so we
+ // remove it when we're done executing
+ var ret = fn.apply(this, arguments);
+ this._super = tmp;
+
+ return ret;
+ };
+ })(name, prop[name]) :
+ prop[name];
+ }
+
+ // The dummy class constructor
+ Class = function () {
+ // All construction is actually done in the init method
+ if (!initializing && this.init)
+ this.init.apply(this, arguments);
+ }
+
+ // Populate our constructed prototype object
+ Class.prototype = prototype;
+
+ // Enforce the constructor to be what we expect
+ Class.constructor = Class;
+
+ // And make this class extendable
+ Class.extend = arguments.callee;
+
+ return Class;
+};
+
+if (!(typeof exports === 'undefined')) {
+ exports.Class = Class;
+}
+

0 comments on commit d6867ce

Please sign in to comment.