From b998e5a7b74905b30b1137a02e14cd5e6f97fccc Mon Sep 17 00:00:00 2001 From: Param Aggarwal Date: Sun, 13 Sep 2015 11:08:44 -0700 Subject: [PATCH] Use getters and setters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: As per discussion in #2423 - possible fix for crash. (cc: @​javache) Please share feedback regarding the PR, we are going to be using this diff in production to see if it fixes the crashes we are seeing. (fixes #2423) Closes https://github.com/facebook/react-native/pull/2494 Reviewed By: @javache Differential Revision: D2433515 Pulled By: @nicklockwood --- React/Base/RCTLog.m | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/React/Base/RCTLog.m b/React/Base/RCTLog.m index ebbf6a1b8..2e5c2c605 100644 --- a/React/Base/RCTLog.m +++ b/React/Base/RCTLog.m @@ -35,17 +35,16 @@ const char *RCTLogLevels[] = { static RCTLogFunction RCTCurrentLogFunction; static RCTLogLevel RCTCurrentLogThreshold; -__attribute__((constructor)) -static void RCTLogSetup() +RCTLogLevel RCTGetLogThreshold() { - RCTCurrentLogFunction = RCTDefaultLogFunction; - + if (!RCTCurrentLogThreshold) { #if RCT_DEBUG - RCTCurrentLogThreshold = RCTLogLevelInfo - 1; + RCTCurrentLogThreshold = RCTLogLevelInfo - 1; #else - RCTCurrentLogThreshold = RCTLogLevelError; + RCTCurrentLogThreshold = RCTLogLevelError; #endif - + } + return RCTCurrentLogThreshold; } RCTLogFunction RCTDefaultLogFunction = ^( @@ -88,23 +87,26 @@ void RCTSetLogFunction(RCTLogFunction logFunction) RCTLogFunction RCTGetLogFunction() { + if (!RCTCurrentLogFunction) { + RCTCurrentLogFunction = RCTDefaultLogFunction; + } return RCTCurrentLogFunction; } void RCTAddLogFunction(RCTLogFunction logFunction) { - RCTLogFunction existing = RCTCurrentLogFunction; + RCTLogFunction existing = RCTGetLogFunction(); if (existing) { - RCTCurrentLogFunction = ^(RCTLogLevel level, - NSString *fileName, - NSNumber *lineNumber, - NSString *message) { + RCTSetLogFunction(^(RCTLogLevel level, + NSString *fileName, + NSNumber *lineNumber, + NSString *message) { existing(level, fileName, lineNumber, message); logFunction(level, fileName, lineNumber, message); - }; + }); } else { - RCTCurrentLogFunction = logFunction; + RCTSetLogFunction(logFunction); } } @@ -120,7 +122,7 @@ static RCTLogFunction RCTGetLocalLogFunction() if (logFunction) { return logFunction; } - return RCTCurrentLogFunction; + return RCTGetLogFunction(); } void RCTPerformBlockWithLogFunction(void (^block)(void), RCTLogFunction logFunction) @@ -194,7 +196,7 @@ void _RCTLogFormat( { RCTLogFunction logFunction = RCTGetLocalLogFunction(); BOOL log = RCT_DEBUG || (logFunction != nil); - if (log && level >= RCTCurrentLogThreshold) { + if (log && level >= RCTGetLogThreshold()) { // Get message va_list args;