Skip to content

Commit

Permalink
update(userspace/falco): improve stdout buffering
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Grasso <[email protected]>
  • Loading branch information
leogr committed Sep 28, 2020
1 parent f4bacc8 commit b0b0631
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
18 changes: 14 additions & 4 deletions userspace/falco/outputs_stdout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ limitations under the License.
#include <iostream>
#include "banned.h" // This raises a compilation error when certain functions are used

void falco::outputs::output_stdout::init_stdout()
{
//
// By default, the standard output stream is fully buffered
// or line buffered if the stream can be determined to refer to
// an interactive device (e.g. in a TTY).
// Just enable automatic flushing when unbuffered output is desired.
//
if(!m_buffered)
{
std::cout << std::nounitbuf;
}
}

void falco::outputs::output_stdout::output_event(gen_event *evt, std::string &rule, std::string &source,
falco_common::priority_type priority, std::string &format, std::string &msg)
{
Expand All @@ -27,10 +41,6 @@ void falco::outputs::output_stdout::output_event(gen_event *evt, std::string &ru
void falco::outputs::output_stdout::output_msg(falco_common::priority_type priority, std::string &msg)
{
std::cout << msg + "\n";
if(!m_buffered)
{
std::cout.flush();
}
}

void falco::outputs::output_stdout::cleanup()
Expand Down
11 changes: 11 additions & 0 deletions userspace/falco/outputs_stdout.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,23 @@ namespace outputs

class output_stdout : public abstract_output
{

void init(config oc, bool buffered,
bool time_format_iso_8601, std::string hostname)
{
abstract_output::init(oc, buffered, time_format_iso_8601, hostname);
init_stdout();
}

void output_event(gen_event *evt, std::string &rule, std::string &source,
falco_common::priority_type priority, std::string &format, std::string &msg);

void output_msg(falco_common::priority_type priority, std::string &msg);

void cleanup();

private:
void init_stdout();
};

} // namespace outputs
Expand Down

0 comments on commit b0b0631

Please sign in to comment.