2

Is there a way to control how messages from failed assertions (assert.h) are displayed?

On OS X they are written to stderr. On Windows I noticed that a dialog box comes up and displays the message. Can I use my own custom display or logging method?

I expect the answer to be platform dependent, so this question is specifically for OS X when using the system compiler.


Notes:

  • It is not sufficient for my purposes to redirect the output to a file. I would like to provide my own function to display or log the message.

  • I cannot simply redefine the assert macro because every subsequent #include <assert.h> restores the original behaviour, per the standard.

  • I need this for dealing with assertion failures from libraries that are not my own. For my own code I can always use my own special assert macro which is named something else than assert.

Community
  • 1
  • 1
Szabolcs
  • 24,728
  • 9
  • 85
  • 174
  • You can always use your own special assert macro, but so can third party libraries you are trying to intercept. – n. m. could be an AI Oct 31 '16 at 11:25
  • @n.m. That's clear., but I know that the libraries I am dealing with don't do that. – Szabolcs Oct 31 '16 at 11:29
  • Why do you need to change the output in the first place? – 2501 Oct 31 '16 at 11:33
  • @2501 So that I can see it. If you are interested in the details: the code is being used from [Mathematica](http://wolfram.com/). I can only see stderr if I run Mathematica in command line mode, which is awfully inconvenient. I want to be able to see the messages in Mathematica's notebook interface (Mathematica's own "terminal", if you like). – Szabolcs Oct 31 '16 at 11:35
  • You can write your own assert.h and cassert files to the project, and the compiler ought to use them over the standard ones. – n. m. could be an AI Oct 31 '16 at 11:40
  • 1
    I don't think it is possible using the C library. You can do it non-portably: http://stackoverflow.com/a/3019733/4082723 by redirecting stderr to stdout. – 2501 Oct 31 '16 at 11:45
  • Agreed that there is not going to be a portable solution to this. Your fundamental request is to change the behavior of code you do not control, which there is no single or reliable solution for. Anything you devise will be a hack that happens to work for the specific problem you're facing. – Rob Napier Oct 31 '16 at 12:23

0 Answers0