Skip to content

Commit

Permalink
core: move logging soruce to googleurl
Browse files Browse the repository at this point in the history
  • Loading branch information
Chilledheart committed Oct 22, 2024
1 parent e6a84a1 commit b8e9207
Show file tree
Hide file tree
Showing 18 changed files with 232 additions and 110 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4077,7 +4077,6 @@ add_library(yass_core STATIC
src/config/config_impl.hpp
src/config/config_impl_local.hpp
src/config/config_impl_windows.hpp
src/core/logging.cpp
src/core/logging.hpp
src/core/process_utils.hpp
src/core/process_utils.cpp
Expand Down
4 changes: 2 additions & 2 deletions src/config/config_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

#include <absl/flags/flag.h>
#include <absl/strings/str_cat.h>
#include <base/process/process_handle.h>
#include <base/rand_util.h>
#include <gmock/gmock.h>
#include <cstdlib>
#include "config/config_impl.hpp"
#include "core/process_utils.hpp"
#include "core/utils_fs.hpp"

using namespace yass;
Expand All @@ -25,7 +25,7 @@ ABSL_FLAG(std::string, test_string, "", "Test string value");
class ConfigTest : public ::testing::Test {
public:
void SetUp() override {
key_prefix_ = absl::StrCat("pid_", GetPID(), "_run_", gurl_base::RandUint64());
key_prefix_ = absl::StrCat("pid_", gurl_base::GetCurrentProcId(), "_run_", gurl_base::RandUint64());
#if !(defined(_WIN32) || defined(__APPLE__))
original_configfile_ = config::g_configfile;
const char* tmpdir = getenv("TMPDIR");
Expand Down
38 changes: 0 additions & 38 deletions src/core/process_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,41 +253,3 @@ int ExecuteProcess(const std::vector<std::string>& params, std::string* output,
}

#endif // _WIN32

#ifdef _MSC_VER
static_assert(sizeof(uint32_t) == sizeof(DWORD), "");
#elif defined(_WIN32)
static_assert(sizeof(pid_t) >= sizeof(DWORD), "");
#endif

// Keep the same implementation with chromium (see base/process/process_handle_posix.cc)
pid_t GetPID() {
// Pthreads doesn't have the concept of a thread ID, so we have to reach down
// into the kernel.
#if BUILDFLAG(IS_POSIX)
return getpid();
#elif BUILDFLAG(IS_WIN)
return GetCurrentProcessId();
#else
#error "Lack GetPID implementation for host environment"
#endif
}

pid_t GetTID() {
return gurl_base::PlatformThread::CurrentId();
}

static pid_t g_main_thread_pid = GetPID();

pid_t GetMainThreadPid() {
return g_main_thread_pid;
}

bool PidHasChanged() {
pid_t pid = GetPID();
if (g_main_thread_pid == pid) {
return false;
}
g_main_thread_pid = pid;
return true;
}
16 changes: 1 addition & 15 deletions src/core/process_utils.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2023 Chilledheart */
/* Copyright (c) 2023-2024 Chilledheart */

#ifndef CORE_PROCESS_UTIL
#define CORE_PROCESS_UTIL
Expand All @@ -12,18 +12,4 @@
int ExecuteProcess(const std::vector<std::string>& params, std::string* output, std::string* error);
#endif

#ifdef _MSC_VER
// On Windows, process id and thread id are of the same type according to the
// return types of GetProcessId() and GetThreadId() are both DWORD, an unsigned
// 32-bit type.
using pid_t = uint32_t;
#else
#include <unistd.h>
#endif

pid_t GetPID();
pid_t GetTID();
pid_t GetMainThreadPid();
bool PidHasChanged();

#endif // CORE_PROCESS_UTIL
6 changes: 0 additions & 6 deletions src/core/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,6 @@ PlatformFile OpenReadFile(const std::string& path) {
}
#endif

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

void PrintMallocStats() {
#ifdef HAVE_TCMALLOC
constexpr const char* properties[] = {
Expand Down
2 changes: 0 additions & 2 deletions src/core/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ using fd_t = int;
#endif
} // namespace internal

bool IsProgramConsole(internal::fd_t fd);

using gurl_base::StringToInt;
using gurl_base::StringToInt64;
using gurl_base::StringToUint;
Expand Down
6 changes: 3 additions & 3 deletions src/core/utils_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
#include <time.h>
#include <unistd.h>

#include <base/threading/platform_thread.h>
#include <filesystem>

#include "core/logging.hpp"
#include "core/process_utils.hpp"
#include "core/utils_fs.hpp"

using std::filesystem::path;
Expand Down Expand Up @@ -110,7 +110,7 @@ void SetThreadPriority(pid_t process_id, pid_t thread_id, ThreadPriority priorit
// This prevents us from requiring to translate the NS TID to
// global TID.
pid_t syscall_tid = thread_id;
if (thread_id == GetTID()) {
if (thread_id == gurl_base::PlatformThread::CurrentId()) {
syscall_tid = 0;
}

Expand All @@ -134,7 +134,7 @@ void SetThreadPriority(pid_t process_id, pid_t thread_id, ThreadPriority priorit

bool SetCurrentThreadPriority(ThreadPriority priority) {
#if BUILDFLAG(IS_LINUX)
SetThreadPriority(getpid(), GetTID(), priority);
SetThreadPriority(getpid(), gurl_base::PlatformThread::CurrentId(), priority);
#endif // BUILDFLAG(IS_LINUX)
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/utils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <gtest/gtest.h>

#include "core/logging.hpp"
#include "core/process_utils.hpp"
#include "core/utils.hpp"
#include "core/utils_fs.hpp"

Expand All @@ -13,6 +12,7 @@
#endif

#include <absl/strings/str_format.h>
#include <base/process/process_handle.h>
#include <base/rand_util.h>
#include <filesystem>
#include <string_view>
Expand Down Expand Up @@ -94,7 +94,7 @@ TEST(UtilsTest, ReadFileAndWrite4K) {
gurl_base::RandBytes(buf.data(), buf.size());
int tmp_suffix;
gurl_base::RandBytes(&tmp_suffix, sizeof(tmp_suffix));
auto tmp_name = absl::StrFormat("read_write_file-%u-%d", GetPID(), tmp_suffix);
auto tmp_name = absl::StrFormat("read_write_file-%u-%d", gurl_base::GetCurrentProcId(), tmp_suffix);
auto tmp_dir = std::filesystem::path(::testing::TempDir());
#ifdef _WIN32
std::string tmp = SysWideToUTF8(tmp_dir / tmp_name);
Expand Down
10 changes: 0 additions & 10 deletions src/core/utils_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,6 @@ uint64_t GetMonotonicTime() {
#endif
}

static bool IsHandleConsole(HANDLE handle) {
DWORD mode;
return handle != HANDLE() && handle != INVALID_HANDLE_VALUE &&
(GetFileType(handle) & ~FILE_TYPE_REMOTE) == FILE_TYPE_CHAR && GetConsoleMode(handle, &mode);
}

bool IsProgramConsole(HANDLE handle) {
return IsHandleConsole(handle);
}

static const wchar_t* kDllWhiteList[] = {
#ifdef HAVE_TCMALLOC
#if defined(_M_X64) || defined(_M_ARM64)
Expand Down
2 changes: 1 addition & 1 deletion src/net/asio_ssl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "net/asio_ssl_internal.hpp"

#ifdef _WIN32
#include "core/windows/dirent.h"
#include "base/win/dirent.h"
#else
#include <dirent.h>
#endif
Expand Down
6 changes: 6 additions & 0 deletions third_party/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ add_library(url STATIC
googleurl-override/base/atomicops.h
googleurl-override/base/atomicops_internals_portable.h
googleurl-override/base/atomicops_internals_atomicword_compat.h
googleurl-override/base/files/file_util.h
googleurl-override/base/macros/concat.h
googleurl-override/base/macros/if.h
googleurl-override/base/macros/remove_parens.h
Expand All @@ -182,13 +183,15 @@ add_library(url STATIC
googleurl-override/base/memory/ref_counted.cc
googleurl-override/base/memory/ref_counted.h
googleurl-override/base/memory/scoped_refptr.h
googleurl-override/base/process/process_handle.h
googleurl-override/base/threading/platform_thread.cc
googleurl-override/base/threading/platform_thread.h
googleurl-override/base/threading/thread_collision_warner.cc
googleurl-override/base/threading/thread_collision_warner.h
googleurl-override/base/rand_util.h
googleurl-override/base/rand_util.cc
googleurl-override/polyfills/base/logging.h
googleurl-override/polyfills/base/logging.cc
googleurl-override/polyfills/base/check.h
googleurl-override/polyfills/base/check.cc
googleurl-override/polyfills/base/check_op.h
Expand All @@ -215,6 +218,7 @@ else()
googleurl-override/base/apple/scoped_typeref.h
googleurl-override/base/apple/foundation_util.mm
googleurl-override/base/apple/foundation_util.h
googleurl-override/base/files/file_util_apple.mm
googleurl-override/base/mac/scoped_ioobject.h
googleurl-override/base/strings/sys_string_conversions_apple.mm
)
Expand All @@ -224,11 +228,13 @@ endif()
if (WIN32)
target_sources(url PRIVATE
googleurl-override/polyfills/base/debug/debugger_win.cc
googleurl-override/base/process/process_handle_win.cc
googleurl-override/base/scoped_clear_last_error_win.cc
googleurl-override/base/rand_util_win.cc)
else()
target_sources(url PRIVATE
googleurl-override/polyfills/base/debug/debugger_posix.cc
googleurl-override/base/process/process_handle_posix.cc
googleurl-override/base/posix/safe_strerror.cc
googleurl-override/base/posix/safe_strerror.h
googleurl-override/base/rand_util_posix.cc)
Expand Down
27 changes: 27 additions & 0 deletions third_party/googleurl-override/base/files/file_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This file contains utility functions for dealing with the local
// filesystem.

#ifndef BASE_FILES_FILE_UTIL_H_
#define BASE_FILES_FILE_UTIL_H_

#include <string>

#include "base/base_export.h"

namespace gurl_base {

// Get the temporary directory provided by the system.
//
// WARNING: In general, you should use CreateTemporaryFile variants below
// instead of this function. Those variants will ensure that the proper
// permissions are set so that other users on the system can't edit them while
// they're open (which can lead to security issues).
BASE_EXPORT bool GetTempDir(std::string* path);

} // namespace gurl_base

#endif // BASE_FILES_FILE_UTIL_H_
38 changes: 38 additions & 0 deletions third_party/googleurl-override/base/files/file_util_apple.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/files/file_util.h"

#import <Foundation/Foundation.h>
#include <stdlib.h>
#include <string.h>

#include "base/strings/sys_string_conversions.h"

namespace gurl_base {

bool GetTempDir(std::string* path) {
// In order to facilitate hermetic runs on macOS, first check
// MAC_CHROMIUM_TMPDIR. This is used instead of TMPDIR for historical reasons.
// This was originally done for https://crbug.com/698759 (TMPDIR too long for
// process singleton socket path), but is hopefully obsolete as of
// https://crbug.com/1266817 (allows a longer process singleton socket path).
// Continue tracking MAC_CHROMIUM_TMPDIR as that's what build infrastructure
// sets on macOS.
const char* env_tmpdir = getenv("GURL_BASE_TMPDIR");
if (env_tmpdir) {
*path = std::string(env_tmpdir);
return true;
}

// If we didn't find it, fall back to the native function.
NSString* tmp = NSTemporaryDirectory();
if (tmp == nil) {
return false;
}
*path = gurl_base::SysNSStringToUTF8(tmp);
return true;
}

} // namespace gurl_base
52 changes: 52 additions & 0 deletions third_party/googleurl-override/base/process/process_handle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_PROCESS_PROCESS_HANDLE_H_
#define BASE_PROCESS_PROCESS_HANDLE_H_

#include <stdint.h>
#include <sys/types.h>

#include <compare>
#include <iosfwd>

#include "base/base_export.h"
#include "build/build_config.h"

#if BUILDFLAG(IS_WIN)
#include "base/win/windows_types.h"
#endif

namespace gurl_base {

// ProcessHandle is a platform specific type which represents the underlying OS
// handle to a process.
// ProcessId is a number which identifies the process in the OS.
#if BUILDFLAG(IS_WIN)
typedef HANDLE ProcessHandle;
typedef DWORD ProcessId;
typedef HANDLE UserTokenHandle;
const ProcessHandle kNullProcessHandle = NULL;
const ProcessId kNullProcessId = 0;
#elif BUILDFLAG(IS_FUCHSIA)
typedef zx_handle_t ProcessHandle;
typedef zx_koid_t ProcessId;
const ProcessHandle kNullProcessHandle = ZX_HANDLE_INVALID;
const ProcessId kNullProcessId = ZX_KOID_INVALID;
#elif BUILDFLAG(IS_POSIX)
// On POSIX, our ProcessHandle will just be the PID.
typedef pid_t ProcessHandle;
typedef pid_t ProcessId;
const ProcessHandle kNullProcessHandle = 0;
const ProcessId kNullProcessId = 0;
#endif // BUILDFLAG(IS_WIN)

// Returns the id of the current process.
// Note that on some platforms, this is not guaranteed to be unique across
// processes (use GetUniqueIdForProcess if uniqueness is required).
BASE_EXPORT ProcessId GetCurrentProcId();

} // namespace gurl_base

#endif // BASE_PROCESS_PROCESS_HANDLE_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/process/process_handle.h"

#include <unistd.h>

namespace gurl_base {

ProcessId GetCurrentProcId() {
return getpid();
}

} // namespace gurl_base
15 changes: 15 additions & 0 deletions third_party/googleurl-override/base/process/process_handle_win.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/process/process_handle.h"

#include <windows.h>

namespace gurl_base {

ProcessId GetCurrentProcId() {
return ::GetCurrentProcessId();
}

} // namespace gurl_base
File renamed without changes.
Loading

0 comments on commit b8e9207

Please sign in to comment.