autobuild: Use make -j on samba-libs/samba-static build as well
[Samba.git] / ctdb / common / logging.c
blobf2304715c8d7776ef3728a010bfd956dd87a8605
1 /*
2 Logging utilities
4 Copyright (C) Amitay Isaacs 2015
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.
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.
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/>.
20 #include <replace.h>
21 #include <system/locale.h>
23 #include "common/logging.h"
25 struct {
26 enum debug_level log_level;
27 const char *log_string;
28 } log_string_map[] = {
29 { DEBUG_ERR, "ERROR" },
30 { DEBUG_WARNING, "WARNING" },
31 { DEBUG_NOTICE, "NOTICE" },
32 { DEBUG_INFO, "INFO" },
33 { DEBUG_DEBUG, "DEBUG" },
36 bool debug_level_parse(const char *log_string, enum debug_level *log_level)
38 int i;
40 if (isdigit(log_string[0])) {
41 int level = atoi(log_string);
43 if (level >= 0 && level < ARRAY_SIZE(log_string_map)) {
44 *log_level = debug_level_from_int(level);
45 return true;
47 return false;
50 for (i=0; i<ARRAY_SIZE(log_string_map); i++) {
51 if (strncasecmp(log_string_map[i].log_string,
52 log_string, strlen(log_string)) == 0) {
53 *log_level = log_string_map[i].log_level;
54 return true;
58 return false;
61 const char *debug_level_to_string(enum debug_level log_level)
63 int i;
65 for (i=0; ARRAY_SIZE(log_string_map); i++) {
66 if (log_string_map[i].log_level == log_level) {
67 return log_string_map[i].log_string;
70 return "UNKNOWN";
73 enum debug_level debug_level_from_string(const char *log_string)
75 bool found;
76 enum debug_level log_level;
78 found = debug_level_parse(log_string, &log_level);
79 if (found) {
80 return log_level;
83 /* Default debug level */
84 return DEBUG_ERR;
87 int debug_level_to_int(enum debug_level log_level)
89 return (int)log_level;
92 enum debug_level debug_level_from_int(int level)
94 enum debug_level log_level;
96 if (level >= 0 && level < ARRAY_SIZE(log_string_map)) {
97 log_level = log_string_map[level].log_level;
98 } else {
99 log_level = DEBUG_ERR;
102 return log_level;