Mailing List Archive

Macro for logging
If not already present, do you think it's useful to add a macro that does
something like

# ifdef Py_DEBUG
fprintf(stderr, "%s\n", message);
# endif

?
Re: Macro for logging [ In reply to ]
On Wed, 21 Oct 2020 14:19:37 +0200
Marco Sulla <Marco.Sulla.Python@gmail.com> wrote:
> If not already present, do you think it's useful to add a macro that does
> something like
>
> # ifdef Py_DEBUG
> fprintf(stderr, "%s\n", message);
> # endif

In general, you want to do this on a per-component basis, so each C
source file tends to redefine its own macros if it needs to.

Regards

Antoine.

_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/ZG4OEHT4B2XCQUQAR5U4MDU2TOV2QO5M/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: Macro for logging [ In reply to ]
Hi,

There is the __debug__ builtin variable which is equal to True by
default, but is equal to False when Python is run with the -O command
line option.

The compiler removes dead code when -O is used. Example:

$ cat x.py
def func():
if __debug__: print("debug")

import dis
dis.dis(func)

# "debug" constant is checked at runtime
$ python3 x.py
2 0 LOAD_GLOBAL 0 (print)
2 LOAD_CONST 1 ('debug')
4 CALL_FUNCTION 1
6 POP_TOP
8 LOAD_CONST 0 (None)
10 RETURN_VALUE

# code removed by the compiler
$ python3 -O x.py
2 0 LOAD_CONST 0 (None)
2 RETURN_VALUE

Victor

Le mer. 21 oct. 2020 à 14:21, Marco Sulla
<Marco.Sulla.Python@gmail.com> a écrit :
>
> If not already present, do you think it's useful to add a macro that does something like
>
> # ifdef Py_DEBUG
> fprintf(stderr, "%s\n", message);
> # endif
>
> ?
> _______________________________________________
> Python-Dev mailing list -- python-dev@python.org
> To unsubscribe send an email to python-dev-leave@python.org
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/6W6YO6JSJZOGWYWNWB2ARUS4LSLY3C7Y/
> Code of Conduct: http://python.org/psf/codeofconduct/



--
Night gathers, and now my watch begins. It shall not end until my death.
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/5V3HNOGDF2I44CKEAYR2XILF6DE7THFL/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: Macro for logging [ In reply to ]
Thank you Victor, I know. I meant a macro in CPython. Maybe not so sane to
have a unique global logger in C, as Antoine pointed out.
Re: Macro for logging [ In reply to ]
21.10.20 15:30, Antoine Pitrou ????:
> On Wed, 21 Oct 2020 14:19:37 +0200
> Marco Sulla <Marco.Sulla.Python@gmail.com> wrote:
>> If not already present, do you think it's useful to add a macro that does
>> something like
>>
>> # ifdef Py_DEBUG
>> fprintf(stderr, "%s\n", message);
>> # endif
>
> In general, you want to do this on a per-component basis, so each C
> source file tends to redefine its own macros if it needs to.

I concur with Antoine.
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/GKP52NSQEE4I4JVC7LVEO2LQISCVXFO3/
Code of Conduct: http://python.org/psf/codeofconduct/