Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Create node.js extension to work with journald

  • Loading branch information...
commit 762d8eeb51ecc8b585c31e136011f6dc0b1153e3 1 parent c2fdf5c
@marktheunissen marktheunissen authored
Showing with 102 additions and 2 deletions.
  1. +3 −1 .gitignore
  2. +31 −1 README.md
  3. +9 −0 binding.gyp
  4. +56 −0 journald.cc
  5. +3 −0  test.js
View
4 .gitignore
@@ -12,4 +12,6 @@ logs
results
node_modules
-npm-debug.log
+npm-debug.log
+
+build
View
32 README.md
@@ -1,2 +1,32 @@
node-systemd
-============
+============
+
+Node.js module for native access to the journald facilities in recent versions of systemd. In particular, this capability includes passing key/value pairs as fields that journald can use for filtering.
+
+Usage
+=====
+
+var journald = require('journald')
+journald.send('MESSAGE=hello world', 'ARG1=first_argument', 'ARG2=second_argument');
+
+Developing
+==========
+
+Install node-gyp to build the extension:
+
+ sudo npm install -g node-gyp
+
+Build:
+
+ node-gyp configure && node-gyp build
+
+Run test app:
+
+ node test.js
+
+Viewing Output
+==============
+
+Quick way to view output with all fields as it comes in:
+
+ sudo journalctl -f --output=json
View
9 binding.gyp
@@ -0,0 +1,9 @@
+{
+ "targets": [
+ {
+ "target_name": "journald",
+ "sources": [ "journald.cc" ],
+ 'libraries': [ '/lib64/libsystemd-journal.so' ]
+ }
+ ]
+}
View
56 journald.cc
@@ -0,0 +1,56 @@
+#include <node.h>
+#include <v8.h>
+#include <systemd/sd-journal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <iostream>
+
+using namespace std;
+using namespace v8;
+
+Handle<Value> Method(const Arguments& args) {
+ HandleScope scope;
+ int argc = args.Length();
+ struct iovec *iov = NULL;
+
+ if (argc < 1) {
+ return ThrowException(String::New("No arguments given"));
+ }
+
+ for (int i = 0; i < argc; ++i) {
+ if (!args[i]->IsString()) {
+ return ThrowException(String::New("Non-string argument given"));
+ }
+ }
+
+ iov = (iovec*) malloc(argc * sizeof(struct iovec));
+ if (!iov) {
+ return ThrowException(String::New("Out of memory"));
+ }
+
+ // Iterate through the JS arguments and fill the iovector.
+ for (int i = 0; i < argc; ++i) {
+ Local<String> v8str = args[i]->ToString();
+
+ iov[i].iov_len = v8str->Length();
+ iov[i].iov_base = (char*) malloc(v8str->Length() + 1);
+
+ v8str->WriteAscii((char*)iov[i].iov_base, 0, iov[i].iov_len);
+ }
+
+ sd_journal_sendv(iov, argc);
+
+ for (int i = 0; i < argc; ++i) {
+ free(iov[i].iov_base);
+ }
+ free(iov);
+
+ return scope.Close(Integer::New(1));
+}
+
+void init(Handle<Object> target) {
+ target->Set(String::NewSymbol("send"),
+ FunctionTemplate::New(Method)->GetFunction());
+}
+
+NODE_MODULE(journald, init)
View
3  test.js
@@ -0,0 +1,3 @@
+var journald = require('./build/Release/journald')
+
+journald.send('MESSAGE=hello world', 'ARG1=first_argument', 'ARG2=second_argument');
Please sign in to comment.
Something went wrong with that request. Please try again.