Created
August 7, 2012 22:36
-
-
Save heapwolf/3290100 to your computer and use it in GitHub Desktop.
fs/close
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
function openAndWriteToSystemLog(writeBuffer, callback) { | |
fs.open('/var/log/system.log', 'a', function opened(err, fd) { | |
if (err) { return callback(err); } | |
function notifyError(err) { | |
fs.close(fd, function() { | |
callback(err); | |
}); | |
} | |
var bufferOffset = 0, | |
bufferLength = writeBuffer.length, filePosition = null; | |
fs.write( fd, writeBuffer, bufferOffset, bufferLength, filePosition, function wrote(err, written) { | |
if (err) { return notifyError(err); } | |
fs.close(fd, function() { | |
callback(err); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
openAndWriteToSystemLog(
new Buffer('writing this string'),
function done(err) {
if (err) {
console.log("error while opening and writing:", err.message);
return;
}
console.log('All done with no errors');
}
);