mirror of
https://github.com/nasa/openmct.git
synced 2024-12-20 05:37:53 +00:00
Merge remote-tracking branch 'origin/open793' into open-master
This commit is contained in:
commit
85cfc00ad2
80
platform/framework/src/LogLevel.js
Normal file
80
platform/framework/src/LogLevel.js
Normal file
@ -0,0 +1,80 @@
|
||||
/*global define*/
|
||||
|
||||
define(
|
||||
[],
|
||||
function () {
|
||||
"use strict";
|
||||
|
||||
// Log levels; note that these must be in order of
|
||||
// most-important-first for LogLevel to function correctly
|
||||
// as implemented.
|
||||
var LOG_LEVELS = [
|
||||
'error',
|
||||
'warn',
|
||||
'info',
|
||||
'log',
|
||||
'debug'
|
||||
];
|
||||
|
||||
// No-op, to replace undesired log levels with
|
||||
function NOOP() {}
|
||||
|
||||
/**
|
||||
* Handles enforcement of logging at different levels, specified
|
||||
* at load time. The provided level should be one of "error",
|
||||
* "warn", "info", "log", or "debug"; otherwise, "warn" is used
|
||||
* as a default. Only log messages of levels equal to or greater
|
||||
* than the specified level will be passed to console.
|
||||
*
|
||||
* @constructor
|
||||
* @param {string} level the logging level
|
||||
*/
|
||||
function LogLevel(level) {
|
||||
// Find the numeric level associated with the string
|
||||
var index = LOG_LEVELS.indexOf(level);
|
||||
|
||||
// Replace logging methods with no-ops, if they are
|
||||
// not of an appropriate level.
|
||||
function decorate(log) {
|
||||
LOG_LEVELS.forEach(function (m, i) {
|
||||
// Determine applicability based on index
|
||||
// (since levels are in descending order)
|
||||
if (i > index) {
|
||||
log[m] = NOOP;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Default to 'warn' level if unspecified
|
||||
if (index < 0) {
|
||||
index = 1;
|
||||
}
|
||||
|
||||
return {
|
||||
/**
|
||||
* Configure logging to suppress log output if it is
|
||||
* not of an appropriate level. Both the Angular app
|
||||
* being initialized and a reference to `$log` should be
|
||||
* passed; the former is used to configure application
|
||||
* logging, while the latter is needed to apply the
|
||||
* same configuration during framework initialization
|
||||
* (since the framework also logs.)
|
||||
*
|
||||
* @param app the Angular app to configure
|
||||
* @param $log Angular's $log (also configured)
|
||||
*/
|
||||
configure: function (app, $log) {
|
||||
decorate($log);
|
||||
app.config(function ($provide) {
|
||||
$provide.decorator('$log', function ($delegate) {
|
||||
decorate($delegate);
|
||||
return $delegate;
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return LogLevel;
|
||||
}
|
||||
);
|
@ -19,6 +19,7 @@ define(
|
||||
'../lib/angular-route.min',
|
||||
'./Constants',
|
||||
'./FrameworkInitializer',
|
||||
'./LogLevel',
|
||||
'./load/BundleLoader',
|
||||
'./resolve/ImplementationLoader',
|
||||
'./resolve/ExtensionResolver',
|
||||
@ -36,6 +37,7 @@ define(
|
||||
angularRoute,
|
||||
Constants,
|
||||
FrameworkInitializer,
|
||||
LogLevel,
|
||||
BundleLoader,
|
||||
ImplementationLoader,
|
||||
ExtensionResolver,
|
||||
@ -52,6 +54,12 @@ define(
|
||||
// services, which are useful to the framework layer.
|
||||
var injector = angular.injector(['ng']);
|
||||
|
||||
// Look up log level from query string
|
||||
function logLevel() {
|
||||
var match = /[?&]log=([a-z]+)/.exec(window.location.search);
|
||||
return match ? match[1] : "";
|
||||
}
|
||||
|
||||
// Polyfill Promise, in case browser does not natively provide Promise
|
||||
window.Promise = window.Promise || es6promise.Promise;
|
||||
|
||||
@ -86,6 +94,11 @@ define(
|
||||
bootstrapper
|
||||
);
|
||||
|
||||
// Apply logging levels; this must be done now, before the
|
||||
// first log statement.
|
||||
new LogLevel(logLevel()).configure(app, $log);
|
||||
|
||||
// Initialize the application
|
||||
$log.info("Initializing application.");
|
||||
initializer.runApplication(Constants.BUNDLE_LISTING_FILE);
|
||||
}
|
||||
|
84
platform/framework/test/LogLevelSpec.js
Normal file
84
platform/framework/test/LogLevelSpec.js
Normal file
@ -0,0 +1,84 @@
|
||||
/*global define,Promise,describe,it,expect,beforeEach,waitsFor,jasmine*/
|
||||
|
||||
define(
|
||||
['../src/LogLevel'],
|
||||
function (LogLevel) {
|
||||
"use strict";
|
||||
|
||||
var LOG_METHODS = [
|
||||
'error',
|
||||
'warn',
|
||||
'info',
|
||||
'log',
|
||||
'debug'
|
||||
];
|
||||
|
||||
describe("The logging level handler", function () {
|
||||
var mockLog,
|
||||
mockApp,
|
||||
mockProvide,
|
||||
mockDelegate,
|
||||
mockMethods;
|
||||
|
||||
function logAll(v) {
|
||||
LOG_METHODS.forEach(function (m) {
|
||||
mockLog[m](v);
|
||||
mockDelegate[m](v);
|
||||
});
|
||||
}
|
||||
|
||||
function expectCalls(calls, v) {
|
||||
LOG_METHODS.forEach(function (m) {
|
||||
if (calls.indexOf(m) > -1) {
|
||||
expect(mockMethods[m]).toHaveBeenCalledWith(v);
|
||||
} else {
|
||||
expect(mockMethods[m]).not.toHaveBeenCalledWith(v);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(function () {
|
||||
mockMethods = jasmine.createSpyObj("levels", LOG_METHODS);
|
||||
mockLog = jasmine.createSpyObj('$log', LOG_METHODS);
|
||||
mockApp = jasmine.createSpyObj('app', ['config']);
|
||||
mockProvide = jasmine.createSpyObj('$provide', ['decorator']);
|
||||
mockDelegate = jasmine.createSpyObj('$delegate', LOG_METHODS);
|
||||
|
||||
LOG_METHODS.forEach(function (m) {
|
||||
mockLog[m].andCallFake(mockMethods[m]);
|
||||
mockDelegate[m].andCallFake(mockMethods[m]);
|
||||
});
|
||||
|
||||
mockApp.config.andCallFake(function (callback) {
|
||||
callback(mockProvide);
|
||||
});
|
||||
|
||||
mockProvide.decorator.andCallFake(function (key, callback) {
|
||||
// Only $log should be configured in any case
|
||||
expect(key).toEqual('$log');
|
||||
callback(mockDelegate);
|
||||
});
|
||||
});
|
||||
|
||||
it("defaults to 'warn' level", function () {
|
||||
new LogLevel("garbage").configure(mockApp, mockLog);
|
||||
logAll("test");
|
||||
expectCalls(['error', 'warn'], 'test');
|
||||
});
|
||||
|
||||
LOG_METHODS.forEach(function (m, i) {
|
||||
it("supports log level '" + m + "'", function () {
|
||||
// Note: This is sensitive to ordering of LOG_METHODS,
|
||||
// which needs to be highest-level-first above.
|
||||
var expected = LOG_METHODS.slice(0, i + 1),
|
||||
message = "test " + m;
|
||||
|
||||
new LogLevel(m).configure(mockApp, mockLog);
|
||||
logAll(message);
|
||||
expectCalls(expected, message);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
);
|
@ -1,5 +1,6 @@
|
||||
[
|
||||
"FrameworkInitializer",
|
||||
"LogLevel",
|
||||
"bootstrap/ApplicationBootstrapper",
|
||||
"load/Bundle",
|
||||
"load/BundleLoader",
|
||||
|
Loading…
Reference in New Issue
Block a user