diff --git a/lib/mu-server.cc b/lib/mu-server.cc index 2844f281..41c7a7c9 100644 --- a/lib/mu-server.cc +++ b/lib/mu-server.cc @@ -78,12 +78,15 @@ struct Server::Private { // // output // - void output_sexp(Sexp&& sexp) const + void output_sexp(Sexp&& sexp, bool flush = false) const { if (output_) - output_(std::move(sexp)); + output_(std::move(sexp), flush); + } + void output_sexp(Sexp::List&& lst, bool flush = false) const + { + output_sexp(Sexp::make_list(std::move(lst)), flush); } - void output_sexp(Sexp::List&& lst) const { output_sexp(Sexp::make_list(std::move(lst))); } size_t output_results(const QueryResults& qres, size_t batch_size) const; // diff --git a/lib/mu-server.hh b/lib/mu-server.hh index d2784748..811ad6d4 100644 --- a/lib/mu-server.hh +++ b/lib/mu-server.hh @@ -33,8 +33,8 @@ namespace Mu { * */ class Server { - public: - using Output = std::function; +public: + using Output = std::function; /** * Construct a new server @@ -59,7 +59,7 @@ class Server { */ bool invoke(const std::string& expr) noexcept; - private: +private: struct Private; std::unique_ptr priv_; }; diff --git a/mu/mu-cmd-server.cc b/mu/mu-cmd-server.cc index bb39dadd..cfa326c8 100644 --- a/mu/mu-cmd-server.cc +++ b/mu/mu-cmd-server.cc @@ -82,7 +82,7 @@ cookie(size_t n) } static void -output_sexp_stdout(Sexp&& sexp) +output_sexp_stdout(Sexp&& sexp, bool flush = false) { const auto str{sexp.to_sexp_string()}; cookie(str.size() + 1); @@ -90,6 +90,9 @@ output_sexp_stdout(Sexp&& sexp) g_critical("failed to write output '%s'", str.c_str()); ::raise(SIGTERM); /* terminate ourselves */ } + + if (flush) + ::fflush(::stdout); } static void @@ -100,7 +103,7 @@ report_error(const Mu::Error& err) noexcept e.add_prop(":error", Sexp::make_number(static_cast(err.code()))); e.add_prop(":message", Sexp::make_string(err.what())); - output_sexp_stdout(Sexp::make_list(std::move(e))); + output_sexp_stdout(Sexp::make_list(std::move(e)), true /*flush*/); } MuError