Bug 384791 - Option for unbuffered output
Summary: Option for unbuffered output
Status: CONFIRMED
Alias: None
Product: kdevelop
Classification: Applications
Component: Output Views (show other bugs)
Version: 5.1.2
Platform: Appimage Linux
: NOR wishlist
Target Milestone: ---
Assignee: kdevelop-bugs-null
URL:
Keywords:
: 427128 (view as bug list)
Depends on:
Blocks:
 
Reported: 2017-09-17 11:49 UTC by Jaka Kranjc
Modified: 2020-09-30 13:48 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jaka Kranjc 2017-09-17 11:49:05 UTC
The Run and Debug output windows seem to do some internal buffering, in effect delaying display of the output until the buffer threshold is reached. I'd like to be able to disable this behaviour. Or force a flush.

I have a program that logs a lot of debug info to stdout and it's annoying to not be able to read it when I need it. So much, that I have to do such debugging outside of kdevelop.
Comment 1 Kevin Funk 2017-09-18 07:36:17 UTC
For the record, relevant code is in kdevelop.git:kdevplatform/outputview/outputmodel.cpp.

If you want to investigate the issue yourself, please have a look at further instructions at:
  https://www.kdevelop.org/contribute-kdevelop
Comment 2 Kevin Funk 2017-09-18 07:37:56 UTC
@Jaka: Does your program log to stdout without printing a newline?

I think if it *does* print a newline, then you shouldn't actually notice the internal buffering -- we're actually processing output at least every 50 ms (cf. BATCH_AGGREGATE_TIME_DELAY variable in code)
Comment 3 Jaka Kranjc 2017-09-18 22:36:23 UTC
They are newline terminated, but maybe multiline strings (these dumps are long) end up treated differently. I'll take a look.
Comment 4 Kevin Funk 2017-09-19 07:31:53 UTC
Perfect, if you have any questions, please join our IRC chat #kdevelop on Freenode.
Comment 5 Jaka Kranjc 2019-10-09 11:12:02 UTC
Ok, finally took the time to investigate. Our logging does some string construction, but everything ends up being printed by fprintf. It's all in this file:
https://github.com/gemrb/gemrb/blob/master/gemrb/core/System/Logger/Stdio.cpp

Even if I cut out all the prettifying stuff, always explicitly print a newline in the fprintf call and print only a few characters of the passed message, there is still buffering in play.

So basically no matter how trivial is what we try to print, kdevelop still buffers the output. 
It doesn't appear to be time-based at all either; with shorter messages I have to generate many more before they get dumped to the window.
Comment 6 Francis Herne 2020-09-29 20:25:33 UTC
*** Bug 427128 has been marked as a duplicate of this bug. ***
Comment 7 Gaël de Chalendar (aka Kleag) 2020-09-29 21:32:50 UTC
I would mark this bug as important instead of normal. It is quite a show stopper for using kdevelop for debugging, dont't you think.

Note that I noticed just after submitting #427128 that it does not always occur: I was able to see the output of a debugged script with no delay while the same output was buffered when just run.
Comment 8 Gaël de Chalendar (aka Kleag) 2020-09-30 08:42:15 UTC
On my side, the problem appear when running python code. I just noticed that output of my python script is also buffered when running in a console like that:
 
$ python tracenn.py 2>&1 | tee output.log

Could it be a problem with the python interpreter itself?
Comment 9 Gaël de Chalendar (aka Kleag) 2020-09-30 08:51:12 UTC
(In reply to Gaël de Chalendar (aka Kleag) from comment #8)
> On my side, the problem appear when running python code. I just noticed that
> output of my python script is also buffered when running in a console like
> that:
>  
> $ python tracenn.py 2>&1 | tee output.log
> 
> Could it be a problem with the python interpreter itself?

Yes, it is:
https://stackoverflow.com/questions/107705/disable-output-buffering

When adding the -u flag to the script interpreter field of the launch configuration, the output is no more buffered. 

Should it be added by default? Or detected and the user warned?

Well, with this new information, I'm not sure anymore that my bug #427128 is really a duplicate of the current one.
Comment 10 Milian Wolff 2020-09-30 13:21:19 UTC
Doing that for the python debugger may be OK, but for C/C++ there's no such option.

@Jaka Kranjc: Your code in StdioLogger::print is doing:

fprintf(stdout, "%s", message);

stdout is buffered, this is not KDevelop's fault. Dito for StdioLogger::LogInternal. If you want to have unbuffered output, flush the stdout stream after calling fprintf, or use an unbuffered channel like stderr.
Comment 11 Jaka Kranjc 2020-09-30 13:48:28 UTC
That could explain it, but:
- stdout is supposed to be line-buffered, not fully buffered
- why doesn't it behave the same when ran from konsole directly?

I can add a setvbuf call that will likely fix it, but I'd like to understand it first.