Skip to content

Commit

Permalink
Change DDLogLevel from enum to options
Browse files Browse the repository at this point in the history
As discussed in [this StackOverflow question](https://stackoverflow.com/questions/68854372/how-to-implement-custom-log-levels-in-cocoalumberjack-from-swift), there's not really a clean way to implement custom log levels in Swift.

As `DDLogFlag` is defined as `NS_OPTIONS`, absolutely any value is valid here, but with `DDLogLevel` defined as an enum, as far as Swift as concerned the existing levels are the only valid levels.  I can't even particularly cleanly write aliases for the existing levels.

Further, to me, it doesn't even make particular sense for `DDLogFlag` to be `NS_OPTIONS` while `DDLogLevel` is `NS_ENUM`.  If anything, shouldn't it almost be the opposite...?  Logically, it doesn't make sense that something might be logged as `[.error, .info]` from Swift, but you could log something using that flag... but what does that even mean?  Meanwhile, from Swift, I can not do as some CocoaLumberjack documentation suggests, and easily filter to say "show me `.info` level, but not `.warnings`, but do show me `.error` level".  This again comes back to the fact that `DDLogLevel` is an enum and from the Swift side I can not create any custom values for this.

By this logic, `DDLogFlag` itself should arguably be `NS_ENUM` rather than `NS_OPTION`, however, I recommend leaving it as `NS_OPTION`, because changing it to `NS_ENUM` once again would limit my ability to use custom log levels from Swift.
  • Loading branch information
nhgrif committed Aug 27, 2021
1 parent 7b4185d commit c74d51b
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/CocoaLumberjack/include/CocoaLumberjack/DDLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ typedef NS_OPTIONS(NSUInteger, DDLogFlag){
/**
* Log levels are used to filter out logs. Used together with flags.
*/
typedef NS_ENUM(NSUInteger, DDLogLevel){
typedef NS_OPTIONS(NSUInteger, DDLogLevel){
/**
* No logs
*/
Expand Down

0 comments on commit c74d51b

Please sign in to comment.