diff --git a/README.md b/README.md index 41a136c..fc6f29c 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,7 @@ As you can use: --selectfields=id,timestamp,total_m3 select fields to be printed --separator= change field separator to c --shell= invokes cmdline with env variables containing the latest reading + --silent do not print informational messages nor warnings --useconfig= load config files from dir/etc --usestderr write notices/debug/verbose and other logging output to stderr (the default) --usestdoutforlogging write debug/verbose and logging output to stdout diff --git a/src/cmdline.cc b/src/cmdline.cc index 14d801b..04f049c 100644 --- a/src/cmdline.cc +++ b/src/cmdline.cc @@ -73,8 +73,8 @@ shared_ptr parseCommandLine(int argc, char **argv) { c->need_help = true; return shared_ptr(c); } - if (!strcmp(argv[i], "--silence")) { - c->silence = true; + if (!strcmp(argv[i], "--silent")) { + c->silent = true; i++; continue; } diff --git a/src/config.cc b/src/config.cc index d14398e..9ff5428 100644 --- a/src/config.cc +++ b/src/config.cc @@ -167,7 +167,7 @@ void handleLoglevel(Configuration *c, string loglevel) // Kick in trace immediately. traceEnabled(c->trace); } - else if (loglevel == "silent") { c->silence = true; } + else if (loglevel == "silent") { c->silent = true; } else if (loglevel == "normal") { } else { warning("No such log level: \"%s\"\n", loglevel.c_str()); diff --git a/src/config.h b/src/config.h index 3708d1f..ee25d51 100644 --- a/src/config.h +++ b/src/config.h @@ -52,7 +52,7 @@ struct Configuration std::string config_root; bool reload {}; bool need_help {}; - bool silence {}; + bool silent {}; bool verbose {}; bool version {}; bool license {}; diff --git a/src/main.cc b/src/main.cc index 6a50abe..e85bd13 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1078,7 +1078,7 @@ bool start(Configuration *config) } // Configure settings. - warningSilenced(config->silence); + silentLogging(config->silent); verboseEnabled(config->verbose); logTelegramsEnabled(config->logtelegrams); debugEnabled(config->debug); diff --git a/src/serial.cc b/src/serial.cc index d9a50e5..2c37372 100644 --- a/src/serial.cc +++ b/src/serial.cc @@ -188,6 +188,9 @@ protected: bool expecting_ascii_ {}; // If true, print using safeString instead if bin2hex bool is_file_ = false; bool is_stdin_ = false; + // When feeding from stdin, to prevent early exit, we want + // at least some data before leaving the loop! + // I.e. do not exit before we have received something! bool no_callbacks_ = false; SerialCommunicationManagerImp *manager_; bool resetting_ {}; // Set to true while resetting. @@ -227,11 +230,19 @@ int SerialDeviceImp::receive(vector *data) } if (nr == 0) { - if (is_stdin_ || is_file_) + if (is_file_) { - debug("(serial) no more data on fd=%d\n", fd_); + debug("(serial) no more data on file fd=%d\n", fd_); close_me = true; } + if (is_stdin_) + { + if (getchar() == EOF) + { + debug("(serial) no more data on stdin fd=%d\n", fd_); + close_me = true; + } + } break; } if (nr < 0) diff --git a/src/util.cc b/src/util.cc index 126bd3d..f753b6a 100644 --- a/src/util.cc +++ b/src/util.cc @@ -278,7 +278,7 @@ string format3fdot3f(double v) bool syslog_enabled_ = false; bool logfile_enabled_ = false; -bool warning_enabled_ = true; +bool logging_silenced_ = false; bool verbose_enabled_ = false; bool debug_enabled_ = false; bool trace_enabled_ = false; @@ -288,8 +288,8 @@ bool internal_testing_enabled_ = false; string log_file_; -void warningSilenced(bool b) { - warning_enabled_ = !b; +void silentLogging(bool b) { + logging_silenced_ = b; } void enableSyslog() { @@ -418,21 +418,25 @@ void outputStuff(int syslog_level, const char *fmt, va_list args) } void info(const char* fmt, ...) { - va_list args; - va_start(args, fmt); - outputStuff(LOG_INFO, fmt, args); - va_end(args); + if (!logging_silenced_) { + va_list args; + va_start(args, fmt); + outputStuff(LOG_INFO, fmt, args); + va_end(args); + } } void notice(const char* fmt, ...) { - va_list args; - va_start(args, fmt); - outputStuff(LOG_NOTICE, fmt, args); - va_end(args); + if (!logging_silenced_) { + va_list args; + va_start(args, fmt); + outputStuff(LOG_NOTICE, fmt, args); + va_end(args); + } } void warning(const char* fmt, ...) { - if (warning_enabled_) { + if (!logging_silenced_) { va_list args; va_start(args, fmt); outputStuff(LOG_WARNING, fmt, args); diff --git a/src/util.h b/src/util.h index 43ed3fc..7130c99 100644 --- a/src/util.h +++ b/src/util.h @@ -72,7 +72,7 @@ void warning(const char* fmt, ...); void info(const char* fmt, ...); void notice(const char* fmt, ...); -void warningSilenced(bool b); +void silentLogging(bool b); void verboseEnabled(bool b); void debugEnabled(bool b); void traceEnabled(bool b); diff --git a/test.sh b/test.sh index 21b2ed7..fd62d46 100755 --- a/test.sh +++ b/test.sh @@ -90,6 +90,9 @@ if [ "$?" != "0" ]; then RC="1"; fi tests/test_ignore_duplicates.sh $PROG if [ "$?" != "0" ]; then RC="1"; fi +tests/test_pipe.sh $PROG +if [ "$?" != "0" ]; then RC="1"; fi + if [ "$(uname)" = "Linux" ] then tests/test_alarm.sh $PROG diff --git a/wmbusmeters.1 b/wmbusmeters.1 index eb3c154..cd563c4 100644 --- a/wmbusmeters.1 +++ b/wmbusmeters.1 @@ -81,6 +81,8 @@ mqtt_publish) sent to a REST API (eg curl) or store it in a database \fB\--shell=\fR invokes cmdline with env variables containing the latest reading +\fB\--silent\fR do not print informational messages nor warnings + \fB\--useconfig=\fR load config files from dir/etc \fB\--usestderr\fR write notices/debug/verbose and other logging output to stderr (the default)