From 0e0ef8b1cff7d25c1331f08389da8c5e3a96593d Mon Sep 17 00:00:00 2001 From: Trumeet Date: Wed, 9 Jun 2021 16:17:24 -0700 Subject: feat: catch overload messages --- mcin/mc_regex.h | 2 ++ mcin/mcin.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++----------- mcin/mcin.h | 2 ++ 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/mcin/mc_regex.h b/mcin/mc_regex.h index 31f5c0e..d05aa48 100644 --- a/mcin/mc_regex.h +++ b/mcin/mc_regex.h @@ -52,4 +52,6 @@ ".* didn't want to live in the same world as.*," \ ".* withered away.*," \ "<.*> .*" +#define MC_REGEX_WARN \ +"Can't keep up! Is the server overloaded\\? Running .*ms or .* ticks behind" #endif // _MC_REGEX_H diff --git a/mcin/mcin.c b/mcin/mcin.c index 1430415..b9c7ec6 100644 --- a/mcin/mcin.c +++ b/mcin/mcin.c @@ -6,23 +6,16 @@ #include #include -int mcin_matcher_init(MCINMatcher *out) +static int mcin_load(char str[], regex_t *arr) { int r = 0; - r = regcomp(&out->regex_master, ".*\\[([0-9][0-9]:[0-9][0-9]:[0-9][0-9])\\] \\[(.*)\\/(.*)\\]: (.*)", REG_EXTENDED); - if(r) - { - fprintf(stderr, _("Cannot compile master regex: %d.\n"), r); - return r; - } int line = 0; - char str[] = MC_REGEX; char *token; char *rest = str; while ((token = strtok_r(rest, ",", &rest))) { int l = line++; - regex_t *reg = &out->regex[l]; + regex_t *reg = &arr[l]; r = regcomp(reg, token, REG_EXTENDED); if(r) { @@ -33,6 +26,23 @@ int mcin_matcher_init(MCINMatcher *out) return r; } +int mcin_matcher_init(MCINMatcher *out) +{ + int r = 0; + r = regcomp(&out->regex_master, ".*\\[([0-9][0-9]:[0-9][0-9]:[0-9][0-9])\\] \\[(.*)\\/(.*)\\]: (.*)", REG_EXTENDED); + if(r) + { + fprintf(stderr, _("Cannot compile master regex: %d.\n"), r); + return r; + } + char str[] = MC_REGEX; + r = mcin_load(str, out->regex); + if(r) return r; + char str1[] = MC_REGEX_WARN; + r = mcin_load(str1, out->regex_warn); + return r; +} + void mcin_matcher_free(MCINMatcher *matcher) { regfree(&matcher->regex_master); @@ -40,6 +50,10 @@ void mcin_matcher_free(MCINMatcher *matcher) { regfree(&matcher->regex[i]); } + for(int i = 0; i < MCIN_REGEX_COUNT_WARN; i ++) + { + regfree(&matcher->regex_warn[i]); + } } char *mcin_matcher_match(MCINMatcher *matcher, const char *str) @@ -47,6 +61,11 @@ char *mcin_matcher_match(MCINMatcher *matcher, const char *str) regmatch_t pmatch[5]; if(regexec(&matcher->regex_master, str, 5, pmatch, 0)) return 0; char *temp_str = calloc(strlen(str) + 1, sizeof(char)); + /* Message level: + * 0 - INFO + * 1 - WARN + */ + int level = 0; for(int i = 1 /* Ignore the string itself */; i < 5; i ++) { const regmatch_t match = pmatch[i]; @@ -73,7 +92,15 @@ char *mcin_matcher_match(MCINMatcher *matcher, const char *str) } if(i == 3) /* Level */ { - if(strcmp(temp_str, "INFO")) + if(!strcmp(temp_str, "INFO")) + { + level = 0; + } + else if(!strcmp(temp_str, "WARN")) + { + level = 1; + } + else { free(temp_str); return NULL; @@ -82,9 +109,22 @@ char *mcin_matcher_match(MCINMatcher *matcher, const char *str) if(i == 4) // Data break; } - for(int i = 0; i < MCIN_REGEX_COUNT; i ++) + int count = 0; + regex_t *arr = NULL; + switch(level) + { + case 0: + count = MCIN_REGEX_COUNT; + arr = matcher->regex; + break; + case 1: + count = MCIN_REGEX_COUNT_WARN; + arr = matcher->regex_warn; + break; + } + for(int i = 0; i < count; i ++) { - if(!regexec(&matcher->regex[i], str, 0, NULL, 0)) + if(!regexec(&arr[i], str, 0, NULL, 0)) { return temp_str; } diff --git a/mcin/mcin.h b/mcin/mcin.h index 5b6f78c..6aa8335 100644 --- a/mcin/mcin.h +++ b/mcin/mcin.h @@ -4,10 +4,12 @@ #include #define MCIN_REGEX_COUNT 51 +#define MCIN_REGEX_COUNT_WARN 1 typedef struct mcin_matcher { regex_t regex_master; regex_t regex[MCIN_REGEX_COUNT]; + regex_t regex_warn[MCIN_REGEX_COUNT_WARN]; } MCINMatcher; int mcin_matcher_init(MCINMatcher *out); -- cgit v1.2.3