LCOV - code coverage report
Current view: top level - src/plugins/syslog - syslog.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 17 32 53.1 %
Date: 2022-05-21 16:19:22 Functions: 4 7 57.1 %

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

Generated by: LCOV version 1.13