Skip to content

Commit

Permalink
Preserve message format in DDLogMessage, add DDLogMessageFormat for S…
Browse files Browse the repository at this point in the history
…wift
  • Loading branch information
ffried committed Jul 11, 2023
1 parent 851ccad commit 2b21ef5
Show file tree
Hide file tree
Showing 11 changed files with 851 additions and 188 deletions.
167 changes: 91 additions & 76 deletions Sources/CocoaLumberjack/DDLog.m
Original file line number Diff line number Diff line change
Expand Up @@ -349,21 +349,16 @@ + (void)log:(BOOL)asynchronous
if (format) {
va_start(args, format);

NSString *message = [[NSString alloc] initWithFormat:format arguments:args];

va_end(args);

va_start(args, format);

[self log:asynchronous
message:message
level:level
flag:flag
context:context
file:file
function:function
line:line
tag:tag];
tag:tag
format:format
args:args];

va_end(args);
}
Expand All @@ -383,21 +378,16 @@ - (void)log:(BOOL)asynchronous
if (format) {
va_start(args, format);

NSString *message = [[NSString alloc] initWithFormat:format arguments:args];

va_end(args);

va_start(args, format);

[self log:asynchronous
message:message
level:level
flag:flag
context:context
file:file
function:function
line:line
tag:tag];
tag:tag
format:format
args:args];

va_end(args);
}
Expand Down Expand Up @@ -427,56 +417,24 @@ - (void)log:(BOOL)asynchronous
format:(NSString *)format
args:(va_list)args {
if (format) {
NSString *message = [[NSString alloc] initWithFormat:format arguments:args];
[self log:asynchronous
message:message
level:level
flag:flag
context:context
file:file
function:function
line:line
tag:tag];
}
}

+ (void)log:(BOOL)asynchronous
message:(NSString *)message
level:(DDLogLevel)level
flag:(DDLogFlag)flag
context:(NSInteger)context
file:(const char *)file
function:(const char *)function
line:(NSUInteger)line
tag:(id)tag {
[self.sharedInstance log:asynchronous message:message level:level flag:flag context:context file:file function:function line:line tag:tag];
}

- (void)log:(BOOL)asynchronous
message:(NSString *)message
level:(DDLogLevel)level
flag:(DDLogFlag)flag
context:(NSInteger)context
file:(const char *)file
function:(const char *)function
line:(NSUInteger)line
tag:(id)tag {
// Nullity checks are handled by -initWithMessage:
// Nullity checks are handled by -initWithMessage:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnullable-to-nonnull-conversion"
DDLogMessage *logMessage = [[DDLogMessage alloc] initWithMessage:message
level:level
flag:flag
context:context
file:@(file)
function:@(function)
line:line
tag:tag
options:(DDLogMessageOptions)0
timestamp:nil];
DDLogMessage *logMessage = [[DDLogMessage alloc] initWithFormat:format
args:args
level:level
flag:flag
context:context
file:@(file)
function:@(function)
line:line
tag:tag
options:(DDLogMessageOptions)0
timestamp:nil];
#pragma clang diagnostic pop

[self queueLogMessage:logMessage asynchronously:asynchronous];
[self queueLogMessage:logMessage asynchronously:asynchronous];
}
}

+ (void)log:(BOOL)asynchronous message:(DDLogMessage *)logMessage {
Expand Down Expand Up @@ -978,25 +936,28 @@ - (instancetype)init {
return self;
}

- (instancetype)initWithMessage:(NSString *)message
level:(DDLogLevel)level
flag:(DDLogFlag)flag
context:(NSInteger)context
file:(NSString *)file
function:(NSString *)function
line:(NSUInteger)line
tag:(id)tag
options:(DDLogMessageOptions)options
timestamp:(NSDate *)timestamp {
- (instancetype)initWithFormat:(NSString *)messageFormat
formatted:(NSString *)message
level:(DDLogLevel)level
flag:(DDLogFlag)flag
context:(NSInteger)context
file:(NSString *)file
function:(NSString *)function
line:(NSUInteger)line
tag:(id)tag
options:(DDLogMessageOptions)options
timestamp:(NSDate *)timestamp {
NSParameterAssert(messageFormat);
NSParameterAssert(message);
NSParameterAssert(file);

if ((self = [super init])) {
BOOL copyMessage = (options & DDLogMessageDontCopyMessage) == 0;
_message = copyMessage ? [message copy] : message;
_level = level;
_flag = flag;
_context = context;
_messageFormat = copyMessage ? [messageFormat copy] : messageFormat;
_message = copyMessage ? [message copy] : message;
_level = level;
_flag = flag;
_context = context;

BOOL copyFile = (options & DDLogMessageCopyFile) != 0;
_file = copyFile ? [file copy] : file;
Expand Down Expand Up @@ -1038,6 +999,57 @@ - (instancetype)initWithMessage:(NSString *)message
return self;
}

- (instancetype)initWithFormat:(NSString *)messageFormat
args:(va_list)messageArgs
level:(DDLogLevel)level
flag:(DDLogFlag)flag
context:(NSInteger)context
file:(NSString *)file
function:(NSString *)function
line:(NSUInteger)line
tag:(id)tag
options:(DDLogMessageOptions)options
timestamp:(NSDate *)timestamp {
BOOL copyMessage = (options & DDLogMessageDontCopyMessage) == 0;
NSString *format = copyMessage ? [messageFormat copy] : messageFormat;
self = [self initWithFormat:format
formatted:[[NSString alloc] initWithFormat:format arguments:messageArgs]
level:level
flag:flag
context:context
file:file
function:function
line:line
tag:tag
options:options | DDLogMessageDontCopyMessage // we already did the copying if needed.
timestamp:timestamp];
return self;
}

- (instancetype)initWithMessage:(NSString *)message
level:(DDLogLevel)level
flag:(DDLogFlag)flag
context:(NSInteger)context
file:(NSString *)file
function:(NSString *)function
line:(NSUInteger)line
tag:(id)tag
options:(DDLogMessageOptions)options
timestamp:(NSDate *)timestamp {
self = [self initWithFormat:message
formatted:message
level:level
flag:flag
context:context
file:file
function:function
line:line
tag:tag
options:options
timestamp:timestamp];
return self;
}

NS_INLINE BOOL _nullable_strings_equal(NSString* _Nullable lhs, NSString* _Nullable rhs)
{
if (lhs == nil) {
Expand All @@ -1058,6 +1070,7 @@ - (BOOL)isEqual:(id)other {
} else {
__auto_type otherMsg = (DDLogMessage *)other;
return [otherMsg->_message isEqualToString:_message]
&& [otherMsg->_messageFormat isEqualToString:_messageFormat]
&& otherMsg->_level == _level
&& otherMsg->_flag == _flag
&& otherMsg->_context == _context
Expand All @@ -1076,6 +1089,7 @@ - (NSUInteger)hash {
// Subclasses of NSObject should not call [super hash] here.
// See https://stackoverflow.com/questions/36593038/confused-about-the-default-isequal-and-hash-implements
return _message.hash
^ _messageFormat.hash
^ _level
^ _flag
^ _context
Expand All @@ -1092,6 +1106,7 @@ - (NSUInteger)hash {
- (id)copyWithZone:(NSZone * __attribute__((unused)))zone {
DDLogMessage *newMessage = [DDLogMessage new];

newMessage->_messageFormat = _messageFormat;
newMessage->_message = _message;
newMessage->_level = _level;
newMessage->_flag = _flag;
Expand Down
72 changes: 68 additions & 4 deletions Sources/CocoaLumberjack/include/CocoaLumberjack/DDLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -791,16 +791,17 @@ DD_SENDABLE
// Direct accessors to be used only for performance
@public
NSString *_message;
NSString *_messageFormat;
DDLogLevel _level;
DDLogFlag _flag;
NSInteger _context;
NSString *_file;
NSString *_fileName;
NSString *_function;
NSUInteger _line;
#if DD_LEGACY_MESSAGE_TAG
#if DD_LEGACY_MESSAGE_TAG
id _tag __attribute__((deprecated("Use _representedObject instead", "_representedObject")));
#endif
#endif
id _representedObject;
DDLogMessageOptions _options;
NSDate * _timestamp;
Expand Down Expand Up @@ -829,6 +830,64 @@ DD_SENDABLE
* so it makes sense to optimize and skip the unnecessary allocations.
* However, if you need them to be copied you may use the options parameter to specify this.
*
* @param messageFormat the message format
* @param message the formatted message
* @param level the log level
* @param flag the log flag
* @param context the context (if any is defined)
* @param file the current file
* @param function the current function
* @param line the current code line
* @param tag potential tag
* @param options a bitmask which supports DDLogMessageCopyFile and DDLogMessageCopyFunction.
* @param timestamp the log timestamp
*
* @return a new instance of a log message model object
*/
- (instancetype)initWithFormat:(NSString *)messageFormat
formatted:(NSString *)message
level:(DDLogLevel)level
flag:(DDLogFlag)flag
context:(NSInteger)context
file:(NSString *)file
function:(nullable NSString *)function
line:(NSUInteger)line
tag:(nullable id)tag
options:(DDLogMessageOptions)options
timestamp:(nullable NSDate *)timestamp NS_DESIGNATED_INITIALIZER;

/**
* Convenience initializer taking a `va_list` as arguments to create the formatted message.
*
* @param messageFormat the message format
* @param messageArgs the message arguments.
* @param level the log level
* @param flag the log flag
* @param context the context (if any is defined)
* @param file the current file
* @param function the current function
* @param line the current code line
* @param tag potential tag
* @param options a bitmask which supports DDLogMessageCopyFile and DDLogMessageCopyFunction.
* @param timestamp the log timestamp
*
* @return a new instance of a log message model object
*/
- (instancetype)initWithFormat:(NSString *)messageFormat
args:(va_list)messageArgs
level:(DDLogLevel)level
flag:(DDLogFlag)flag
context:(NSInteger)context
file:(NSString *)file
function:(nullable NSString *)function
line:(NSUInteger)line
tag:(nullable id)tag
options:(DDLogMessageOptions)options
timestamp:(nullable NSDate *)timestamp;

/**
* Deprecated initialier. See initWithFormat:args:formatted:level:flag:context:file:function:line:tag:options:timestamp:.
*
* @param message the message
* @param level the log level
* @param flag the log flag
Expand All @@ -851,16 +910,21 @@ DD_SENDABLE
line:(NSUInteger)line
tag:(nullable id)tag
options:(DDLogMessageOptions)options
timestamp:(nullable NSDate *)timestamp NS_DESIGNATED_INITIALIZER;
timestamp:(nullable NSDate *)timestamp
__attribute__((deprecated("Use initializer taking unformatted message and args instead", "initWithFormat:formatted:level:flag:context:file:function:line:tag:options:timestamp:")));

/**
* Read-only properties
**/

/**
* The log message
* The log message.
*/
@property (readonly, nonatomic) NSString *message;
/**
* The message format. When the deprecated initializer is used, this might be the same as `message`.
*/
@property (readonly, nonatomic) NSString *messageFormat;
@property (readonly, nonatomic) DDLogLevel level;
@property (readonly, nonatomic) DDLogFlag flag;
@property (readonly, nonatomic) NSInteger context;
Expand Down
Loading

0 comments on commit 2b21ef5

Please sign in to comment.