|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/logging/cerr_logger.h" |
| 21 | + |
| 22 | +#include <chrono> |
| 23 | +#include <cstdint> |
| 24 | +#include <format> |
| 25 | +#include <iostream> |
| 26 | +#include <mutex> |
| 27 | +#include <string> |
| 28 | +#include <string_view> |
| 29 | + |
| 30 | +#if defined(_WIN32) |
| 31 | +# include <windows.h> |
| 32 | +#elif defined(__APPLE__) |
| 33 | +# include <pthread.h> |
| 34 | +#else |
| 35 | +# include <unistd.h> |
| 36 | + |
| 37 | +# include <sys/syscall.h> |
| 38 | +#endif |
| 39 | + |
| 40 | +namespace iceberg { |
| 41 | + |
| 42 | +namespace { |
| 43 | + |
| 44 | +/// \brief OS-native thread id, cached per thread to avoid a syscall per log. |
| 45 | +/// |
| 46 | +/// Matches the cross-process-correlatable id used by spdlog/glog (not the opaque |
| 47 | +/// std::thread::id), and avoids the std::formatter<std::thread::id> (P2693) |
| 48 | +/// minimum-toolchain dependency. |
| 49 | +uint64_t OsThreadId() noexcept { |
| 50 | + static thread_local uint64_t tid = []() -> uint64_t { |
| 51 | +#if defined(_WIN32) |
| 52 | + return static_cast<uint64_t>(::GetCurrentThreadId()); |
| 53 | +#elif defined(__APPLE__) |
| 54 | + uint64_t id = 0; |
| 55 | + pthread_threadid_np(nullptr, &id); |
| 56 | + return id; |
| 57 | +#else |
| 58 | + return static_cast<uint64_t>(::syscall(SYS_gettid)); |
| 59 | +#endif |
| 60 | + }(); |
| 61 | + return tid; |
| 62 | +} |
| 63 | + |
| 64 | +/// \brief Trailing path component of a source file path. |
| 65 | +std::string_view Basename(std::string_view path) noexcept { |
| 66 | + auto pos = path.find_last_of("/\\"); |
| 67 | + return pos == std::string_view::npos ? path : path.substr(pos + 1); |
| 68 | +} |
| 69 | + |
| 70 | +/// \brief Format a record into a single newline-terminated line. |
| 71 | +std::string FormatLine(const LogMessage& message) { |
| 72 | + auto now = |
| 73 | + std::chrono::floor<std::chrono::milliseconds>(std::chrono::system_clock::now()); |
| 74 | + return std::format("{:%Y-%m-%dT%H:%M:%S}Z {} [{}] {}:{}] {}\n", now, |
| 75 | + ToString(message.level), OsThreadId(), |
| 76 | + Basename(message.location.file_name()), message.location.line(), |
| 77 | + message.message); |
| 78 | +} |
| 79 | + |
| 80 | +} // namespace |
| 81 | + |
| 82 | +void CerrLogger::Log(LogMessage&& message) noexcept { |
| 83 | + try { |
| 84 | + std::string line = FormatLine(message); |
| 85 | + std::lock_guard<std::mutex> lock(mutex_); |
| 86 | + std::cerr << line; |
| 87 | + } catch (...) { |
| 88 | + // Logging must never throw. Reached if either formatting or the write fails; |
| 89 | + // emit a short marker best-effort and swallow anything further. |
| 90 | + try { |
| 91 | + std::lock_guard<std::mutex> lock(mutex_); |
| 92 | + std::cerr << "<fmt error>\n"; |
| 93 | + } catch (...) { |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +void CerrLogger::Flush() noexcept { |
| 99 | + try { |
| 100 | + std::lock_guard<std::mutex> lock(mutex_); |
| 101 | + std::cerr.flush(); |
| 102 | + } catch (...) { |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +} // namespace iceberg |
0 commit comments