Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Automated Exception Throwing #23

Closed
hartbit opened this Issue · 10 comments

4 participants

@hartbit

First of all, thanks a lot for Cocoa Lumberjack, it's a really nice piece of software.

The way I usually program, I would of liked Cocoa Lumberjack to be able to automatically throw exceptions/assert. For example, I would like to set: I want all logs > Error to assert in Debug and none in Release. Like that, errors are fixed quickly instead of pilling up in the console.

What do you think?

@robbiehanson

Very cool feature request! I'll have to think about the best way to implement this...

@hartbit

Can I offer my opinion on the implementation?

#ifdef DEBUG
  static const int ddAssertLevel = LOG_LEVEL_WARN;
#endif

I guess this is the easiest solution to implement. Just setting a constant to set at what level to start throwing asserts. It has the advantage of being simple, but I see one big drawback: it kills the easy of use of conditional asserts.

// Old assert
NSAssert(param != nil, @"Param should not be nil.");

// Becomes
static const int ddAssertLevel = LOG_LEVEL_WARN;
if (param == nil) {
    DDLogError(@"Param should not be nil.");
}

One way to fix the previous problem would be to create assert versions of all DDLog macros. I know that's a fair amount of work, but here is what it could look like:

// Old assert
NSAssert(param != nil, @"Param should not be nil.");

// Becomes
static const int ddAssertLevel = LOG_LEVEL_WARN;
DDAssertError(param != nil, @"Param should not be nil.");

DDAssertError would assert and call DDLogError.

@hartbit

Any news on the progress of this feature?

@bpoplauschi
Collaborator

@TrahDivad This sounds like a very interesting proposal. The issue I see is how can we make this available for some people while most of the people don't get blocked by asserts (I assume a lot of them log stuff like network errors as errors, case in which there isn't an issue with their code). Perhaps adding a define?

@hartbit

How about using my original proposal:

static const int ddAssertLevel = LOG_LEVEL_WARN;
if (param == nil) {
    DDLogError(@"Param should not be nil.");
}

By default, ddAssertLevel could be set to never assert. People could then configure it to never assert at runtime, and to assert at soon as warn in debug mode.

@bpoplauschi
Collaborator

@robbiehanson did you ever come to a conclusion about this proposal? @rivera-ernesto what do you think?
I'm ok with doing this, as long as the default version of the library (no configuration needed) doesn't assert and you can activate that via setting something (can be ddAssertLevel)

@rivera-ernesto
Collaborator

First I would confine all asserts to a separate DDLog+Assert.h header file, with something like:

#define ASSERT_MAYBE(lvl, flg, condition, frmt, ...) \
do { if(lvl & flg) NSAssert(condition, fret, ##__VA_ARGS__); } while(0)

#define DDAssertError(condition, frmt, ...)   if (!(condition)) { DDLogError(frmt, ##__VA_ARGS__); } ASSERT_MAYBE(ddAssertLevel, LOG_FLAG_ERROR, condition, frmt, ##__VA_ARGS__)
// ... Other levels

Then from my source file:

#import <CocoaLumberJack/DDLog+Assert.h>

#ifdef DEBUG
static const int ddLogLevel = LOG_LEVEL_INFO;
static const int ddAssertLevel = LOG_LEVEL_WARN;
#else
static const int ddLogLevel = LOG_LEVEL_WARN;
static const int ddAssertLevel = LOG_LEVEL_OFF;
#endif

// Test
DDAssertError(NO, @"A mock error %@", self);

Which I quickly tested and works both with log messages and/or NSAsserts according to the user configuration.

@hartbit

This is missing one thing I'd like to have: when ddAssertLevel = LOG_LEVEL_WARN; is set, I'd also like simple DDLogWarn to assert, not only the DDAssertWarn. I basically see it as a switch to force you to fix warnings/errors/etc… that you've currently logged, with or without the Assert shorthand.

@rivera-ernesto
Collaborator

The thing is that to achieve that you'll have to overload the DDLog macros for both people who use asserts and don't.

I see two solutions:

a) People who want assertions just use DDAssert macros all the time (recommended). Just like most people customize DDLog by defining and using their own XXLog macros.

b) In DDLog+Assert.h you #undef and redefine all DDLog macros.

@bpoplauschi
Collaborator

Closing for now, please reopen if you have a fresh view on this matter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.