@@ -46,6 +46,16 @@ class ExecutionResult:
4646 name : str = ""
4747
4848
49+ def _emit_log (log_callback : Optional [Callable [[str ], None ]], message : str ) -> None :
50+ """Emit a log line via callback if one was provided."""
51+ if log_callback is None :
52+ return
53+ try :
54+ log_callback (message )
55+ except Exception :
56+ pass
57+
58+
4959def _check_stop (stop_requested : Optional [Callable [[], bool ]]) -> bool :
5060 """Return True if the caller requested a stop."""
5161 if stop_requested is None :
@@ -89,21 +99,28 @@ def executor(
8999 task_name = name or getattr (func , "__name__" , repr (func ))
90100
91101 if _check_stop (stop_requested ):
92- output .info (log_callback , f"Cancelled before start: { task_name } " )
102+ msg = f"Cancelled before start: { task_name } "
103+ _emit_log (log_callback , msg )
104+ output .info (msg )
93105 return ExecutionResult (
94106 success = False ,
95107 error = "Execution cancelled before start" ,
96108 name = task_name ,
97109 )
98110
99- output .info (log_callback , f"Start: { task_name } " )
111+ msg = f"Start: { task_name } "
112+ _emit_log (log_callback , msg )
113+ output .info (msg )
100114 start = time .perf_counter ()
101115
102116 try :
103117 value = func (* args , ** kwargs )
104118 duration_ms = (time .perf_counter () - start ) * 1000.0
105- output . success (
119+ _emit_log (
106120 log_callback ,
121+ f"Success: { task_name } ({ duration_ms :.1f} ms)" ,
122+ )
123+ output .success (
107124 f" { task_name } ({ duration_ms :.1f} ms)" ,
108125 )
109126 return ExecutionResult (
@@ -115,8 +132,11 @@ def executor(
115132 except Exception as exc :
116133 duration_ms = (time .perf_counter () - start ) * 1000.0
117134 error_message = str (exc )
118- output . error (
135+ _emit_log (
119136 log_callback ,
137+ f"Failure: { task_name } - { error_message } " ,
138+ )
139+ output .error (
120140 f" { task_name } - { error_message } " ,
121141 )
122142 if not catch_exceptions :
0 commit comments