-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add progress monitor #1030
Comments
Congrats, this looks really really awesome! |
Perhaps focus first on the
Is the general design OK with you? Focussing on just the |
+1 |
this is really nice |
Closing. Although very nice, this feature doesn't fit well in a log library imo. |
@gabime I can make it a separate lib to be added on top of spdlog. I do need some way of subclassing the ansicolor sink though. Any chance of having an intermediate class that isn't final? |
The alternative is copying that sink I suppose. |
(Keeping it separate is probably the best way indeed. If I look at other implementations, whether it's boost progress/logging or python tqdm/logging or others, it's usually distinct pieces of code) |
Copy paste is your friend. Especially in this case, where a progress sink isn't really a color sink, but something else indeed. |
@epruesse This looks really nice! Do you have any plans to make a separate progress class or should I just copy/paste from https://github.com/epruesse/SINA/blob/dev/src/progress.h? I am a big fan the python version of tqdm, but the C++ port does not seem to have much to it. I am also a big fan of spdlog, so having the two together looks really awesome. I do, however, see how gabime thinks it is best to leave it out of the core spdlog codebase. But what about a separate repo like https://github.com/guangie88/spdlog_setup? @gabime perhaps a separate spdlog_contrib or extensions repo for add-ons like this that could be quite helpful but are not part of your core purpose for spdlog? Just a thought. Thanks to both of you for your contributions. |
@pwm1234 You can't just copy paste that file - I patched some parts of spdlog. As Gabi said, I'll have to do some copy pasting. Yes, I'm planning to create a repo for that. Give me a few weeks to fit that in. |
Great! Can you please update this issue with the url for the repo when you have made it? (I am subscribed to this issue so I should get an email--I do not want to miss it!) |
Hi everyone! |
Looking good. |
From memory, not having looked at the new code yet: The usage is like boost program monitor or python tqdm. You create a progress monitor as an object with the logger as argument. As long as this exists, a status bar with the progress bar as content will be displayed as the bottom most row. The progress monitor object can then be incremented. Fast code avoids doing anything if the last update was recent, so this doesn't cost too much in tight loops. You can create multiple of these, e.g. in hierarchical loops. They disappear when the object is destructed. |
Exactly as @epruesse said. Moreover, |
As promised in #854 I had a go at adding a progress monitor to spdlog.

See https://github.com/epruesse/SINA/blob/dev/src/progress.h
It needs cleanup, but it works. I considered a few options, including using
fopencookie
to create a customFILE*
and receiving writes fromansicolor_sink
. Instead, I ended up creating aterminal_sink
that expectsTargetStream
to not just understand ANSI color codes, but alsomove cursor up
(ESC [ A) andclear line
(ESC [ K).terminal_sink
allows registering persistentstatus_msg
objects which are always shown at the bottom below the latest log message. Every time a log message is printed, these messages are re-rendered (which would allow other use cases as well - e.g. showing system usage data). Thestatus_msg
itself can also trigger a re-render.logger_progress
is then a progress monitor class that given alogger
will maintain itself as astatus_msg
for applicable sinks on thelogger
during its lifetime. So to show a progress logger, you'd do:Additional options set the log level to something other than
info
, tell the monitor to use plain ascii characters instead of unicode bars and set a width.Caveats: I did have to patch spdlog:
target_file_
andmutex_
beprotected
rather thanprivate
console_mutex::mutex_t
into astd::recursive_mutex
.And yes ... it's using
dynamic_cast
and plenty virtuals and probably made the assembly look horrific. It's still fast though. I borrowed some tricks from the pythontqdm
package -only re-rendering the status after a minimum time and guessing at that from the typical number of iterations. That way, the 'hot' part oflogger_progress.update(int)
doesn't take many cycles to decide that it won't need to do anything.Right now it just picks up the terminal width at startup - technically it should listen to(edit 3/28/19 - implemented)SIGWINCH
and re-render to match the new terminal size. Its working for my purposes at this point though.Would you be interested in a PR?
The text was updated successfully, but these errors were encountered: