Changed log file to use a counting index instead of a now meaningless timestamp.
diff --git a/aos/linux_code/logging/binary_log_writer.cc b/aos/linux_code/logging/binary_log_writer.cc
index 67cb166..2be484a 100644
--- a/aos/linux_code/logging/binary_log_writer.cc
+++ b/aos/linux_code/logging/binary_log_writer.cc
@@ -8,6 +8,7 @@
#include <sys/types.h>
#include <pwd.h>
#include <fcntl.h>
+#include <dirent.h>
#include <map>
#include <unordered_set>
@@ -17,6 +18,7 @@
#include "aos/linux_code/init.h"
#include "aos/linux_code/configuration.h"
#include "aos/common/queue_types.h"
+#include "aos/common/die.h"
namespace aos {
namespace logging {
@@ -58,6 +60,43 @@
written_type_ids.insert(type_id);
}
+void AllocateLogName(char **filename, const char *directory) {
+ int fileindex = 0;
+ DIR* d;
+ if ((d = opendir(directory))) {
+ int index = 0;
+ struct dirent* dir;
+ while ((dir = readdir(d)) != NULL) {
+ if (sscanf(dir->d_name, "aos_log-%d", &index) == 1) {
+ if (index >= fileindex) {
+ fileindex = index + 1;
+ }
+ }
+ }
+ closedir(d);
+ } else {
+ aos::Die("could not open directory %s because of %d (%s).",
+ directory, errno, strerror(errno));
+ }
+
+ char previous[512];
+ ::std::string path = ::std::string(directory)+"/aos_log-current";
+ ssize_t len = ::readlink(path.c_str(), previous, sizeof(previous) - 1);
+ if (len != -1) {
+ previous[len] = '\0';
+ } else {
+ previous[0] = '\0';
+ LOG(INFO, "Could not find aos_log-current\n");
+ printf("Could not find aos_log-current\n");
+ }
+ if (asprintf(filename, "%s/aos_log-%d", directory, fileindex) == -1) {
+ aos::Die("BinaryLogReader: couldn't create final name because of %d (%s)."
+ " exiting\n", errno, strerror(errno));
+ }
+ LOG(INFO, "Created log file (aos_log-%d) in directory (%s). Pervious file was (%s)\n", fileindex, directory, previous);
+ printf("Created log file (aos_log-%d) in directory (%s). Pervious file was (%s)\n", fileindex, directory, previous);
+}
+
int BinaryLogReaderMain() {
InitNRT();
@@ -67,15 +106,8 @@
}
LOG(INFO, "logging to folder '%s'\n", folder);
- const time_t t = time(NULL);
char *tmp;
- if (asprintf(&tmp, "%s/aos_log-%jd", folder, static_cast<uintmax_t>(t)) ==
- -1) {
- fprintf(stderr,
- "BinaryLogReader: couldn't create final name because of %d (%s)."
- " exiting\n", errno, strerror(errno));
- return EXIT_FAILURE;
- }
+ AllocateLogName(&tmp, folder);
char *tmp2;
if (asprintf(&tmp2, "%s/aos_log-current", folder) == -1) {
fprintf(stderr,
diff --git a/aos/linux_code/logging/logging.gyp b/aos/linux_code/logging/logging.gyp
index 32df43d..52f8b96 100644
--- a/aos/linux_code/logging/logging.gyp
+++ b/aos/linux_code/logging/logging.gyp
@@ -11,6 +11,7 @@
'<(AOS)/build/aos.gyp:logging',
'<(AOS)/linux_code/linux_code.gyp:init',
'<(AOS)/linux_code/linux_code.gyp:configuration',
+ '<(AOS)/common/common.gyp:die',
'binary_log_file',
'<(AOS)/common/common.gyp:queue_types',
],