Skip to content

Commit

Permalink
feat(log): Expose logging to formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
thepian committed Mar 15, 2015
1 parent 595fed3 commit 1c23879
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 123 deletions.
11 changes: 10 additions & 1 deletion docs/js/docs-setup.js
Expand Up @@ -320,6 +320,15 @@ NG_DOCS={
"shortDescription": "Client bundling API",
"keywords": "actual api args arguments assets bundler bundlers bundling call client containerdir content custom default define definition describe describing descriptor destinations destsfor determine dir directly entry entry-points file implementing libraries locations method module object offer params passed paths query relpaths replace require return service single ss store system systemlibs systemmodule true tweak wrap wrapped"
},
{
"section": "api",
"id": "ss.client.formatters:formatters",
"shortName": "formatters",
"type": "service",
"moduleName": "ss.client",
"shortDescription": "Formatter registry",
"keywords": "add api asset client config configuring define formatter formatters method nameormodule object parameters registry rendering service ss"
},
{
"section": "api",
"id": "ss.client:client",
Expand All @@ -344,7 +353,7 @@ NG_DOCS={
"type": "service",
"moduleName": "ss",
"shortDescription": "Contains method stubs for logging to console (by default) or",
"keywords": "api assigning choose console debug default error fairly function happened info informed keeping level log logging method override parameters provider require service socketstream ss stubs sysadmin takes time trivial unexpected var wakeup warn winston"
"keywords": "api apply arguments assigning calls choose console debug default error fairly function happened info informed keeping level log logging method override parameters plugins provider require service socketstream ss stubs switch sysadmin takes time trace tracing trivial unexpected var wakeup warn winston"
},
{
"section": "api",
Expand Down
17 changes: 17 additions & 0 deletions docs/partials/api/ss.client.formatters.formatters.html
@@ -0,0 +1,17 @@
<h1><code ng:non-bindable="">formatters</code>
<div><span class="hint">service in module <code ng:non-bindable="">ss.client</code>
</span>
</div>
</h1>
<div><h2 id="description">Description</h2>
<div class="description"><div class="ss-client-formatters-page ss-client-formatters-formatters-page"><p>Formatter registry</p>
</div></div>
<div class="member method"><h2 id="methods">Methods</h2>
<ul class="methods"><li><h3 id="methods_add">add(nameOrModule, config)</h3>
<div class="add"><h5 id="methods_add_parameters">Parameters</h5><table class="variables-matrix table table-bordered table-striped"><thead><tr><th>Param</th><th>Type</th><th>Details</th></tr></thead><tbody><tr><td>nameOrModule</td><td><a href="" class="label type-hint type-hint-string">string</a><a href="" class="label type-hint type-hint-object">object</a></td><td><div class="ss-client-formatters-page ss-client-formatters-formatters-add-page"><p>the formatter object or name</p>
</div></td></tr><tr><td>config</td><td><a href="" class="label type-hint type-hint-object">object</a></td><td><div class="ss-client-formatters-page ss-client-formatters-formatters-add-page"><p>parameters configuring the formatter</p>
</div></td></tr></tbody></table></div>
</li>
</ul>
</div>
</div>
9 changes: 9 additions & 0 deletions docs/partials/api/ss.log.log.html
Expand Up @@ -34,6 +34,15 @@ <h1><code ng:non-bindable="">log</code>
</code></pre></div></div>
</div>
</li>
<li><h3 id="methods_trace">trace()</h3>
<div class="trace"><div class="ss-log-trace-page"><p>Trace function calls in socketstream and plugins. By default nothing is done.
If you want to switch on tracing override the <code>trace</code> method.</p>
<pre><code>var ss = require(&#39;socketstream&#39;);
ss.api.log.trace = function() {
console.log.apply(console,arguments);
};
</code></pre></div></div>
</li>
<li><h3 id="methods_warn">warn()</h3>
<div class="warn"><div class="ss-log-warn-page"><p>Warn level logging, uses console.log by default. Override by assigning a
function that takes the same parameters as console.log:</p>
Expand Down
51 changes: 34 additions & 17 deletions lib/client/formatters.js
Expand Up @@ -3,28 +3,45 @@
// Loads default code formatters and presents an API for loading custom formatters
'use strict';


/**
* @ngdoc service
* @name ss.client.formatters:formatters
* @description
* Formatter registry
*/
module.exports = function(ss,options) {
var mods;
mods = [];
var mods = [];
return {
/**
* @ngdoc method
* @name ss.client.formatters:formatters#add
* @methodOf ss.client.formatters:formatters
* @function
* Define a formatter for client asset (JS/CSS/HTML) rendering
* @param {string|object} nameOrModule the formatter object or name
* @param {object} config parameters configuring the formatter
*/
add: function(nameOrModule, config) {
var formatter, mod, modPath;
if (!config) {
config = {};
}
mod = (function() {
if (typeof nameOrModule === 'object') {
return nameOrModule;
var mod, formatter;
config = config || {};
if (typeof nameOrModule === 'object') {
mod = nameOrModule;
} else {
var modPath = './formatters/' + nameOrModule;
if (require.resolve(modPath)) {
mod = require(modPath);
} else {
modPath = './formatters/' + nameOrModule;
if (require.resolve(modPath)) {
return require(modPath);
} else {
throw new Error('The ' + nameOrModule + ' formatter is not supported by SocketStream internally. Please pass a compatible module instead');
}
throw new Error('The ' + nameOrModule + ' formatter is not supported by SocketStream internally. Please pass a compatible module instead');
}
})();
formatter = mod.init(ss.root, config, options);
}
if (typeof mod === 'function') {
formatter = mod(ss, config, options);
} else if (mod.newAPI) {
formatter = mod.init(ss, config, options);
} else {
formatter = mod.init(ss.root, config, options);
}
return mods.push(formatter);
},
load: function() {
Expand Down
5 changes: 3 additions & 2 deletions lib/client/formatters/css.js
Expand Up @@ -5,13 +5,14 @@ var fs = require('fs');
/**
* Plain CSS Formatter
*/
exports.init = function() {
module.exports = function(ss) {
return {
extensions: ['css'],
assetType: 'css',
contentType: 'text/css',
compile: function (path, options, cb) {
ss.log.trace('Compiling plain CSS',path,options);
return cb(fs.readFileSync(path, 'utf8'));
}
};
};
};
3 changes: 2 additions & 1 deletion lib/client/formatters/html.js
Expand Up @@ -5,12 +5,13 @@ var fs = require('fs');
/**
* Plain HTML Formatter
*/
exports.init = function() {
module.exports = function(ss) {
return {
extensions: ['html'],
assetType: 'html',
contentType: 'text/html',
compile: function(path, options, cb) {
ss.log.trace('Compiling plain HTML',path,options);
var input;
input = fs.readFileSync(path, 'utf8');

Expand Down
5 changes: 3 additions & 2 deletions lib/client/formatters/javascript.js
Expand Up @@ -5,13 +5,14 @@ var fs = require('fs');
/**
* Javascript formatter
*/
exports.init = function() {
module.exports = function(ss) {
return {
extensions: ['js'],
assetType: 'js',
contentType: 'text/javascript; charset=utf-8',
compile: function(path, options, cb) {
ss.log.trace('Compiling plain JS',path,options);
return cb(fs.readFileSync(path, 'utf8'));
}
};
};
};
7 changes: 3 additions & 4 deletions lib/client/formatters/map.js
Expand Up @@ -10,17 +10,16 @@
//
var fs = require('fs');



exports.init = function() {
module.exports = function(ss) {

return {
extensions: ['map'],
assetType: 'js',
contentType: 'application/json',
compile: function(path, options, cb) {
ss.log.trace('Compiling plain MAP',path,options);
return cb(fs.readFileSync(path, 'utf8'));
}
};

};
};
7 changes: 6 additions & 1 deletion lib/client/template_engine.js
Expand Up @@ -45,7 +45,12 @@ module.exports = function(ss,options) {
if (!(dirs instanceof Array)) {
dirs = [dirs];
}
var engine = mod.init(ss, config, options);
var engine;
if (typeof mod === 'function') {
engine = mod(ss, config, options);
} else {
engine = mod.init(ss, config, options);
}
return mods.push({
engine: engine,
dirs: dirs
Expand Down

0 comments on commit 1c23879

Please sign in to comment.