forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syslog.c
104 lines (86 loc) · 2.89 KB
/
syslog.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#ifndef HAVE_KDBCONFIG
#include "kdbconfig.h"
#endif
#include "log.h"
int elektraSyslogOpen (Plugin * handle, Key * parentKey ELEKTRA_UNUSED)
{
/* plugin initialization logic */
if (!ksLookupByName (elektraPluginGetConfig (handle), "/dontopensyslog", 0))
{
openlog ("elektra", LOG_PID, LOG_USER);
}
return 0; /* success */
}
int elektraSyslogClose (Plugin * handle, Key * parentKey ELEKTRA_UNUSED)
{
/* free all plugin resources and shut it down */
if (!ksLookupByName (elektraPluginGetConfig (handle), "/dontopensyslog", 0))
{
closelog ();
}
return 0; /* success */
}
int elektraSyslogGet (Plugin * handle, KeySet * returned, Key * parentKey)
{
if (!strcmp (keyName (parentKey), "system/elektra/modules/syslog"))
{
KeySet * n;
ksAppend (returned,
n = ksNew (30,
keyNew ("system/elektra/modules/syslog", KEY_VALUE, "syslog plugin waits for your orders", KEY_END),
keyNew ("system/elektra/modules/syslog/exports", KEY_END),
keyNew ("system/elektra/modules/syslog/exports/open", KEY_FUNC, elektraSyslogOpen, KEY_END),
keyNew ("system/elektra/modules/syslog/exports/close", KEY_FUNC, elektraSyslogClose, KEY_END),
keyNew ("system/elektra/modules/syslog/exports/get", KEY_FUNC, elektraSyslogGet, KEY_END),
keyNew ("system/elektra/modules/syslog/exports/set", KEY_FUNC, elektraSyslogSet, KEY_END),
keyNew ("system/elektra/modules/syslog/exports/error", KEY_FUNC, elektraSyslogError, KEY_END),
#include "readme_syslog.c"
keyNew ("system/elektra/modules/syslog/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END));
ksDel (n);
return 1;
}
if (strncmp (keyString (ksLookupByName (elektraPluginGetConfig (handle), "/log/get", 0)), "1", 1) == 0)
{
syslog (LOG_NOTICE, "loading configuration %s", keyName (parentKey));
}
return 1;
}
int elektraSyslogSet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
size_t changed = 0;
Key * k = 0;
ksRewind (returned);
while ((k = ksNext (returned)))
{
if (keyNeedSync (k))
{
syslog (LOG_NOTICE, "change %s to %s", keyName (k), keyString (k));
changed++;
}
}
syslog (LOG_NOTICE, "committed configuration %s with %zd keys (%zu changed)", keyName (parentKey), ksGetSize (returned), changed);
return 1;
}
int elektraSyslogError (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
syslog (LOG_NOTICE, "rollback configuration %s with %zd keys", keyName (parentKey), ksGetSize (returned));
return 1;
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport("syslog",
ELEKTRA_PLUGIN_OPEN, &elektraSyslogOpen,
ELEKTRA_PLUGIN_CLOSE, &elektraSyslogClose,
ELEKTRA_PLUGIN_GET, &elektraSyslogGet,
ELEKTRA_PLUGIN_SET, &elektraSyslogSet,
ELEKTRA_PLUGIN_ERROR, &elektraSyslogError,
ELEKTRA_PLUGIN_END);
}