LCOV - code coverage report
Current view: top level - basic - syslog-util.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 17 36 47.2 %
Date: 2019-08-22 15:41:25 Functions: 4 7 57.1 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <string.h>
       4             : #include <syslog.h>
       5             : 
       6             : #include "hexdecoct.h"
       7             : #include "macro.h"
       8             : #include "string-table.h"
       9             : #include "syslog-util.h"
      10             : 
      11           6 : int syslog_parse_priority(const char **p, int *priority, bool with_facility) {
      12           6 :         int a = 0, b = 0, c = 0;
      13             :         const char *end;
      14             :         size_t k;
      15             : 
      16           6 :         assert(p);
      17           6 :         assert(*p);
      18           6 :         assert(priority);
      19             : 
      20           6 :         if ((*p)[0] != '<')
      21           2 :                 return 0;
      22             : 
      23           4 :         end = strchr(*p, '>');
      24           4 :         if (!end)
      25           0 :                 return 0;
      26             : 
      27           4 :         k = end - *p;
      28           4 :         assert(k > 0);
      29             : 
      30           4 :         if (k == 2)
      31           0 :                 c = undecchar((*p)[1]);
      32           4 :         else if (k == 3) {
      33           0 :                 b = undecchar((*p)[1]);
      34           0 :                 c = undecchar((*p)[2]);
      35           4 :         } else if (k == 4) {
      36           0 :                 a = undecchar((*p)[1]);
      37           0 :                 b = undecchar((*p)[2]);
      38           0 :                 c = undecchar((*p)[3]);
      39             :         } else
      40           4 :                 return 0;
      41             : 
      42           0 :         if (a < 0 || b < 0 || c < 0 ||
      43           0 :             (!with_facility && (a || b || c > 7)))
      44           0 :                 return 0;
      45             : 
      46           0 :         if (with_facility)
      47           0 :                 *priority = a*100 + b*10 + c;
      48             :         else
      49           0 :                 *priority = (*priority & LOG_FACMASK) | c;
      50             : 
      51           0 :         *p += k + 1;
      52           0 :         return 1;
      53             : }
      54             : 
      55             : static const char *const log_facility_unshifted_table[LOG_NFACILITIES] = {
      56             :         [LOG_FAC(LOG_KERN)] = "kern",
      57             :         [LOG_FAC(LOG_USER)] = "user",
      58             :         [LOG_FAC(LOG_MAIL)] = "mail",
      59             :         [LOG_FAC(LOG_DAEMON)] = "daemon",
      60             :         [LOG_FAC(LOG_AUTH)] = "auth",
      61             :         [LOG_FAC(LOG_SYSLOG)] = "syslog",
      62             :         [LOG_FAC(LOG_LPR)] = "lpr",
      63             :         [LOG_FAC(LOG_NEWS)] = "news",
      64             :         [LOG_FAC(LOG_UUCP)] = "uucp",
      65             :         [LOG_FAC(LOG_CRON)] = "cron",
      66             :         [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
      67             :         [LOG_FAC(LOG_FTP)] = "ftp",
      68             :         [LOG_FAC(LOG_LOCAL0)] = "local0",
      69             :         [LOG_FAC(LOG_LOCAL1)] = "local1",
      70             :         [LOG_FAC(LOG_LOCAL2)] = "local2",
      71             :         [LOG_FAC(LOG_LOCAL3)] = "local3",
      72             :         [LOG_FAC(LOG_LOCAL4)] = "local4",
      73             :         [LOG_FAC(LOG_LOCAL5)] = "local5",
      74             :         [LOG_FAC(LOG_LOCAL6)] = "local6",
      75             :         [LOG_FAC(LOG_LOCAL7)] = "local7"
      76             : };
      77             : 
      78           3 : DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_facility_unshifted, int, LOG_FAC(~0));
      79             : 
      80           0 : bool log_facility_unshifted_is_valid(int facility) {
      81           0 :         return facility >= 0 && facility <= LOG_FAC(~0);
      82             : }
      83             : 
      84             : static const char *const log_level_table[] = {
      85             :         [LOG_EMERG] = "emerg",
      86             :         [LOG_ALERT] = "alert",
      87             :         [LOG_CRIT] = "crit",
      88             :         [LOG_ERR] = "err",
      89             :         [LOG_WARNING] = "warning",
      90             :         [LOG_NOTICE] = "notice",
      91             :         [LOG_INFO] = "info",
      92             :         [LOG_DEBUG] = "debug"
      93             : };
      94             : 
      95         197 : DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_level, int, LOG_DEBUG);
      96             : 
      97           0 : bool log_level_is_valid(int level) {
      98           0 :         return level >= 0 && level <= LOG_DEBUG;
      99             : }

Generated by: LCOV version 1.14