@@ -301,7 +301,7 @@ class InfoLogger::Impl
301301
302302 // message flood prevention
303303 // constants
304- const bool flood_protection = 1 ; // if set, flood protection mechanism enabled
304+ const bool flood_protection = 1 ; // if set, flood protection mechanism enabled
305305 const unsigned int flood_maxmsg_sec=500 ; // maximum messages in one second to trigger flood
306306 const unsigned int flood_maxmsg_min=1000 ; // maximum messages in one minute to trigger flood
307307 const unsigned int flood_maxmsg_file=1000 ; // maximum number of messages in overflow log file
@@ -477,81 +477,81 @@ int InfoLogger::Impl::pushMessage(const InfoLoggerMessageOption& options, const
477477 }
478478
479479 if (flood_protection) {
480-
481- // update message statistics */
482- if (now-floodStat_time_lastsec>1 ) {
483- floodStat_time_lastsec=now;
484- floodStat_msgs_sec=1 ;
485- } else {
486- floodStat_msgs_sec++;
487- }
488- if (now-floodStat_time_lastmin>60 ) {
489- floodStat_time_lastmin=now;
490- floodStat_msgs_lastmin=floodStat_msgs_min;
491- floodStat_msgs_min=1 ;
492- } else {
493- floodStat_msgs_min++;
494- }
495-
496- // message flood prevention
497- if (!noFlood) {
498- switch (floodMode) {
499- case 0 :
500- if ((floodStat_msgs_sec>flood_maxmsg_sec)||(floodStat_msgs_min>flood_maxmsg_min)) {
501- floodFile_msg=0 ;
502- floodFile_msg_drop=0 ;
503- std::string floodFile_path = floodFile_dir + " /infoLogger.flood-" + std::to_string (context.processId ) + " @" + context.hostName + " -" + std::to_string ((int )now);
504- floodFile_fp=fopen (floodFile_path.c_str ()," w" );
505- if (floodFile_fp==NULL ) {
506- floodMode=2 ;
507- std::string msg = " Message flood detected - further messages will be dropped (failed to create flood file " + floodFile_path + " )" ;
508- pushMessage (LogWarningSupport_ (1101 ), currentContext, msg.c_str (),1 );
509- goto case2;
480+
481+ // update message statistics */
482+ if (now - floodStat_time_lastsec > 1 ) {
483+ floodStat_time_lastsec = now;
484+ floodStat_msgs_sec = 1 ;
485+ } else {
486+ floodStat_msgs_sec++;
487+ }
488+ if (now - floodStat_time_lastmin > 60 ) {
489+ floodStat_time_lastmin = now;
490+ floodStat_msgs_lastmin = floodStat_msgs_min;
491+ floodStat_msgs_min = 1 ;
492+ } else {
493+ floodStat_msgs_min++;
494+ }
495+
496+ // message flood prevention
497+ if (!noFlood) {
498+ switch (floodMode) {
499+ case 0 :
500+ if ((floodStat_msgs_sec > flood_maxmsg_sec) || (floodStat_msgs_min > flood_maxmsg_min)) {
501+ floodFile_msg = 0 ;
502+ floodFile_msg_drop = 0 ;
503+ std::string floodFile_path = floodFile_dir + " /infoLogger.flood-" + std::to_string (context.processId ) + " @" + context.hostName + " -" + std::to_string ((int )now);
504+ floodFile_fp = fopen (floodFile_path.c_str (), " w" );
505+ if (floodFile_fp == NULL ) {
506+ floodMode = 2 ;
507+ std::string msg = " Message flood detected - further messages will be dropped (failed to create flood file " + floodFile_path + " )" ;
508+ pushMessage (LogWarningSupport_ (1101 ), currentContext, msg.c_str (), 1 );
509+ goto case2;
510+ } else {
511+ floodMode = 1 ;
512+ std::string msg = " Message flood detected - further messages will be stored locally in " + floodFile_path;
513+ pushMessage (LogWarningSupport_ (1101 ), currentContext, msg.c_str (), 1 );
514+ }
515+ } else {
516+ break ;
517+ }
518+ case 1 :
519+ if (floodStat_msgs_lastmin < flood_maxmsg_reset) {
520+ // reset flood mode
521+ break ;
522+ }
523+ if (floodFile_msg >= flood_maxmsg_file) {
524+ if (floodFile_fp != nullptr ) {
525+ fclose (floodFile_fp);
526+ floodFile_fp = nullptr ;
527+ }
528+ pushMessage (LogWarningSupport_ (1101 ), currentContext, " Message flood - maximum entries in local file exceeded, further messages will be dropped" , 1 );
529+ floodMode = 2 ;
510530 } else {
511- floodMode=1 ;
512- std::string msg = " Message flood detected - further messages will be stored locally in " + floodFile_path;
513- pushMessage (LogWarningSupport_ (1101 ), currentContext, msg.c_str (),1 );
531+ // log to flood file
532+ fprintf (floodFile_fp, " %f\t %c\t %s\t %s\n " , now, (char )options.severity , context.facility .c_str (), messageBody);
533+ fflush (floodFile_fp);
534+ floodFile_msg++;
535+ return 0 ;
514536 }
515- } else {
516- break ;
517- }
518- case 1 :
519- if (floodStat_msgs_lastmin<flood_maxmsg_reset) {
520- // reset flood mode
521- break ;
522- }
523- if (floodFile_msg>=flood_maxmsg_file) {
524- if (floodFile_fp!=nullptr ) {
525- fclose (floodFile_fp);
526- floodFile_fp=nullptr ;
537+ case 2 :
538+ case2:
539+ if (floodStat_msgs_lastmin < flood_maxmsg_reset) {
540+ // reset flood mode
541+ break ;
527542 }
528- pushMessage (LogWarningSupport_ (1101 ), currentContext, " Message flood - maximum entries in local file exceeded, further messages will be dropped" ,1 );
529- floodMode=2 ;
530- } else {
531- // log to flood file
532- fprintf (floodFile_fp," %f\t %c\t %s\t %s\n " ,now,(char )options.severity ,context.facility .c_str (),messageBody);
533- fflush (floodFile_fp);
534- floodFile_msg++;
543+ // drop message
544+ floodFile_msg_drop++;
535545 return 0 ;
536- }
537- case 2 :
538- case2:
539- if (floodStat_msgs_lastmin<flood_maxmsg_reset) {
540- // reset flood mode
541- break ;
542- }
543- // drop message
544- floodFile_msg_drop++;
545- return 0 ;
546- }
547- if (floodMode) {
548- std::string msg = " Message flood - resuming normal operation: " + std::to_string (floodFile_msg) + " messages stored locally, " + std::to_string (floodFile_msg_drop) + " messages dropped" ;
549- pushMessage (LogWarningSupport_ (1102 ), currentContext, msg.c_str (),1 );
550- floodReset ();
546+ }
547+ if (floodMode) {
548+ std::string msg = " Message flood - resuming normal operation: " + std::to_string (floodFile_msg) + " messages stored locally, " + std::to_string (floodFile_msg_drop) + " messages dropped" ;
549+ pushMessage (LogWarningSupport_ (1102 ), currentContext, msg.c_str (), 1 );
550+ floodReset ();
551+ }
551552 }
552553 }
553- }
554-
554+
555555 if (client != nullptr ) {
556556 char buffer[LOG_MAX_SIZE];
557557 msgHelper.MessageToText (&msg, buffer, sizeof (buffer), InfoLoggerMessageHelper::Format::Encoded);
0 commit comments