Skip to content

Commit 89b6d3f

Browse files
committed
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.
1 parent 32b270b commit 89b6d3f

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

configure

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,9 @@ for opt do
347347
BINDIR="$optarg"
348348
;;
349349
--service-name=*)
350-
SERVICE_NAME="$optarg"
351-
;;
350+
SERVICE_NAME="$optarg"
351+
CPPFLAGS="-DMY_SERVICE_NAME=\"${optarg}\" $CPPFLAGS"
352+
;;
352353
--no-clean*)
353354
NO_CLEAN="1"
354355
;;

hal/architecture/Linux/MyHwLinuxGeneric.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ bool hwInit(void)
3838
exit(1);
3939
}
4040

41+
logInfo("Using eeprom file %s\n", conf.eeprom_file);
42+
4143
return true;
4244
}
4345

hal/architecture/Linux/drivers/core/EthernetServer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void EthernetServer::begin(IPAddress address)
9090
}
9191

9292
if (p == NULL) {
93-
logError("Failed to bind!\n");
93+
logError("Failed to bind to port %d! Is another instance running?\n", port);
9494
freeaddrinfo(servinfo);
9595
return;
9696
}

hal/architecture/Linux/drivers/core/config.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
#include <string.h>
2626
#include <unistd.h>
2727
#include <sys/stat.h>
28+
#include <sys/file.h>
2829
#include "log.h"
30+
#include <errno.h>
2931

3032
static int _config_create(const char *config_file);
3133
static int _config_parse_int(char *token, const char *name, int *value);
@@ -36,6 +38,7 @@ int config_parse(const char *config_file)
3638
FILE *fptr;
3739
char buf[1024];
3840
struct stat fileInfo;
41+
logInfo("Using config file %s\n", config_file);
3942

4043
if (stat(config_file, &fileInfo) != 0) {
4144
//File does not exist. Create it.
@@ -45,7 +48,7 @@ int config_parse(const char *config_file)
4548

4649
fptr = fopen(config_file, "rt");
4750
if (!fptr) {
48-
logError("Error opening config file \"%s\".\n", config_file);
51+
logError("Error opening config file \"%s\": %s\n", config_file, strerror(errno));
4952
return -1;
5053
}
5154

@@ -260,7 +263,7 @@ int _config_create(const char *config_file)
260263

261264
myFile = fopen(config_file, "w");
262265
if (!myFile) {
263-
logError("Unable to create config file %s.\n", config_file);
266+
logError("Unable to create config file %s: %s\n", config_file, strerror(errno));
264267
return -1;
265268
}
266269
ret = fputs(default_conf, myFile);

hal/architecture/Linux/drivers/core/log.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
#include <time.h>
2929
#include <errno.h>
3030

31+
#ifndef MY_SERVICE_NAME
32+
#define MY_SERVICE_NAME "mysgw"
33+
#endif
34+
35+
#define STR_HELPER(x) #x //!< Helper macro, STR_HELPER()
36+
#define STR(x) STR_HELPER(x) //!< Helper macro, STR()
37+
3138
static const char *_log_level_colors[] = {
3239
"\x1b[1;5;91m", "\x1b[1;91m", "\x1b[91m", "\x1b[31m", "\x1b[33m", "\x1b[34m", "\x1b[32m", "\x1b[36m"
3340
};
@@ -80,6 +87,7 @@ int logSetPipe(char *pipe_file)
8087
_log_pipe = 1;
8188
}
8289

90+
logInfo("Using log pipe %s\n", _log_pipe_file);
8391
return ret;
8492
}
8593

@@ -94,6 +102,7 @@ int logSetFile(char *file)
94102
return errno;
95103
}
96104

105+
logInfo("Using log file %s\n", file);
97106
return 0;
98107
}
99108

@@ -138,18 +147,18 @@ void vlog(int level, const char *fmt, va_list args)
138147
date[strftime(date, sizeof(date), "%b %d %H:%M:%S", lt)] = '\0';
139148

140149
if (_log_file_fp != NULL) {
141-
fprintf(_log_file_fp, "%s %-5s ", date, _log_level_names[level]);
150+
fprintf(_log_file_fp, "%s %s %-5s ", date, STR(MY_SERVICE_NAME), _log_level_names[level]);
142151
vfprintf(_log_file_fp, fmt, args);
143152
fflush(_log_file_fp);
144153
}
145154

146155
if (!_log_quiet) {
147156
#ifdef LOG_DISABLE_COLOR
148157
(void)_log_level_colors;
149-
fprintf(stderr, "%s %-5s ", date, _log_level_names[level]);
158+
fprintf(stderr, "%s %s %-5s ", date, STR(MY_SERVICE_NAME) _log_level_names[level]);
150159
vfprintf(stderr, fmt, args);
151160
#else
152-
fprintf(stderr, "%s %s%-5s\x1b[0m ", date, _log_level_colors[level], _log_level_names[level]);
161+
fprintf(stderr, "%s %s %s%-5s\x1b[0m ", date, STR(MY_SERVICE_NAME), _log_level_colors[level], _log_level_names[level]);
153162
vfprintf(stderr, fmt, args);
154163
#endif
155164
}

0 commit comments

Comments
 (0)