|
1 | 1 | #pragma once |
2 | 2 |
|
3 | 3 | #include <chrono> |
| 4 | +#include <filesystem> |
| 5 | +#include <format> |
4 | 6 | #include <fstream> |
5 | 7 | #include <iostream> |
6 | 8 | #include <regex> |
| 9 | +#include <string> |
| 10 | +#include <system_error> |
| 11 | +#include <utility> |
7 | 12 | #include <vector> |
8 | 13 |
|
9 | | -#include <fmt/format.h> |
10 | | -#include <fmt/chrono.h> |
11 | | -#include <fmt/std.h> |
12 | | - |
13 | 14 | #include <binlog/binlog.hpp> |
14 | 15 | #include <binlog/TextOutputStream.hpp> |
15 | 16 | #include <binlog/EventFilter.hpp> |
|
20 | 21 |
|
21 | 22 | #include "swift/logging/SwiftDiagnostics.h" |
22 | 23 |
|
| 24 | +namespace codeql::detail { |
| 25 | + |
| 26 | +template <typename T> |
| 27 | +const T& diagnosticFormatArg(const T& value) { |
| 28 | + return value; |
| 29 | +} |
| 30 | + |
| 31 | +// C++23 has no formatters for paths or error codes. |
| 32 | +inline std::string diagnosticFormatArg(const std::filesystem::path& value) { |
| 33 | + return value.string(); |
| 34 | +} |
| 35 | + |
| 36 | +inline std::string diagnosticFormatArg(const std::error_code& value) { |
| 37 | + return std::format("{}:{}", value.category().name(), value.value()); |
| 38 | +} |
| 39 | + |
| 40 | +template <typename... Args> |
| 41 | +std::string formatDiagnostic( |
| 42 | + std::format_string<decltype(diagnosticFormatArg(std::declval<const Args&>()))...> format, |
| 43 | + const Args&... args) { |
| 44 | + return std::format(format, diagnosticFormatArg(args)...); |
| 45 | +} |
| 46 | + |
| 47 | +} // namespace codeql::detail |
| 48 | + |
23 | 49 | // Logging macros. These will call `logger()` to get a Logger instance, picking up any `logger` |
24 | 50 | // defined in the current scope. Domain-specific loggers can be added or used by either: |
25 | 51 | // * providing a class field called `logger` (as `Logger::operator()()` returns itself) |
|
52 | 78 |
|
53 | 79 | // Emit errors with a specified SwiftDiagnostic object. These will be both logged and outputted as |
54 | 80 | // JSON DB diagnostics. The format must be appliable to the following arguments both as binlog and |
55 | | -// as fmt::format formatting. |
| 81 | +// as std::format formatting. |
56 | 82 | // Beware that contrary to LOG_* macros, arguments right of the format will be evaluated twice. ID |
57 | 83 | // is evaluated once though. |
58 | 84 | #define DIAGNOSE_ERROR(ID, ...) DIAGNOSE_WITH_LEVEL(error, ID, __VA_ARGS__) |
|
63 | 89 | do { \ |
64 | 90 | auto _now = ::binlog::clockNow(); \ |
65 | 91 | const ::codeql::Diagnostic& _id = ID; \ |
66 | | - ::codeql::Log::diagnose(_id, std::chrono::nanoseconds{_now}, \ |
67 | | - fmt::format(FORMAT __VA_OPT__(, ) __VA_ARGS__)); \ |
| 92 | + ::codeql::Log::diagnose( \ |
| 93 | + _id, std::chrono::nanoseconds{_now}, \ |
| 94 | + ::codeql::detail::formatDiagnostic(FORMAT __VA_OPT__(, ) __VA_ARGS__)); \ |
68 | 95 | LOG_WITH_LEVEL_AND_TIME(LEVEL, _now, CODEQL_DIAGNOSTIC_LOG_FORMAT_PREFIX FORMAT, \ |
69 | 96 | _id.abbreviation() __VA_OPT__(, ) __VA_ARGS__); \ |
70 | 97 | } while (false) |
|
0 commit comments