Skip to content

Commit

Permalink
Send out log messages via UDP even if global logging queue is suspended
Browse files Browse the repository at this point in the history
  • Loading branch information
tmolitor-stud-tu committed Dec 29, 2024
1 parent c220dc8 commit 8782b09
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
32 changes: 30 additions & 2 deletions Monal/Classes/HelperTools.m
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,38 @@ @implementation DDLog (AllowQueueFreeze)

-(void) swizzled_queueLogMessage:(DDLogMessage*) logMessage asynchronously:(BOOL) asyncFlag
{
//make sure this method remains performant even when checking for udp logging presence
static BOOL udpLoggerEnabled = NO;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
udpLoggerEnabled = [[HelperTools defaultsDB] boolForKey:@"udpLoggerEnabled"];
});

//don't do sync logging for any message (usually ERROR), while the global logging queue is suspended
@synchronized(_suspensionHandling_lock) {
return [self swizzled_queueLogMessage:logMessage asynchronously:_suspensionHandling_isSuspended ? YES : asyncFlag];
//don't use _suspensionHandling_lock here because that can introduce deadlocks
//(for example if we have log statements in our MLLogFileManager code rotating the logfile and creating a new one)
BOOL isSuspended = _suspensionHandling_isSuspended;
if(isSuspended && udpLoggerEnabled)
{
//use udp logger to log all messages, even if the loggging queue is in suspended state
//this hopefully enables us to catch strange bugs sometimes hanging and then watchdog-killing the app when resuming from resumption
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[MLUDPLogger directlyWriteLogMessage:[[DDLogMessage alloc]
initWithFormat:logMessage.messageFormat
formatted:[NSString stringWithFormat:@"LOG_QUEUE_DISABLED: %@", logMessage.message]
level:logMessage.level
flag:logMessage.flag
context:logMessage.context
file:logMessage.file
function:logMessage.function
line:logMessage.line
tag:logMessage.tag
options:logMessage.options
timestamp:logMessage.timestamp]];
#pragma clang diagnostic pop
}
return [self swizzled_queueLogMessage:logMessage asynchronously:isSuspended ? YES : asyncFlag];
}

//see https://stackoverflow.com/a/13326633 and https://fek.io/blog/method-swizzling-in-obj-c-and-swift/
Expand Down
3 changes: 3 additions & 0 deletions Monal/Classes/MLUDPLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@

NS_ASSUME_NONNULL_BEGIN

FOUNDATION_EXPORT DDLoggerName const DDLoggerNameUDP NS_SWIFT_NAME(DDLoggerName.udp); // MLUDPLogger

@interface MLUDPLogger : DDAbstractLogger <DDLogger>

+(void) flushWithTimeout:(double) timeout;
+(void) directlyWriteLogMessage:(DDLogMessage*) logMessage;

@end

Expand Down
20 changes: 19 additions & 1 deletion Monal/Classes/MLUDPLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#import "MLContact.h"
#import "xmpp.h"

DDLoggerName const DDLoggerNameUDP = @"monal.loggger.udp.mainQueue";

static NSData* _key;
static volatile MLUDPLogger* _self;

Expand Down Expand Up @@ -78,6 +80,16 @@ +(void) flushWithTimeout:(double) timeout
}
}

+(void) directlyWriteLogMessage:(DDLogMessage*) logMessage
{
if(_self == nil)
{
[[self class] logError:@"Ignoring call to directlySyncWriteLogMessage: _self still nil!"];
return;
}
return [_self logMessage:logMessage];
}

-(void) dealloc
{
_self = nil;
Expand All @@ -87,14 +99,20 @@ -(void) didAddLogger
{
_self = self;
_send_condition = [NSCondition new];
_send_queue = dispatch_queue_create("MLUDPLoggerSendQueue", dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INTERACTIVE, 0));
_send_queue = dispatch_queue_create("MLUDPLoggerInternalSendQueue", dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INTERACTIVE, 0));
}

-(void) willRemoveLogger
{
_self = nil;
}


-(DDLoggerName) loggerName
{
return DDLoggerNameUDP;
}

+(void) logError:(NSString*) format, ... NS_FORMAT_FUNCTION(1, 2)
{
#ifdef IS_ALPHA
Expand Down

0 comments on commit 8782b09

Please sign in to comment.