2012-03-05 14:07:38 +01:00
|
|
|
/**
|
|
|
|
* EGroupware clientside API object
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Andreas Stöckel (as AT stylite.de)
|
|
|
|
* @author Ralf Becker <RalfBecker@outdoor-training.de>
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*egw:uses
|
|
|
|
egw_core;
|
|
|
|
*/
|
|
|
|
|
2012-03-07 15:04:25 +01:00
|
|
|
egw.extend('debug', egw.MODULE_GLOBAL, function(_app, _wnd) {
|
2012-03-05 14:07:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DEBUGLEVEL specifies which messages are printed to the console.
|
|
|
|
* Decrease the value of EGW_DEBUGLEVEL to get less messages.
|
2014-01-20 16:23:40 +01:00
|
|
|
*
|
|
|
|
* @type Number
|
|
|
|
* 0 = off, no logging
|
|
|
|
* 1 = only "error"
|
|
|
|
* 2 = -- " -- plus "warning"
|
|
|
|
* 3 = -- " -- plus "info"
|
|
|
|
* 4 = -- " -- plus "log"
|
|
|
|
* 5 = -- " -- plus a stacktrace
|
2012-03-05 14:07:38 +01:00
|
|
|
*/
|
2014-01-21 10:21:46 +01:00
|
|
|
var DEBUGLEVEL = 3;
|
2012-03-05 14:07:38 +01:00
|
|
|
|
2014-01-20 16:23:40 +01:00
|
|
|
/**
|
|
|
|
* Log-level for local storage
|
|
|
|
*
|
|
|
|
* @type Number
|
|
|
|
*/
|
2014-01-21 10:21:46 +01:00
|
|
|
var LOCAL_LOG_LEVEL = 2;
|
2014-01-20 16:23:40 +01:00
|
|
|
/**
|
|
|
|
* Number of log-entries stored on client, new errors overwrite old ones
|
|
|
|
*
|
|
|
|
* @type Number
|
|
|
|
*/
|
|
|
|
var MAX_LOGS = 1000;
|
|
|
|
/**
|
|
|
|
* Number of last old log entry = next one to overwrite
|
|
|
|
*
|
|
|
|
* @type String
|
|
|
|
*/
|
|
|
|
var LASTLOG = 'lastLog';
|
|
|
|
/**
|
|
|
|
* Prefix for key of log-message, message number gets appended to it
|
|
|
|
*
|
|
|
|
* @type String
|
|
|
|
*/
|
|
|
|
var LOG_PREFIX = 'log_';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log to clientside html5 localStorage
|
|
|
|
*
|
|
|
|
* @param {String} _level "navigation", "log", "info", "warn", "error"
|
|
|
|
* @param {Array} _args arguments to egw.debug
|
|
|
|
* @returns {Boolean} false if localStorage is NOT supported, null if level requires no logging, true if logged
|
|
|
|
*/
|
|
|
|
function log_on_client(_level, _args)
|
|
|
|
{
|
|
|
|
if (!window.localStorage) return false;
|
|
|
|
|
|
|
|
switch(_level)
|
|
|
|
{
|
|
|
|
case 'warn':
|
|
|
|
if (LOCAL_LOG_LEVEL < 2) return null;
|
|
|
|
case 'info':
|
|
|
|
if (LOCAL_LOG_LEVEL < 3) return null;
|
|
|
|
case 'log':
|
|
|
|
if (LOCAL_LOG_LEVEL < 4) return null;
|
|
|
|
default:
|
|
|
|
if (!LOCAL_LOG_LEVEL) return null;
|
|
|
|
}
|
|
|
|
var data = {
|
|
|
|
time: (new Date()).getTime(),
|
|
|
|
level: _level,
|
2014-01-20 16:25:43 +01:00
|
|
|
args: _args
|
2014-01-20 16:23:40 +01:00
|
|
|
};
|
|
|
|
// Add in a trace, if no navigation _level
|
|
|
|
if (_level != 'navigation' && typeof (new Error).stack != 'undefined')
|
|
|
|
{
|
|
|
|
data.stack = (new Error).stack;
|
|
|
|
}
|
|
|
|
if (typeof window.localStorage[LASTLOG] == 'undefined')
|
|
|
|
{
|
|
|
|
window.localStorage[LASTLOG] = 0;
|
|
|
|
}
|
2014-01-21 12:23:26 +01:00
|
|
|
try {
|
|
|
|
window.localStorage[LOG_PREFIX+window.localStorage[LASTLOG]] = JSON.stringify(data);
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
// one of the args is not JSON.stringify, because it contains circular references eg. an et2 widget
|
|
|
|
for(var i=0; i < data.args.length; ++i)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
JSON.stringify(data.args[i]);
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
// for Class we try removing _parent and _children attributes and try again to stringify
|
|
|
|
if (data.args[i] instanceof Class)
|
|
|
|
{
|
2014-02-03 10:59:30 +01:00
|
|
|
data.args[i] = jQuery.extend({}, data.args[i]);
|
2014-01-21 12:23:26 +01:00
|
|
|
delete data.args[i]._parent;
|
|
|
|
delete data.args[i]._children;
|
|
|
|
try {
|
|
|
|
JSON.stringify(data.args[i]);
|
|
|
|
continue; // stringify worked --> check other arguments
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
// ignore error and remove whole argument
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if above doesnt work, we remove the attribute
|
|
|
|
data.args[i] = '** removed, circular reference **';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.localStorage[LOG_PREFIX+window.localStorage[LASTLOG]] = JSON.stringify(data);
|
|
|
|
}
|
2014-01-20 16:23:40 +01:00
|
|
|
window.localStorage[LASTLOG] = (1 + parseInt(window.localStorage[LASTLOG])) % MAX_LOGS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get log from localStorage with oldest message first
|
|
|
|
*
|
|
|
|
* @returns {Array} of Object with values for attributes level, message, trace
|
|
|
|
*/
|
|
|
|
function get_client_log()
|
|
|
|
{
|
|
|
|
var logs = [];
|
|
|
|
|
|
|
|
if (window.localStorage && typeof window.localStorage[LASTLOG] != 'undefined')
|
|
|
|
{
|
|
|
|
var lastlog = parseInt(window.localStorage[LASTLOG]);
|
2014-01-21 16:09:06 +01:00
|
|
|
for(var i=lastlog; i < lastlog+MAX_LOGS; ++i)
|
2014-01-20 16:23:40 +01:00
|
|
|
{
|
2014-01-21 16:09:06 +01:00
|
|
|
var log = window.localStorage[LOG_PREFIX+(i%MAX_LOGS)];
|
|
|
|
if (typeof log != 'undefined')
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
logs.push(JSON.parse(log));
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
// ignore not existing log entries
|
|
|
|
}
|
|
|
|
}
|
2014-01-20 16:23:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return logs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears whole client log
|
|
|
|
*/
|
|
|
|
function clear_client_log()
|
|
|
|
{
|
2014-01-21 10:21:46 +01:00
|
|
|
// Remove indicator icon
|
|
|
|
jQuery('#topmenu_info_error').remove();
|
2014-01-21 12:23:26 +01:00
|
|
|
|
2014-01-20 16:23:40 +01:00
|
|
|
if (!window.localStorage) return false;
|
|
|
|
|
|
|
|
for(var i=0; i < MAX_LOGS; ++i)
|
|
|
|
{
|
|
|
|
if (typeof window.localStorage[LOG_PREFIX+i] != 'undefined')
|
|
|
|
{
|
|
|
|
delete window.localStorage[LOG_PREFIX+i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete window.localStorage[LASTLOG];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-21 10:21:46 +01:00
|
|
|
/**
|
|
|
|
* Format one log message for display
|
|
|
|
*
|
2014-01-21 12:23:26 +01:00
|
|
|
* @param {Object} log {{level: string, time: number, stack: string, args: array[]}} Log information
|
2014-01-21 10:21:46 +01:00
|
|
|
* Actual message is in args[0]
|
|
|
|
* @returns {DOMNode}
|
|
|
|
*/
|
|
|
|
function format_message(log)
|
|
|
|
{
|
|
|
|
var row = document.createElement('tr');
|
|
|
|
row.setAttribute('class', log.level);
|
|
|
|
var timestamp = row.insertCell(-1);
|
|
|
|
timestamp.appendChild(document.createTextNode(new Date(log.time)));
|
|
|
|
timestamp.setAttribute('class', 'timestamp');
|
2014-01-21 12:23:26 +01:00
|
|
|
|
2014-01-21 10:21:46 +01:00
|
|
|
var level = row.insertCell(-1);
|
|
|
|
level.appendChild(document.createTextNode(log.level));
|
|
|
|
level.setAttribute('class', 'level');
|
|
|
|
|
|
|
|
var message = row.insertCell(-1);
|
|
|
|
for(var i = 0; i < log.args.length; i++)
|
|
|
|
{
|
|
|
|
|
|
|
|
var arg = document.createElement('p');
|
|
|
|
arg.appendChild(
|
|
|
|
document.createTextNode(typeof log.args[i] == 'string' ? log.args[i] : JSON.stringify( log.args[i]))
|
|
|
|
);
|
|
|
|
message.appendChild(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
var stack = row.insertCell(-1);
|
|
|
|
stack.appendChild(document.createTextNode(log.stack||''));
|
|
|
|
stack.setAttribute('class','stack');
|
|
|
|
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
2014-01-20 16:23:40 +01:00
|
|
|
/**
|
|
|
|
* Show user an error happend by displaying a clickable icon with tooltip of current error
|
|
|
|
*/
|
|
|
|
function raise_error()
|
|
|
|
{
|
|
|
|
var icon = jQuery('#topmenu_info_error');
|
|
|
|
if (!icon.length)
|
|
|
|
{
|
|
|
|
var icon = jQuery(egw(_wnd).image_element(egw.image('dialog_error')));
|
|
|
|
icon.addClass('topmenu_info_item').attr('id', 'topmenu_info_error');
|
|
|
|
// ToDo: tooltip
|
|
|
|
icon.on('click', egw(_wnd).show_log);
|
2014-01-21 12:23:26 +01:00
|
|
|
jQuery('#egw_fw_topmenu_info_items,#topmenu_info').append(icon);
|
2014-01-20 16:23:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// bind to global error handler
|
|
|
|
jQuery(_wnd).on('error', function(e)
|
|
|
|
{
|
|
|
|
log_on_client('error', [e.originalEvent.message]);
|
|
|
|
raise_error();
|
|
|
|
// rethrow error to let browser log and show it in usual way too
|
2014-02-10 10:18:08 +01:00
|
|
|
if (typeof e.originalEvent == 'object' && typeof e.originalEvent.error == 'object')
|
|
|
|
{
|
|
|
|
throw e.originalEvent.error;
|
|
|
|
}
|
|
|
|
throw e.message;
|
2014-01-20 16:23:40 +01:00
|
|
|
});
|
|
|
|
|
2012-03-05 14:07:38 +01:00
|
|
|
/**
|
|
|
|
* The debug function can be used to send a debug message to the
|
|
|
|
* java script console. The first parameter specifies the debug
|
|
|
|
* level, all other parameters are passed to the corresponding
|
|
|
|
* console function.
|
|
|
|
*/
|
|
|
|
return {
|
|
|
|
debug: function(_level) {
|
|
|
|
if (typeof _wnd.console != "undefined")
|
|
|
|
{
|
|
|
|
// Get the passed parameters and remove the first entry
|
|
|
|
var args = [];
|
|
|
|
for (var i = 1; i < arguments.length; i++)
|
|
|
|
{
|
|
|
|
args.push(arguments[i]);
|
|
|
|
}
|
|
|
|
|
2012-12-04 01:12:13 +01:00
|
|
|
// Add in a trace
|
2013-04-10 16:14:06 +02:00
|
|
|
if (DEBUGLEVEL >= 5 && typeof (new Error).stack != "undefined")
|
2012-12-04 01:12:13 +01:00
|
|
|
{
|
|
|
|
var stack = (new Error).stack;
|
|
|
|
args.push(stack);
|
|
|
|
}
|
|
|
|
|
2012-03-05 14:07:38 +01:00
|
|
|
if (_level == "log" && DEBUGLEVEL >= 4 &&
|
|
|
|
typeof _wnd.console.log == "function")
|
|
|
|
{
|
|
|
|
_wnd.console.log.apply(_wnd.console, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_level == "info" && DEBUGLEVEL >= 3 &&
|
|
|
|
typeof _wnd.console.info == "function")
|
|
|
|
{
|
|
|
|
_wnd.console.info.apply(_wnd.console, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_level == "warn" && DEBUGLEVEL >= 2 &&
|
|
|
|
typeof _wnd.console.warn == "function")
|
|
|
|
{
|
|
|
|
_wnd.console.warn.apply(_wnd.console, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_level == "error" && DEBUGLEVEL >= 1 &&
|
|
|
|
typeof _wnd.console.error == "function")
|
|
|
|
{
|
|
|
|
_wnd.console.error.apply(_wnd.console, args);
|
|
|
|
}
|
|
|
|
}
|
2014-01-20 16:23:40 +01:00
|
|
|
// raise errors to user
|
|
|
|
if (_level == "error") raise_error(args);
|
|
|
|
|
|
|
|
// log to html5 localStorage
|
|
|
|
if (typeof stack != 'undefined') args.pop(); // remove stacktrace again
|
|
|
|
log_on_client(_level, args);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display log to user because he clicked on icon showed by raise_error
|
|
|
|
*
|
|
|
|
* @returns {undefined}
|
|
|
|
*/
|
|
|
|
show_log: function()
|
|
|
|
{
|
2014-01-21 10:21:46 +01:00
|
|
|
var table = document.createElement('table');
|
|
|
|
var body = document.createElement('tbody');
|
|
|
|
var client_log = get_client_log();
|
|
|
|
for(var i = 0; i < client_log.length; i++)
|
|
|
|
{
|
|
|
|
body.appendChild(format_message(client_log[i]));
|
|
|
|
}
|
|
|
|
table.appendChild(body);
|
2014-01-21 12:23:26 +01:00
|
|
|
|
2014-01-21 10:21:46 +01:00
|
|
|
// Use a wrapper div for ease of styling
|
2014-01-21 12:23:26 +01:00
|
|
|
var wrapper = document.createElement('div');
|
2014-01-21 10:21:46 +01:00
|
|
|
wrapper.setAttribute('class', 'client_error_log');
|
|
|
|
wrapper.appendChild(table);
|
|
|
|
|
|
|
|
if(window.jQuery && window.jQuery.ui.dialog)
|
|
|
|
{
|
|
|
|
var $wrapper = $j(wrapper);
|
|
|
|
// Start hidden
|
|
|
|
$j('tr',$wrapper).addClass('hidden')
|
|
|
|
.on('click', function() {
|
|
|
|
$j(this).toggleClass('hidden',{});
|
|
|
|
$j(this).find('.stack').children().toggleClass('ui-icon ui-icon-circle-plus');
|
2014-01-21 12:23:26 +01:00
|
|
|
});
|
2014-01-21 10:21:46 +01:00
|
|
|
// Wrap in div so we can control height
|
|
|
|
$j('td',$wrapper).wrapInner('<div/>')
|
|
|
|
.filter('.stack').children().addClass('ui-icon ui-icon-circle-plus');
|
|
|
|
|
|
|
|
$wrapper.dialog({
|
|
|
|
title: egw.lang('Error log'),
|
|
|
|
buttons: [
|
|
|
|
{text: egw.lang('OK'), click: function() {$j(this).dialog( "close" ); }},
|
|
|
|
{text: egw.lang('clear'), click: function() {clear_client_log(); $j(this).empty();}}
|
|
|
|
],
|
|
|
|
width: 800,
|
|
|
|
height: 400
|
2014-01-21 12:23:26 +01:00
|
|
|
});
|
2014-01-21 10:21:46 +01:00
|
|
|
$wrapper[0].scrollTop = $wrapper[0].scrollHeight;
|
|
|
|
}
|
2014-01-20 16:23:40 +01:00
|
|
|
if (_wnd.console) _wnd.console.log(get_client_log());
|
2012-03-05 14:07:38 +01:00
|
|
|
}
|
2014-01-20 16:23:40 +01:00
|
|
|
};
|
2012-03-05 14:07:38 +01:00
|
|
|
});
|
2012-03-09 16:32:29 +01:00
|
|
|
|