repl: log errors writing to history file

These errors are now logged and explicitly ignored, rather than
implicitly ignored.

The simple logic is duplicated in ReadlineLikeInteractor's getLine() and
destructor, rather than refactoring history writing into a separate
function. This is a tiny amount of logic and these two locations are
right next to each other.

Change-Id: Ia26015466a17f2b11952df5317a4d150d79dc184
This commit is contained in:
Qyriad 2024-05-19 12:57:14 -06:00
parent 73ef8cd50a
commit c46083426d

View file

@ -1,6 +1,8 @@
#include <cstdio>
#include <iostream>
#include <string>
#include <string_view>
#include <cerrno>
#ifdef READLINE
#include <readline/history.h>
@ -176,7 +178,14 @@ bool ReadlineLikeInteracter::getLine(std::string & input, ReplPromptType promptT
if (!s)
return false;
write_history(historyFile.c_str());
if (write_history(historyFile.c_str()) != 0) {
// write_history returns the result of fclose() (which also flushes).
// We should explicitly ignore these errors, but log them so the user
// isn't confused why their history is getting eaten.
error_t const fcloseErr = errno;
std::string_view const errMsg(strerror(fcloseErr));
warn("ignoring error writing repl history to %s: %s", historyFile, errMsg);
}
input += s;
input += '\n';
return true;
@ -184,7 +193,13 @@ bool ReadlineLikeInteracter::getLine(std::string & input, ReplPromptType promptT
ReadlineLikeInteracter::~ReadlineLikeInteracter()
{
write_history(historyFile.c_str());
if (write_history(historyFile.c_str()) != 0) {
// write_history returns the result of fclose() (which also flushes).
// We should explicitly ignore these errors, but log them.
error_t const fcloseErr = errno;
std::string_view const errMsg(strerror(fcloseErr));
warn("ignoring error writing repl history to %s: %s", historyFile, errMsg);
}
}
AutomationInteracter::Guard AutomationInteracter::init(detail::ReplCompleterMixin *)