1 /**
  2  * @author Nick de Groot <nick.degroot[at]vivesta[dot]com>
  3  * @package Banana.Util
  4  * @summary Dom Helper. dom writer for various banana actions
  5  */
  6 
  7 goog.provide('Banana.Util.LogManager');
  8 
  9 // include different loggers
 10 goog.require('Banana.Util.Logger');
 11 goog.require('Banana.Util.Logger.Console');
 12 
 13 /**
 14  * @namespace Banana.Util
 15  */
 16 namespace('Banana.Util');
 17 
 18 /**
 19  * This file defines the LogManager. It allows sending log messages
 20  * to different logging instances
 21  * 
 22  * @constructor
 23  */
 24 Banana.Util.LogManager = function()
 25 {
 26 	this.loggers = [];
 27 	
 28 	this.errorData = []; // storing error data
 29 	
 30 	this.levels = {};
 31 	this.levels.debug = false;
 32 	this.levels.notice = false;
 33 	this.levels.warning = false;
 34 	this.levels.error = false;
 35 	this.levels.route = false;	
 36 	
 37 	this.showTime = true;
 38 };
 39 
 40 /**
 41  * Add a logger
 42  * 
 43  * @param String|Banana.Util.Logger logger Logger to add
 44  */
 45 Banana.Util.LogManager.prototype.addLogger = function(logger)
 46 {
 47 	if (typeof(logger) == 'string')
 48 		logger = eval('new Banana.Util.Logger.' + logger);
 49 	this.loggers.push(logger);
 50 };
 51 
 52 /**
 53  * Show information about the logging. Which loggers are loaded
 54  * and which levels are shown
 55  * 
 56  */
 57 Banana.Util.LogManager.prototype.getInfo = function()
 58 {
 59 	var msg = "";
 60 	if (!this.loggers.length)
 61 	{
 62 		msg += "No loggers are defined\r\n";
 63 	}
 64 	else
 65 	{
 66 		msg += "Banana is using loggers:\r\n";
 67 		for (var x = 0; x < this.loggers.length; x++)
 68 		{
 69 			msg += this.loggers[x].id + "\r\n";
 70 		}
 71 	}
 72 	
 73 	msg += "\r\nLogger is showing messages:\r\n";
 74 	for (var i in this.levels)
 75 	{
 76 		if (this.levels[i])
 77 		{
 78 			msg += i + "\r\n";
 79 		}
 80 	}
 81 	
 82 	alert(msg);	
 83 };
 84 
 85 /**
 86  * Turn a level on or off
 87  * 
 88  * @param String  level Level to set
 89  * @param Boolean value Value to set
 90  */
 91 Banana.Util.LogManager.prototype.setLevel = function (level, value)
 92 {
 93 	this.levels[level] = value;
 94 };
 95 
 96 /**
 97  * Should times be shown in the messages
 98  * 
 99  * @param Boolean value True if it should
100  */
101 Banana.Util.LogManager.prototype.showTimes = function (value)
102 {
103 	this.showTime = value;
104 };
105 
106 /**
107  * Add error data to the logger to store
108  * 
109  * @param {Mixed}  data Error data
110  * @param {String} page [optional] Page error occured
111  */
112 Banana.Util.LogManager.prototype.addErrorData = function(data, page)
113 {
114 	if (!page)
115 		page = window.location.href;
116 	
117 	var error = {};
118 	error.page = page;
119 	error.data = data;
120 	
121 	this.errorData.push(error);
122 };
123 
124 /**
125  * Return the error data
126  * 
127  * @return {Array} The error data
128  */
129 Banana.Util.LogManager.prototype.getErrorData = function()
130 {
131 	return this.errorData;
132 }
133 
134 /**
135  * Clear the error data
136  *
137  */
138 Banana.Util.LogManager.prototype.resetErrorData = function()
139 {
140 	this.errorData = [];
141 }
142 
143 /**
144  * Write a message to all loggers
145  * 
146  * @param String msg Message to write
147  */
148 Banana.Util.LogManager.prototype.write = function(msg, type)
149 {
150 	if (type)
151 		msg = "[" + type + "]\t" + msg;
152 	
153 	if (this.showTime)
154 	{
155 		var d = new Date();
156 		var curr_hour = d.getHours();
157 		var curr_min = d.getMinutes();
158 
159 		var curr_sec = d.getSeconds();
160 		var curr_msec = d.getMilliseconds();
161 
162 		msg = curr_hour + ":" + curr_min + ":"	+ curr_sec + ":" + curr_msec + " | " + msg;		
163 	}
164 	
165 	for (var x = 0; x < this.loggers.length; x++)
166 		this.loggers[x].write(msg);
167 };
168 
169 /**
170  * Send a debug message
171  * 
172  * @param String msg Message
173  */
174 Banana.Util.LogManager.prototype.debug = function(msg)
175 {
176 	if (this.levels['debug'])
177 		this.write(msg, 'DEBUG');	
178 };
179 
180 /**
181  * Send a notice message
182  * 
183  * @param String msg Message
184  */
185 Banana.Util.LogManager.prototype.notice = function(msg)
186 {
187 	if (this.levels['notice'])
188 		this.write(msg, 'NOTICE');	
189 };
190 
191 /**
192  * Send a warning message
193  * 
194  * @param String msg Message
195  */
196 Banana.Util.LogManager.prototype.warning = function(msg)
197 {
198 	if (this.levels['warning'])
199 		this.write(msg, 'WARNING');	
200 };
201 
202 /**
203  * Send an error message
204  * 
205  * @param String msg Message
206  */
207 Banana.Util.LogManager.prototype.error = function(msg)
208 {
209 	if (this.levels['error'])
210 		this.write(msg, 'ERROR');	
211 };
212 
213 /**
214  * Send a route message
215  * 
216  * @param String msg Message
217  */
218 Banana.Util.LogManager.prototype.route = function(msg)
219 {
220 	if (this.levels['route'])
221 		this.write(msg, 'ROUTE');	
222 };
223 
224 
225