| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/* |
| 3 |
* This file is part of libnvme. |
| 4 |
* Copyright (C) 2021 SUSE LLC |
| 5 |
* |
| 6 |
* Authors: Hannes Reinecke <[email protected]> |
| 7 |
* |
| 8 |
* This file implements basic logging functionality. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <sys/types.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <stdarg.h> |
| 15 |
#include <stdbool.h> |
| 16 |
#include <syslog.h> |
| 17 |
#include <unistd.h> |
| 18 |
#include <time.h> |
| 19 |
#include <string.h> |
| 20 |
#define LOG_FUNCNAME 1 |
| 21 |
#include "private.h" |
| 22 |
#include "log.h" |
| 23 |
#include "cleanup.h" |
| 24 |
|
| 25 |
#ifndef LOG_CLOCK |
| 26 |
#define LOG_CLOCK CLOCK_MONOTONIC |
| 27 |
#endif |
| 28 |
|
| 29 |
static struct nvme_log def_log = { |
| 30 |
.fd = STDERR_FILENO, |
| 31 |
.level = DEFAULT_LOGLEVEL, |
| 32 |
.pid = false, |
| 33 |
.timestamp = false, |
| 34 |
}; |
| 35 |
|
| 36 |
void __attribute__((format(printf, 4, 5))) |
| 37 |
__nvme_msg(nvme_root_t r, int level, |
| 38 |
const char *func, const char *format, ...) |
| 39 |
{ |
| 40 |
struct nvme_log *l; |
| 41 |
va_list ap; |
| 42 |
char pidbuf[16]; |
| 43 |
char timebuf[32]; |
| 44 |
static const char *const formats[] = { |
| 45 |
"%s%s%s", |
| 46 |
"%s%s%s: ", |
| 47 |
"%s<%s>%s ", |
| 48 |
"%s<%s> %s: ", |
| 49 |
"[%s] %s%s ", |
| 50 |
"[%s]%s %s: ", |
| 51 |
"[%s] <%s>%s ", |
| 52 |
"[%s] <%s> %s: ", |
| 53 |
}; |
| 54 |
_cleanup_free_ char *header = NULL; |
| 55 |
_cleanup_free_ char *message = NULL; |
| 56 |
int idx = 0; |
| 57 |
|
| 58 |
if (r) |
| 59 |
l = &r->log; |
| 60 |
else |
| 61 |
l = &def_log; |
| 62 |
|
| 63 |
if (level > l->level) |
| 64 |
return; |
| 65 |
|
| 66 |
if (l->timestamp) { |
| 67 |
struct timespec now; |
| 68 |
|
| 69 |
clock_gettime(LOG_CLOCK, &now); |
| 70 |
snprintf(timebuf, sizeof(timebuf), "%6ld.%06ld", |
| 71 |
(long)now.tv_sec, now.tv_nsec / 1000); |
| 72 |
idx |= 1 << 2; |
| 73 |
} else |
| 74 |
*timebuf = '\0'; |
| 75 |
|
| 76 |
if (l->pid) { |
| 77 |
snprintf(pidbuf, sizeof(pidbuf), "%ld", (long)getpid()); |
| 78 |
idx |= 1 << 1; |
| 79 |
} else |
| 80 |
*pidbuf = '\0'; |
| 81 |
|
| 82 |
if (func) |
| 83 |
idx |= 1 << 0; |
| 84 |
|
| 85 |
if (asprintf(&header, formats[idx], |
| 86 |
timebuf, pidbuf, func ? func : "") == -1) |
| 87 |
header = NULL; |
| 88 |
|
| 89 |
va_start(ap, format); |
| 90 |
if (vasprintf(&message, format, ap) == -1) |
| 91 |
message = NULL; |
| 92 |
va_end(ap); |
| 93 |
|
| 94 |
dprintf(l->fd, "%s%s", |
| 95 |
header ? header : "<error>", |
| 96 |
message ? message : "<error>"); |
| 97 |
} |
| 98 |
|
| 99 |
void nvme_init_logging(nvme_root_t r, int lvl, bool log_pid, bool log_tstamp) |
| 100 |
{ |
| 101 |
r->log.level = lvl; |
| 102 |
r->log.pid = log_pid; |
| 103 |
r->log.timestamp = log_tstamp; |
| 104 |
} |
| 105 |
|
| 106 |
int nvme_get_logging_level(nvme_root_t r, bool *log_pid, bool *log_tstamp) |
| 107 |
{ |
| 108 |
struct nvme_log *l; |
| 109 |
|
| 110 |
if (r) |
| 111 |
l = &r->log; |
| 112 |
else |
| 113 |
l = &def_log; |
| 114 |
|
| 115 |
if (log_pid) |
| 116 |
*log_pid = l->pid; |
| 117 |
if (log_tstamp) |
| 118 |
*log_tstamp = l->timestamp; |
| 119 |
return l->level; |
| 120 |
} |
| 121 |
|
| 122 |
void nvme_init_default_logging(FILE *fp, int level, bool log_pid, bool log_tstamp) |
| 123 |
{ |
| 124 |
def_log.fd = fileno(fp); |
| 125 |
def_log.level = level; |
| 126 |
def_log.pid = log_pid; |
| 127 |
def_log.timestamp = log_tstamp; |
| 128 |
} |
| 129 |
|
| 130 |
void nvme_set_root(nvme_root_t r) |
| 131 |
{ |
| 132 |
def_log.fd = r->log.fd; |
| 133 |
def_log.level = r->log.level; |
| 134 |
def_log.pid = r->log.pid; |
| 135 |
def_log.timestamp = r->log.timestamp; |
| 136 |
} |
| 137 |
|
| 138 |
void nvme_set_debug(bool debug) |
| 139 |
{ |
| 140 |
def_log.level = debug ? LOG_DEBUG : DEFAULT_LOGLEVEL; |
| 141 |
} |
| 142 |
|
| 143 |
bool nvme_get_debug(void) |
| 144 |
{ |
| 145 |
return def_log.level == LOG_DEBUG; |
| 146 |
} |