0

Here is the situation: I have a python program that crashes when i do something that shouldn't crash. I'd like to debug it myself so i am not asking for help on why it crashes but how i debug it like i want to.

I want to compare what lines execute in what order when it works and when i do the thing that doesn't work.

I found the trace module which you can use like this:

python -m trace -t program.py

And it will "Display lines as they are executed" (print them). However, the program i am making is an open world game in curses and therefore the output gets really weird since the terminal is already used by my curses game and i basically cannot use it.

So i have to write the output to a file somehow if i want to use it.

How do i make it write to a file, or are there any other methods?

j307
  • 108
  • 1
  • 7
  • I do not know about the `trace` module, but you may try to use the [logging](https://docs.python.org/3/library/logging.html) module to debug your program if you don't find a solution. – SpaceBurger Jun 18 '21 at 15:36
  • It seems like your question is quite similar to this one : https://stackoverflow.com/questions/4286693/python-trace-module-trace-lines-as-they-are-executed-but-save-to-file-rather. – SpaceBurger Jun 18 '21 at 15:39
  • @SpaceBurger i know of the logging module but i wanted a way more detailed log as i had no clue what caused the error – j307 Jun 20 '21 at 16:11
  • Understandable. In case you haven't solved this issue yet ; the link above suggests you modify the code of the `trace` module. Following this idea, you could replace the `print()` statements of the `trace` module with calls to `logging.debug()` for example. The `logging` module allowing you to easily write messages to a file, you would need to setup a logger or `basicConfig` first, but that should do the trick, without having to write your own trace module from scratch. – SpaceBurger Jun 22 '21 at 13:38
  • @SpaceBurger took some time but i did get it to work and give me the output i wanted and needed. needed to debug the debugger. however it didn't help me solve the problem – j307 Jun 22 '21 at 19:57

1 Answers1

0

I suggest using the python logging library. This will give you more control since you can add logging wherever you want including values of variables. You can also direct the output to stderr (along with command line redirection) or to a file in order to separate it from the curses ui of your actual program.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268