Skip to content

Commit 87d95ed

Browse files
committed
Changing the name and default status of the feature flag, only redirect if remote logging is enabled
1 parent 7845059 commit 87d95ed

File tree

9 files changed

+33
-19
lines changed

9 files changed

+33
-19
lines changed

Agent/FeatureFlags/NRMAFlags.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555

5656
+ (BOOL) shouldEnableBackgroundReporting;
5757

58-
+ (BOOL) shouldEnableRedirectStdOut;
58+
+ (BOOL) shouldEnableAutoCollectLogs;
5959

6060
+ (NSArray<NSString*>*) namesForFlags:(NRMAFeatureFlags)flags;
6161

Agent/FeatureFlags/NRMAFlags.m

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ + (NRMAFeatureFlags) featureFlags
4747
NRFeatureFlag_NetworkRequestEvents |
4848
NRFeatureFlag_RequestErrorEvents |
4949
NRFeatureFlag_DistributedTracing |
50-
NRFeatureFlag_AppStartMetrics;
50+
NRFeatureFlag_AppStartMetrics |
51+
NRFeatureFlag_AutoCollectLogs;
5152
});
5253
return __flags;
5354
}
@@ -177,8 +178,8 @@ + (BOOL) shouldEnableBackgroundReporting {
177178
return ([NRMAFlags featureFlags] & NRFeatureFlag_BackgroundReporting) != 0;
178179
}
179180

180-
+ (BOOL) shouldEnableRedirectStdOut {
181-
return ([NRMAFlags featureFlags] & NRFeatureFlag_RedirectStdOutStdErr) != 0;
181+
+ (BOOL) shouldEnableAutoCollectLogs {
182+
return ([NRMAFlags featureFlags] & NRFeatureFlag_AutoCollectLogs) != 0;
182183
}
183184

184185
+ (NSArray<NSString*>*) namesForFlags:(NRMAFeatureFlags)flags {
@@ -243,8 +244,8 @@ + (BOOL) shouldEnableRedirectStdOut {
243244
if ((flags & NRFeatureFlag_BackgroundReporting) == NRFeatureFlag_BackgroundReporting) {
244245
[retArray addObject:@"BackgroundReporting"];
245246
}
246-
if ((flags & NRFeatureFlag_RedirectStdOutStdErr) == NRFeatureFlag_RedirectStdOutStdErr) {
247-
[retArray addObject:@"RedirectStdOut"];
247+
if ((flags & NRFeatureFlag_AutoCollectLogs) == NRFeatureFlag_AutoCollectLogs) {
248+
[retArray addObject:@"AutoCollectLogs"];
248249
}
249250

250251
return retArray;

Agent/General/NewRelicAgentInternal.m

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,6 @@ - (id) initWithCollectorAddress:(NSString*)collectorHost
160160

161161
self = [super init];
162162
if (self) {
163-
164-
if ([NRMAFlags shouldEnableRedirectStdOut]) {
165-
[NRAutoLogCollector redirectStandardOutputAndError];
166-
}
167163

168164
// NOTE: BackgroundReporting is only enabled for iOS 13+.
169165
#if !TARGET_OS_WATCH

Agent/Harvester/NRMAHarvester.mm

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#import "NRMAFlags.h"
2323
#import "Constants.h"
2424
#import "NewRelicAgentInternal.h"
25+
#import "NRAutoLogCollector.h"
2526

2627
#define kNRSupportabilityResponseCode kNRSupportabilityPrefix @"/Collector/ResponseStatusCodes"
2728

@@ -772,9 +773,14 @@ - (void) handleLoggingConfigurationUpdate {
772773
// This if/else chain should only be entered if log_reporting was found in the config
773774
if (configuration.has_log_reporting_config) {
774775
if (configuration.log_reporting_enabled) {
775-
776-
// it is required to enable NRLogTargetFile when using LogReporting.
777-
[NRLogger setLogTargets:NRLogTargetConsole | NRLogTargetFile];
776+
if ([NRMAFlags shouldEnableAutoCollectLogs]) {
777+
[NRAutoLogCollector redirectStandardOutputAndError];
778+
// it is required to enable NRLogTargetFile when using LogReporting.
779+
[NRLogger setLogTargets:NRLogTargetFile];
780+
} else {
781+
[NRLogger setLogTargets:NRLogTargetConsole | NRLogTargetFile];
782+
}
783+
778784
// Parse NSString into NRLogLevel
779785
NRLogLevels level = [NRLogger stringToLevel: configuration.log_reporting_level];
780786
[NRLogger setRemoteLogLevel:level];
@@ -786,6 +792,9 @@ - (void) handleLoggingConfigurationUpdate {
786792
// OVERWRITE user selected value for LogReporting.
787793
else {
788794
NRLOG_AGENT_DEBUG(@"config: Has log reporting DISABLED");
795+
if ([NRMAFlags shouldEnableAutoCollectLogs]) {
796+
[NRAutoLogCollector restoreStandardOutputAndError];
797+
}
789798
[NRLogger setLogTargets:NRLogTargetConsole];
790799

791800
[NRMAFlags disableFeatures:NRFeatureFlag_LogReporting];

Agent/Public/NewRelicFeatureFlags.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,6 @@ typedef NS_OPTIONS(unsigned long long, NRMAFeatureFlags){
102102
NRFeatureFlag_NewEventSystem = 1 << 20, // Disabled by default
103103
NRFeatureFlag_OfflineStorage = 1 << 21, // Disabled by default
104104
NRFeatureFlag_BackgroundReporting = 1 << 22, // Disabled by default
105-
NRFeatureFlag_RedirectStdOutStdErr = 1 << 23, // Disabled by default
105+
NRFeatureFlag_AutoCollectLogs = 1 << 23,
106106

107107
};

Agent/Utilities/NRAutoLogCollector.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
int saved_stderr;
1414
int stdoutPipe[2];
1515
int stderrPipe[2];
16+
static BOOL hasRedirectedStdOut = false;
1617

1718
@interface NRAutoLogCollector()
1819

@@ -21,6 +22,9 @@ @interface NRAutoLogCollector()
2122
@implementation NRAutoLogCollector
2223

2324
+ (BOOL) redirectStandardOutputAndError {
25+
if (hasRedirectedStdOut){
26+
return true;
27+
}
2428
// Create pipes for stdout and stderr
2529
if (pipe(stdoutPipe) == -1 || pipe(stderrPipe) == -1) {
2630
return false;
@@ -63,6 +67,8 @@ + (BOOL) redirectStandardOutputAndError {
6367
[NRAutoLogCollector restoreStandardOutputAndError];
6468
});
6569

70+
hasRedirectedStdOut = true;
71+
6672
return true;
6773
}
6874

@@ -85,10 +91,15 @@ + (void) readAndLog:(int) fd {
8591
}
8692

8793
+ (void) restoreStandardOutputAndError {
94+
if (!hasRedirectedStdOut){
95+
return;
96+
}
8897
dup2(saved_stdout, fileno(stdout));
8998
dup2(saved_stderr, fileno(stderr));
9099
close(saved_stdout);
91100
close(saved_stderr);
101+
102+
hasRedirectedStdOut = false;
92103
}
93104

94105
+ (BOOL) isValidTimestamp:(NSString *) timestampString {

Agent/Utilities/NRLogger.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ - (void)dealloc {
277277
- (void)addLogMessage:(NSDictionary *)message : (BOOL) agentLogsOn {
278278
// The static method checks the log level before we get here.
279279
dispatch_async(logQueue, ^{
280-
if ((self->logTargets & NRLogTargetConsole) && (![NRMAFlags shouldEnableRedirectStdOut])) {
280+
if ((self->logTargets & NRLogTargetConsole) && (![NRMAFlags shouldEnableAutoCollectLogs])) {
281281
NSLog(@"NewRelic(%@,%p):\t%@:%@\t%@\n\t%@",
282282
[NewRelicInternalUtils agentVersion],
283283
[NSThread currentThread],

Test Harness/NRTestApp/NRTestApp/AppDelegate.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1717
#if DEBUG
1818
// The New Relic agent is set to log at NRLogLevelInfo by default, debug logging should only be used for debugging when all agent logs are desired.
1919
NRLogger.setLogLevels(NRLogLevelDebug.rawValue)
20+
NewRelic.disableFeatures([NRMAFeatureFlags.NRFeatureFlag_AutoCollectLogs])
2021
#endif
2122

2223
// To enable or disable feature flags in New Relic iOS Agent.
@@ -34,8 +35,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
3435
// Note: Disabled by default. Enable or disable (default) flag to enable background reporting.
3536
// NewRelic.enableFeatures([NRMAFeatureFlags.NRFeatureFlag_BackgroundReporting])
3637

37-
// Note: Disabled by default. Enable or disable (default) flag to enable auto collect of stdout.
38-
//NewRelic.enableFeatures([NRMAFeatureFlags.NRFeatureFlag_RedirectStdOutStdErr])
3938
NewRelic.saltDeviceUUID(true)
4039

4140
// NewRelic.replaceDeviceIdentifier("myDeviceId")

Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRLoggerTests.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,6 @@ - (void) testRemoteLogLevels {
255255
XCTAssertEqual(foundCount, 3, @"Three remote messages should be found.");
256256
}
257257

258-
#if !TARGET_OS_WATCH
259258
- (void) testAutoCollectedLogs {
260259
[NRMAFlags enableFeatures: NRFeatureFlag_RedirectStdOutStdErr];
261260
// Set the remote log level to debug.
@@ -318,5 +317,4 @@ - (void) testAutoCollectedLogs {
318317
XCTAssertEqual(foundCount, 5, @"Five remote messages should be found.");
319318
[NRMAFlags disableFeatures: NRFeatureFlag_RedirectStdOutStdErr];
320319
}
321-
#endif
322320
@end

0 commit comments

Comments
 (0)