Skip to content

Commit

Permalink
No need to log to files from a console process
Browse files Browse the repository at this point in the history
  • Loading branch information
yukawa committed Oct 29, 2023
1 parent d3151bb commit 0c8ef97
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/base/init_mozc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#ifdef _WIN32
#include <windows.h>
#include <winnt.h>
#endif // _WIN32

#include <string>
Expand Down Expand Up @@ -59,6 +60,24 @@ ABSL_FLAG(std::string, log_dir, "",
ABSL_FLAG(std::string, program_invocation_name, "",
"Program name copied from argv[0].");

#if defined(_WIN32)
EXTERN_C IMAGE_DOS_HEADER __ImageBase;

namespace {
bool IsCuiProcess() {
const std::uintptr_t base_addr =
reinterpret_cast<std::uintptr_t>(&__ImageBase);
const auto *nt_header = reinterpret_cast<const IMAGE_NT_HEADERS *>(
base_addr + __ImageBase.e_lfanew);
if (nt_header->Signature != IMAGE_NT_SIGNATURE) {
return false;
}
return nt_header->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI;
}
} // namespace
#endif


namespace mozc {
namespace {

Expand Down Expand Up @@ -103,6 +122,10 @@ void ParseCommandLineFlags(int argc, char **argv) {
void InitMozc(const char *arg0, int *argc, char ***argv) {
absl::SetFlag(&FLAGS_program_invocation_name, *argv[0]);
#ifdef _WIN32
if (IsCuiProcess()) {
absl::SetFlag(&FLAGS_logtostderr, true);
}

// InitMozc() is supposed to be used for code generator or
// other programs which are not included in the production code.
// In these code, we don't want to show any error messages when
Expand Down

0 comments on commit 0c8ef97

Please sign in to comment.