Skip to content

Commit 00a786e

Browse files
committed
Add logger and replace print calls with appropriate logging functions
This commit adds a logger.py file and also replaces all `print` calls with logging functions like `logger.info`, `logger.warning`, `logger.error` etc. Also adds rich as a dependency.
1 parent 865c4c1 commit 00a786e

File tree

14 files changed

+45
-25
lines changed

14 files changed

+45
-25
lines changed

manim/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
from . import config
33
from . import constants
44

5-
6-
def main():
5+
def main():
76
args = config.parse_cli()
87
cfg = config.get_configuration(args)
98
constants.initialize_directories(cfg)

manim/camera/camera.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import numpy as np
1111

1212
from ..constants import *
13+
from ..logger import logger
1314
from ..mobject.types.image_mobject import AbstractImageMobject
1415
from ..mobject.mobject import Mobject
1516
from ..mobject.types.point_cloud_mobject import PMobject
@@ -24,7 +25,6 @@
2425
from ..utils.space_ops import angle_of_vector
2526
from ..utils.space_ops import get_norm
2627

27-
2828
class Camera(object):
2929
"""
3030
Base Camera class.
@@ -348,14 +348,14 @@ def make_background_from_func(self, coords_to_colors_func):
348348
The pixel array which can then be passed to set_background.
349349
"""
350350

351-
print("Starting set_background; for reference, the current time is ", time.strftime("%H:%M:%S"))
351+
logger.info("Starting set_background; for reference, the current time is ", time.strftime("%H:%M:%S"))
352352
coords = self.get_coords_of_all_pixels()
353353
new_background = np.apply_along_axis(
354354
coords_to_colors_func,
355355
2,
356356
coords
357357
)
358-
print("Ending set_background; for reference, the current time is ", time.strftime("%H:%M:%S"))
358+
logger.info("Ending set_background; for reference, the current time is ", time.strftime("%H:%M:%S"))
359359

360360
return self.convert_pixel_array(new_background, convert_from_floats=True)
361361

manim/config.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import types
66

77
from . import constants
8+
from .logger import logger
89

10+
911

1012
def parse_cli():
1113
try:
@@ -133,7 +135,7 @@ def parse_cli():
133135
)
134136
return parser.parse_args()
135137
except argparse.ArgumentError as err:
136-
print(str(err))
138+
logger.error(str(err))
137139
sys.exit(2)
138140

139141

@@ -229,8 +231,8 @@ def get_camera_configuration(args):
229231
try:
230232
camera_config["background_color"] = colour.Color(args.color)
231233
except AttributeError as err:
232-
print("Please use a valid color")
233-
print(err)
234+
logger.warning("Please use a valid color")
235+
logger.error(err)
234236
sys.exit(2)
235237

236238
# If rendering a transparent image/move, make sure the

manim/constants.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import numpy as np
22
import os
3+
from .logger import logger
34

45
MEDIA_DIR = ""
56
VIDEO_DIR = ""
67
VIDEO_OUTPUT_DIR = ""
78
TEX_DIR = ""
89
TEXT_DIR = ""
910

10-
1111
def initialize_directories(config):
1212
global MEDIA_DIR
1313
global VIDEO_DIR
@@ -27,13 +27,13 @@ def initialize_directories(config):
2727
)
2828
if not os.path.isdir(MEDIA_DIR):
2929
MEDIA_DIR = "./media"
30-
print(
30+
logger.info(
3131
f"Media will be written to {MEDIA_DIR + os.sep}. You can change "
3232
"this behavior with the --media_dir flag."
3333
)
3434
else:
3535
if config["media_dir"]:
36-
print(
36+
logger.warning(
3737
"Ignoring --media_dir, since both --tex_dir and a video "
3838
"directory were both passed"
3939
)
@@ -53,7 +53,6 @@ def initialize_directories(config):
5353
os.makedirs(folder)
5454

5555
NOT_SETTING_FONT_MSG='''
56-
Warning:
5756
You haven't set font.
5857
If you are not using English, this may cause text rendering problem.
5958
You set font like:

manim/extract_scene.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from .utils.sounds import play_error_sound
1212
from .utils.sounds import play_finish_sound
1313
from . import constants
14+
from .logger import logger
1415

1516

1617
def open_file_if_needed(file_writer, **config):
@@ -83,7 +84,7 @@ def prompt_user_for_choice(scene_classes):
8384
for num_str in user_input.split(",")
8485
]
8586
except KeyError:
86-
print(constants.INVALID_NUMBER_MESSAGE)
87+
logger.error(constants.INVALID_NUMBER_MESSAGE)
8788
sys.exit(2)
8889
user_input = input(constants.CHOOSE_NUMBER_MESSAGE)
8990
return [
@@ -96,7 +97,7 @@ def prompt_user_for_choice(scene_classes):
9697

9798
def get_scenes_to_render(scene_classes, config):
9899
if len(scene_classes) == 0:
99-
print(constants.NO_SCENE_MESSAGE)
100+
logger.error(constants.NO_SCENE_MESSAGE)
100101
return []
101102
if config["write_all"]:
102103
return scene_classes
@@ -109,7 +110,7 @@ def get_scenes_to_render(scene_classes, config):
109110
found = True
110111
break
111112
if not found and (scene_name != ""):
112-
print(
113+
logger.error(
113114
constants.SCENE_NOT_FOUND_MESSAGE.format(
114115
scene_name
115116
),
@@ -141,7 +142,7 @@ def get_module(file_name):
141142
exec(code, module.__dict__)
142143
return module
143144
except Exception as e:
144-
print(f"Failed to render scene: {str(e)}")
145+
logger.error(f"Failed to render scene: {str(e)}")
145146
sys.exit(2)
146147
else:
147148
module_name = file_name.replace(os.sep, ".").replace(".py", "")

manim/logger.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import logging
2+
from rich.logging import RichHandler
3+
4+
## Setting config for logger.
5+
logging.basicConfig(
6+
level="NOTSET",
7+
format="%(message)s",
8+
datefmt="[%X]",
9+
handlers=[RichHandler()]
10+
)
11+
12+
logger = logging.getLogger("rich")

manim/mobject/svg/text_mobject.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import cairo
66
from ...constants import *
77
from ...container.container import Container
8+
from ...logger import logger
89
from ...mobject.geometry import Dot, Rectangle
910
from ...mobject.svg.svg_mobject import SVGMobject
1011
from ...mobject.types.vectorized_mobject import VGroup
@@ -287,7 +288,7 @@ def text2svg(self):
287288

288289
if self.font == '':
289290
if NOT_SETTING_FONT_MSG != '':
290-
print(NOT_SETTING_FONT_MSG)
291+
logger.warning(NOT_SETTING_FONT_MSG)
291292

292293
dir_name = TEXT_DIR
293294
hash_name = self.text2hash()

manim/mobject/vector_field.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import random
66

77
from ..constants import *
8-
8+
from ..logger import logger
99
from ..animation.composition import AnimationGroup
1010
from ..animation.indication import ShowPassingFlash
1111
from ..mobject.geometry import Vector
@@ -85,7 +85,7 @@ def get_color_field_image_file(scalar_func,
8585
file_name = "%d.png" % func_hash
8686
full_path = os.path.join(RASTER_IMAGE_DIR, file_name)
8787
if not os.path.exists(full_path):
88-
print("Rendering color field image " + str(func_hash))
88+
logger.info("Rendering color field image " + str(func_hash))
8989
rgb_gradient_func = get_rgb_gradient_function(
9090
min_value=min_value,
9191
max_value=max_value,

manim/scene/scene.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from ..camera.camera import Camera
1212
from ..constants import *
1313
from ..container.container import Container
14+
from ..logger import logger
1415
from ..mobject.mobject import Mobject
1516
from ..scene.scene_file_writer import SceneFileWriter
1617
from ..utils.iterables import list_update
@@ -110,7 +111,7 @@ def print_end_message(self):
110111
Used internally to print the number of
111112
animations played after the scene ends.
112113
"""
113-
print("Played {} animations".format(self.num_plays))
114+
logger.info("Played {} animations".format(self.num_plays))
114115

115116
def set_variables_as_attrs(self, *objects, **newly_named_objects):
116117
"""

manim/scene/scene_file_writer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from ..constants import STREAMING_PROTOCOL
1515
from ..constants import VIDEO_DIR
1616
from ..constants import VIDEO_OUTPUT_DIR
17+
from ..logger import logger
1718
from ..utils.config_ops import digest_config
1819
from ..utils.file_ops import guarantee_existence
1920
from ..utils.file_ops import add_extension_if_not_present
@@ -473,7 +474,7 @@ def combine_movie_files(self):
473474
**kwargs
474475
)
475476
if len(partial_movie_files) == 0:
476-
print("No animations in this scene")
477+
logger.error("No animations in this scene")
477478
return
478479

479480
# Write a file partial_file_list.txt containing all
@@ -542,4 +543,4 @@ def print_file_ready_message(self, file_path):
542543
"""
543544
Prints the "File Ready" message to STDOUT.
544545
"""
545-
print("\nFile ready at {}\n".format(file_path))
546+
logger.info("\nFile ready at {}\n".format(file_path))

0 commit comments

Comments
 (0)