Skip to content

Commit

Permalink
Merge pull request #668 from Chilledheart/core_implement_is_program_c…
Browse files Browse the repository at this point in the history
…onsole

core: implement IsProgramConsole
  • Loading branch information
Chilledheart authored Jan 15, 2024
2 parents a3d0084 + 3678c59 commit 15c8c99
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/core/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2269,7 +2269,7 @@ void LogMessage::SendToLog() ABSL_EXCLUSIVE_LOCKS_REQUIRED(log_mutex) {
LogDestination::WaitForSinks(data_);

#ifdef OS_WIN
if (!IsProgramConsole()) {
if (!IsProgramConsole(GetStdHandle(STD_ERROR_HANDLE))) {
std::wstring message = SysUTF8ToWide(data_->message_text_);
MessageBoxW(nullptr, message.c_str(), L"Fatal Error", MB_ICONERROR);
// Ignore errors.
Expand Down
6 changes: 6 additions & 0 deletions src/core/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,12 @@ PlatformFile OpenReadFile(const std::string &path) {
}
#endif

#ifndef _WIN32
bool IsProgramConsole(int fd) {
return isatty(fd) == 1;
}
#endif

#ifdef HAVE_TCMALLOC
void PrintTcmallocStats() {
std::vector<const char*> properties = {
Expand Down
10 changes: 9 additions & 1 deletion src/core/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,15 @@ uint64_t GetMonotonicTime();

#define NS_PER_SECOND (1000 * 1000 * 1000)

bool IsProgramConsole();
namespace internal {
#ifdef _WIN32
using fd_t = HANDLE;
#else
using fd_t = int;
#endif
} // namespace internal

bool IsProgramConsole(internal::fd_t fd);

bool SetUTF8Locale();

Expand Down
5 changes: 0 additions & 5 deletions src/core/utils_freebsd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ uint64_t GetMonotonicTime() {
ts.tv_nsec - start_ts.tv_nsec;
}

// TBD
bool IsProgramConsole() {
return true;
}

bool SetUTF8Locale() {
if (setlocale(LC_ALL, "C.UTF-8") == nullptr)
return false;
Expand Down
5 changes: 0 additions & 5 deletions src/core/utils_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,6 @@ uint64_t GetMonotonicTime() {
ts.tv_nsec - start_ts.tv_nsec;
}

// TBD
bool IsProgramConsole() {
return true;
}

bool SetUTF8Locale() {
if (setlocale(LC_ALL, "C.UTF-8") == nullptr)
return false;
Expand Down
5 changes: 0 additions & 5 deletions src/core/utils_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ uint64_t GetMonotonicTime() {
return clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW);
}

// TBD
bool IsProgramConsole() {
return true;
}

bool SetUTF8Locale() {
// C.UTF-8 doesn't exists on macOS
if (setlocale(LC_ALL, "en_US.UTF-8") == nullptr)
Expand Down
8 changes: 3 additions & 5 deletions src/core/utils_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,8 @@ static bool IsHandleConsole(HANDLE handle) {
GetConsoleMode(handle, &mode);
}

bool IsProgramConsole() {
return IsHandleConsole(GetStdHandle(STD_INPUT_HANDLE)) ||
IsHandleConsole(GetStdHandle(STD_OUTPUT_HANDLE)) ||
IsHandleConsole(GetStdHandle(STD_ERROR_HANDLE));
bool IsProgramConsole(HANDLE handle) {
return IsHandleConsole(handle);
}

#ifndef CP_UTF8
Expand All @@ -231,7 +229,7 @@ bool IsProgramConsole() {
bool SetUTF8Locale() {
bool success = false;

bool is_console = IsProgramConsole();
bool is_console = false /* IsProgramConsole() */;

if (is_console) {
// Calling SetConsoleCP
Expand Down

0 comments on commit 15c8c99

Please sign in to comment.