15"""Definition and tools for message callbacks.
17Message callbacks are used to get the text messages emitted by solvers during
20 Typical usage example:
22 # Print messages to stdout.
24 model, parameters.SolverType.GSCIP,
25 msg_cb=message_callback.printer_message_callback(prefix='[solver] '))
27 # Log messages with absl.logging.
29 model, parameters.SolverType.GSCIP,
30 msg_cb=lambda msgs: message_callback.log_messages(
31 msgs, prefix='[solver] '))
36from typing
import Callable, List, Sequence, TextIO
38from absl
import logging
40SolveMessageCallback = Callable[[Sequence[str]],
None]
44 *, file: TextIO = sys.stdout, prefix: str =
""
45) -> SolveMessageCallback:
46 """Returns a message callback that prints to a file.
48 It prints its output to the given text file, prefixing each line with the
51 For each call to the returned message callback, the output_stream is flushed.
54 file: The file to print to. It prints to stdout by default.
55 prefix: The prefix to print in front of each line.
58 A function matching the expected signature for message callbacks.
60 mutex = threading.Lock()
62 def callback(messages: Sequence[str]) ->
None:
64 for message
in messages:
74 messages: Sequence[str], *, level: int = logging.INFO, prefix: str =
""
76 """Logs the input messages from a message callback using absl.logging.log().
78 It logs each line with the given prefix. It setups absl.logging so that the
79 logs use the file name and line of the caller of this function.
81 Typical usage example:
84 model, parameters.SolverType.GSCIP,
85 msg_cb=lambda msgs: message_callback.log_messages(
86 msgs, prefix='[solver] '))
89 messages: The messages received in the message callback (typically a lambda
90 function in the caller code).
91 level: One of absl.logging.(DEBUG|INFO|WARNING|ERROR|FATAL).
92 prefix: The prefix to print in front of each line.
94 for message
in messages:
95 logging.log(level,
"%s%s", prefix, message)
98logging.ABSLLogger.register_frame_to_skip(__file__, log_messages.__name__)
101def vlog_messages(messages: Sequence[str], level: int, *, prefix: str =
"") ->
None:
102 """Logs the input messages from a message callback using absl.logging.vlog().
104 It logs each line with the given prefix. It setups absl.logging so that the
105 logs use the file name and line of the caller of this function.
107 Typical usage example:
109 result = solve.solve(
110 model, parameters.SolverType.GSCIP,
111 msg_cb=lambda msgs: message_callback.vlog_messages(
112 msgs, 1, prefix='[solver] '))
115 messages: The messages received in the message callback (typically a lambda
116 function in the caller code).
117 level: The verbose log level, e.g. 1, 2...
118 prefix: The prefix to print in front of each line.
120 for message
in messages:
121 logging.vlog(level,
"%s%s", prefix, message)
124logging.ABSLLogger.register_frame_to_skip(__file__, vlog_messages.__name__)
128 """Returns a message callback that logs messages to a list.
131 sink: The list to append messages to.
134 A function matching the expected signature for message callbacks.
136 mutex = threading.Lock()
138 def callback(messages: Sequence[str]) ->
None:
140 for message
in messages: