Hi Phuc,

Ack.

Thanks, Vu

On 1/7/20 3:11 PM, phuc.h.chau wrote:
Adding a new option '--all' that means to rotate all existing logtrace
if given along with the '--rotate' option.

This patch also corrects wrong indentation in osaflog.cc file.
---
  src/base/log_writer.h           |  1 +
  src/dtm/README                  | 15 +++++++++++++-
  src/dtm/tools/osaflog.cc        | 44 +++++++++++++++++++++++++++--------------
  src/dtm/transport/log_server.cc | 12 ++++++++++-
  src/dtm/transport/log_server.h  |  8 ++++++--
  5 files changed, 61 insertions(+), 19 deletions(-)

diff --git a/src/base/log_writer.h b/src/base/log_writer.h
index ab2bf32..abd6d47 100644
--- a/src/base/log_writer.h
+++ b/src/base/log_writer.h
@@ -47,6 +47,7 @@ class LogWriter {
    void Flush();
    void RotateLog();
    void SetLogFile(const std::string& log_file) { log_file_ = log_file; }
+  size_t file_size() const { return current_file_size_; }
private:
    constexpr static const size_t kBufferSize = 128 * size_t{1024};
diff --git a/src/dtm/README b/src/dtm/README
index 430ff19..ff73af7 100644
--- a/src/dtm/README
+++ b/src/dtm/README
@@ -190,6 +190,9 @@ Options:
  --delete              Delete the specified LOGSTREAM(s) by
                        removing allocated resources in the log
                        server. Does not delete log files from disk.
+--rotate              Rotate the specified LOGSTREAM(s).
+--all                 Rotate all LOGSTREAM(s).
+                      This option only works with '--rotate'.
  --max-file-size=SIZE  Set the maximum size of the log file to
                        SIZE bytes. The log file will be rotated
                        when it exceeds this size. Suffixes k, M and
@@ -197,4 +200,14 @@ Options:
                        gigabytes.
  --max-backups=NUM     Set the maximum number of backup files to
                        retain during log rotation to NUM.
-
+--extract-trace <corefile> <tracefile>
+                      If a process produces a core dump file has
+                      THREAD_TRACE_BUFFER enabled, this option
+                      reads the <corefile> to extract the trace
+                      strings in all threads and writes them to
+                      the <tracefile> file.
+--max-idle=NUM        Set the maximum number of idle time to NUM"
+                      minutes. If a stream has not been used for
+                      the given time, the stream will be closed.
+                      Given zero (default) to max-idle to disable
+                      this functionality.
diff --git a/src/dtm/tools/osaflog.cc b/src/dtm/tools/osaflog.cc
index f6fa168..b1fb461 100644
--- a/src/dtm/tools/osaflog.cc
+++ b/src/dtm/tools/osaflog.cc
@@ -55,6 +55,7 @@ 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);
+bool RotateAll();
  std::list<int> OpenLogFiles(const std::string& log_stream);
  std::string PathName(const std::string& log_stream, int suffix);
  uint64_t GetInode(int fd);
@@ -72,6 +73,7 @@ int main(int argc, char** argv) {
                                    {"print", no_argument, nullptr, 'p'},
                                    {"delete", no_argument, nullptr, 'd'},
                                    {"rotate", no_argument, nullptr, 'r'},
+                                  {"all", no_argument, nullptr, 'a'},
                                    {"extract-trace", required_argument, 0, 
'e'},
                                    {"max-idle", required_argument, 0, 'i'},
                                    {0, 0, 0, 0}};
@@ -93,6 +95,7 @@ int main(int argc, char** argv) {
    bool pretty_print_set = false;
    bool delete_set = false;
    bool rotate_set = false;
+  bool rotate_all = false;
    bool max_file_size_set = false;
    bool max_backups_set = false;
    bool max_idle_set = false;
@@ -105,7 +108,7 @@ int main(int argc, char** argv) {
      exit(EXIT_FAILURE);
    }
- while ((option = getopt_long(argc, argv, "m:b:p:f:e:i:r",
+  while ((option = getopt_long(argc, argv, "m:b:p:f:e:i:ra",
                                 long_options, &long_index)) != -1) {
      switch (option) {
        case 'p':
@@ -121,6 +124,9 @@ int main(int argc, char** argv) {
        case 'r':
          rotate_set = true;
          break;
+      case 'a':
+        rotate_all = true;
+        break;
        case 'm':
          max_file_size = base::StrToUint64(optarg,
                                            &max_file_size_set);
@@ -175,15 +181,15 @@ int main(int argc, char** argv) {
      pretty_print_set = true;
      flush_set = true;
    }
-
-  if ((argc <= optind && (pretty_print_set || delete_set || rotate_set)) ||
-      (pretty_print_set && delete_set)) {
-     PrintUsage(argv[0]);
-     exit(EXIT_FAILURE);
-  }
-
+  if ((argc <= optind && (pretty_print_set || delete_set)) ||
+      (pretty_print_set && delete_set) ||
+      (rotate_all && !rotate_set) ||
+      (argc == optind && rotate_set && !rotate_all)) {
+    PrintUsage(argv[0]);
+    exit(EXIT_FAILURE);
+   }
    if (flush_set == true) {
-     flush_result = Flush();
+    flush_result = Flush();
    }
    if (pretty_print_set == true) {
      while (print_result && optind < argc) {
@@ -195,16 +201,18 @@ 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 (rotate_all == true) {
+    rotate_result = RotateAll();
+  } else {
+      while (rotate_result && optind < argc) {
+        rotate_result = Rotate(argv[optind++]);
+      }
    }
    if (max_backups_set == true) {
-     number_of_backups_result = NoOfBackupFiles(max_backups);
+    number_of_backups_result = NoOfBackupFiles(max_backups);
    }
    if (max_file_size_set == true) {
-     max_file_size_result = MaxTraceFileSize(max_file_size);
+    max_file_size_result = MaxTraceFileSize(max_file_size);
    }
    if (max_idle_set == true) {
      max_idle_result = SetMaxIdleTime(max_idle);
@@ -237,6 +245,8 @@ void PrintUsage(const char* program_name) {
            "                      removing allocated resources in the log\n"
            "                      server. Does not delete log files from 
disk.\n"
            "--rotate              Rotate the specified LOGSTREAM(s).\n"
+          "--all                 Rotate all LOGSTREAM(s).\n"
+          "                      This option only works with '--rotate'.\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"
@@ -398,6 +408,10 @@ bool Rotate(const std::string& log_stream) {
    return SendCommand(std::string("rotate ") + log_stream);
  }
+bool RotateAll() {
+  return SendCommand(std::string("rotate-all"));
+}
+
  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 201ed26..1beb0c5 100644
--- a/src/dtm/transport/log_server.cc
+++ b/src/dtm/transport/log_server.cc
@@ -339,6 +339,15 @@ std::string LogServer::RotateCmd(const std::string& cmd,
    return std::string{"!rotate " + arg};
  }
+std::string LogServer::RotateAllCmd(const std::string& /*cmd*/,
+                                    const std::string& /*arg*/) {
+  for (const auto& s : log_streams_) {
+    LogStream* stream = s.second;
+    stream->Rotate();
+  }
+  return std::string{"!rotate-all"};
+}
+
  std::string LogServer::ExecuteCommand(const std::string& command,
                                        const std::string& argument) {
    using CmdPtr = std::string (LogServer::*)(const std::string&,
@@ -349,7 +358,8 @@ std::string LogServer::ExecuteCommand(const std::string& 
command,
      {"?delete", &LogServer::DeleteCmd},
      {"?flush", &LogServer::FlushCmd},
      {"?max-idle-time", &LogServer::MaxIdleCmd},
-    {"?rotate", &LogServer::RotateCmd}
+    {"?rotate", &LogServer::RotateCmd},
+    {"?rotate-all", &LogServer::RotateAllCmd}
    };
if (cmd_dispatcher.find(command) != cmd_dispatcher.end()) {
diff --git a/src/dtm/transport/log_server.h b/src/dtm/transport/log_server.h
index 884a851..0cd8015 100644
--- a/src/dtm/transport/log_server.h
+++ b/src/dtm/transport/log_server.h
@@ -68,7 +68,10 @@ class LogServer {
      // I/O.
      void Write(size_t size);
      void Flush();
-    void Rotate() { log_writer_.RotateLog(); }
+    void Rotate() {
+      if (log_writer_.file_size() == 0) return;
+      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 {
@@ -103,7 +106,8 @@ class LogServer {
    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);
-
+  std::string RotateAllCmd(const std::string& /*cmd*/,
+                           const std::string& /*arg*/);
    int term_fd_;
    // Configuration for LogServer
    size_t max_backups_;



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

Reply via email to