File headers: use SPDX license identifiers
[blender-addons.git] / io_scene_gltf2 / io / com / gltf2_io_debug.py
blob5b09da10b3a6e4d20673fa08537ff31b678751f9
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2018-2021 The glTF-Blender-IO authors.
5 # Imports
8 import time
9 import logging
12 # Globals
15 OUTPUT_LEVELS = ['ERROR', 'WARNING', 'INFO', 'PROFILE', 'DEBUG', 'VERBOSE']
17 g_current_output_level = 'DEBUG'
18 g_profile_started = False
19 g_profile_start = 0.0
20 g_profile_end = 0.0
21 g_profile_delta = 0.0
24 # Functions
28 def set_output_level(level):
29 """Set an output debug level."""
30 global g_current_output_level
32 if OUTPUT_LEVELS.index(level) < 0:
33 return
35 g_current_output_level = level
38 def print_console(level, output):
39 """Print to Blender console with a given header and output."""
40 global OUTPUT_LEVELS
41 global g_current_output_level
43 if OUTPUT_LEVELS.index(level) > OUTPUT_LEVELS.index(g_current_output_level):
44 return
46 print(get_timestamp() + " | " + level + ': ' + output)
49 def print_newline():
50 """Print a new line to Blender console."""
51 print()
54 def get_timestamp():
55 current_time = time.gmtime()
56 return time.strftime("%H:%M:%S", current_time)
59 def print_timestamp(label=None):
60 """Print a timestamp to Blender console."""
61 output = 'Timestamp: ' + get_timestamp()
63 if label is not None:
64 output = output + ' (' + label + ')'
66 print_console('PROFILE', output)
69 def profile_start():
70 """Start profiling by storing the current time."""
71 global g_profile_start
72 global g_profile_started
74 if g_profile_started:
75 print_console('ERROR', 'Profiling already started')
76 return
78 g_profile_started = True
80 g_profile_start = time.time()
83 def profile_end(label=None):
84 """Stop profiling and printing out the delta time since profile start."""
85 global g_profile_end
86 global g_profile_delta
87 global g_profile_started
89 if not g_profile_started:
90 print_console('ERROR', 'Profiling not started')
91 return
93 g_profile_started = False
95 g_profile_end = time.time()
96 g_profile_delta = g_profile_end - g_profile_start
98 output = 'Delta time: ' + str(g_profile_delta)
100 if label is not None:
101 output = output + ' (' + label + ')'
103 print_console('PROFILE', output)
106 # TODO: need to have a unique system for logging importer/exporter
107 # TODO: this logger is used for importer, but in io and in blender part, but is written here in a _io_ file
108 class Log:
109 def __init__(self, loglevel):
110 self.logger = logging.getLogger('glTFImporter')
111 self.hdlr = logging.StreamHandler()
112 formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
113 self.hdlr.setFormatter(formatter)
114 self.logger.addHandler(self.hdlr)
115 self.logger.setLevel(int(loglevel))