From b3e0bf956657eb339680e2e4714730a5d9a00b65 Mon Sep 17 00:00:00 2001 From: CismonX Date: Mon, 31 Jul 2023 08:19:17 +0800 Subject: [PATCH] fix: arif_rime: prevent rime from logging to stderr --- configure.ac | 1 + examples/Makefile.am | 4 ++- examples/arif_rime.c | 4 +++ examples/arif_rime_workaround.cc | 48 ++++++++++++++++++++++++++++++++ examples/arif_rime_workaround.h | 37 ++++++++++++++++++++++++ 5 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 examples/arif_rime_workaround.cc create mode 100644 examples/arif_rime_workaround.h diff --git a/configure.ac b/configure.ac index 946cb14..151419b 100644 --- a/configure.ac +++ b/configure.ac @@ -22,6 +22,7 @@ LT_INIT([dlopen]) AC_PROG_AWK AC_PROG_CC AC_PROG_CPP +AC_PROG_CXX AC_PROG_INSTALL AC_PROG_LN_S AC_PROG_MAKE_SET diff --git a/examples/Makefile.am b/examples/Makefile.am index 4e94664..5feccb6 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -7,6 +7,7 @@ # include_HEADERS = +noinst_HEADERS = lib_LTLIBRARIES = dist_man3_MANS = @@ -14,12 +15,13 @@ dist_man3_MANS = if BUILD_ARIF_RIME include_HEADERS += arif_rime.h + noinst_HEADERS += arif_rime_workaround.h lib_LTLIBRARIES += libarifrime.la dist_man3_MANS += arif_rime.3 libarifrime_la_CPPFLAGS = $(RIME_CFLAGS) -I$(top_srcdir)/include libarifrime_la_LDFLAGS = -module libarifrime_la_LIBADD = $(RIME_LIBS) - libarifrime_la_SOURCES = arif_rime.c + libarifrime_la_SOURCES = arif_rime.c arif_rime_workaround.cc endif # BUILD_ARIF_RIME diff --git a/examples/arif_rime.c b/examples/arif_rime.c index 0be6504..4e3d37b 100644 --- a/examples/arif_rime.c +++ b/examples/arif_rime.c @@ -39,6 +39,7 @@ #include #include "arif_defs.h" +#include "arif_rime_workaround.h" #ifndef ARIF_RIME_APP_NAME # define ARIF_RIME_APP_NAME "rime.arif" @@ -283,6 +284,9 @@ init_rime (void) traits.modules = get_modules("ARIF_RIME_MODULES"); rime_api->setup(&traits); + // Prevent glog (used by Rime for logging) from writing to stderr, + // since it may break Readline output. + arif_rime_workaround_glog_nostderr(); rime_api->initialize(&traits); // Rime uses static C++ variables to store its states, which get destroyed diff --git a/examples/arif_rime_workaround.cc b/examples/arif_rime_workaround.cc new file mode 100644 index 0000000..64a87e3 --- /dev/null +++ b/examples/arif_rime_workaround.cc @@ -0,0 +1,48 @@ +/** + * arif/examples/arif_rime_workaround.cc + * ---- + * + * Copyright (C) 2023 CismonX + * + * This file is part of ARIF, Another Readline Input Framework. + * + * ARIF is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ARIF is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with ARIF. If not, see . + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "arif_rime_workaround.h" + +// It's better practice to just `#include ` here instead. +// However, we don't want to make glog our dependency just for the sake of +// this workaround. + +namespace fLB { + bool FLAGS_alsologtostderr; +} + +namespace fLI { + int FLAGS_stderrthreshold; +} + +void +arif_rime_workaround_glog_nostderr (void) +{ + // Rime sets this to true during init + fLB::FLAGS_alsologtostderr = false; + // default value is ERROR (2) instead of FATAL + fLI::FLAGS_stderrthreshold = 3; +} diff --git a/examples/arif_rime_workaround.h b/examples/arif_rime_workaround.h new file mode 100644 index 0000000..7efa16f --- /dev/null +++ b/examples/arif_rime_workaround.h @@ -0,0 +1,37 @@ +/** + * arif/examples/arif_rime_workaround.h + * ---- + * + * Copyright (C) 2023 CismonX + * + * This file is part of ARIF, Another Readline Input Framework. + * + * ARIF is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ARIF is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with ARIF. If not, see . + */ + +#ifndef ARIF_RIME_WORKAROUND_H_ +#define ARIF_RIME_WORKAROUND_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +void +arif_rime_workaround_glog_nostderr (void); + +#ifdef __cplusplus +} +#endif + +#endif // !defined(ARIF_RIME_WORKAROUND_H_)