Skip to content

Commit

Permalink
Further work on supporting multiple instances
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
mfalkvidd committed May 14, 2020
1 parent 32b270b commit 89b6d3f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
5 changes: 3 additions & 2 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -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"
;;
Expand Down
2 changes: 2 additions & 0 deletions hal/architecture/Linux/MyHwLinuxGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ bool hwInit(void)
exit(1);
}

logInfo("Using eeprom file %s\n", conf.eeprom_file);

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion hal/architecture/Linux/drivers/core/EthernetServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
7 changes: 5 additions & 2 deletions hal/architecture/Linux/drivers/core/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/file.h>
#include "log.h"
#include <errno.h>

static int _config_create(const char *config_file);
static int _config_parse_int(char *token, const char *name, int *value);
Expand All @@ -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.
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
15 changes: 12 additions & 3 deletions hal/architecture/Linux/drivers/core/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
#include <time.h>
#include <errno.h>

#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"
};
Expand Down Expand Up @@ -80,6 +87,7 @@ int logSetPipe(char *pipe_file)
_log_pipe = 1;
}

logInfo("Using log pipe %s\n", _log_pipe_file);
return ret;
}

Expand All @@ -94,6 +102,7 @@ int logSetFile(char *file)
return errno;
}

logInfo("Using log file %s\n", file);
return 0;
}

Expand Down Expand Up @@ -138,18 +147,18 @@ 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);
}

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
}
Expand Down

0 comments on commit 89b6d3f

Please sign in to comment.