Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow exception handling to be disabled. #136

Closed
JodiTheTigger opened this issue Oct 15, 2015 · 13 comments
Closed

Allow exception handling to be disabled. #136

JodiTheTigger opened this issue Oct 15, 2015 · 13 comments

Comments

@JodiTheTigger
Copy link

I build my code without exception handling, and spdlog cannot build as it uses exceptions.

Would you be able to make exception support optional for spdlog?

@JodiTheTigger
Copy link
Author

cppformat uses the following method to make exceptions optional:

#if FMT_EXCEPTIONS
# define FMT_TRY try
# define FMT_CATCH(x) catch (x)
#else
# define FMT_TRY if (true)
# define FMT_CATCH(x) if (false)
#endif

#ifndef FMT_THROW
# if FMT_EXCEPTIONS
#  define FMT_THROW(x) throw x
# else
#  define FMT_THROW(x) assert(false)
# endif
#endif

@gabime
Copy link
Owner

gabime commented Oct 16, 2015

which means log errors would be just ignored if exceptions are disabled?

@JodiTheTigger
Copy link
Author

If you follow the cppformat style, then yes.
However instead you could have an api for error conditions that gets called instead of throwing an exception.

Either way this is just giving the choice to the user of your library to disable exceptions. It's up to them deal with the consiquences of doing so. The default would to have exceptions enabled.

@gabime
Copy link
Owner

gabime commented Oct 17, 2015

Sorry, would cause too much code diversion.

@gabime gabime closed this as completed Oct 17, 2015
@DoxaLogosGit
Copy link

This would be nice to have. A lot of embedded systems that use C++ can't use exceptions for performance reasons.

@conz27
Copy link

conz27 commented Mar 31, 2016

I am in the exact same boat ... just dumped easyloggingpp because of the exceptions.

Disabling exceptions is a key requirement for our particular system and IoT-based systems in general. I won't be able to use this logger because of that. I do like the speed performance spdlog offers.

@gabime
Copy link
Owner

gabime commented Mar 31, 2016

reopened due to popular request

@gabime gabime reopened this Mar 31, 2016
@netheril96
Copy link

netheril96 commented May 14, 2016

I have something similar but different: my program has exceptions enabled but I do not want a logger to throw any exceptions. In another words, I want a runtime option, instead of compile time, to disable exceptions so that I can drop the try ... catch everywhere. No one is capable of handling logging errors. What are they gonna do? Log those errors?

@tbastos
Copy link
Contributor

tbastos commented May 20, 2016

A fairly common use case for logging is to keep track of errors in situations where you cannot throw an exception. Therefore it makes sense for a logging library to never throw exceptions, or at least make it optional.

Moreover projects such as games are developed with -fno-exceptions and -fno-rtti, and they can't enable exceptions because of one library.

Theoretically if all throws in spdlog are replaced by a macro or function call, the user could redefine this macro/function to do anything, and exceptions could be made optional. When exceptions are disabled the default should be to just terminate the app. This should be easy to implement, or am I missing something?

@gabime
Copy link
Owner

gabime commented May 21, 2016

Should be easy, yes. I was thinking to add to the API some kind of set_error_handler() as suggested above by @JodiTheTigger. This way the user can terminate if he wants

@godbyk
Copy link
Contributor

godbyk commented Jun 17, 2016

@gabime I'm happy to work on a PR for this as it's functionality I'd like as well. Where would be a good place to store a function pointer to a user-supplied error handler?

@gabime
Copy link
Owner

gabime commented Aug 5, 2016

Implemented in 39cdd08.

Exceptions will be thrown only if the ctor failed.

If an error happens during logging, the library will print an error message to stderr (max rate of 1 message per minute).

This behaviour can be changed using

spdlog::set_error_handler(new_handler_fun)

or (for specific logger)

logger->set_error_handler(new_handler_fun):

@TheQuantumPhysicist
Copy link

There's a copy of fmt inside spdlog. Are the exceptions inside that disabled by default as well? So can I just mark any wrappers with noexcept without worrying?

bachittle pushed a commit to bachittle/spdlog that referenced this issue Dec 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants