diff options
| author | Giovanni | 2020-07-22 09:28:42 +0200 |
|---|---|---|
| committer | GitHub | 2020-07-22 09:28:42 +0200 |
| commit | e8d3cd9b26f0b4d660e424f2657f11bbc01eb171 (patch) | |
| tree | fa2b312815be8b50afae7fd0447fe891ab19a487 /src/sp_utils.c | |
| parent | 5a655dda1c3b666adf552fd50f5ebf5f4cbd3ce7 (diff) | |
refactoring sp_log_* (#340)
Co-authored-by: Giovanni Dante Grazioli <giovanni.dantegrazioli@nbs-system.com>
Diffstat (limited to 'src/sp_utils.c')
| -rw-r--r-- | src/sp_utils.c | 30 |
1 files changed, 14 insertions, 16 deletions
diff --git a/src/sp_utils.c b/src/sp_utils.c index 146fe77..8032e0a 100644 --- a/src/sp_utils.c +++ b/src/sp_utils.c | |||
| @@ -40,7 +40,7 @@ const char* get_ipaddr() { | |||
| 40 | return default_ipaddr; | 40 | return default_ipaddr; |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | void sp_log_msg(char const* restrict feature, int type, | 43 | void sp_log_msgf(char const* restrict feature, int level, int type, |
| 44 | const char* restrict fmt, ...) { | 44 | const char* restrict fmt, ...) { |
| 45 | char* msg; | 45 | char* msg; |
| 46 | va_list args; | 46 | va_list args; |
| @@ -51,16 +51,14 @@ void sp_log_msg(char const* restrict feature, int type, | |||
| 51 | 51 | ||
| 52 | const char* client_ip = get_ipaddr(); | 52 | const char* client_ip = get_ipaddr(); |
| 53 | const char* logtype = NULL; | 53 | const char* logtype = NULL; |
| 54 | int bailout = type == SP_LOG_DROP; | ||
| 55 | switch(type) { | 54 | switch(type) { |
| 56 | case SP_LOG_SIMULATION: | 55 | case SP_TYPE_SIMULATION: |
| 57 | logtype = "simulation"; | 56 | logtype = "simulation"; |
| 58 | type = E_WARNING; | ||
| 59 | break; | 57 | break; |
| 60 | case SP_LOG_DROP: | 58 | case SP_TYPE_DROP: |
| 61 | logtype = "drop"; | 59 | logtype = "drop"; |
| 62 | type = E_ERROR; | ||
| 63 | break; | 60 | break; |
| 61 | case SP_TYPE_LOG: | ||
| 64 | default: | 62 | default: |
| 65 | logtype = "log"; | 63 | logtype = "log"; |
| 66 | break; | 64 | break; |
| @@ -69,20 +67,20 @@ void sp_log_msg(char const* restrict feature, int type, | |||
| 69 | switch (SNUFFLEUPAGUS_G(config).log_media) { | 67 | switch (SNUFFLEUPAGUS_G(config).log_media) { |
| 70 | case SP_SYSLOG: { | 68 | case SP_SYSLOG: { |
| 71 | const char* error_filename = zend_get_executed_filename(); | 69 | const char* error_filename = zend_get_executed_filename(); |
| 72 | int syslog_level = (type == E_ERROR) ? LOG_ERR : LOG_INFO; | 70 | int syslog_level = (level == E_ERROR) ? LOG_ERR : LOG_INFO; |
| 73 | int error_lineno = zend_get_executed_lineno(TSRMLS_C); | 71 | int error_lineno = zend_get_executed_lineno(TSRMLS_C); |
| 74 | openlog(PHP_SNUFFLEUPAGUS_EXTNAME, LOG_PID, LOG_AUTH); | 72 | openlog(PHP_SNUFFLEUPAGUS_EXTNAME, LOG_PID, LOG_AUTH); |
| 75 | syslog(syslog_level, "[snuffleupagus][%s][%s][%s] %s in %s on line %d", | 73 | syslog(syslog_level, "[snuffleupagus][%s][%s][%s] %s in %s on line %d", |
| 76 | client_ip, feature, logtype, msg, error_filename, error_lineno); | 74 | client_ip, feature, logtype, msg, error_filename, error_lineno); |
| 77 | closelog(); | 75 | closelog(); |
| 78 | if (bailout) { | 76 | if (type == SP_TYPE_DROP) { |
| 79 | zend_bailout(); | 77 | zend_bailout(); |
| 80 | } | 78 | } |
| 81 | break; | 79 | break; |
| 82 | } | 80 | } |
| 83 | case SP_ZEND: | 81 | case SP_ZEND: |
| 84 | default: | 82 | default: |
| 85 | zend_error(type, "[snuffleupagus][%s][%s][%s] %s", client_ip, feature, logtype, msg); | 83 | zend_error(level, "[snuffleupagus][%s][%s][%s] %s", client_ip, feature, logtype, msg); |
| 86 | break; | 84 | break; |
| 87 | } | 85 | } |
| 88 | } | 86 | } |
| @@ -282,12 +280,12 @@ void sp_log_disable(const char* restrict path, const char* restrict arg_name, | |||
| 282 | char_repr = zend_string_to_char(arg_value); | 280 | char_repr = zend_string_to_char(arg_value); |
| 283 | } | 281 | } |
| 284 | if (alias) { | 282 | if (alias) { |
| 285 | sp_log_msg("disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, | 283 | sp_log_auto("disabled_function", sim, |
| 286 | "Aborted execution on call of the function '%s', " | 284 | "Aborted execution on call of the function '%s', " |
| 287 | "because its argument '%s' content (%s) matched the rule '%s'", | 285 | "because its argument '%s' content (%s) matched the rule '%s'", |
| 288 | path, arg_name, char_repr ? char_repr : "?", ZSTR_VAL(alias)); | 286 | path, arg_name, char_repr ? char_repr : "?", ZSTR_VAL(alias)); |
| 289 | } else { | 287 | } else { |
| 290 | sp_log_msg("disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, | 288 | sp_log_auto("disabled_function", sim, |
| 291 | "Aborted execution on call of the function '%s', " | 289 | "Aborted execution on call of the function '%s', " |
| 292 | "because its argument '%s' content (%s) matched a rule", | 290 | "because its argument '%s' content (%s) matched a rule", |
| 293 | path, arg_name, char_repr ? char_repr : "?"); | 291 | path, arg_name, char_repr ? char_repr : "?"); |
| @@ -295,12 +293,12 @@ void sp_log_disable(const char* restrict path, const char* restrict arg_name, | |||
| 295 | efree(char_repr); | 293 | efree(char_repr); |
| 296 | } else { | 294 | } else { |
| 297 | if (alias) { | 295 | if (alias) { |
| 298 | sp_log_msg("disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, | 296 | sp_log_auto("disabled_function", sim, |
| 299 | "Aborted execution on call of the function '%s', " | 297 | "Aborted execution on call of the function '%s', " |
| 300 | "because of the the rule '%s'", | 298 | "because of the the rule '%s'", |
| 301 | path, ZSTR_VAL(alias)); | 299 | path, ZSTR_VAL(alias)); |
| 302 | } else { | 300 | } else { |
| 303 | sp_log_msg("disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, | 301 | sp_log_auto("disabled_function", sim, |
| 304 | "Aborted execution on call of the function '%s'", path); | 302 | "Aborted execution on call of the function '%s'", path); |
| 305 | } | 303 | } |
| 306 | } | 304 | } |
| @@ -322,13 +320,13 @@ void sp_log_disable_ret(const char* restrict path, | |||
| 322 | char_repr = zend_string_to_char(ret_value); | 320 | char_repr = zend_string_to_char(ret_value); |
| 323 | } | 321 | } |
| 324 | if (alias) { | 322 | if (alias) { |
| 325 | sp_log_msg( | 323 | sp_log_auto( |
| 326 | "disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, | 324 | "disabled_function", sim, |
| 327 | "Aborted execution on return of the function '%s', " | 325 | "Aborted execution on return of the function '%s', " |
| 328 | "because the function returned '%s', which matched the rule '%s'", | 326 | "because the function returned '%s', which matched the rule '%s'", |
| 329 | path, char_repr ? char_repr : "?", ZSTR_VAL(alias)); | 327 | path, char_repr ? char_repr : "?", ZSTR_VAL(alias)); |
| 330 | } else { | 328 | } else { |
| 331 | sp_log_msg("disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, | 329 | sp_log_auto("disabled_function", sim, |
| 332 | "Aborted execution on return of the function '%s', " | 330 | "Aborted execution on return of the function '%s', " |
| 333 | "because the function returned '%s', which matched a rule", | 331 | "because the function returned '%s', which matched a rule", |
| 334 | path, char_repr ? char_repr : "?"); | 332 | path, char_repr ? char_repr : "?"); |
