xref: /spdk/lib/log/log_flags.c (revision 7506a7aa53d239f533af3bc768f0d2af55e735fe)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 
36 #include "spdk/log.h"
37 
38 static TAILQ_HEAD(, spdk_log_flag) g_log_flags = TAILQ_HEAD_INITIALIZER(g_log_flags);
39 
40 static struct spdk_log_flag *
41 get_log_flag(const char *name)
42 {
43 	struct spdk_log_flag *flag;
44 
45 	TAILQ_FOREACH(flag, &g_log_flags, tailq) {
46 		if (strcasecmp(name, flag->name) == 0) {
47 			return flag;
48 		}
49 	}
50 
51 	return NULL;
52 }
53 
54 void
55 spdk_log_register_flag(const char *name, struct spdk_log_flag *flag)
56 {
57 	struct spdk_log_flag *iter;
58 
59 	if (name == NULL || flag == NULL) {
60 		SPDK_ERRLOG("missing spdk_log_flag parameters\n");
61 		assert(false);
62 		return;
63 	}
64 
65 	if (get_log_flag(name)) {
66 		SPDK_ERRLOG("duplicate spdk_log_flag '%s'\n", name);
67 		assert(false);
68 		return;
69 	}
70 
71 	TAILQ_FOREACH(iter, &g_log_flags, tailq) {
72 		if (strcasecmp(iter->name, flag->name) > 0) {
73 			TAILQ_INSERT_BEFORE(iter, flag, tailq);
74 			return;
75 		}
76 	}
77 
78 	TAILQ_INSERT_TAIL(&g_log_flags, flag, tailq);
79 }
80 
81 bool
82 spdk_log_get_flag(const char *name)
83 {
84 	struct spdk_log_flag *flag = get_log_flag(name);
85 
86 	if (flag && flag->enabled) {
87 		return true;
88 	}
89 
90 	return false;
91 }
92 
93 static int
94 log_set_flag(const char *name, bool value)
95 {
96 	struct spdk_log_flag *flag;
97 
98 	if (strcasecmp(name, "all") == 0) {
99 		TAILQ_FOREACH(flag, &g_log_flags, tailq) {
100 			flag->enabled = value;
101 		}
102 		return 0;
103 	}
104 
105 	flag = get_log_flag(name);
106 	if (flag == NULL) {
107 		return -1;
108 	}
109 
110 	flag->enabled = value;
111 
112 	return 0;
113 }
114 
115 int
116 spdk_log_set_flag(const char *name)
117 {
118 	return log_set_flag(name, true);
119 }
120 
121 int
122 spdk_log_clear_flag(const char *name)
123 {
124 	return log_set_flag(name, false);
125 }
126 
127 struct spdk_log_flag *
128 spdk_log_get_first_flag(void)
129 {
130 	return TAILQ_FIRST(&g_log_flags);
131 }
132 
133 struct spdk_log_flag *
134 spdk_log_get_next_flag(struct spdk_log_flag *flag)
135 {
136 	return TAILQ_NEXT(flag, tailq);
137 }
138 
139 void
140 spdk_log_usage(FILE *f, const char *log_arg)
141 {
142 	struct spdk_log_flag *flag;
143 	fprintf(f, " %s, --logflag <flag>    enable log flag (all", log_arg);
144 
145 	TAILQ_FOREACH(flag, &g_log_flags, tailq) {
146 		fprintf(f, ", %s", flag->name);
147 	}
148 
149 	fprintf(f, ")\n");
150 }
151