From 89b6d3f52d56e14e45fc056e76f78d166807bdc7 Mon Sep 17 00:00:00 2001 From: mfalkvidd Date: Thu, 14 May 2020 15:27:00 +0200 Subject: [PATCH] Further work on supporting multiple instances * On stderr, the service name is printed on each line. * Print the name of the eeprom file, the log file and the log pipe on startup. Hopefully, this will help people notice when they are running multiple instances, and to distinguish between them. --- configure | 5 +++-- hal/architecture/Linux/MyHwLinuxGeneric.cpp | 2 ++ .../Linux/drivers/core/EthernetServer.cpp | 2 +- hal/architecture/Linux/drivers/core/config.c | 7 +++++-- hal/architecture/Linux/drivers/core/log.c | 15 ++++++++++++--- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/configure b/configure index fedb54b7e..f5f473afa 100755 --- a/configure +++ b/configure @@ -347,8 +347,9 @@ for opt do BINDIR="$optarg" ;; --service-name=*) - SERVICE_NAME="$optarg" - ;; + SERVICE_NAME="$optarg" + CPPFLAGS="-DMY_SERVICE_NAME=\"${optarg}\" $CPPFLAGS" + ;; --no-clean*) NO_CLEAN="1" ;; diff --git a/hal/architecture/Linux/MyHwLinuxGeneric.cpp b/hal/architecture/Linux/MyHwLinuxGeneric.cpp index 2a78e2574..3f0f1c51f 100644 --- a/hal/architecture/Linux/MyHwLinuxGeneric.cpp +++ b/hal/architecture/Linux/MyHwLinuxGeneric.cpp @@ -38,6 +38,8 @@ bool hwInit(void) exit(1); } + logInfo("Using eeprom file %s\n", conf.eeprom_file); + return true; } diff --git a/hal/architecture/Linux/drivers/core/EthernetServer.cpp b/hal/architecture/Linux/drivers/core/EthernetServer.cpp index 22309d6e7..46d00423e 100644 --- a/hal/architecture/Linux/drivers/core/EthernetServer.cpp +++ b/hal/architecture/Linux/drivers/core/EthernetServer.cpp @@ -90,7 +90,7 @@ void EthernetServer::begin(IPAddress address) } if (p == NULL) { - logError("Failed to bind!\n"); + logError("Failed to bind to port %d! Is another instance running?\n", port); freeaddrinfo(servinfo); return; } diff --git a/hal/architecture/Linux/drivers/core/config.c b/hal/architecture/Linux/drivers/core/config.c index 23c717e52..9a30c423c 100644 --- a/hal/architecture/Linux/drivers/core/config.c +++ b/hal/architecture/Linux/drivers/core/config.c @@ -25,7 +25,9 @@ #include #include #include +#include #include "log.h" +#include static int _config_create(const char *config_file); static int _config_parse_int(char *token, const char *name, int *value); @@ -36,6 +38,7 @@ int config_parse(const char *config_file) FILE *fptr; char buf[1024]; struct stat fileInfo; + logInfo("Using config file %s\n", config_file); if (stat(config_file, &fileInfo) != 0) { //File does not exist. Create it. @@ -45,7 +48,7 @@ int config_parse(const char *config_file) fptr = fopen(config_file, "rt"); if (!fptr) { - logError("Error opening config file \"%s\".\n", config_file); + logError("Error opening config file \"%s\": %s\n", config_file, strerror(errno)); return -1; } @@ -260,7 +263,7 @@ int _config_create(const char *config_file) myFile = fopen(config_file, "w"); if (!myFile) { - logError("Unable to create config file %s.\n", config_file); + logError("Unable to create config file %s: %s\n", config_file, strerror(errno)); return -1; } ret = fputs(default_conf, myFile); diff --git a/hal/architecture/Linux/drivers/core/log.c b/hal/architecture/Linux/drivers/core/log.c index 7f6c6b407..86f059906 100644 --- a/hal/architecture/Linux/drivers/core/log.c +++ b/hal/architecture/Linux/drivers/core/log.c @@ -28,6 +28,13 @@ #include #include +#ifndef MY_SERVICE_NAME +#define MY_SERVICE_NAME "mysgw" +#endif + +#define STR_HELPER(x) #x //!< Helper macro, STR_HELPER() +#define STR(x) STR_HELPER(x) //!< Helper macro, STR() + static const char *_log_level_colors[] = { "\x1b[1;5;91m", "\x1b[1;91m", "\x1b[91m", "\x1b[31m", "\x1b[33m", "\x1b[34m", "\x1b[32m", "\x1b[36m" }; @@ -80,6 +87,7 @@ int logSetPipe(char *pipe_file) _log_pipe = 1; } + logInfo("Using log pipe %s\n", _log_pipe_file); return ret; } @@ -94,6 +102,7 @@ int logSetFile(char *file) return errno; } + logInfo("Using log file %s\n", file); return 0; } @@ -138,7 +147,7 @@ void vlog(int level, const char *fmt, va_list args) date[strftime(date, sizeof(date), "%b %d %H:%M:%S", lt)] = '\0'; if (_log_file_fp != NULL) { - fprintf(_log_file_fp, "%s %-5s ", date, _log_level_names[level]); + fprintf(_log_file_fp, "%s %s %-5s ", date, STR(MY_SERVICE_NAME), _log_level_names[level]); vfprintf(_log_file_fp, fmt, args); fflush(_log_file_fp); } @@ -146,10 +155,10 @@ void vlog(int level, const char *fmt, va_list args) if (!_log_quiet) { #ifdef LOG_DISABLE_COLOR (void)_log_level_colors; - fprintf(stderr, "%s %-5s ", date, _log_level_names[level]); + fprintf(stderr, "%s %s %-5s ", date, STR(MY_SERVICE_NAME) _log_level_names[level]); vfprintf(stderr, fmt, args); #else - fprintf(stderr, "%s %s%-5s\x1b[0m ", date, _log_level_colors[level], _log_level_names[level]); + fprintf(stderr, "%s %s %s%-5s\x1b[0m ", date, STR(MY_SERVICE_NAME), _log_level_colors[level], _log_level_names[level]); vfprintf(stderr, fmt, args); #endif }