14"""Definition and tools for message callbacks.
16Message callbacks are used to get the text messages emitted by solvers during
19 Typical usage example:
21 # Print messages to stdout.
23 model, parameters.SolverType.GSCIP,
24 msg_cb=message_callback.printer_message_callback(prefix='[solver] '))
26 # Log messages with absl.logging.
28 model, parameters.SolverType.GSCIP,
29 msg_cb=lambda msgs: message_callback.log_messages(
30 msgs, prefix='[solver] '))
35from typing
import Callable, List, Sequence, TextIO
37from absl
import logging
39SolveMessageCallback = Callable[[Sequence[str]],
None]
43 *, file: TextIO = sys.stdout, prefix: str =
""
44) -> SolveMessageCallback:
45 """Returns a message callback that prints to a file.
47 It prints its output to the given text file, prefixing each line with the
50 For each call to the returned message callback, the output_stream is flushed.
53 file: The file to print to. It prints to stdout by default.
54 prefix: The prefix to print in front of each line.
57 A function matching the expected signature for message callbacks.
59 mutex = threading.Lock()
61 def callback(messages: Sequence[str]) ->
None:
63 for message
in messages:
73 messages: Sequence[str], *, level: int = logging.INFO, prefix: str =
""
75 """Logs the input messages from a message callback using absl.logging.log().
77 It logs each line with the given prefix. It setups absl.logging so that the
78 logs use the file name and line of the caller of this function.
80 Typical usage example:
83 model, parameters.SolverType.GSCIP,
84 msg_cb=lambda msgs: message_callback.log_messages(
85 msgs, prefix='[solver] '))
88 messages: The messages received in the message callback (typically a lambda
89 function in the caller code).
90 level: One of absl.logging.(DEBUG|INFO|WARNING|ERROR|FATAL).
91 prefix: The prefix to print in front of each line.
93 for message
in messages:
94 logging.log(level,
"%s%s", prefix, message)
97logging.ABSLLogger.register_frame_to_skip(__file__, log_messages.__name__)
100def vlog_messages(messages: Sequence[str], level: int, *, prefix: str =
"") ->
None:
101 """Logs the input messages from a message callback using absl.logging.vlog().
103 It logs each line with the given prefix. It setups absl.logging so that the
104 logs use the file name and line of the caller of this function.
106 Typical usage example:
108 result = solve.solve(
109 model, parameters.SolverType.GSCIP,
110 msg_cb=lambda msgs: message_callback.vlog_messages(
111 msgs, 1, prefix='[solver] '))
114 messages: The messages received in the message callback (typically a lambda
115 function in the caller code).
116 level: The verbose log level, e.g. 1, 2...
117 prefix: The prefix to print in front of each line.
119 for message
in messages:
120 logging.vlog(level,
"%s%s", prefix, message)
123logging.ABSLLogger.register_frame_to_skip(__file__, vlog_messages.__name__)
127 """Returns a message callback that logs messages to a list.
130 sink: The list to append messages to.
133 A function matching the expected signature for message callbacks.
135 mutex = threading.Lock()
137 def callback(messages: Sequence[str]) ->
None:
139 for message
in messages: