1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-11 05:18:09 +03:00

update the handling of debug levels so that we always can use a literal instead of a numeric value.

validate the input values used and refuse setting the debug level to an unknown value

(This used to be ctdb commit daec49cea1790bcc64599959faf2159dec2c5929)
This commit is contained in:
Ronnie Sahlberg 2009-07-01 09:17:13 +10:00
parent 9802a0c2f6
commit 93026f4cbf
4 changed files with 62 additions and 48 deletions

View File

@ -24,16 +24,19 @@
#include "../include/ctdb.h"
#include "../include/ctdb_private.h"
#include "../common/rb_tree.h"
#include <ctype.h>
/* Handle common command line options for ctdb test progs
*/
static struct {
const char *socketname;
const char *debuglevel;
int torture;
const char *events;
} ctdb_cmdline = {
.torture = 0,
.debuglevel = "ERR",
};
enum {OPT_EVENTSYSTEM=1};
@ -54,7 +57,7 @@ static void ctdb_cmdline_callback(poptContext con,
struct poptOption popt_ctdb_cmdline[] = {
{ NULL, 0, POPT_ARG_CALLBACK, (void *)ctdb_cmdline_callback },
{ "socket", 0, POPT_ARG_STRING, &ctdb_cmdline.socketname, 0, "local socket name", "filename" },
{ "debug", 'd', POPT_ARG_INT, &LogLevel, 0, "debug level"},
{ "debug", 'd', POPT_ARG_STRING, &ctdb_cmdline.debuglevel, 0, "debug level"},
{ "torture", 0, POPT_ARG_NONE, &ctdb_cmdline.torture, 0, "enable nastiness in library", NULL },
{ "events", 0, POPT_ARG_STRING, NULL, OPT_EVENTSYSTEM, "event system", NULL },
{ NULL }
@ -91,6 +94,13 @@ struct ctdb_context *ctdb_cmdline_init(struct event_context *ev)
}
}
/* Set the debug level */
if (isalpha(ctdb_cmdline.debuglevel[0]) || ctdb_cmdline.debuglevel[0] == '-') {
LogLevel = get_debug_by_desc(ctdb_cmdline.debuglevel);
} else {
LogLevel = strtol(ctdb_cmdline.debuglevel, NULL, 0);
}
/* set up the tree to store server ids */
ctdb->server_ids = trbt_create(ctdb, 0);

View File

@ -624,3 +624,41 @@ void ctdb_unblock_signal(int signum)
sigaddset(&set,signum);
sigprocmask(SIG_UNBLOCK,&set,NULL);
}
struct debug_levels debug_levels[] = {
{DEBUG_EMERG, "EMERG"},
{DEBUG_ALERT, "ALERT"},
{DEBUG_CRIT, "CRIT"},
{DEBUG_ERR, "ERR"},
{DEBUG_WARNING, "WARNING"},
{DEBUG_NOTICE, "NOTICE"},
{DEBUG_INFO, "INFO"},
{DEBUG_DEBUG, "DEBUG"},
{0, NULL}
};
const char *get_debug_by_level(int32_t level)
{
int i;
for (i=0; debug_levels[i].description != NULL; i++) {
if (debug_levels[i].level == level) {
return debug_levels[i].description;
}
}
return "Unknown";
}
int32_t get_debug_by_desc(const char *desc)
{
int i;
for (i=0; debug_levels[i].description != NULL; i++) {
if (!strcmp(debug_levels[i].description, desc)) {
return debug_levels[i].level;
}
}
return DEBUG_ERR;
}

View File

@ -644,4 +644,14 @@ int ctdb_ctrl_getscriptstatus(struct ctdb_context *ctdb,
TALLOC_CTX *mem_ctx, struct ctdb_monitoring_wire **script_status);
struct debug_levels {
int32_t level;
const char *description;
};
extern struct debug_levels debug_levels[];
const char *get_debug_by_level(int32_t level);
int32_t get_debug_by_desc(const char *desc);
#endif

View File

@ -2181,50 +2181,6 @@ static int control_listvars(struct ctdb_context *ctdb, int argc, const char **ar
return 0;
}
static struct {
int32_t level;
const char *description;
} debug_levels[] = {
{DEBUG_EMERG, "EMERG"},
{DEBUG_ALERT, "ALERT"},
{DEBUG_CRIT, "CRIT"},
{DEBUG_ERR, "ERR"},
{DEBUG_WARNING, "WARNING"},
{DEBUG_NOTICE, "NOTICE"},
{DEBUG_INFO, "INFO"},
{DEBUG_DEBUG, "DEBUG"}
};
static const char *get_debug_by_level(int32_t level)
{
int i;
for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
if (debug_levels[i].level == level) {
return debug_levels[i].description;
}
}
return "Unknown";
}
static int32_t get_debug_by_desc(const char *desc)
{
int i;
for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
if (!strcmp(debug_levels[i].description, desc)) {
return debug_levels[i].level;
}
}
fprintf(stderr, "Invalid debug level '%s'\nMust be one of\n", desc);
for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
fprintf(stderr, " %s\n", debug_levels[i].description);
}
exit(10);
}
/*
display debug level on a node
*/
@ -2304,7 +2260,7 @@ static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **ar
if (argc == 0) {
printf("You must specify the debug level. Valid levels are:\n");
for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
for (i=0; debug_levels[i].description != NULL; i++) {
printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
}
@ -2317,12 +2273,12 @@ static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **ar
level = strtol(argv[0], NULL, 0);
}
for (i=0;i<ARRAY_SIZE(debug_levels);i++) {
for (i=0; debug_levels[i].description != NULL; i++) {
if (level == debug_levels[i].level) {
break;
}
}
if (i == ARRAY_SIZE(debug_levels)) {
if (debug_levels[i].description == NULL) {
printf("Invalid debug level\n");
return -1;
}