if (should_log) {
btsnoop_net_open();
- const char *path = stack_config->get_btsnoop_log_path();
- logfile_fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
+ const char *log_path = stack_config->get_btsnoop_log_path();
+
+ // Save the old log if configured to do so
+ if (stack_config->get_btsnoop_should_save_last()) {
+ char last_log_path[PATH_MAX];
+ snprintf(last_log_path, PATH_MAX, "%s.last", log_path);
+ if (!rename(log_path, last_log_path) && errno != ENOENT)
+ LOG_ERROR("%s unable to rename '%s' to '%s': %s", __func__, log_path, last_log_path, strerror(errno));
+ }
+ logfile_fd = open(log_path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
if (logfile_fd == INVALID_FD) {
- LOG_ERROR("%s unable to open '%s': %s", __func__, path, strerror(errno));
+ LOG_ERROR("%s unable to open '%s': %s", __func__, log_path, strerror(errno));
+ is_logging = false;
return;
}
typedef struct {
const char *(*get_btsnoop_log_path)(void);
bool (*get_btsnoop_turned_on)(void);
+ bool (*get_btsnoop_should_save_last)(void);
bool (*get_trace_config_enabled)(void);
config_t *(*get_all)(void);
} stack_config_t;
const char *BTSNOOP_LOG_PATH_KEY = "BtSnoopFileName";
const char *BTSNOOP_TURNED_ON_KEY = "BtSnoopLogOutput";
+const char *BTSNOOP_SHOULD_SAVE_LAST_KEY = "BtSnoopSaveLog";
const char *TRACE_CONFIG_ENABLED_KEY = "TraceConf";
static config_t *config;
return config_get_bool(config, CONFIG_DEFAULT_SECTION, BTSNOOP_TURNED_ON_KEY, false);
}
+static bool get_btsnoop_should_save_last(void) {
+ return config_get_bool(config, CONFIG_DEFAULT_SECTION, BTSNOOP_SHOULD_SAVE_LAST_KEY, false);
+}
+
static bool get_trace_config_enabled(void) {
return config_get_bool(config, CONFIG_DEFAULT_SECTION, TRACE_CONFIG_ENABLED_KEY, false);
}
const stack_config_t interface = {
get_btsnoop_log_path,
get_btsnoop_turned_on,
+ get_btsnoop_should_save_last,
get_trace_config_enabled,
get_all
};