Merge branch 'MDL-49269-master' of git://github.com/andrewnicols/moodle

This commit is contained in:
David Monllao 2015-03-11 08:23:30 +08:00
commit 7f7fd7e1e2
6 changed files with 252 additions and 0 deletions

1
lib/amd/build/log.min.js vendored Normal file
View File

@ -0,0 +1 @@
define(["core/loglevel"],function(a){var b=a.methodFactory;return a.methodFactory=function(a,c){var d=b(a,c);return function(a,b){d(b?b+": "+a:a)}},a.setConfig=function(b){"undefined"!=typeof b.level&&a.setLevel(b.level)},a});

1
lib/amd/build/loglevel.min.js vendored Normal file
View File

@ -0,0 +1 @@
!function(a,b){"object"==typeof module&&module.exports&&"function"==typeof require?module.exports=b():"function"==typeof define&&"object"==typeof define.amd?define(b):a.log=b()}(this,function(){function a(a){return typeof console===i?!1:void 0!==console[a]?b(console,a):void 0!==console.log?b(console,"log"):h}function b(a,b){var c=a[b];if("function"==typeof c.bind)return c.bind(a);try{return Function.prototype.bind.call(c,a)}catch(d){return function(){return Function.prototype.apply.apply(c,[a,arguments])}}}function c(a,b){return function(){typeof console!==i&&(d(b),g[a].apply(g,arguments))}}function d(a){for(var b=0;b<j.length;b++){var c=j[b];g[c]=a>b?h:g.methodFactory(c,a)}}function e(a){var b=(j[a]||"silent").toUpperCase();try{return void(window.localStorage.loglevel=b)}catch(c){}try{window.document.cookie="loglevel="+b+";"}catch(c){}}function f(){var a;try{a=window.localStorage.loglevel}catch(b){}if(typeof a===i)try{a=/loglevel=([^;]+)/.exec(window.document.cookie)[1]}catch(b){}void 0===g.levels[a]&&(a="WARN"),g.setLevel(g.levels[a])}var g={},h=function(){},i="undefined",j=["trace","debug","info","warn","error"];g.levels={TRACE:0,DEBUG:1,INFO:2,WARN:3,ERROR:4,SILENT:5},g.methodFactory=function(b,d){return a(b)||c(b,d)},g.setLevel=function(a){if("string"==typeof a&&void 0!==g.levels[a.toUpperCase()]&&(a=g.levels[a.toUpperCase()]),!("number"==typeof a&&a>=0&&a<=g.levels.SILENT))throw"log.setLevel() called with invalid level: "+a;return e(a),d(a),typeof console===i&&a<g.levels.SILENT?"No console available for logging":void 0},g.enableAll=function(){g.setLevel(g.levels.TRACE)},g.disableAll=function(){g.setLevel(g.levels.SILENT)};var k=typeof window!==i?window.log:void 0;return g.noConflict=function(){return typeof window!==i&&window.log===g&&(window.log=k),g},f(),g});

53
lib/amd/src/log.js Normal file
View File

@ -0,0 +1,53 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This is an empty module, that is required before all other modules.
* Because every module is returned from a request for any other module, this
* forces the loading of all modules with a single request.
*
* @module core/log
* @package core
* @copyright 2015 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define(['core/loglevel'], function(log) {
var originalFactory = log.methodFactory;
log.methodFactory = function (methodName, logLevel) {
var rawMethod = originalFactory(methodName, logLevel);
return function (message, source) {
if (source) {
rawMethod(source + ": " + message);
} else {
rawMethod(message);
}
};
};
/**
* Set default config settings.
*
* @param {String} level The level to use.
* @method setConfig
*/
log.setConfig = function(config) {
if (typeof config.level !== "undefined") {
log.setLevel(config.level);
}
};
return log;
});

184
lib/amd/src/loglevel.js Normal file
View File

@ -0,0 +1,184 @@
// The MIT License
//
// Copyright (c) 2014 Tom Perry
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Description of import into Moodle:
// Download from https://github.com/pimterry/loglevel/releases
// Copy loglevel.js into lib/amd/src/ in Moodle folder.
// Add the license as a comment to the file and these instructions.
// Add the jshint ignore:start and ignore:end comments.
/* jshint ignore:start */
/*! loglevel - v1.2.0 - https://github.com/pimterry/loglevel - (c) 2014 Tim Perry - licensed MIT */
(function (root, definition) {
if (typeof module === 'object' && module.exports && typeof require === 'function') {
module.exports = definition();
} else if (typeof define === 'function' && typeof define.amd === 'object') {
define(definition);
} else {
root.log = definition();
}
}(this, function () {
var self = {};
var noop = function() {};
var undefinedType = "undefined";
function realMethod(methodName) {
if (typeof console === undefinedType) {
return false; // We can't build a real method without a console to log to
} else if (console[methodName] !== undefined) {
return bindMethod(console, methodName);
} else if (console.log !== undefined) {
return bindMethod(console, 'log');
} else {
return noop;
}
}
function bindMethod(obj, methodName) {
var method = obj[methodName];
if (typeof method.bind === 'function') {
return method.bind(obj);
} else {
try {
return Function.prototype.bind.call(method, obj);
} catch (e) {
// Missing bind shim or IE8 + Modernizr, fallback to wrapping
return function() {
return Function.prototype.apply.apply(method, [obj, arguments]);
};
}
}
}
function enableLoggingWhenConsoleArrives(methodName, level) {
return function () {
if (typeof console !== undefinedType) {
replaceLoggingMethods(level);
self[methodName].apply(self, arguments);
}
};
}
var logMethods = [
"trace",
"debug",
"info",
"warn",
"error"
];
function replaceLoggingMethods(level) {
for (var i = 0; i < logMethods.length; i++) {
var methodName = logMethods[i];
self[methodName] = (i < level) ? noop : self.methodFactory(methodName, level);
}
}
function persistLevelIfPossible(levelNum) {
var levelName = (logMethods[levelNum] || 'silent').toUpperCase();
// Use localStorage if available
try {
window.localStorage['loglevel'] = levelName;
return;
} catch (ignore) {}
// Use session cookie as fallback
try {
window.document.cookie = "loglevel=" + levelName + ";";
} catch (ignore) {}
}
function loadPersistedLevel() {
var storedLevel;
try {
storedLevel = window.localStorage['loglevel'];
} catch (ignore) {}
if (typeof storedLevel === undefinedType) {
try {
storedLevel = /loglevel=([^;]+)/.exec(window.document.cookie)[1];
} catch (ignore) {}
}
if (self.levels[storedLevel] === undefined) {
storedLevel = "WARN";
}
self.setLevel(self.levels[storedLevel]);
}
/*
*
* Public API
*
*/
self.levels = { "TRACE": 0, "DEBUG": 1, "INFO": 2, "WARN": 3,
"ERROR": 4, "SILENT": 5};
self.methodFactory = function (methodName, level) {
return realMethod(methodName) ||
enableLoggingWhenConsoleArrives(methodName, level);
};
self.setLevel = function (level) {
if (typeof level === "string" && self.levels[level.toUpperCase()] !== undefined) {
level = self.levels[level.toUpperCase()];
}
if (typeof level === "number" && level >= 0 && level <= self.levels.SILENT) {
persistLevelIfPossible(level);
replaceLoggingMethods(level);
if (typeof console === undefinedType && level < self.levels.SILENT) {
return "No console available for logging";
}
} else {
throw "log.setLevel() called with invalid level: " + level;
}
};
self.enableAll = function() {
self.setLevel(self.levels.TRACE);
};
self.disableAll = function() {
self.setLevel(self.levels.SILENT);
};
// Grab the current global log variable in case of overwrite
var _log = (typeof window !== undefinedType) ? window.log : undefined;
self.noConflict = function() {
if (typeof window !== undefinedType &&
window.log === self) {
window.log = _log;
}
return self;
};
loadPersistedLevel();
return self;
}));
/* jshint ignore:end */

View File

@ -1524,6 +1524,13 @@ class page_requirements_manager {
global $CFG;
$output = '';
// Set the log level for the JS logging.
$logconfig = new stdClass();
if ($CFG->debugdeveloper) {
$logconfig->level = 'trace';
}
$this->js_call_amd('core/log', 'setConfig', array($logconfig));
// Call amd init functions.
$output .= $this->get_amd_footercode();

View File

@ -259,4 +259,10 @@
<version>2.1.15</version>
<licenseversion></licenseversion>
</library>
<library>
<location>amd/src/loglevel.js</location>
<name>loglevel.js</name>
<license>MIT</license>
<version>1.2.0</version>
</library>
</libraries>