Skip to content

Instantly share code, notes, and snippets.

@heapwolf
Created August 7, 2012 22:36
Show Gist options
  • Save heapwolf/3290100 to your computer and use it in GitHub Desktop.
Save heapwolf/3290100 to your computer and use it in GitHub Desktop.
fs/close
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);
});
});
});
@heapwolf
Copy link
Author

heapwolf commented Aug 7, 2012

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');
}
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment