Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ bazel_dep(name = "rules_shell", version = "0.10.0")
bazel_dep(name = "bazel_skylib", version = "1.9.2")
bazel_dep(name = "abseil-cpp", version = "20260817.0", repo_name = "absl")
bazel_dep(name = "nlohmann_json", version = "3.12.0.bcr.2", repo_name = "json")
bazel_dep(name = "fmt", version = "12.2.0.bcr.1-codeql.1")
bazel_dep(name = "rules_kotlin", version = "2.4.21-codeql.1")
bazel_dep(name = "gazelle", version = "0.54.0")
bazel_dep(name = "rules_dotnet")
Expand Down
1 change: 0 additions & 1 deletion shared/cpp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ cc_library(
visibility = ["//visibility:public"],
deps = [
"@absl//absl/strings",
"@fmt",
"@json",
],
)
5 changes: 2 additions & 3 deletions shared/cpp/Diagnostics.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "Diagnostics.h"

#include <fmt/format.h>
#include <fmt/chrono.h>
#include <format>

#include "absl/strings/str_join.h"
#include "absl/strings/str_cat.h"
Expand Down Expand Up @@ -41,7 +40,7 @@ nlohmann::json Diagnostic::json(const std::chrono::system_clock::time_point& tim
}},
{"severity", severityToString(severity)},
{"markdownMessage", absl::StrCat(message, "\n\n", action)},
{"timestamp", fmt::format("{:%FT%T%z}", timestamp)},
{"timestamp", std::format("{:%FT%T%z}", timestamp)},
};
if (location) {
ret["location"] = location->json();
Expand Down
1 change: 1 addition & 0 deletions shared/cpp/Diagnostics.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <chrono>
#include <optional>
#include <string_view>

Expand Down
1 change: 0 additions & 1 deletion swift/logging/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ cc_library(
"//shared/cpp:extractor_shared",
"@absl//absl/strings",
"@binlog",
"@fmt",
"@json",
],
)
41 changes: 34 additions & 7 deletions swift/logging/SwiftLogging.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#pragma once

#include <chrono>
#include <filesystem>
#include <format>
#include <fstream>
#include <iostream>
#include <regex>
#include <string>
#include <system_error>
#include <utility>
#include <vector>

#include <fmt/format.h>
#include <fmt/chrono.h>
#include <fmt/std.h>

#include <binlog/binlog.hpp>
#include <binlog/TextOutputStream.hpp>
#include <binlog/EventFilter.hpp>
Expand All @@ -20,6 +21,31 @@

#include "swift/logging/SwiftDiagnostics.h"

namespace codeql::detail {

template <typename T>
const T& diagnosticFormatArg(const T& value) {
return value;
}

// C++23 has no formatters for paths or error codes.
inline std::string diagnosticFormatArg(const std::filesystem::path& value) {
return value.string();
}

inline std::string diagnosticFormatArg(const std::error_code& value) {
return std::format("{}:{}", value.category().name(), value.value());
}

template <typename... Args>
std::string formatDiagnostic(
std::format_string<decltype(diagnosticFormatArg(std::declval<const Args&>()))...> format,
const Args&... args) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FP: #23265

return std::format(format, diagnosticFormatArg(args)...);
}

} // namespace codeql::detail

// Logging macros. These will call `logger()` to get a Logger instance, picking up any `logger`
// defined in the current scope. Domain-specific loggers can be added or used by either:
// * providing a class field called `logger` (as `Logger::operator()()` returns itself)
Expand Down Expand Up @@ -52,7 +78,7 @@

// Emit errors with a specified SwiftDiagnostic object. These will be both logged and outputted as
// JSON DB diagnostics. The format must be appliable to the following arguments both as binlog and
// as fmt::format formatting.
// as std::format formatting.
// Beware that contrary to LOG_* macros, arguments right of the format will be evaluated twice. ID
// is evaluated once though.
#define DIAGNOSE_ERROR(ID, ...) DIAGNOSE_WITH_LEVEL(error, ID, __VA_ARGS__)
Expand All @@ -63,8 +89,9 @@
do { \
auto _now = ::binlog::clockNow(); \
const ::codeql::Diagnostic& _id = ID; \
::codeql::Log::diagnose(_id, std::chrono::nanoseconds{_now}, \
fmt::format(FORMAT __VA_OPT__(, ) __VA_ARGS__)); \
::codeql::Log::diagnose( \
_id, std::chrono::nanoseconds{_now}, \
::codeql::detail::formatDiagnostic(FORMAT __VA_OPT__(, ) __VA_ARGS__)); \
LOG_WITH_LEVEL_AND_TIME(LEVEL, _now, CODEQL_DIAGNOSTIC_LOG_FORMAT_PREFIX FORMAT, \
_id.abbreviation() __VA_OPT__(, ) __VA_ARGS__); \
} while (false)
Expand Down
Loading