Hi Thien, I have same opinion with Thang, it looks redundant for these variables. Just need check and return directly, or you can write comments for clear.
// Return 0 if file name is not started with specific file name if (strncmp(finfo->d_name, file_name_find_g.c_str(), name_len) != 0) return 0; // Return 0 if next part of file name is not underscored character If (finfo->d_name[day_offset - 1] != '_') return 0; // Return 0 if next part of file name is not yyyymmdd format // Return 0 if next part of file name is not underscored character // Return 0 if next part of file name is not hhmmss format // Return 0 if last part of file name is not ".log" // Return 1 when file name is completely matching the format. return 1 Best Regards, ThuanTr -----Original Message----- From: Thien Minh Huynh <thien.m.hu...@dektech.com.au> Sent: Monday, April 13, 2020 11:29 AM To: Thang Duc Nguyen <thang.d.ngu...@dektech.com.au>; Thuan Tran <thuan.t...@dektech.com.au>; Vu Minh Nguyen <vu.m.ngu...@dektech.com.au> Cc: opensaf-devel@lists.sourceforge.net Subject: RE: [devel] [PATCH 1/1] log: log content is placed in a file of another stream [#3175] Hi Thang, Thanks for your comment. The purpose check through variable, it is clearer. Because the variable name tells me what I want to check and easy to view code. In the same function all_digits(), One then check yyyymmdd and one then check hhmmss. Best Regards, ThienHuynh -----Original Message----- From: Thang Duc Nguyen <thang.d.ngu...@dektech.com.au> Sent: Monday, April 13, 2020 11:00 AM To: Thien Minh Huynh <thien.m.hu...@dektech.com.au>; Thuan Tran <thuan.t...@dektech.com.au>; Vu Minh Nguyen <vu.m.ngu...@dektech.com.au> Cc: opensaf-devel@lists.sourceforge.net Subject: RE: [devel] [PATCH 1/1] log: log content is placed in a file of another stream [#3175] Hi Thien, Just a minor comment. Instead of using many variables then just use them to compare with 0/1 or true/false. I think it's better to use if directly. E.g, + bool hhmmss_format = all_digits(finfo->d_name + time_offset, + time_length); if (hhmmss_format == false) return 0; -> // time format + if (!all_digits(finfo->d_name + time_offset, time_length) ) + return 0; -----Original Message----- From: thien.m.huynh <thien.m.hu...@dektech.com.au> Sent: Monday, April 13, 2020 9:49 AM To: Thuan Tran <thuan.t...@dektech.com.au>; Vu Minh Nguyen <vu.m.ngu...@dektech.com.au> Cc: opensaf-devel@lists.sourceforge.net Subject: [devel] [PATCH 1/1] log: log content is placed in a file of another stream [#3175] Replace a previous patch of this ticket due to regex is not yet fully supported by gcc 4.8. --- src/log/logd/lgs_filehdl.cc | 53 +++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/src/log/logd/lgs_filehdl.cc b/src/log/logd/lgs_filehdl.cc index 9f4e27979..1743c53fd 100644 --- a/src/log/logd/lgs_filehdl.cc +++ b/src/log/logd/lgs_filehdl.cc @@ -26,7 +26,6 @@ #include <sys/stat.h> #include <unistd.h> #include <string> -#include <regex> #include "base/logtrace.h" #include "base/osaf_time.h" @@ -955,6 +954,19 @@ static int chr_cnt_b(char *str, char c, int lim) { return cnt; } +/** + * Check if all character is decimal digit + * @param data string to be checked + * @param size number of characters to check + * @return: true if all character is decimal digit */ static bool +all_digits(const char *data, int size) { + for (int i = 0; i < size; i++) { + if (!isdigit(data[i])) return false; + } + return true; +} + /** * Filter function used by scandir. * Find a current log file if it exist @@ -965,13 +977,38 @@ static int chr_cnt_b(char *str, char c, int lim) { /* Filename prefix (no timestamps or extension */ static std::string file_name_find_g; static int filter_logfile_name(const struct dirent *finfo) { - const std::string pattern = - "^" + file_name_find_g + "_[0-9]{8}_[0-9]{6}.log$"; - const std::regex e{pattern}; - if (std::regex_match(finfo->d_name, e)) { - return 1; - } - return 0; + size_t name_len = strlen(file_name_find_g.c_str()); size_t + fixed_length = name_len + strlen("_yyyymmdd_hhmmss.log"); + + if (strlen(finfo->d_name) != fixed_length) return 0; + + size_t day_length = strlen("yyyymmdd"); size_t time_length = + strlen("hhmmss"); int day_offset = 1 + name_len; int time_offset = 1 + + day_offset + day_length; int extension_offset = time_offset + + time_length; + + bool start_with_filename = + strncmp(finfo->d_name, file_name_find_g.c_str(), name_len) == 0; + if (start_with_filename == false) return 0; + + bool underscore_1 = finfo->d_name[day_offset - 1] == '_'; if + (underscore_1 == false) return 0; + + bool yyyymmdd_format = all_digits(finfo->d_name + day_offset, + day_length); if (yyyymmdd_format == false) return 0; + + bool underscore_2 = finfo->d_name[time_offset - 1] == '_'; if + (underscore_2 == false) return 0; + + bool hhmmss_format = all_digits(finfo->d_name + time_offset, + time_length); if (hhmmss_format == false) return 0; + + bool log_extension = + strncmp(finfo->d_name + extension_offset, ".log", strlen(".log")) + == 0; if (log_extension == false) return 0; + + return 1; } /** -- 2.17.1 _______________________________________________ Opensaf-devel mailing list Opensaf-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/opensaf-devel _______________________________________________ Opensaf-devel mailing list Opensaf-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/opensaf-devel