Skip to content

Commit

Permalink
Logging fix and enhancement
Browse files Browse the repository at this point in the history
Corrected issue where loglevel in mbusd.conf wasn't parsed correctly. Added support for logfile in mbusd.conf.  Updated README and mbusd..conf.example to true-up with "-h" output and new logging options.
  • Loading branch information
kpr0th committed Dec 22, 2023
1 parent 68f9880 commit b76ac4b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
8 changes: 8 additions & 0 deletions conf/mbusd.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
# #
#############################################

########## Logging settings #############

# Logging verbosity level
loglevel = 2

# Logfile (fully-qualified path, or filename [stored at /var/log/] or - for STDOUT only)
logfile = /var/log/mbus.log

########## Serial port settings #############

# Serial port device name
Expand Down
52 changes: 51 additions & 1 deletion src/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ cfg_ltrim(const char *s)
return (char *) s;
}

static int
cfg_is_empty(char *s)
{
char *p = s + strlen(s);
if (strlen(s) == 0)
return 1;
while (p > s && isspace((unsigned char )(*--p)))
{ //no-op
}
if (p == s && isspace((unsigned char )(*p)))
return 1;
return 0;
}

int
cfg_handle_param(char *name, char *value)
{
Expand Down Expand Up @@ -231,7 +245,43 @@ cfg_handle_param(char *name, char *value)
}
else if (CFG_NAME_MATCH("loglevel"))
{
cfg.dbglvl = (char)strtol(optarg, NULL, 0);
cfg.dbglvl = (char)strtol(value, NULL, 0);
# ifdef DEBUG
if (!(isdigit(*value)) || cfg.dbglvl < 0 || cfg.dbglvl > 9)
{ /* report about invalid log level */
CFG_ERR("invalid loglevel value: %s (must be 0-9)", value);
# else
if (!(isdigit(*value)) || cfg.dbglvl < 0 || cfg.dbglvl > 2)
{ /* report about invalid log level */
CFG_ERR("invalid loglevel value: %s (must be 0-2)", value);
# endif
return(0);
}
}
else if (CFG_NAME_MATCH("logfile"))
{
if (cfg_is_empty(value))
{
CFG_ERR("missing logfile value", value);
return(0);
}
else if (*value != '/')
{
if (*value == '-')
{
/* logging to file disabled */
*cfg.logname = '\0';
}
else
{ /* concatenate given log file name with default path */
strncpy(cfg.logname, LOGPATH, INTBUFSIZE);
strncat(cfg.logname, value, INTBUFSIZE - strlen(cfg.logname));
}
}
else strncpy(cfg.logname, value, INTBUFSIZE);



#endif
}
else {
Expand Down
30 changes: 25 additions & 5 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ ttydata_t tty;
/* Connections queue descriptor */
queue_t queue;

static int
main_is_empty(char *s)
{
char *p = s + strlen(s);
if (strlen(s) == 0)
return 1;
while (p > s && isspace((unsigned char )(*--p)))
{ //no-op
}
if (p == s && isspace((unsigned char )(*p)))
return 1;
return 0;
}

#ifndef HAVE_DAEMON
#include <fcntl.h>
#include <unistd.h>
Expand Down Expand Up @@ -167,6 +181,7 @@ usage(char *exename)
exit(0);
}


int
main(int argc, char *argv[])
{
Expand Down Expand Up @@ -242,21 +257,26 @@ main(int argc, char *argv[])
case 'v':
cfg.dbglvl = (char)strtol(optarg, NULL, 0);
# ifdef DEBUG
if (cfg.dbglvl > 9)
if (!(isdigit(*optarg)) || cfg.dbglvl < 0 || cfg.dbglvl > 9)
{ /* report about invalid log level */
printf("%s: -v: invalid loglevel value"
" (%d, must be 0-9)\n", exename, cfg.dbglvl);
" (%s, must be 0-9)\n", exename, optarg);
# else
if (cfg.dbglvl < 0 || cfg.dbglvl > 9)
if (!(isdigit(*optarg)) || cfg.dbglvl < 0 || cfg.dbglvl > 2)
{ /* report about invalid log level */
printf("%s: -v: invalid loglevel value"
" (%d, must be 0-2)\n", exename, cfg.dbglvl);
" (%s, must be 0-2)\n", exename, optarg);
# endif
exit(-1);
}
break;
case 'L':
if (*optarg != '/')
if (main_is_empty(optarg))
{ /* report about invalid log file */
printf("%s: -L: missing logfile value\n", exename, optarg);
exit(-1);
}
else if (*optarg != '/')
{
if (*optarg == '-')
{
Expand Down

0 comments on commit b76ac4b

Please sign in to comment.