11
11
12
12
int saved_stdout;
13
13
int saved_stderr;
14
+ FILE* fileDescriptor;
14
15
15
16
@interface NRAutoLogCollector ()
16
17
@@ -38,7 +39,9 @@ + (void) redirectStandardOutputAndError {
38
39
39
40
// Redirect stdout to the file
40
41
freopen ([[NRAutoLogCollector logFileURL ].path cStringUsingEncoding: NSUTF8StringEncoding], " a+" , stdout);
41
- freopen ([[NRAutoLogCollector logFileURL ].path cStringUsingEncoding: NSUTF8StringEncoding], " a+" , stderr);
42
+ fileDescriptor = freopen ([[NRAutoLogCollector logFileURL ].path cStringUsingEncoding: NSUTF8StringEncoding], " a+" , stderr);
43
+
44
+ [NRAutoLogCollector monitorFile: [NRAutoLogCollector logFileURL ].path];
42
45
}
43
46
44
47
+ (void ) restoreStandardOutputAndError {
@@ -91,10 +94,9 @@ + (BOOL) isValidTimestamp:(NSString *) timestampString {
91
94
92
95
+ (NSNumber *) extractTimestamp : (NSString *) inputString {
93
96
// Define the regular expression pattern to match the t: value
94
- NSString *pattern = @" t:(\\ d+\\ .\\ d+)" ;
97
+ NSString *pattern = @" t:(\\ d+( \\ .\\ d+)? )" ;
95
98
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern: pattern options: 0 error: nil ];
96
99
97
- // Find matches in the input string
98
100
NSTextCheckingResult *match = [regex firstMatchInString: inputString options: 0 range: NSMakeRange (0 , [inputString length ])];
99
101
100
102
if (match) {
@@ -107,9 +109,14 @@ + (NSNumber *) extractTimestamp:(NSString *) inputString {
107
109
NSNumberFormatter *formatter = [[NSNumberFormatter alloc ] init ];
108
110
formatter.numberStyle = NSNumberFormatterDecimalStyle;
109
111
NSNumber * originalTimestamp = [formatter numberFromString: timestampString];
110
- double timestampInSeconds = [originalTimestamp doubleValue ];
111
- long long timestampInMilliseconds = (long long )(timestampInSeconds * 1000 );
112
- return [NSNumber numberWithLongLong: timestampInMilliseconds];
112
+ // If the timestamp has a decimal it is in second format, convert it to milliseconds.
113
+ if ([timestampString containsString: @" ." ]){
114
+ double timestampInSeconds = [originalTimestamp doubleValue ];
115
+ long long timestampInMilliseconds = (long long )(timestampInSeconds * 1000 );
116
+ return [NSNumber numberWithLongLong: timestampInMilliseconds];
117
+ } else {
118
+ return originalTimestamp;
119
+ }
113
120
}
114
121
}
115
122
@@ -142,5 +149,28 @@ + (unsigned int) extractType:(NSString *) inputString {
142
149
return NRLogLevelNone;
143
150
}
144
151
152
+ + (void ) monitorFile : (NSString *) filePath {
153
+ // Create a dispatch queue for handling log file events
154
+ dispatch_queue_t queue = dispatch_queue_create (" newrelic.log.monitor.queue" , NULL );
155
+
156
+ // Create a dispatch source to monitor the file descriptor for writes
157
+ dispatch_source_t source = dispatch_source_create (DISPATCH_SOURCE_TYPE_VNODE, fileno (fileDescriptor), DISPATCH_VNODE_WRITE, queue);
158
+
159
+ // Set the event handler block
160
+ dispatch_source_set_event_handler (source, ^{
161
+ unsigned long flags = dispatch_source_get_data (source);
162
+ if (flags & DISPATCH_VNODE_WRITE) {
163
+ [NRAutoLogCollector readAndParseLogFile ];
164
+ }
165
+ });
166
+
167
+ // Set the cancel handler block
168
+ dispatch_source_set_cancel_handler (source, ^{
169
+ close (fileno (fileDescriptor));
170
+ });
171
+
172
+ // Start monitoring
173
+ dispatch_resume (source);
174
+ }
145
175
146
176
@end
0 commit comments