2015-05-13 23:42:35 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Open MCT Web, Copyright (c) 2014-2015, United States Government
|
|
|
|
* as represented by the Administrator of the National Aeronautics and Space
|
|
|
|
* Administration. All rights reserved.
|
|
|
|
*
|
|
|
|
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
* Open MCT Web includes source code licensed under additional open source
|
|
|
|
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
|
|
|
* this source code distribution or the Licensing information page available
|
|
|
|
* at runtime from the About dialog for additional information.
|
|
|
|
*****************************************************************************/
|
2015-02-04 01:43:33 +00:00
|
|
|
/*global define*/
|
|
|
|
|
|
|
|
define(
|
|
|
|
[],
|
|
|
|
function () {
|
|
|
|
"use strict";
|
|
|
|
|
2015-02-04 02:01:29 +00:00
|
|
|
// 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'
|
|
|
|
];
|
2015-02-04 01:43:33 +00:00
|
|
|
|
2015-02-04 02:01:29 +00:00
|
|
|
// 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.
|
|
|
|
*
|
2015-08-07 18:44:54 +00:00
|
|
|
* @memberof platform/framework
|
2015-02-04 02:01:29 +00:00
|
|
|
* @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)
|
2015-08-07 18:44:54 +00:00
|
|
|
* @memberof platform/framework.LogLevel#
|
2015-02-04 02:01:29 +00:00
|
|
|
*/
|
|
|
|
configure: function (app, $log) {
|
|
|
|
decorate($log);
|
|
|
|
app.config(function ($provide) {
|
2015-02-04 02:42:30 +00:00
|
|
|
$provide.decorator('$log', function ($delegate) {
|
|
|
|
decorate($delegate);
|
|
|
|
return $delegate;
|
|
|
|
});
|
2015-02-04 02:01:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2015-02-04 01:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return LogLevel;
|
|
|
|
}
|
2015-08-07 18:44:54 +00:00
|
|
|
);
|