WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit c89b6ae

Browse files
committed
updated build script with jshint, gulp-plugins loader and non-minified dist file
1 parent 3cee237 commit c89b6ae

File tree

4 files changed

+149
-18
lines changed

4 files changed

+149
-18
lines changed

bower.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
],
1111
"main": "./dist/logging-enhancer.min.js",
1212
"dependencies": {
13+
14+
},
15+
"devDependencies": {
1316
"momentjs": "*",
1417
"sprintf": "*"
1518
},

dist/logging-enhancer.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
LoggingEnhancer can be used to enhance any logging function and can be tested without angular
3+
*/
4+
(function() {
5+
var LoggingEnhancer = function(sprintf, moment) {
6+
var self = this;
7+
8+
this.LEVEL = { TRACE: 4, DEBUG: 3, INFO: 2, WARN: 1, ERROR: 0, OFF: -1 };
9+
10+
// returns a value for testing purposes only
11+
this.enhanceLogging = function(loggingFunc, level, context, config, datetimePattern, datetimeLocale, prefixPattern) {
12+
config.logLevels = config.logLevels || [];
13+
return function() {
14+
if (levelPassesThreshold(context, level, config)) {
15+
var enhancedArguments = enhanceLogline(arguments, context, datetimePattern, datetimeLocale, prefixPattern);
16+
loggingFunc.apply(null, enhancedArguments);
17+
return enhancedArguments;
18+
}
19+
else {
20+
return null; // no log produced
21+
}
22+
};
23+
24+
function levelPassesThreshold(context, level, config) {
25+
return level > self.LEVEL.OFF && level <= getLogLevelThreshold(context, config);
26+
27+
function getLogLevelThreshold(context, config) {
28+
if (context) {
29+
if (config.logLevels[context] !== undefined) {
30+
return config.logLevels[context];
31+
}
32+
else if (context.indexOf('.') !== -1) {
33+
return getLogLevelThreshold(context.substring(0, context.lastIndexOf('.')), config);
34+
}
35+
}
36+
return config.logLevels['*'] !== undefined ? config.logLevels['*'] : self.LEVEL.TRACE;
37+
}
38+
}
39+
40+
function enhanceLogline(args, context, datetimePattern, datetimeLocale, prefixPattern) {
41+
var prefix = generatePrefix(context, datetimePattern, datetimeLocale, prefixPattern);
42+
var processedArgs = maybeApplySprintf([].slice.call(args));
43+
return [prefix].concat([].slice.call(processedArgs));
44+
45+
function maybeApplySprintf(args) {
46+
var sprintfAvailable = typeof sprintf !== 'undefined';
47+
var sprintfCandidate = sprintfAvailable && args.length >= 2 && typeof args[0] === 'string' && args[0].indexOf('%') !== -1;
48+
if (sprintfCandidate) {
49+
try {
50+
// apply sprintf with the proper arguments
51+
var placeholderCount = self.countSprintfHolders(args[0]);
52+
if (placeholderCount > 0) {
53+
args[0] = sprintf.apply(null, args);
54+
args.splice(1, placeholderCount); // remove arguments consumed by sprintf
55+
}
56+
}
57+
catch (e) {
58+
// invalid arguments passed into sprintf, continue without applying
59+
args.unshift(e);
60+
}
61+
}
62+
63+
return args;
64+
}
65+
}
66+
67+
function generatePrefix(context, datetimePattern, datetimeLocale, prefixPattern) {
68+
var dateStr = '';
69+
if (typeof moment !== 'undefined') {
70+
dateStr = moment().locale(datetimeLocale).format(datetimePattern);
71+
}
72+
else {
73+
var d = new Date();
74+
var timeStr = new Date().toTimeString().match(/^([0-9]{2}:[0-9]{2}:[0-9]{2})/)[0];
75+
dateStr = d.getDate() + '-' + (d.getMonth() + 1) + '-' + d.getFullYear() + ' ' + timeStr;
76+
}
77+
78+
if (typeof sprintf !== 'undefined') {
79+
return sprintf(prefixPattern, dateStr, context);
80+
}
81+
else {
82+
// use fixed layout: '%s::[%s]> '
83+
return dateStr + '::[' + context + ']> ';
84+
}
85+
}
86+
};
87+
88+
self.countSprintfHolders = function(pattern) {
89+
var hasNamedHolders = /\x25\([a-zA-Z0-9_]+\)[b-fijosuxX]/.test(pattern);
90+
if (hasNamedHolders) {
91+
return 1;
92+
}
93+
94+
var placeholderCounter = 0;
95+
96+
function f(index) {
97+
return function() {
98+
// keep track of highest arg index, needed for single -but indexed- placeholders placeholder (ie. %6$s consumes the first 6 arguments)
99+
placeholderCounter = Math.max(placeholderCounter, index);
100+
};
101+
}
102+
103+
sprintf(pattern, f(1), f(2), f(3), f(4), f(5), f(6), f(7), f(8), f(9), f(10));
104+
return placeholderCounter;
105+
};
106+
};
107+
108+
if (typeof module !== 'undefined') {
109+
module.exports.LoggingEnhancer = LoggingEnhancer;
110+
} else if (typeof exports !== 'undefined') {
111+
exports.LoggingEnhancer = LoggingEnhancer;
112+
} else if (typeof window !== 'undefined') {
113+
window.loggingEnhancer = new LoggingEnhancer(window.sprintf, window.moment);
114+
} else {
115+
throw new Error('unable to expose LoggingEnhancer: no module, exports object and no global window detected');
116+
}
117+
118+
})();

gulpfile.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,52 @@
1-
var gulp = require('gulp');
2-
var concat = require('gulp-concat');
31
var del = require('del');
4-
var uglify = require('gulp-uglify');
5-
var jasmine = require('gulp-jasmine');
6-
var cover = require('gulp-coverage');
7-
var coveralls = require('gulp-coveralls');
82
var lazypipe = require('lazypipe');
3+
var gulp = require('gulp');
4+
var $ = require('gulp-load-plugins')();
95

106
gulp.task('clean', function() {
117
del(['dist/*', 'reports', 'debug', '.coverdata']);
128
});
139

14-
gulp.task('build', ['clean'], function() {
10+
gulp.task('analyse', function() {
11+
gulp.src(['src/logging-enhancer.js'])
12+
.pipe($.jshint())
13+
.pipe($.jshint.reporter('jshint-stylish'))
14+
.pipe($.jshint.reporter('fail'));
15+
});
16+
17+
gulp.task('build', ['clean', 'analyse'], function() {
18+
gulp.src(['src/logging-enhancer.js'])
19+
.pipe($.concat("logging-enhancer.js"))
20+
.pipe(gulp.dest('dist'));
21+
});
22+
23+
gulp.task('dist', ['build'], function() {
1524
gulp.src(['src/logging-enhancer.js'])
16-
.pipe(uglify())
17-
.pipe(concat("logging-enhancer.min.js"))
25+
.pipe($.uglify())
26+
.pipe($.concat("logging-enhancer.min.js"))
1827
.pipe(gulp.dest('dist'));
1928
});
2029

2130
var testAndGather = lazypipe()
22-
.pipe(cover.instrument, {
31+
.pipe($.coverage.instrument, {
2332
pattern: ['src/**/*.js'],
2433
debugDirectory: 'debug'
2534
})
26-
.pipe(jasmine, {includeStackTrace: true})
27-
.pipe(cover.gather);
35+
.pipe($.jasmine, {includeStackTrace: true})
36+
.pipe($.coverage.gather);
2837

2938
gulp.task('test', ['build'], function () {
3039
gulp.src('spec/**/*spec.js')
3140
.pipe(testAndGather())
32-
.pipe(cover.format(['html']))
41+
.pipe($.coverage.format(['html']))
3342
.pipe(gulp.dest('reports'));
3443
});
3544

3645
gulp.task('travis', ['build'], function () {
3746
gulp.src('spec/**/*spec.js')
3847
.pipe(testAndGather())
39-
.pipe(cover.format(['lcov']))
40-
.pipe(coveralls());
48+
.pipe($.coverage.format(['lcov']))
49+
.pipe($.coveralls());
4150
});
4251

43-
gulp.task('default', ['build'], function() {
44-
// place code for your default task here
45-
});
52+
gulp.task('default', ['build'], function() {});

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
"gulp-jasmine": "~2.0.1",
3333
"gulp-coverage": "~0.3.35",
3434
"gulp-coveralls": "~0.1.4",
35+
"gulp-load-plugins": "0.10.0",
36+
"gulp-jshint": "1.11.0",
37+
"jshint-stylish": "2.0.0",
3538
"lazypipe": "~0.2.3"
3639
}
3740
}

0 commit comments

Comments
 (0)