@@ -47,16 +47,19 @@ int ItemIncluded(const char *Line, double secs)
47
47
48
48
49
49
50
- #define ITEM_NORMAL 0
51
- #define ITEM_START -1
52
- #define ITEM_END -2
50
+ #define ITEM_NORMAL 0
51
+ #define ITEM_START -1
52
+ #define ITEM_END -2
53
+ #define ITEM_EXCLUDE -3
53
54
54
- int ProcessTimedItem (struct timeval * When , double Secs , const char * Line )
55
+ int ProcessTimedItem (struct timeval * When , double Secs , const char * Line , char * * Output )
55
56
{
56
57
const char * prefix = "" , * postfix = "" ;
57
58
char * Tempstr = NULL ;
58
59
int RetVal = ITEM_NORMAL ;
59
60
61
+ //if a start is declared, and we have not encountered a start-line, or
62
+ //are between a start-line and an end-line, then ignore this line
60
63
if (Started )
61
64
{
62
65
if ((Settings .WarnTime > -1 ) && (Secs > Settings .WarnTime ))
@@ -71,8 +74,8 @@ int ProcessTimedItem(struct timeval *When, double Secs, const char *Line)
71
74
postfix = Settings .NormPostfix ;
72
75
}
73
76
74
- if (Settings .Flags & FLAG_NANOSECS ) printf ( "%s%14.6f%s %s\n" , prefix , Secs , postfix , Line );
75
- else printf ( "%s%10.3f%s %s\n" , prefix , Secs , postfix , Line );
77
+ if (Settings .Flags & FLAG_NANOSECS ) * Output = FormatStr ( * Output , "%s%14.6f%s %s\n" , prefix , Secs , postfix , Line );
78
+ else * Output = FormatStr ( * Output , "%s%10.3f%s %s\n" , prefix , Secs , postfix , Line );
76
79
77
80
StatsInsert ("default" , When , Secs );
78
81
@@ -92,28 +95,33 @@ int ProcessTimedItem(struct timeval *When, double Secs, const char *Line)
92
95
93
96
94
97
95
- double ProcessLine (struct timeval * Prev , struct timeval * Curr , const char * Line )
98
+ double ProcessLine (struct timeval * Prev , struct timeval * Curr , const char * Line , char * * Output )
96
99
{
97
100
double secs = 0 ;
98
101
int result ;
99
102
100
103
secs = CalcTimediff (Prev , Curr );
104
+
105
+ //if it's a 'Start' line, then we won't be calculating a difference between it
106
+ //and the previous line. Also, if a start is declared, then we will ignore lines
107
+ //before a start, or between a start and an end if an end is declared.
101
108
if (InPatternList (Line , Settings .StartStrings , NULL ))
102
109
{
103
110
//if we are in 'warn only' mode, where we only display items that have a time warning
104
111
//then we want a 'start string' that matches the start of an interaction to be registered as the start time
105
112
//but we don't want it displayed
106
- if (! (Settings .Flags & FLAG_WARN_ONLY )) printf ( "%s%10s%s %s\n" , Settings .NormPrefix , " -- " , Settings .NormPostfix , Line );
113
+ if (! (Settings .Flags & FLAG_WARN_ONLY )) * Output = FormatStr ( * Output , "%s%10s%s %s\n" , Settings .NormPrefix , " -- " , Settings .NormPostfix , Line );
107
114
Started = TRUE;
108
115
secs = ITEM_START ;
109
116
}
110
117
else
111
118
{
112
119
if (ItemIncluded (Line , secs ))
113
120
{
114
- result = ProcessTimedItem (Curr , secs , Line );
121
+ result = ProcessTimedItem (Curr , secs , Line , Output );
115
122
if (result != ITEM_NORMAL ) secs = (double ) result ;
116
123
}
124
+ else secs = ITEM_EXCLUDE ;
117
125
}
118
126
119
127
return (secs );
@@ -160,11 +168,52 @@ char *TimediffReadLine(char *RetStr, STREAM *S)
160
168
}
161
169
162
170
171
+ char * HandleStartEndBlock (char * RetStr , double secs , char * Line , struct timeval * PrevTime , struct timeval * CurrTime )
172
+ {
173
+ //if we aren't using start or end strings, then we will just output the current line
174
+ //and PrevTime becomes the CurrTime. We don't use the OutputBuffer provided by RetStr
175
+ if ( (! StrValid (Settings .StartStrings )) && (! StrValid (Settings .EndStrings )) )
176
+ {
177
+ memcpy (PrevTime , CurrTime , sizeof (struct timeval ));
178
+ printf ("%s\n" , Line );
179
+ return (CopyStr (RetStr , "" ));
180
+ }
181
+
182
+ if (secs == ITEM_START )
183
+ {
184
+ // on a start we always start a new output block,
185
+ // and set PrevTime to CurrTime
186
+ memcpy (PrevTime , CurrTime , sizeof (struct timeval ));
187
+ RetStr = CopyStr (RetStr , Line );
188
+ }
189
+ else if (secs == ITEM_END )
190
+ {
191
+ //on an end we output the always buffered block of lines
192
+ RetStr = CatStr (RetStr , Line );
193
+ printf ("%s\n" , RetStr );
194
+ RetStr = CopyStr (RetStr , "" );
195
+ }
196
+ else if (secs != ITEM_EXCLUDE )
197
+ {
198
+ RetStr = CatStr (RetStr , Line );
199
+
200
+ //if we are measuring time from a start line, then we don't update PrevTime on every line
201
+ if (! (Settings .Flags & FLAG_FROM_START )) memcpy (PrevTime , CurrTime , sizeof (struct timeval ));
202
+ if (! StrValid (Settings .EndStrings ))
203
+ {
204
+ printf ("%s\n" , RetStr );
205
+ RetStr = CopyStr (RetStr , "" );
206
+ }
207
+ }
208
+
209
+ return (RetStr );
210
+ }
211
+
163
212
164
213
int main (int argc , const char * argv [])
165
214
{
166
215
STREAM * S ;
167
- char * Tempstr = NULL ;
216
+ char * Tempstr = NULL , * InputLine = NULL , * Output = NULL ;
168
217
struct timeval Prev , Curr ;
169
218
double secs ;
170
219
@@ -178,21 +227,17 @@ int main(int argc, const char *argv[])
178
227
S = STREAMOpen (Settings .Input , "r" );
179
228
if (S )
180
229
{
181
- Tempstr = TimediffReadLine (Tempstr , S );
182
- while (Tempstr )
230
+ InputLine = TimediffReadLine (InputLine , S );
231
+ while (InputLine )
183
232
{
184
- if (StrValid (Tempstr ))
233
+ if (StrValid (InputLine ))
185
234
{
186
- ParseDateTime (Tempstr , & Curr );
187
- secs = ProcessLine (& Prev , & Curr , Tempstr );
188
-
189
- if (Settings .Flags & FLAG_FROM_START )
190
- {
191
- if (secs == ITEM_START ) memcpy (& Prev , & Curr , sizeof (struct timeval ));
192
- }
193
- else memcpy (& Prev , & Curr , sizeof (struct timeval ));
235
+ ParseDateTime (InputLine , & Curr );
236
+ secs = ProcessLine (& Prev , & Curr , InputLine , & Tempstr );
237
+ //actual printing of output happens in HandleStartEndBlock
238
+ Output = HandleStartEndBlock (Output , secs , Tempstr , & Prev , & Curr );
194
239
}
195
- Tempstr = TimediffReadLine (Tempstr , S );
240
+ InputLine = TimediffReadLine (InputLine , S );
196
241
}
197
242
//Display stats before closing stream,
198
243
//because the stream might be stdin/stdout
@@ -203,5 +248,9 @@ int main(int argc, const char *argv[])
203
248
STREAMClose (S );
204
249
}
205
250
251
+ Destroy (InputLine );
252
+ Destroy (Tempstr );
253
+ Destroy (Output );
254
+
206
255
return (0 );
207
256
}
0 commit comments