From 15532e2a03b7e4b92c896ce48eb295416a8e5556 Mon Sep 17 00:00:00 2001 From: manupap1 Date: Tue, 11 Nov 2014 22:31:58 +0100 Subject: [PATCH] Fix issues with event notes --- src/zm_config.h.in | 2 ++ src/zm_event.cpp | 28 +++++++++++++++++++--------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/zm_config.h.in b/src/zm_config.h.in index 1df1edf16..e311ebdbc 100644 --- a/src/zm_config.h.in +++ b/src/zm_config.h.in @@ -42,6 +42,8 @@ #define ZM_MAX_IMAGE_DIM (ZM_MAX_IMAGE_WIDTH*ZM_MAX_IMAGE_HEIGHT) #define ZM_MAX_IMAGE_SIZE (ZM_MAX_IMAGE_DIM*ZM_MAX_IMAGE_COLOURS) +#define ZM_NOTES_MAX_SIZE 4096 // The maximum size of a note + #define ZM_SCALE_BASE 100 // The factor by which we bump up 'scale' to simulate FP #define ZM_RATE_BASE 100 // The factor by which we bump up 'rate' to simulate FP diff --git a/src/zm_event.cpp b/src/zm_event.cpp index 158ad3546..a812b6bca 100644 --- a/src/zm_event.cpp +++ b/src/zm_event.cpp @@ -63,6 +63,8 @@ Event::Event( Monitor *p_monitor, struct timeval p_start_time, const std::string std::string notes; createNotes( notes ); + static char escapedNotes[ZM_NOTES_MAX_SIZE + 1]; + mysql_real_escape_string( &dbconn, escapedNotes, notes.c_str(), notes.length() ); bool untimedEvent = false; if ( !start_time.tv_sec ) @@ -74,7 +76,7 @@ Event::Event( Monitor *p_monitor, struct timeval p_start_time, const std::string static char sql[ZM_SQL_MED_BUFSIZ]; struct tm *stime = localtime( &start_time.tv_sec ); - snprintf( sql, sizeof(sql), "insert into Events ( MonitorId, Name, StartTime, Width, Height, Cause, Notes ) values ( %d, 'New Event', from_unixtime( %ld ), %d, %d, '%s', '%s' )", monitor->Id(), start_time.tv_sec, monitor->Width(), monitor->Height(), cause.c_str(), notes.c_str() ); + snprintf( sql, sizeof(sql), "insert into Events ( MonitorId, Name, StartTime, Width, Height, Cause, Notes ) values ( %d, 'New Event', from_unixtime( %ld ), %d, %d, '%s', '%s' )", monitor->Id(), start_time.tv_sec, monitor->Width(), monitor->Height(), cause.c_str(), escapedNotes ); if ( mysql_query( &dbconn, sql ) ) { Error( "Can't insert event: %s", mysql_error( &dbconn ) ); @@ -185,7 +187,7 @@ Event::~Event() struct DeltaTimeval delta_time; DELTA_TIMEVAL( delta_time, end_time, start_time, DT_PREC_2 ); - snprintf( sql, sizeof(sql), "update Events set Name='%s%d', EndTime = from_unixtime( %ld ), Length = %s%ld.%02ld, Frames = %d, AlarmFrames = %d, TotScore = %d, AvgScore = %d, MaxScore = %d where Id = %d", monitor->EventPrefix(), id, end_time.tv_sec, delta_time.positive?"":"-", delta_time.sec, delta_time.fsec, frames, alarm_frames, tot_score, (int)(alarm_frames?(tot_score/alarm_frames):0), max_score, id ); + snprintf( sql, sizeof(sql), "update Events set Name='%s%d', Cause='%s', EndTime = from_unixtime( %ld ), Length = %s%ld.%02ld, Frames = %d, AlarmFrames = %d, TotScore = %d, AvgScore = %d, MaxScore = %d where Id = %d", monitor->EventPrefix(), id, cause.c_str(), end_time.tv_sec, delta_time.positive?"":"-", delta_time.sec, delta_time.fsec, frames, alarm_frames, tot_score, (int)(alarm_frames?(tot_score/alarm_frames):0), max_score, id ); if ( mysql_query( &dbconn, sql ) ) { Error( "Can't update event: %s", mysql_error( &dbconn ) ); @@ -198,16 +200,24 @@ void Event::createNotes( std::string ¬es ) notes.clear(); for ( StringSetMap::const_iterator mapIter = noteSetMap.begin(); mapIter != noteSetMap.end(); mapIter++ ) { - notes += mapIter->first; - notes += ": "; + // No need to display the cause inside the note + //notes += mapIter->first; + //notes += ": "; const StringSet &stringSet = mapIter->second; for ( StringSet::const_iterator setIter = stringSet.begin(); setIter != stringSet.end(); setIter++ ) { - if ( setIter != stringSet.begin() ) - notes += ", "; + // Don't format here + //if ( setIter != stringSet.begin() ) + // notes += ", "; notes += *setIter; } } + if (notes.size() > ZM_NOTES_MAX_SIZE) + { + std::string sTrunc = "... (content truncated)"; + notes = notes.substr(0, (ZM_NOTES_MAX_SIZE - sTrunc.size())); + notes += sTrunc; + } } void Event::AddCause( const std::string new_cause ) @@ -432,11 +442,11 @@ void Event::updateNotes( const StringSetMap &newNoteSetMap ) createNotes( notes ); Debug( 2, "Updating notes for event %d, '%s'", id, notes.c_str() ); - static char sql[ZM_SQL_MED_BUFSIZ]; + static char sql[ZM_SQL_LGE_BUFSIZ]; #if USE_PREPARED_SQL static MYSQL_STMT *stmt = 0; - char notesStr[ZM_SQL_MED_BUFSIZ] = ""; + char notesStr[ZM_NOTES_MAX_SIZE] = ""; unsigned long notesLen = 0; if ( !stmt ) @@ -485,7 +495,7 @@ void Event::updateNotes( const StringSetMap &newNoteSetMap ) Fatal( "Unable to execute sql '%s': %s", sql, mysql_stmt_error(stmt) ); } #else - static char escapedNotes[ZM_SQL_MED_BUFSIZ]; + static char escapedNotes[ZM_NOTES_MAX_SIZE]; mysql_real_escape_string( &dbconn, escapedNotes, notes.c_str(), notes.length() );