Skip to content

Commit 605a50a

Browse files
committed
New logging system
1 parent b562aed commit 605a50a

18 files changed

Lines changed: 174 additions & 185 deletions

acl.js

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ var path = require('path');
88
var $rdf = require('rdflib');
99
var S = require('string');
1010

11+
var debug = require('./logging').ACL;
1112
var file = require('./fileStore.js');
12-
var logging = require('./logging.js');
13-
1413
var ns = require('./vocab/ns.js').ns;
1514
var rdfVocab = require('./vocab/rdf.js');
1615

@@ -41,24 +40,24 @@ function allow(mode, req, res) {
4140
var pathUri = file.filenameToBaseUri(filepath, options.uriBase, options.fileBase);
4241
relativePath = path.relative(options.fileBase, filepath);
4342

44-
logging.log("ACL -- Checking " + accessType + "<" + mode + "> to " +
43+
debug("Checking " + accessType + "<" + mode + "> to " +
4544
pathUri + " for WebID: " + req.session.userId);
46-
logging.log("ACL -- Looking for policies in " + pathAcl);
45+
debug("Looking for policies in " + pathAcl);
4746

4847
var aclData;
4948
var aclGraph = $rdf.graph();
5049
try {
5150
aclData = fs.readFileSync(pathAcl, {encoding: 'utf8'});
5251
$rdf.parse(aclData, aclGraph, pathUri, 'text/turtle');
5352
} catch (parseErr) {
54-
logging.log("ACL -- Error parsing ACL policy: " + parseErr);
53+
debug("Error parsing ACL policy: " + parseErr);
5554
//Resetting graph to prevent the code from taking the next if brach.
5655
aclGraph = $rdf.graph();
5756
}
5857

5958

6059
if (aclGraph.statements.length > 0) {
61-
logging.log("ACL -- Found policies in " + pathAcl);
60+
debug("Found policies in " + pathAcl);
6261
var controlStatements = aclGraph.each(undefined, ns.acl("mode"),
6362
ns.acl("Control"));
6463
for(var controlIndex in controlStatements) {
@@ -72,11 +71,11 @@ function allow(mode, req, res) {
7271
var originsControl = aclGraph.each(modeElem, ns.acl("origin"), undefined);
7372
var originControlValue;
7473
if (origin.length > 0 && originsControl.length > 0) {
75-
logging.log("ACL -- Origin set to: " + rdfVocab.brack(origin));
74+
debug("Origin set to: " + rdfVocab.brack(origin));
7675
for(var originsControlIndex in originsControl) {
7776
var originsControlElem = originsControl[originsControlIndex];
7877
if (rdfVocab.brack(origin) === originsControlElem.toString()) {
79-
logging.log("ACL -- Found policy for origin: " +
78+
debug("Found policy for origin: " +
8079
originsControlElem.toString());
8180
originControlValue = allowOrigin(mode, req, res, aclGraph, controlElem);
8281
if (originControlValue) {
@@ -86,7 +85,7 @@ function allow(mode, req, res) {
8685
}
8786
continue;
8887
} else {
89-
logging.log("ACL -- No origin found, moving on.");
88+
debug("No origin found, moving on.");
9089
}
9190
originControlValue = allowOrigin(mode, req, res, aclGraph, controlElem);
9291
if (originControlValue) {
@@ -96,7 +95,7 @@ function allow(mode, req, res) {
9695
var ownerStatements = aclGraph.each(accessElem,
9796
ns.acl("owner"), aclGraph.sym(req.session.userId));
9897
for(var ownerIndex in ownerStatements) {
99-
logging.log("ACL -- " + mode + " access allowed (as owner)" +
98+
debug(mode + " access allowed (as owner)" +
10099
" for: " + req.session.userId);
101100
return {
102101
status: 200,
@@ -107,7 +106,7 @@ function allow(mode, req, res) {
107106
var agentStatements = aclGraph.each(controlElem,
108107
ns.acl("agent"), aclGraph.sym(req.session.userId));
109108
for(var agentIndex in agentStatements) {
110-
logging.log("ACL -- " + mode + " access allowed (as agent)" +
109+
debug(mode + " access allowed (as agent)" +
111110
" for: " + req.session.userId);
112111
return {
113112
status: 200,
@@ -119,9 +118,9 @@ function allow(mode, req, res) {
119118
ns.acl("agentClass"), undefined);
120119
for (var agentClassIndex in agentClassStatements) {
121120
var agentClassElem = agentClassStatements[agentClassIndex];
122-
logging.log("ACL -- Found agentClass policy");
121+
debug("Found agentClass policy");
123122
if (agentClassElem.sameTerm(ns.foaf("Agent"))) {
124-
logging.log("ACL -- " + mode +
123+
debug(mode +
125124
" access allowed as FOAF agent");
126125
return {
127126
status: 200,
@@ -141,7 +140,7 @@ function allow(mode, req, res) {
141140
agentClassElem, ns.foaf("member"),
142141
groupGraph.sym(req.session.userId));
143142
for(var memberIndex in memberStatements) {
144-
logging.log("ACL -- " + req.session.userId +
143+
debug(req.session.userId +
145144
" listed as member of the group " + groupURI);
146145
return {
147146
status: 200,
@@ -156,19 +155,19 @@ function allow(mode, req, res) {
156155
var modeStatements = aclGraph.each(undefined, ns.acl("mode"), ns.acl(mode));
157156
for(var modeIndex in modeStatements) {
158157
var modeElem = modeStatements[modeIndex];
159-
logging.log("ACL -- Found " + accessType + " policy for <" + mode + ">");
158+
debug("Found " + accessType + " policy for <" + mode + ">");
160159
var accessTypeStatements = aclGraph.each(modeElem, ns.acl(accessType),
161160
aclGraph.sym(pathUri));
162161
for(var accessTypeIndex in accessTypeStatements) {
163162
var accessTypeElem = accessTypeStatements[accessTypeIndex];
164163
var origins = aclGraph.each(modeElem, ns.acl("origin"), undefined);
165164
var originValue;
166165
if (origin.length > 0 && origins.length > 0) {
167-
logging.log("ACL -- Origin set to: " + rdfVocab.brack(origin));
166+
debug("Origin set to: " + rdfVocab.brack(origin));
168167
for(var originsIndex in origins) {
169168
var originsElem = origins[originsIndex];
170169
if (rdfVocab.brack(origin) === originsElem.toString()) {
171-
logging.log("ACL -- Found policy for origin: " +
170+
debug("Found policy for origin: " +
172171
originsElem.toString());
173172
originValue = allowOrigin(mode, req, res, aclGraph, modeElem);
174173
if (originValue) {
@@ -178,7 +177,7 @@ function allow(mode, req, res) {
178177
}
179178
continue;
180179
} else {
181-
logging.log("ACL -- No origin found, moving on.");
180+
debug("No origin found, moving on.");
182181
}
183182
originValue = allowOrigin(mode, req, res, aclGraph, modeElem);
184183
if (originValue) {
@@ -188,13 +187,13 @@ function allow(mode, req, res) {
188187
}
189188

190189
if (req.session.userId.length === 0 || req.session.identified === false) {
191-
logging.log("ACL -- Authentication required");
190+
debug("Authentication required");
192191
return {
193192
status: 401,
194193
err: "Access to " + pathUri + " requires authorization"
195194
};
196195
}
197-
logging.log("ACL -- " + mode + " access denied for: " + req.session.userId);
196+
debug(mode + " access denied for: " + req.session.userId);
198197
return {
199198
status: 403,
200199
err: "Access denied for " + req.session.userId
@@ -225,19 +224,19 @@ function allow(mode, req, res) {
225224
}
226225
}
227226

228-
logging.log("ACL -- No ACL policies present - access allowed");
227+
debug("No ACL policies present - access allowed");
229228
return {
230229
status: 200,
231230
err: null
232231
};
233232
}
234233

235234
function allowOrigin(mode, req, res, aclGraph, subject) {
236-
logging.log("ACL -- In allow origin");
235+
debug("In allow origin");
237236
var ownerStatements = aclGraph.each(subject, ns.acl("owner"),
238237
aclGraph.sym(req.session.userId));
239238
for (var ownerIndex in ownerStatements) {
240-
logging.log("ACL -- " + mode + " access allowed (as owner) for: " + req.session.userId);
239+
debug(mode + " access allowed (as owner) for: " + req.session.userId);
241240
return {
242241
status: 200,
243242
err: null
@@ -246,7 +245,7 @@ function allowOrigin(mode, req, res, aclGraph, subject) {
246245
var agentStatements = aclGraph.each(subject, ns.acl("agent"),
247246
aclGraph.sym(req.session.userId));
248247
for (var agentIndex in agentStatements) {
249-
logging.log("ACL -- " + mode + " access allowed (as agent) for: " + req.session.userId);
248+
debug(mode + " access allowed (as agent) for: " + req.session.userId);
250249
return {
251250
status: 200,
252251
return: null
@@ -256,9 +255,9 @@ function allowOrigin(mode, req, res, aclGraph, subject) {
256255
for (var agentClassIndex in agentClassStatements) {
257256
var agentClassElem = agentClassStatements[agentClassIndex];
258257
//Check for FOAF groups
259-
logging.log("ACL -- Found agentClass policy");
258+
debug("Found agentClass policy");
260259
if (agentClassElem.sameTerm(ns.foaf("Agent"))) {
261-
logging.log("ACL -- " + mode + " allowed access as FOAF agent");
260+
debug(mode + " allowed access as FOAF agent");
262261
return {
263262
status: 200,
264263
err: null
@@ -273,7 +272,7 @@ function allowOrigin(mode, req, res, aclGraph, subject) {
273272
var memberStatements = groupGraph.each(agentClassElem, ns.foaf("member"),
274273
groupGraph.sym(req.session.userId));
275274
for (var memberIndex in memberStatements) {
276-
logging.log("ACL -- " + req.session.userId + " listed as member of the group " + groupURI);
275+
debug(req.session.userId + " listed as member of the group " + groupURI);
277276
return {
278277
status: 200,
279278
err: null

bin/ldnode.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var fs = require('fs');
2+
var debug = require('../logging').server;
23
var ldnode = require('../index');
3-
var logging = require('../logging.js');
44

55
var argv = require('optimist')
66
.boolean('cors')
@@ -42,12 +42,12 @@ if (argv.h || argv.help || argv['?']) {
4242
if (process.platform !== 'win32') {
4343
// Signal handlers don't work on Windows.
4444
process.on('SIGINT', function() {
45-
logging.log("Server -- http-server stopped.");
45+
debug("http-server stopped.");
4646
process.exit();
4747
});
4848
}
49-
process.env.DEBUG = argv.v;
50-
var app = ldnode.createServer(argv)
49+
process.env.DEBUG = argv.v ? '*' : false;
50+
var app = ldnode.createServer(argv);
5151
app.listen(argv.p, function() {
52-
logging.log('LDP started on port ' + argv.p);
52+
debug('LDP started on port ' + argv.p);
5353
});

container.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ var $rdf = require('rdflib');
66
var path = require('path');
77
var S = require('string');
88
var uuid = require('node-uuid');
9+
var debug = require('./logging').container;
910

10-
var logging = require('./logging.js');
1111
var metadata = require('./metadata.js');
12-
1312
var rdfVocab = require('./vocab/rdf.js');
1413
var ldpVocab = require('./vocab/ldp.js');
1514

@@ -35,12 +34,9 @@ function createRootContainer(options) {
3534
serializedContainer, function(err) {
3635
if (err) {
3736
//TODO handle error
38-
logging.log(
39-
"Container -- Could not write root container");
37+
debug("Could not write root container");
4038
} else {
41-
logging.log(
42-
"Container -- Wrote root container to " +
43-
options.fileBase);
39+
debug("Wrote root container to " + options.fileBase);
4440
}
4541
});
4642
}
@@ -82,13 +78,13 @@ function createNewResource(options, resourcePath, resourceGraph, callback) {
8278
//TODO write files with relative URIS.
8379
var rawResource = $rdf.serialize(undefined,
8480
resourceGraph, options.uriBase + resourceURI, 'text/turtle');
85-
logging.log("Container -- Writing new resource to " + resourcePath);
86-
logging.log("Container -- Resource:\n" + rawResource);
81+
debug("Writing new resource to " + resourcePath);
82+
debug("Resource:\n" + rawResource);
8783
fs.writeFile(resourcePath, rawResource, writeResourceCallback);
8884

8985
function writeResourceCallback(err) {
9086
if (err) {
91-
logging.log("Container -- Error writing resource: " + err);
87+
debug("Error writing resource: " + err);
9288
module.exports.releaseResourceUri(options, resourcePath);
9389
callback(err);
9490
} else {
@@ -102,26 +98,25 @@ function createNewContainer(options, containerPath, containerGraph, callback) {
10298

10399
function mkdirCallback(err) {
104100
if (err) {
105-
logging.log("Container -- Error creating directory" +
106-
" for new container: " + err);
101+
debug("Error creating directory for new container: " + err);
107102
module.exports.releaseResourceUri(options, containerPath);
108103
return callback(err);
109104
} else {
110105
var rawContainer = $rdf.serialize(undefined, containerGraph,
111106
options.uriBase, 'text/turtle');
112-
logging.log("rawContainer " + rawContainer);
107+
debug("rawContainer " + rawContainer);
113108
metadata.writeContainerMetadata(containerPath, rawContainer,
114109
writeContainerCallback);
115110
}
116111
}
117112

118113
function writeContainerCallback(err) {
119114
if (err) {
120-
logging.log("Container -- Error writing container: " + err);
115+
debug("Error writing container: " + err);
121116
module.exports.releaseResourceUri(options, containerPath);
122117
return callback(err);
123118
} else {
124-
logging.log("Container -- Wrote container to " + containerPath);
119+
debug("Wrote container to " + containerPath);
125120
module.exports.releaseResourceUri(options, containerPath);
126121
return callback(err);
127122
}

fileStore.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
var fs = require('fs');
55
var path = require('path');
66
var S = require('string');
7-
var logging = require('./logging.js');
87

98
function uriToFilename(uri, fileBase) {
10-
console.log(uri, fileBase)
119
var filename = path.join(fileBase, uri);
1210
// Make sure filename ends with '/' if filename exists and is a directory.
1311
try {

handlers/delete.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
"use strict";
33

44
var fs = require('fs');
5+
var debug = require('../logging').handlers;
56

67
var file = require('../fileStore.js');
7-
var logging = require('../logging.js');
88
var metadata = require('../metadata.js');
99

1010
function handler(req, res) {
1111
var options = req.app.locals.ldp;
12-
logging.log('DELETE -- ' + req.path);
12+
debug('DELETE -- ' + req.path);
1313
var filename = file.uriToFilename(req.path, options.fileBase);
1414
fs.stat(filename, function(err, stats) {
1515
if (err) {
16-
logging.log("DELETE -- unlink() error: " + err);
16+
debug("DELETE -- unlink() error: " + err);
1717
return res.status(404).send("Can't delete file: " + err);
1818
} else if (stats.isDirectory()) {
1919
metadata.deleteContainerMetadata(filename, containerCallback);
@@ -24,20 +24,20 @@ function handler(req, res) {
2424

2525
function fileCallback(err) {
2626
if (err) {
27-
logging.log("DELETE -- unlink() error: " + err);
27+
debug("DELETE -- unlink() error: " + err);
2828
return res.status(404).send("Can't delete file: " + err);
2929
} else {
30-
logging.log("DELETE -- Ok. Bytes deleted: " + req.text.length);
30+
debug("DELETE -- Ok. Bytes deleted: " + req.text.length);
3131
res.sendStatus(200);
3232
}
3333
}
3434

3535
function containerCallback(err) {
3636
if (err) {
37-
logging.log("DELETE -- unlink() error: " + err);
37+
debug("DELETE -- unlink() error: " + err);
3838
return res.status(404).send("Can't delete container: " + err);
3939
} else {
40-
logging.log("DELETE -- Ok.");
40+
debug("DELETE -- Ok.");
4141
res.sendStatus(200);
4242
}
4343
}

0 commit comments

Comments
 (0)