7373d9d9c934bdb01a216b3c28af89d2350ca4f5
[obnox/samba/samba-obnox.git] / ctdb / common / logging.c
1 /*
2    Logging utilities
3
4    Copyright (C) Amitay Isaacs  2015
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <replace.h>
21
22 #include "common/logging.h"
23
24 struct {
25         enum debug_level log_level;
26         const char *log_string;
27 } log_string_map[] = {
28         { DEBUG_ERR,     "ERROR" },
29         { DEBUG_WARNING, "WARNING" },
30         { DEBUG_NOTICE,  "NOTICE" },
31         { DEBUG_INFO,    "INFO" },
32         { DEBUG_DEBUG,   "DEBUG" },
33 };
34
35 bool debug_level_parse(const char *log_string, enum debug_level *log_level)
36 {
37         int i;
38
39         for (i=0; ARRAY_SIZE(log_string_map); i++) {
40                 if (strcasecmp(log_string_map[i].log_string,
41                                log_string) == 0) {
42                         *log_level = log_string_map[i].log_level;
43                         return true;
44                 }
45         }
46
47         return false;
48 }
49
50 const char *debug_level_to_string(enum debug_level log_level)
51 {
52         int i;
53
54         for (i=0; ARRAY_SIZE(log_string_map); i++) {
55                 if (log_string_map[i].log_level == log_level) {
56                         return log_string_map[i].log_string;
57                 }
58         }
59         return "UNKNOWN";
60 }
61
62 enum debug_level debug_level_from_string(const char *log_string)
63 {
64         bool found;
65         enum debug_level log_level;
66
67         found = debug_level_parse(log_string, &log_level);
68         if (found) {
69                 return log_level;
70         }
71
72         /* Default debug level */
73         return DEBUG_ERR;
74 }
75
76 int debug_level_to_int(enum debug_level log_level)
77 {
78         return (int)log_level;
79 }
80
81 enum debug_level debug_level_from_int(int level)
82 {
83         enum debug_level log_level;
84
85         if (level >= 0 && level < ARRAY_SIZE(log_string_map)) {
86                 log_level = log_string_map[level].log_level;
87         } else {
88                 log_level = DEBUG_ERR;
89         }
90
91         return log_level;
92 }