Skip to content

Commit 51a094e

Browse files
committed
Merge branch 'develop'
2 parents f589de1 + b0eae08 commit 51a094e

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Node.js module for native bindings to the ~~dearly beloved~~ systemd-journald.
44

5-
Successfully tested under NodeJS v0.10.47, v0.12.16, v4.6.0, v6.7.0
5+
Successfully tested under NodeJS v0.10.48, v0.12.17, v4.6.1, v6.9.1, v7.0.0
66

77

88
## Example
@@ -128,6 +128,7 @@ log.debug( message, fields ); // - LOG_DEBUG
128128
Sepcial thanks to:
129129
* [ianare](https://github.com/ianare) for improving compatibility with older systemd versions.
130130
* [jez9999](https://github.com/jez9999) for making this module immune to future changes of syslog levels.
131+
* [Z3TA](https://github.com/Z3TA) is responsible for ```CODE_FILE```, ```CODE_FUNC``` and ```CODE_LINE``` being settable by the ```fields``` parameter.
131132

132133
I owe you a drink!
133134

index.js

+29-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
var journal = require( './build/Release/journal_send.node' );
44

5+
var stackTraceRE = /at ([^\ ]+) \(([^:]+):([0-9]+):[0-9]+\)$/;
6+
57
function obj2iovec( iovec, obj, prefix ) {
68

79
if( prefix === undefined ) prefix = '';
@@ -11,7 +13,7 @@ function obj2iovec( iovec, obj, prefix ) {
1113
var name = o.toUpperCase();
1214
if( typeof obj[o] == 'object' ) {
1315
obj2iovec( iovec, obj[o], prefix + name + "_" );
14-
} else if( prefix.length > 0 || (name != 'PRIORITY' && name != 'MESSAGE') ) {
16+
} else if( obj[o] !== undefined && (prefix.length > 0 || (name != 'PRIORITY' && name != 'MESSAGE')) ) {
1517
iovec.push( prefix + name + '=' + obj[o].toString() );
1618
}
1719
}
@@ -32,10 +34,34 @@ function log( priority, message, fields ) {
3234

3335
// If the message is an instnce of Error, extract its message
3436
if( message instanceof Error ) {
35-
fields.STACK_TRACE = message.stack;
37+
38+
var stack = message.stack.toString();
39+
40+
// Store stack trace and message
41+
fields.STACK_TRACE = stack;
3642
message = message.message;
43+
44+
// Try to extract callee name, line and file
45+
var tmp = stack.split('\n');
46+
if( tmp.length >= 2 ) {
47+
48+
// Second line knows the error source
49+
var errSource = tmp[1];
50+
51+
// Match regular expression and add info to iovec
52+
var re = stackTraceRE.exec( errSource );
53+
if( re !== null ) {
54+
fields.CODE_FILE = re[2];
55+
fields.CODE_FUNC = re[1];
56+
fields.CODE_LINE = re[3];
57+
}
58+
59+
}
60+
3761
} else if( typeof message != 'string' ) {
62+
3863
message = message.toString();
64+
3965
}
4066

4167
var iovec = [];
@@ -46,7 +72,7 @@ function log( priority, message, fields ) {
4672
// Add additional fields
4773
obj2iovec( iovec, fields );
4874

49-
// Send it to out beloved journald
75+
// Send it to our beloved journald
5076
journal.send.apply( null, [ parseInt( priority ) ].concat( iovec ) );
5177

5278
}

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "systemd-journald",
3-
"version": "1.1.3",
3+
"version": "1.2.0",
44
"description": "Native bindings to journald",
55
"keywords": [
66
"logging",
@@ -33,7 +33,7 @@
3333
"linux"
3434
],
3535
"jshintConfig": {
36-
"esversion": 6,
36+
"esversion": 5,
3737
"node": true,
3838
"strict": true,
3939
"loopfunc": true

src/journal_send.cc

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
// Internet. It just copies an ordinary JavaScript string array into an iovec
33
// and then finally calls sd_journal_sendv. Rocket science included!
44

5-
// Priority is a special case as it needs to be taken from the syslog.h
6-
// preprocessor definitions, which aren't available from JavaScript.
75

86
#include <nan.h>
7+
8+
// Instead of the locations being this file, let the user define their own
9+
// CODE_FILE, CODE_LINE and CODE_FUNC, via stack trace (ex: npm callsite)
10+
#define SD_JOURNAL_SUPPRESS_LOCATION 1
911
#include <systemd/sd-journal.h>
12+
13+
// Priority is a special case as it needs to be taken from the syslog.h
14+
// preprocessor definitions, which aren't available from JavaScript.
1015
#include <syslog.h>
1116

1217
// Macros for int to string conversion

test/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ describe( "node-systemd-journald", function() {
192192
try {
193193
assert.strictEqual( journal_send.getField( 'PRIORITY' ), '7' );
194194
assert.strictEqual( journal_send.getField( 'MESSAGE' ), 'Test' );
195+
assert.strictEqual( journal_send.getField( 'CODE_LINE' ), '190' );
196+
assert.strictEqual( journal_send.getField( 'CODE_FUNC' ), 'Context.<anonymous>' );
197+
assert.strictEqual( journal_send.getField( 'CODE_FILE' ).substr( -13 ), 'test/index.js' );
195198
assert.notStrictEqual( journal_send.getField( 'STACK_TRACE' ), undefined );
196199
done();
197200
} catch( e ) {

0 commit comments

Comments
 (0)