|
1 | 1 | ###################
|
2 | 2 | Debugging & Logging
|
3 | 3 | ###################
|
| 4 | + |
| 5 | +Debugging problems with your build123d design involves the same techniques |
| 6 | +one would use to debug any Python source code; however, there are some |
| 7 | +specific techniques that might be of assistance. The following sections |
| 8 | +describe these techniques. |
| 9 | + |
| 10 | +*************** |
| 11 | +Python Debugger |
| 12 | +*************** |
| 13 | + |
| 14 | +Many Python IDEs have step by step debugging systems that can be used to |
| 15 | +walk through your code monitoring its operation with full visibility |
| 16 | +of all Python objects. Here is a screenshot of the Visual Studio Code |
| 17 | +debugger in action: |
| 18 | + |
| 19 | +.. image:: assets/VSC_debugger.png |
| 20 | + :align: center |
| 21 | + |
| 22 | +This shows that a break-point has been encountered and the code operation |
| 23 | +has been stopped. From here all of the Python variables are visible and |
| 24 | +the system is waiting on input from the user on how to proceed. One can |
| 25 | +enter the code that assigns ``top_face`` by pressing the down arrow button |
| 26 | +on the top right. Following code execution like this is a very powerful |
| 27 | +debug technique. |
| 28 | + |
| 29 | +******* |
| 30 | +Logging |
| 31 | +******* |
| 32 | + |
| 33 | +Build123d support standard python logging and generates its own log stream. |
| 34 | +If one is using **cq-editor** as a display system there is a built in |
| 35 | +``Log viewer`` tab that shows the current log stream - here is an example |
| 36 | +of a log stream: |
| 37 | + |
| 38 | +.. code-block:: bash |
| 39 | +
|
| 40 | + [18:43:44.678646] INFO: Entering BuildPart with mode=Mode.ADD which is in different scope as parent |
| 41 | + [18:43:44.679233] INFO: WorkplaneList is pushing 1 workplanes: [Plane(o=(0.00, 0.00, 0.00), x=(1.00, 0.00, 0.00), z=(0.00, 0.00, 1.00))] |
| 42 | + [18:43:44.679888] INFO: LocationList is pushing 1 points: [(p=(0.00, 0.00, 0.00), o=(-0.00, 0.00, -0.00))] |
| 43 | + [18:43:44.681751] INFO: BuildPart context requested by Box |
| 44 | + [18:43:44.685950] INFO: Completed integrating 1 object(s) into part with Mode=Mode.ADD |
| 45 | + [18:43:44.690072] INFO: GridLocations is pushing 4 points: [(p=(-30.00, -20.00, 0.00), o=(-0.00, 0.00, -0.00)), (p=(-30.00, 20.00, 0.00), o=(-0.00, 0.00, -0.00)), (p=(30.00, -20.00, 0.00), o=(-0.00, 0.00, -0.00)), (p=(30.00, 20.00, 0.00), o=(-0.00, 0.00, -0.00))] |
| 46 | + [18:43:44.691604] INFO: BuildPart context requested by Hole |
| 47 | + [18:43:44.724628] INFO: Completed integrating 4 object(s) into part with Mode=Mode.SUBTRACT |
| 48 | + [18:43:44.728681] INFO: GridLocations is popping 4 points |
| 49 | + [18:43:44.747358] INFO: BuildPart context requested by chamfer |
| 50 | + [18:43:44.762429] INFO: Completed integrating 1 object(s) into part with Mode=Mode.REPLACE |
| 51 | + [18:43:44.765380] INFO: LocationList is popping 1 points |
| 52 | + [18:43:44.766106] INFO: WorkplaneList is popping 1 workplanes |
| 53 | + [18:43:44.766729] INFO: Exiting BuildPart |
| 54 | +
|
| 55 | +The build123d logger is defined by: |
| 56 | + |
| 57 | +.. code-block:: python |
| 58 | +
|
| 59 | + logging.getLogger("build123d").addHandler(logging.NullHandler()) |
| 60 | + logger = logging.getLogger("build123d") |
| 61 | +
|
| 62 | +To export logs to a file, the following configuration is recommended: |
| 63 | + |
| 64 | +.. code-block:: python |
| 65 | +
|
| 66 | + logging.basicConfig( |
| 67 | + filename="myapp.log", |
| 68 | + level=logging.INFO, |
| 69 | + format="%(name)s-%(levelname)s %(asctime)s - [%(filename)s:%(lineno)s - \ |
| 70 | + %(funcName)20s() ] - %(message)s", |
| 71 | + ) |
| 72 | +
|
| 73 | +Logs can be easily placed in your code - here is an example: |
| 74 | + |
| 75 | +.. code-block:: python |
| 76 | +
|
| 77 | + logger.info("Exiting %s", type(self).__name__) |
| 78 | +
|
| 79 | +
|
| 80 | +******** |
| 81 | +Printing |
| 82 | +******** |
| 83 | + |
| 84 | +Sometimes the best debugging aid is just placing a print statement in your code. Many |
| 85 | +of the build123d classes are setup to provide useful information beyond their class and |
| 86 | +location in memory, as follows: |
| 87 | + |
| 88 | +.. code-block:: python |
| 89 | +
|
| 90 | + plane = Plane.XY.offset(1) |
| 91 | + print(f"{plane=}") |
| 92 | +
|
| 93 | +.. code-block:: bash |
| 94 | +
|
| 95 | + plane=Plane(o=(0.00, 0.00, 1.00), x=(1.00, 0.00, 0.00), z=(0.00, 0.00, 1.00)) |
| 96 | +
|
| 97 | +which shows the origin, x direction, and z direction of the plane. |
0 commit comments