summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore4
-rw-r--r--README.md22
-rw-r--r--journald/__init__.py30
-rw-r--r--journald/_journald.c (renamed from journald.c)12
-rw-r--r--setup.py9
5 files changed, 60 insertions, 17 deletions
diff --git a/.gitignore b/.gitignore
index 4e6e435..4469289 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,6 @@
+__pycache__/
*.py[co]
-/journald.so
-/journald.*.so
+/journald/*.so
# Packages
*.egg
diff --git a/README.md b/README.md
index f7d4912..e21b601 100644
--- a/README.md
+++ b/README.md
@@ -19,14 +19,26 @@ Usage
Quick example:
import journald
- journald.send('MESSAGE=Hello world')
- journald.send('MESSAGE=Hello, again, world', 'FIELD2=Greetings!', 'FIELD3=Guten tag')
- journald.send('ARBITRARY=anything', 'FIELD3=Greetings!')
+ journald.send('Hello world')
+ journald.send('Hello, again, world', FIELD2='Greetings!', FIELD3='Guten tag')
+ journald.send('Binary message', BINARY='\xde\xad\xbe\xef')
+
+There is one required argument -- the message, and additional fields
+can be specified as keyword arguments. Following the journald API, all
+names are uppercase.
+
+The journald sendv call can also be accessed directly:
+
+ import journald
+ journald.sendv('MESSAGE=Hello world')
+ journald.sendv('MESSAGE=Hello, again, world', 'FIELD2=Greetings!',
+ 'FIELD3=Guten tag')
+ journald.sendv('MESSAGE=Binary message', 'BINARY=\xde\xad\xbe\xef')
+
+The two examples should give the same results in the log.
Notes:
- * Each argument must be in the form of a KEY=value pair,
- environmental variable style.
* Unlike the native C version of journald's sd_journal_send(),
printf-style substitution is not supported. Perform any
substitution using Python's % operator or .format() capabilities
diff --git a/journald/__init__.py b/journald/__init__.py
new file mode 100644
index 0000000..b892faf
--- /dev/null
+++ b/journald/__init__.py
@@ -0,0 +1,30 @@
+from ._journald import sendv
+
+def send(MESSAGE, MESSAGE_ID=None, **kwargs):
+ """Send a message to journald.
+
+ >>> journald.send('Hello world')
+ >>> journald.send('Hello, again, world', FIELD2='Greetings!')
+ >>> journald.send('Binary message', BINARY='\xde\xad\xbe\xef')
+
+ Value of the MESSAGE argument will be used for the MESSAGE= field.
+
+ MESSAGE_ID can be given to uniquely identify the type of message.
+
+ Other parts of the message can be specified as keyword arguments.
+
+ CODE_LINE, CODE_FILE, and CODE_FUNC can be specified to identify
+ the caller. Unless at least on of the three is given, values are
+ extracted from the stack frame of the caller of send(). CODE_FILE
+ and CODE_FUNC should be strings, CODE_LINE should be an integer.
+
+ Other useful fields include PRIORITY, SYSLOG_FACILITY,
+ SYSLOG_IDENTIFIER, SYSLOG_PID.
+ """
+
+ args = ['MESSAGE=' + MESSAGE]
+ if MESSAGE_ID is not None:
+ args.append('MESSAGE_ID=' + MESSAGE_ID)
+ for key, val in kwargs.items():
+ args.append(key + '=' + val)
+ return sendv(*args)
diff --git a/journald.c b/journald/_journald.c
index 4ddbc8c..024ebc6 100644
--- a/journald.c
+++ b/journald/_journald.c
@@ -2,7 +2,7 @@
#include <systemd/sd-journal.h>
static PyObject *
-journald_send(PyObject *self, PyObject *args) {
+journald_sendv(PyObject *self, PyObject *args) {
struct iovec *iov = NULL;
int argc = PyTuple_Size(args);
int i, r;
@@ -79,7 +79,7 @@ out1:
}
static PyMethodDef methods[] = {
- {"send", journald_send, METH_VARARGS,
+ {"sendv", journald_sendv, METH_VARARGS,
"Send an entry to journald."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
@@ -87,23 +87,23 @@ static PyMethodDef methods[] = {
#if PY_MAJOR_VERSION < 3
PyMODINIT_FUNC
-initjournald(void)
+init_journald(void)
{
- (void) Py_InitModule("journald", methods);
+ (void) Py_InitModule("_journald", methods);
}
#else
static struct PyModuleDef module = {
PyModuleDef_HEAD_INIT,
- "journald", /* name of module */
+ "_journald", /* name of module */
NULL, /* module documentation, may be NULL */
0, /* size of per-interpreter state of the module */
methods
};
PyMODINIT_FUNC
-PyInit_journald(void)
+PyInit__journald(void)
{
return PyModule_Create(&module);
}
diff --git a/setup.py b/setup.py
index 69d345d..4d8839c 100644
--- a/setup.py
+++ b/setup.py
@@ -1,12 +1,13 @@
from distutils.core import setup, Extension
-journald = Extension('journald',
- libraries = ['systemd-journal'],
- sources = ['journald.c'])
+cjournald = Extension('journald/_journald',
+ libraries = ['systemd-journal'],
+ sources = ['journald/_journald.c'])
setup (name = 'journald',
version = '0.1',
description = 'Native interface to the journald facilities of systemd',
author_email = 'david@davidstrauss.net',
url = 'https://github.com/davidstrauss/journald-python',
- ext_modules = [journald])
+ py_modules = ['journald'],
+ ext_modules = [cjournald])