|
| 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 <format> |
| 24 | +#include <iostream> |
| 25 | +#include <mutex> |
| 26 | +#include <string> |
| 27 | +#include <string_view> |
| 28 | + |
| 29 | +#include "iceberg/util/thread_util.h" |
| 30 | + |
| 31 | +namespace iceberg { |
| 32 | + |
| 33 | +namespace { |
| 34 | + |
| 35 | +/// \brief Trailing path component of a source file path. |
| 36 | +/// |
| 37 | +/// Scans for both '/' and '\\' so it handles compiler __FILE__ separators on |
| 38 | +/// every platform, and returns a view without copying -- unlike |
| 39 | +/// std::filesystem::path, which would allocate on this per-line path. |
| 40 | +std::string_view Basename(std::string_view path) noexcept { |
| 41 | + auto pos = path.find_last_of("/\\"); |
| 42 | + return pos == std::string_view::npos ? path : path.substr(pos + 1); |
| 43 | +} |
| 44 | + |
| 45 | +/// \brief Format a record into a single newline-terminated line. |
| 46 | +std::string FormatLine(const LogMessage& message) { |
| 47 | + auto now = |
| 48 | + std::chrono::floor<std::chrono::milliseconds>(std::chrono::system_clock::now()); |
| 49 | + return std::format("{:%Y-%m-%dT%H:%M:%S}Z {} [{}] [{}:{}] {}\n", now, |
| 50 | + ToString(message.level), OsThreadId(), |
| 51 | + Basename(message.location.file_name()), message.location.line(), |
| 52 | + message.message); |
| 53 | +} |
| 54 | + |
| 55 | +} // namespace |
| 56 | + |
| 57 | +void CerrLogger::Log(LogMessage&& message) noexcept { |
| 58 | + try { |
| 59 | + std::string line = FormatLine(message); |
| 60 | + std::lock_guard<std::mutex> lock(mutex_); |
| 61 | + std::cerr << line; |
| 62 | + } catch (...) { |
| 63 | + // Logging must never throw. Reached if either formatting or the write fails; |
| 64 | + // emit a short marker best-effort and swallow anything further. |
| 65 | + try { |
| 66 | + std::lock_guard<std::mutex> lock(mutex_); |
| 67 | + std::cerr << "<fmt error>\n"; |
| 68 | + } catch (...) { |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +void CerrLogger::Flush() noexcept { |
| 74 | + try { |
| 75 | + std::lock_guard<std::mutex> lock(mutex_); |
| 76 | + std::cerr.flush(); |
| 77 | + } catch (...) { |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +} // namespace iceberg |
0 commit comments