Introducing a new option '--rotate' to rotate given logtrace stream(s).

This patch also cleans the code of LogServer::ExecuteCommand().
---
 src/base/log_writer.h           |   2 +-
 src/dtm/tools/osaflog.cc        |  25 ++++++-
 src/dtm/transport/log_server.cc | 125 +++++++++++++++++++++-----------
 src/dtm/transport/log_server.h  |  11 ++-
 4 files changed, 115 insertions(+), 48 deletions(-)

diff --git a/src/base/log_writer.h b/src/base/log_writer.h
index 0a03e9253..ab2bf32ae 100644
--- a/src/base/log_writer.h
+++ b/src/base/log_writer.h
@@ -45,13 +45,13 @@ class LogWriter {
   void Write(size_t size);
   void Write(const char* bytes, size_t size);
   void Flush();
+  void RotateLog();
   void SetLogFile(const std::string& log_file) { log_file_ = log_file; }
 
  private:
   constexpr static const size_t kBufferSize = 128 * size_t{1024};
   void Open();
   void Close();
-  void RotateLog();
 
   std::string log_file(size_t backup) const;
 
diff --git a/src/dtm/tools/osaflog.cc b/src/dtm/tools/osaflog.cc
index 4e0956aa2..f6fa16801 100644
--- a/src/dtm/tools/osaflog.cc
+++ b/src/dtm/tools/osaflog.cc
@@ -54,6 +54,7 @@ base::UnixServerSocket* CreateSocket();
 uint64_t Random64Bits(uint64_t seed);
 bool PrettyPrint(const std::string& log_stream);
 bool Delete(const std::string& log_stream);
+bool Rotate(const std::string& log_stream);
 std::list<int> OpenLogFiles(const std::string& log_stream);
 std::string PathName(const std::string& log_stream, int suffix);
 uint64_t GetInode(int fd);
@@ -70,6 +71,7 @@ int main(int argc, char** argv) {
                                   {"flush", no_argument, 0, 'f'},
                                   {"print", no_argument, nullptr, 'p'},
                                   {"delete", no_argument, nullptr, 'd'},
+                                  {"rotate", no_argument, nullptr, 'r'},
                                   {"extract-trace", required_argument, 0, 'e'},
                                   {"max-idle", required_argument, 0, 'i'},
                                   {0, 0, 0, 0}};
@@ -83,12 +85,14 @@ int main(int argc, char** argv) {
   bool flush_result =  true;
   bool print_result =  true;
   bool delete_result =  true;
+  bool rotate_result = true;
   bool max_file_size_result = true;
   bool number_of_backups_result = true;
   bool max_idle_result = true;
   bool flush_set = false;
   bool pretty_print_set = false;
   bool delete_set = false;
+  bool rotate_set = false;
   bool max_file_size_set = false;
   bool max_backups_set = false;
   bool max_idle_set = false;
@@ -101,7 +105,7 @@ int main(int argc, char** argv) {
     exit(EXIT_FAILURE);
   }
 
-  while ((option = getopt_long(argc, argv, "m:b:p:f:e:",
+  while ((option = getopt_long(argc, argv, "m:b:p:f:e:i:r",
                                long_options, &long_index)) != -1) {
     switch (option) {
       case 'p':
@@ -114,6 +118,9 @@ int main(int argc, char** argv) {
       case 'f':
         flush_set = true;
         break;
+      case 'r':
+        rotate_set = true;
+        break;
       case 'm':
         max_file_size = base::StrToUint64(optarg,
                                           &max_file_size_set);
@@ -164,12 +171,12 @@ int main(int argc, char** argv) {
 
   if (thread_trace) exit(ExtractTrace(input_core, output_trace));
 
-  if (argc > optind && !pretty_print_set && !delete_set) {
+  if (argc > optind && !pretty_print_set && !delete_set && !rotate_set) {
     pretty_print_set = true;
     flush_set = true;
   }
 
-  if ((argc <= optind && (pretty_print_set || delete_set)) ||
+  if ((argc <= optind && (pretty_print_set || delete_set || rotate_set)) ||
       (pretty_print_set && delete_set)) {
      PrintUsage(argv[0]);
      exit(EXIT_FAILURE);
@@ -188,6 +195,11 @@ int main(int argc, char** argv) {
       delete_result = Delete(argv[optind++]);
     }
   }
+  if (rotate_set == true) {
+    while (rotate_result && optind < argc) {
+      rotate_result = Rotate(argv[optind++]);
+    }
+  }
   if (max_backups_set == true) {
      number_of_backups_result = NoOfBackupFiles(max_backups);
   }
@@ -197,7 +209,7 @@ int main(int argc, char** argv) {
   if (max_idle_set == true) {
     max_idle_result = SetMaxIdleTime(max_idle);
   }
-  if (flush_result && print_result && max_file_size_result &&
+  if (flush_result && print_result && max_file_size_result && rotate_result &&
       delete_result && number_of_backups_result && max_idle_result)
      exit(EXIT_SUCCESS);
   exit(EXIT_FAILURE);
@@ -224,6 +236,7 @@ void PrintUsage(const char* program_name) {
           "--delete              Delete the specified LOGSTREAM(s) by\n"
           "                      removing allocated resources in the log\n"
           "                      server. Does not delete log files from 
disk.\n"
+          "--rotate              Rotate the specified LOGSTREAM(s).\n"
           "--max-file-size=SIZE  Set the maximum size of the log file to\n"
           "                      SIZE bytes. The log file will be rotated\n"
           "                      when it exceeds this size. Suffixes k, M 
and\n"
@@ -381,6 +394,10 @@ bool Delete(const std::string& log_stream) {
                      log_stream);
 }
 
+bool Rotate(const std::string& log_stream) {
+  return SendCommand(std::string("rotate ") + log_stream);
+}
+
 std::list<int> OpenLogFiles(const std::string& log_stream) {
   std::list<int> result{};
   bool last_open_failed = false;
diff --git a/src/dtm/transport/log_server.cc b/src/dtm/transport/log_server.cc
index 959bd2135..201ed2695 100644
--- a/src/dtm/transport/log_server.cc
+++ b/src/dtm/transport/log_server.cc
@@ -22,6 +22,7 @@
 #include <cstdint>
 #include <cstdlib>
 #include <cstring>
+#include <map>
 #include "base/osaf_poll.h"
 #include "base/string_parse.h"
 #include "base/time.h"
@@ -271,51 +272,91 @@ bool LogServer::ValidateAddress(const struct sockaddr_un& 
addr,
   }
 }
 
+std::string LogServer::MaxFileSizeCmd(const std::string& cmd,
+                                      const std::string& arg) {
+  bool success = false;
+  uint64_t max_file_size = base::StrToUint64(arg.c_str(), &success);
+  if (success && max_file_size <= SIZE_MAX) max_file_size_ = max_file_size;
+  return std::string{"!max-file-size " + std::to_string(max_file_size_)};
+}
+
+std::string LogServer::MaxBackupCmd(const std::string& cmd,
+                                    const std::string& arg) {
+  bool success = false;
+  uint64_t max_backups = base::StrToUint64(arg.c_str(), &success);
+  if (success && max_backups <= SIZE_MAX) max_backups_ = max_backups;
+  return std::string{"!max-backups " + std::to_string(max_backups_)};
+}
+
+std::string LogServer::DeleteCmd(const std::string& cmd,
+                                 const std::string& arg) {
+  if (!ValidateLogName(arg.c_str(), arg.size()) ||
+      arg == kMdsLogStreamName) {
+    return std::string{"Invalid stream name"};
+  }
+  auto iter = log_streams_.find(arg);
+  if (iter == log_streams_.end()) {
+    return std::string{"Stream not found"};
+  }
+  LogStream* stream = iter->second;
+  log_streams_.erase(iter);
+  if (current_stream_ == stream) {
+    current_stream_ = log_streams_.begin()->second;
+  }
+  delete stream;
+  --no_of_log_streams_;
+  return std::string{"!delete " + arg};
+}
+
+std::string LogServer::FlushCmd(const std::string& cmd,
+                                const std::string& arg) {
+  for (const auto& s : log_streams_) {
+    LogStream* stream = s.second;
+    stream->Flush();
+  }
+  return std::string{"!flush"};
+}
+
+std::string LogServer::MaxIdleCmd(const std::string& cmd,
+                                  const std::string& arg) {
+  bool success = false;
+  uint64_t max_idle_time = base::StrToUint64(arg.c_str(), &success);
+  if (success && max_idle_time <= Osaflog::kOneDayInMinute) {
+    max_idle_time_.tv_sec = max_idle_time*60;
+  }
+  return std::string{"!max-idle-time " + std::to_string(max_idle_time)};
+}
+
+std::string LogServer::RotateCmd(const std::string& cmd,
+                                 const std::string& arg) {
+  for (const auto& s : log_streams_) {
+    LogStream* stream = s.second;
+    if (stream->name() == arg) {
+      stream->Rotate();
+      break;
+    }
+  }
+  return std::string{"!rotate " + arg};
+}
+
 std::string LogServer::ExecuteCommand(const std::string& command,
                                       const std::string& argument) {
-  if (command == "?max-file-size") {
-    bool success;
-    uint64_t max_file_size = base::StrToUint64(argument.c_str(), &success);
-    if (success && max_file_size <= SIZE_MAX) max_file_size_ = max_file_size;
-    return std::string{"!max-file-size " + std::to_string(max_file_size_)};
-  } else if (command == "?max-backups") {
-    bool success;
-    uint64_t max_backups = base::StrToUint64(argument.c_str(), &success);
-    if (success && max_backups <= SIZE_MAX) max_backups_ = max_backups;
-    return std::string{"!max-backups " + std::to_string(max_backups_)};
-  } else if (command == "?delete") {
-    if (!ValidateLogName(argument.c_str(), argument.size()) ||
-        argument == kMdsLogStreamName) {
-      return std::string{"Invalid stream name"};
-    }
-    auto iter = log_streams_.find(argument);
-    if (iter == log_streams_.end()) {
-      return std::string{"Stream not found"};
-    }
-    LogStream* stream = iter->second;
-    log_streams_.erase(iter);
-    if (current_stream_ == stream) {
-      current_stream_ = log_streams_.begin()->second;
-    }
-    delete stream;
-    --no_of_log_streams_;
-    return std::string{"!delete " + argument};
-  } else if (command == "?flush") {
-    for (const auto& s : log_streams_) {
-      LogStream* stream = s.second;
-      stream->Flush();
-    }
-    return std::string{"!flush"};
-  } else if (command == "?max-idle-time") {
-      bool success = false;
-      uint64_t max_idle_time = base::StrToUint64(argument.c_str(), &success);
-      if (success && max_idle_time <= Osaflog::kOneDayInMinute) {
-        max_idle_time_.tv_sec = max_idle_time*60;
-      }
-      return std::string{"!max-idle-time " + std::to_string(max_idle_time)};
-  } else {
-    return std::string{"!not_supported"};
+  using CmdPtr = std::string (LogServer::*)(const std::string&,
+                                            const std::string&);
+  std::map<std::string, CmdPtr> cmd_dispatcher = {
+    {"?max-file-size", &LogServer::MaxFileSizeCmd},
+    {"?max-backups", &LogServer::MaxBackupCmd},
+    {"?delete", &LogServer::DeleteCmd},
+    {"?flush", &LogServer::FlushCmd},
+    {"?max-idle-time", &LogServer::MaxIdleCmd},
+    {"?rotate", &LogServer::RotateCmd}
+  };
+
+  if (cmd_dispatcher.find(command) != cmd_dispatcher.end()) {
+    return (this->*cmd_dispatcher[command])(command, argument);
   }
+
+  return std::string{"!not_supported"};
 }
 
 LogServer::LogStream::LogStream(const std::string& log_name,
diff --git a/src/dtm/transport/log_server.h b/src/dtm/transport/log_server.h
index 65b95b9b0..884a851e6 100644
--- a/src/dtm/transport/log_server.h
+++ b/src/dtm/transport/log_server.h
@@ -46,7 +46,6 @@ class LogServer {
   // process has received the SIGTERM signal, which is indicated by the caller
   // by making the term_fd (provided in the constructor) readable.
   void Run();
-  void CloseIdleStreams();
   // To read Transportd.conf
   bool ReadConfig(const char *transport_config_file);
 
@@ -69,6 +68,7 @@ class LogServer {
     // I/O.
     void Write(size_t size);
     void Flush();
+    void Rotate() { log_writer_.RotateLog(); }
     const char* name() const { return log_name_.c_str(); }
     struct timespec last_write() const { return last_write_; }
     struct timespec last_flush() const {
@@ -95,6 +95,15 @@ class LogServer {
                               socklen_t addrlen);
   std::string ExecuteCommand(const std::string& command,
                              const std::string& argument);
+  void CloseIdleStreams();
+
+  std::string MaxFileSizeCmd(const std::string& cmd, const std::string& arg);
+  std::string MaxBackupCmd(const std::string& cmd, const std::string& arg);
+  std::string DeleteCmd(const std::string& cmd, const std::string& arg);
+  std::string FlushCmd(const std::string& cmd, const std::string& arg);
+  std::string MaxIdleCmd(const std::string& cmd, const std::string& arg);
+  std::string RotateCmd(const std::string& cmd, const std::string& arg);
+
   int term_fd_;
   // Configuration for LogServer
   size_t max_backups_;
-- 
2.17.1



_______________________________________________
Opensaf-devel mailing list
Opensaf-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/opensaf-devel

Reply via email to