Coverage Report

Created: 2026-05-16 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/open62541_15/src_generated/mdnsd/log.c
Line
Count
Source
1
#include "open62541/config.h"
2
/*
3
 * Copyright (c) 2018-2022  Joachim Wiberg <troglobit@gmail.com>
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *     * Redistributions of source code must retain the above copyright
9
 *       notice, this list of conditions and the following disclaimer.
10
 *     * Redistributions in binary form must reproduce the above copyright
11
 *       notice, this list of conditions and the following disclaimer in the
12
 *       documentation and/or other materials provided with the distribution.
13
 *     * Neither the name of the copyright holders nor the names of its
14
 *       contributors may be used to endorse or promote products derived from
15
 *       this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
21
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
 * POSSIBILITY OF SUCH DAMAGE.
28
 */
29
30
#define SYSLOG_NAMES
31
#include <ctype.h>
32
#include <stdio.h>
33
#include <stdarg.h>
34
#include <stdlib.h>
35
#include <string.h>
36
#include <syslog.h>
37
#include <time.h>
38
#include <unistd.h>
39
#include <sys/time.h>
40
#include "mdnsd.h"
41
42
#ifndef MAX
43
0
#define MAX(x,y) ((x) > (y) ? (x) : (y))
44
#endif
45
46
#ifndef INTERNAL_NOPRI    /* Illumos/SmartOS/Bionic */
47
#define INTERNAL_NOPRI 0x10
48
49
struct {
50
  char *c_name;
51
  int   c_val;
52
} prioritynames[] =
53
  {
54
    { "alert",   LOG_ALERT },
55
    { "crit",    LOG_CRIT },
56
    { "debug",   LOG_DEBUG },
57
    { "emerg",   LOG_EMERG },
58
    { "error",   LOG_ERR },
59
    { "err",     LOG_ERR },
60
    { "info",    LOG_INFO },
61
    { "none",    INTERNAL_NOPRI },
62
    { "notice",  LOG_NOTICE },
63
    { "panic",   LOG_EMERG },
64
    { "warning", LOG_WARNING },
65
    { "warn",    LOG_WARNING },
66
    { NULL, -1 }
67
  };
68
#endif
69
70
static int do_syslog = 0;
71
static int loglevel  = LOG_NOTICE;
72
73
int mdnsd_log_level(char *level)
74
0
{
75
0
  int lvl = -1;
76
77
0
  for (int i = 0; prioritynames[i].c_name; i++) {
78
0
    size_t len = MAX(strlen(prioritynames[i].c_name),
79
0
         strlen(level));
80
81
0
    if (!strncasecmp(prioritynames[i].c_name, level, len)) {
82
0
      lvl = prioritynames[i].c_val;
83
0
      break;
84
0
    }
85
0
  }
86
87
0
  if (-1 == lvl)
88
0
    lvl = atoi(level);
89
0
  if (lvl >= 0)
90
0
    loglevel = lvl;
91
92
0
  if (do_syslog)
93
0
    setlogmask(LOG_UPTO(loglevel));
94
95
0
  return lvl;
96
0
}
97
98
void mdnsd_log_open(const char *ident)
99
0
{
100
0
  openlog(ident, LOG_PID | LOG_NDELAY, LOG_DAEMON);
101
0
  setlogmask(LOG_UPTO(loglevel));
102
0
  do_syslog = 1;
103
0
}
104
105
void mdnsd_log_hex(const char *msg, unsigned char *buffer, ssize_t len)
106
0
{
107
0
  char ascii[17];
108
0
  int i;
109
110
0
  if (do_syslog)
111
0
    return;
112
113
0
  if (loglevel < LOG_DEBUG)
114
0
    return;
115
116
0
  printf("%s", msg);
117
118
0
  memset(ascii, 0, sizeof(ascii));
119
0
  for (i = 0; i < len; i++) {
120
0
    if (i % 16 == 0)
121
0
      printf("%s\n%06x ", ascii, i);
122
0
    printf("%02X ", buffer[i]);
123
124
0
    if (isprint((int)(buffer[i])))
125
0
      ascii[i%16] = buffer[i];
126
0
    else
127
0
      ascii[i%16] = '.';
128
0
  }
129
130
0
  ascii[i % 16] = 0;
131
0
  while (i % 16) {
132
0
    printf("   ");
133
0
    i++;
134
0
  }
135
136
0
  printf("%s\n", ascii);
137
0
  printf("\n");
138
0
}
139
140
void mdnsd_log(int severity, const char *fmt, ...)
141
0
{
142
0
  FILE *file;
143
0
        va_list args;
144
145
0
  if (loglevel == INTERNAL_NOPRI)
146
0
    return;
147
148
0
  if (severity > LOG_WARNING)
149
0
    file = stdout;
150
0
  else
151
0
    file = stderr;
152
153
0
        va_start(args, fmt);
154
0
  if (do_syslog)
155
0
    vsyslog(severity, fmt, args);
156
0
  else if (severity <= loglevel) {
157
0
    vfprintf(file, fmt, args);
158
0
    fprintf(file, "\n");
159
0
    fflush(file);
160
0
  }
161
0
        va_end(args);
162
0
}
163
164
void mdnsd_log_time(struct timeval *tv, char *buf, size_t len)
165
0
{
166
0
  char tmp[15];
167
0
  time_t t;
168
0
  struct tm *tm;
169
170
0
  if (loglevel < LOG_DEBUG)
171
0
    return;
172
173
0
  t = tv->tv_sec;
174
0
  tm = localtime(&t);
175
0
  tm->tm_sec += tv->tv_usec / 1000000;
176
0
  if (buf && len > 8) {
177
0
    strftime(buf, len, "%H:%M:%S", tm);
178
0
    return;
179
0
  }
180
181
0
  strftime(tmp, sizeof(tmp), "%H:%M:%S", tm);
182
  mdnsd_log(LOG_DEBUG, "@%s", tmp);
183
0
}