3030#include <stdlib.h>
3131#include "protocol_extension.h"
3232
33- #define NEW_DO_USERLOG 1
34-
3533static EXTENSION_LOG_LEVEL current_log_level = EXTENSION_LOG_WARNING ;
3634SERVER_HANDLE_V1 * sapi ;
3735
@@ -62,14 +60,6 @@ static int current_flength; // current file length
6260static char hname_pid [100 ]; // hostname + pid for prefix
6361static int hname_len ; // length of hname_pid
6462static int prefix_len ; // length of date_time + hname_pid
65- #ifdef NEW_DO_USERLOG
66- #else
67- static time_t prev_time ; // save the previous time
68- static int msg_cnt ; // # of inputted messages in the interval
69- static int drop_cnt ; // # of messages to be dropped in a second
70- static bool drop_mode ; // indicate the DROP mode when over RATE_LIMIT
71- static int samelog_cnt ;
72- #endif
7363static bool reduction_mode ; // duplication reduction or not. user setting or default(=False)
7464static char prevtime_str [200 ];
7565static char prev_log [2048 ];
@@ -117,23 +107,13 @@ static void do_print_dup(char *time_str, int dup_cnt)
117107 fflush (current_fp );
118108}
119109
120- #ifdef NEW_DO_USERLOG
121- #else
122- static void do_process_dup (char * time_str , int dup_cnt )
123- {
124- do_print_dup (time_str , dup_cnt );
125- samelog_cnt = 1 ;
126- }
127- #endif
128-
129110static void do_save_log (char * log_buf , int len )
130111{
131112 memcpy (prev_log , log_buf , len );
132113 prev_log [len ] = 0 ;
133114 prev_len = len ;
134115}
135116
136- #ifdef NEW_DO_USERLOG
137117static void do_print_drop_begin_message (time_t cur_time )
138118{
139119 char * tmpstr = " user_logger begins to drop messages due to rate-limiting\n" ;
@@ -217,97 +197,6 @@ static void do_userlog_ratelimit(char *body_buf, int len)
217197 }
218198 }
219199}
220- #endif
221-
222- #ifdef NEW_DO_USERLOG
223- #else
224- static void do_userlog (char * body_buf , int len )
225- {
226- char prefix_buf [200 ];
227- double time_diff ;
228- time_t cur_time = time (0 );
229- if (loglimit_interval == 0 ) { // 1. No Log Rate Limiting. Process all logs regardless of speed.
230- if (reduction_mode == false) { // 1.1 No Reduction. The simplest case.
231- do_make_prefix (prefix_buf , cur_time );
232- do_print_msg (prefix_buf , prefix_len , body_buf , len );
233- } else { // 1.2 Reduction-mode.
234- if ((len != prev_len ) || strcmp (body_buf , prev_log ) != 0 ) {
235- // Two log messages are different. Print the count and the previous time,
236- // then restart the count.
237- if (samelog_cnt > 1 ) do_process_dup (prevtime_str , samelog_cnt - 1 );
238- do_make_prefix (prevtime_str , cur_time );
239- do_print_msg (prevtime_str , prefix_len , body_buf , len );
240- do_save_log (body_buf , len );
241- } else { // The current log 'body_buf' and the previous log are same
242- samelog_cnt ++ ; // Just increase the count and save the time
243- do_make_prefix (prevtime_str , cur_time );
244- }
245- }
246- } else { // 2. Check Log Rate Limit
247- time_diff = difftime (cur_time , prev_time );
248- do_make_prefix (prefix_buf , cur_time );
249- if (time_diff <= (double )loglimit_interval ) { // check interval and burst rate.
250- if (drop_mode == false) { // not yet reach the limit
251- if (reduction_mode == false) {
252- do_print_msg (prefix_buf , prefix_len , body_buf , len );
253- } else { // The most complex case : Reduction-ON, Log Limit-ON
254- if ((len != prev_len ) || strcmp (body_buf , prev_log ) != 0 ) {
255- if (samelog_cnt > 1 ) do_process_dup (prevtime_str , samelog_cnt - 1 );
256- do_print_msg (prefix_buf , prefix_len , body_buf , len );
257- do_save_log (body_buf , len );
258- } else { // The current log 'body_buf' and the previous log are same
259- samelog_cnt ++ ;
260- }
261- memcpy (prevtime_str , prefix_buf , prefix_len );
262- prevtime_str [prefix_len ] = 0 ;
263- }
264- msg_cnt ++ ;
265- if (msg_cnt >= loglimit_burst ) { // if duplicates remains, print that.
266- if (samelog_cnt > 1 ) do_process_dup (prevtime_str , samelog_cnt - 1 );
267- char * tmpstr = " user_logger begins to drop messages due to rate-limiting\n" ;
268- do_print_msg (prefix_buf , prefix_len , tmpstr , 60 );
269- drop_mode = true;
270- drop_cnt = 0 ;
271- }
272- } else { // drop_mode = true
273- drop_cnt ++ ; // Increase the dropped log, regardless of Reduction-mode.
274- }
275- } else { // 'loglimit_interval' seconds passed.
276- if (drop_mode == true) { // Start a new interval regardless of Reduction-mode.
277- if (drop_cnt > 0 ) {
278- fprintf (current_fp , "%s user_logger lost %d messages due to rate-limiting (%d)\n" ,
279- prefix_buf , drop_cnt , loglimit_burst );
280- fflush (current_fp );
281- current_flength += prefix_len + 55 ;
282- drop_cnt = 0 ;
283- } // We have to consider the special case that drop_mode=true && drop_cnt=0
284- do_print_msg (prefix_buf , prefix_len , body_buf , len );
285- drop_mode = false;
286- if (reduction_mode == true) {
287- samelog_cnt = 1 ;
288- do_save_log (body_buf , len );
289- }
290- } else { // Normal status.
291- if (reduction_mode == false)
292- do_print_msg (prefix_buf , prefix_len , body_buf , len );
293- else {
294- if ((len != prev_len ) || strcmp (body_buf , prev_log ) != 0 ) {
295- if (samelog_cnt > 1 ) do_process_dup (prevtime_str , samelog_cnt - 1 );
296- do_print_msg (prefix_buf , prefix_len , body_buf , len );
297- do_save_log (body_buf , len );
298- } else { // The current log 'body_buf' and the previous log are same
299- samelog_cnt ++ ;
300- }
301- }
302- }
303- msg_cnt = 1 ;
304- prev_time = cur_time ;
305- memcpy (prevtime_str , prefix_buf , prefix_len );
306- prevtime_str [prefix_len ] = 0 ;
307- }
308- }
309- }
310- #endif
311200
312201static void logger_log (EXTENSION_LOG_LEVEL severity ,
313202 const void * client_cookie ,
@@ -339,15 +228,11 @@ static void logger_log(EXTENSION_LOG_LEVEL severity,
339228 }
340229 }
341230
342- #ifdef NEW_DO_USERLOG
343231 if (loglimit_interval == 0 ) {
344232 do_userlog_basic (log_buf , len );
345233 } else {
346234 do_userlog_ratelimit (log_buf , len );
347235 }
348- #else
349- do_userlog (log_buf , len ); // userlog codes
350- #endif
351236
352237 if (current_flength >= MAX_LOGFILE_SIZE ) {
353238 fclose (current_fp );
@@ -425,13 +310,6 @@ EXTENSION_ERROR_CODE memcached_extensions_initialize(const char *config,
425310 }
426311 }
427312 if (loglimit_interval != 0 ) {
428- #ifdef NEW_DO_USERLOG
429- #else
430- prev_time = time (0 );
431- msg_cnt = 0 ;
432- drop_cnt = 0 ;
433- drop_mode = false;
434- #endif
435313 loglimit_burst = DEFAULT_RATELIMIT ;
436314 env = getenv ("UserLogRateLimitBurst" );
437315 if (env != NULL ) {
@@ -445,10 +323,6 @@ EXTENSION_ERROR_CODE memcached_extensions_initialize(const char *config,
445323 if (env != NULL && strcmp (env , "on" ) == 0 )
446324 reduction_mode = true;
447325 if (reduction_mode == true) { // default is false
448- #ifdef NEW_DO_USERLOG
449- #else
450- samelog_cnt = 1 ;
451- #endif
452326 prev_log [0 ] = 0 ;
453327 prev_len = 0 ;
454328 prevtime_str [0 ] = 0 ;
0 commit comments