Hi Phuc, Maybe you need updating the Readme file to align with this change too src/dtm/README
-----Original Message----- From: phuc.h.chau <phuc.h.c...@dektech.com.au> Sent: Tuesday, January 7, 2020 11:11 AM To: thang.d.ngu...@dektech.com.au; minh.c...@dektech.com.au; gary....@dektech.com.au; vu.m.ngu...@dektech.com.au Cc: opensaf-devel@lists.sourceforge.net; phuc.h.chau <phuc.h.c...@dektech.com.au> Subject: [PATCH 1/1] dtm: rotate all logtraces on demand [#3133] 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/tools/osaflog.cc | 41 +++++++++++++++++++++++++++++------------ src/dtm/transport/log_server.cc | 10 ++++++++++ src/dtm/transport/log_server.h | 7 ++++++- 4 files changed, 46 insertions(+), 13 deletions(-) mode change 100644 => 100755 src/base/log_writer.h mode change 100644 => 100755 src/dtm/tools/osaflog.cc mode change 100644 => 100755 src/dtm/transport/log_server.cc mode change 100644 => 100755 src/dtm/transport/log_server.h diff --git a/src/base/log_writer.h b/src/base/log_writer.h old mode 100644 new mode 100755 index ab2bf32..abd6d47 --- 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/tools/osaflog.cc b/src/dtm/tools/osaflog.cc old mode 100644 new mode 100755 index f6fa168..4d00e2b --- 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); @@ -176,14 +182,16 @@ int main(int argc, char** argv) { 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,20 +203,23 @@ 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); } + if (flush_result && print_result && max_file_size_result && rotate_result && delete_result && number_of_backups_result && max_idle_result) exit(EXIT_SUCCESS); @@ -237,6 +248,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 +411,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 old mode 100644 new mode 100755 index 201ed26..839a21b --- 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,6 +358,7 @@ std::string LogServer::ExecuteCommand(const std::string& command, {"?delete", &LogServer::DeleteCmd}, {"?flush", &LogServer::FlushCmd}, {"?max-idle-time", &LogServer::MaxIdleCmd}, + {"?rotate-all", &LogServer::RotateAllCmd}, {"?rotate", &LogServer::RotateCmd} }; diff --git a/src/dtm/transport/log_server.h b/src/dtm/transport/log_server.h old mode 100644 new mode 100755 index 884a851..b26b08a --- 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,6 +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 -- 2.7.4 _______________________________________________ Opensaf-devel mailing list Opensaf-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/opensaf-devel