stdout synchronization #3151
-
I'm working on a program that has both stdout logging through spdlog and a built-in interactive "terminal" for program commands. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Avoiding mixed prints to stdout can be achieved with critical sections in a single process. You might be able to achieve your goal by piping the output of multiple processes, but my guess is that it would be quite difficult. See also: https://unix.stackexchange.com/a/476089 |
Beta Was this translation helpful? Give feedback.
Ah, I finally understand.
So you have one multi-threaded application with the terminal running in a dedicated thread (and spdlog in a another thread).
Sorry it took me so long to understand.
Then use the mutex that spdlog uses internally to protect access to stdout.
You can access it using
spdlog::details::console_mutex::mutex()
:spdlog/include/spdlog/details/console_globals.h
Lines 12 to 18 in 5ebfc92
spdlog/include/spdlog/sinks/stdout_sinks.h
Lines 59 to 63 in 5ebfc92