OSDN Git Service

Reinstate btsnoop last log saving
authorZach Johnson <zachoverflow@google.com>
Wed, 4 Mar 2015 22:06:13 +0000 (14:06 -0800)
committerAndre Eisenbach <eisenbach@google.com>
Mon, 16 Mar 2015 23:51:48 +0000 (16:51 -0700)
Waited for the merge to complete before reworking
the feature into the stack.

hci/src/btsnoop.c
include/stack_config.h
main/stack_config.c

index 2d45c10..5fc309f 100644 (file)
@@ -141,11 +141,20 @@ static void update_logging() {
   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;
     }
 
index d2f89c9..a4262c5 100644 (file)
@@ -28,6 +28,7 @@ static const char STACK_CONFIG_MODULE[] = "stack_config_module";
 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;
index 0175c08..d71ee44 100644 (file)
@@ -26,6 +26,7 @@
 
 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;
@@ -73,6 +74,10 @@ static bool get_btsnoop_turned_on(void) {
   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);
 }
@@ -84,6 +89,7 @@ static config_t *get_all(void) {
 const stack_config_t interface = {
   get_btsnoop_log_path,
   get_btsnoop_turned_on,
+  get_btsnoop_should_save_last,
   get_trace_config_enabled,
   get_all
 };