summaryrefslogtreecommitdiffstats
path: root/src/nvme/log.c
blob: eaf74e1d0cf9d860e848c9785a2238c985d74d37 (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
 * This file is part of libnvme.
 * Copyright (C) 2021 SUSE LLC
 *
 * Authors: Hannes Reinecke <hare@suse.de>
 *
 * This file implements basic logging functionality.
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <syslog.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#define LOG_FUNCNAME 1
#include "private.h"
#include "log.h"
#include "cleanup.h"

#ifndef LOG_CLOCK
#define LOG_CLOCK CLOCK_MONOTONIC
#endif

static struct nvme_log def_log = {
	.fd = STDERR_FILENO,
	.level = DEFAULT_LOGLEVEL,
	.pid = false,
	.timestamp = false,
};

void __attribute__((format(printf, 4, 5)))
__nvme_msg(nvme_root_t r, int level,
	   const char *func, const char *format, ...)
{
	struct nvme_log *l;
	va_list ap;
	char pidbuf[16];
	char timebuf[32];
	static const char *const formats[] = {
		"%s%s%s",
		"%s%s%s: ",
		"%s<%s>%s ",
		"%s<%s> %s: ",
		"[%s] %s%s ",
		"[%s]%s %s: ",
		"[%s] <%s>%s ",
		"[%s] <%s> %s: ",
	};
	_cleanup_free_ char *header = NULL;
	_cleanup_free_ char *message = NULL;
	int idx = 0;

	if (r)
		l = &r->log;
	else
		l = &def_log;

	if (level > l->level)
		return;

	if (l->timestamp) {
		struct timespec now;

		clock_gettime(LOG_CLOCK, &now);
		snprintf(timebuf, sizeof(timebuf), "%6ld.%06ld",
			 (long)now.tv_sec, now.tv_nsec / 1000);
		idx |= 1 << 2;
	} else
		*timebuf = '\0';

	if (l->pid) {
		snprintf(pidbuf, sizeof(pidbuf), "%ld", (long)getpid());
		idx |= 1 << 1;
	} else
		*pidbuf = '\0';

	if (func)
		idx |= 1 << 0;

	if (asprintf(&header, formats[idx],
		     timebuf, pidbuf, func ? func : "") == -1)
		header = NULL;

	va_start(ap, format);
	if (vasprintf(&message, format, ap) == -1)
		message = NULL;
	va_end(ap);

	dprintf(l->fd, "%s%s",
		header ? header : "<error>",
		message ? message : "<error>");
}

void nvme_init_logging(nvme_root_t r, int lvl, bool log_pid, bool log_tstamp)
{
	r->log.level = lvl;
	r->log.pid = log_pid;
	r->log.timestamp = log_tstamp;
}

int nvme_get_logging_level(nvme_root_t r, bool *log_pid, bool *log_tstamp)
{
	struct nvme_log *l;

	if (r)
		l = &r->log;
	else
		l = &def_log;

	if (log_pid)
		*log_pid = l->pid;
	if (log_tstamp)
		*log_tstamp = l->timestamp;
	return l->level;
}

void nvme_init_default_logging(FILE *fp, int level, bool log_pid, bool log_tstamp)
{
	def_log.fd = fileno(fp);
	def_log.level = level;
	def_log.pid = log_pid;
	def_log.timestamp = log_tstamp;
}

void nvme_set_root(nvme_root_t r)
{
	def_log.fd = r->log.fd;
	def_log.level = r->log.level;
	def_log.pid = r->log.pid;
	def_log.timestamp = r->log.timestamp;
}

void nvme_set_debug(bool debug)
{
	def_log.level = debug ? LOG_DEBUG : DEFAULT_LOGLEVEL;
}

bool nvme_get_debug(void)
{
	return def_log.level == LOG_DEBUG;
}