Skip to content

Commit

Permalink
deps, Diag: add spdlog, new file from Logger (#1144)
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-matsui authored Feb 28, 2025
1 parent f442f2f commit 4a0f803
Show file tree
Hide file tree
Showing 28 changed files with 297 additions and 345 deletions.
1 change: 1 addition & 0 deletions .github/actions/setup-macos-deps/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ runs:
brew install \
curl \
fmt \
spdlog \
libgit2 \
nlohmann-json \
tbb
Expand Down
1 change: 1 addition & 0 deletions .github/actions/setup-ubuntu-deps/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ runs:
sudo apt-get install -y \
libcurl4-openssl-dev \
libfmt-dev \
libspdlog-dev \
libgit2-dev \
nlohmann-json3-dev \
libtbb-dev
Expand Down
16 changes: 10 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ ARG base=ubuntu:24.04

FROM $base AS builder

RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential ca-certificates git pkg-config libfmt-dev libgit2-dev libcurl4-openssl-dev nlohmann-json3-dev libtbb-dev \
&& rm -rf /var/lib/apt/lists/*
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
build-essential ca-certificates git pkg-config \
libfmt-dev libspdlog-dev libgit2-dev libcurl4-openssl-dev nlohmann-json3-dev libtbb-dev \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /app

Expand All @@ -21,9 +23,11 @@ RUN make RELEASE=1 install

FROM $base

RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential clang libfmt-dev libgit2-dev libcurl4-openssl-dev nlohmann-json3-dev libtbb-dev \
&& rm -rf /var/lib/apt/lists/*
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
build-essential clang \
libfmt-dev libspdlog-dev libgit2-dev libcurl4-openssl-dev nlohmann-json3-dev libtbb-dev \
&& rm -rf /var/lib/apt/lists/*

COPY --from=builder /usr/local/bin/cabin /usr/local/bin/cabin

Expand Down
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ LIBGIT2_VERREQ := libgit2 >= 1.7.0, libgit2 < 1.10.0
LIBCURL_VERREQ := libcurl >= 7.79.1, libcurl < 9.0.0
NLOHMANN_JSON_VERREQ := nlohmann_json >= 3.10.5, nlohmann_json < 4.0.0
TBB_VERREQ := tbb >= 2021.5.0, tbb < 2023.0.0
FMT_VERREQ := fmt >= 9, fmt < 12.0.0
FMT_VERREQ := fmt >= 9.0.0, fmt < 12.0.0
SPDLOG_VERREQ := spdlog >= 1.8.0, spdlog < 2.0.0
TOML11_VER := $(shell grep -m1 toml11 cabin.toml | sed 's/.*rev = \(.*\)}/\1/' | tr -d '"')
RESULT_VER := $(shell grep -m1 cpp-result cabin.toml | sed 's/.*tag = \(.*\)}/\1/' | tr -d '"')

Expand All @@ -40,11 +41,13 @@ INCLUDES := -isystem $(O)/DEPS/toml11/include \
$(shell pkg-config --cflags '$(LIBCURL_VERREQ)') \
$(shell pkg-config --cflags '$(NLOHMANN_JSON_VERREQ)') \
$(shell pkg-config --cflags '$(TBB_VERREQ)') \
$(shell pkg-config --cflags '$(FMT_VERREQ)')
$(shell pkg-config --cflags '$(FMT_VERREQ)') \
$(shell pkg-config --cflags '$(SPDLOG_VERREQ)')
LIBS := $(shell pkg-config --libs '$(LIBGIT2_VERREQ)') \
$(shell pkg-config --libs '$(LIBCURL_VERREQ)') \
$(shell pkg-config --libs '$(TBB_VERREQ)') \
$(shell pkg-config --libs '$(FMT_VERREQ)')
$(shell pkg-config --libs '$(FMT_VERREQ)') \
$(shell pkg-config --libs '$(SPDLOG_VERREQ)')

SRCS := $(shell find src -name '*.cc')
OBJS := $(patsubst src/%,$(O)/%,$(SRCS:.cc=.o))
Expand All @@ -71,6 +74,7 @@ check_deps:
@pkg-config '$(NLOHMANN_JSON_VERREQ)' || (echo "Error: $(NLOHMANN_JSON_VERREQ) not found" && exit 1)
@pkg-config '$(TBB_VERREQ)' || (echo "Error: $(TBB_VERREQ) not found" && exit 1)
@pkg-config '$(FMT_VERREQ)' || (echo "Error: $(FMT_VERREQ) not found" && exit 1)
@pkg-config '$(SPDLOG_VERREQ)' || (echo "Error: $(SPDLOG_VERREQ) not found" && exit 1)

$(PROJECT): $(OBJS)
$(CXX) $(CXXFLAGS) $^ $(LIBS) $(LDFLAGS) -o $@
Expand Down
1 change: 1 addition & 0 deletions cabin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ version = "0.12.0-dev"
toml11 = {git = "https://github.com/ToruNiina/toml11.git", rev = "fdd5e29f78eb9e58cc02736f2dc4263ed0058662"}
mitama-cpp-result = {git = "https://github.com/loliGothicK/mitama-cpp-result.git", tag = "v11.0.0"}
fmt = {version = ">=9 && <12", system = true}
spdlog = {version = ">=1.8 && <2", system = true}
libcurl = {version = ">=7.79.1 && <9", system = true}
libgit2 = {version = ">=1.7 && <1.10", system = true}
nlohmann_json = {version = "3.10.5", system = true}
Expand Down
6 changes: 3 additions & 3 deletions src/Algos.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include "Algos.hpp"

#include "Command.hpp"
#include "Logger.hpp"
#include "Rustify/Result.hpp"

#include <cctype>
#include <chrono>
#include <cstdlib>
#include <optional>
#include <spdlog/spdlog.h>
#include <string>
#include <string_view>
#include <thread>
Expand Down Expand Up @@ -49,13 +49,13 @@ replaceAll(

Result<ExitStatus>
execCmd(const Command& cmd) noexcept {
logger::debug("Running `{}`", cmd.toString());
spdlog::debug("Running `{}`", cmd.toString());
return Try(cmd.spawn()).wait();
}

Result<std::string>
getCmdOutput(const Command& cmd, const std::size_t retry) noexcept {
logger::trace("Running `{}`", cmd.toString());
spdlog::trace("Running `{}`", cmd.toString());

ExitStatus exitStatus;
std::string stdErr;
Expand Down
23 changes: 12 additions & 11 deletions src/BuildConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include "Algos.hpp"
#include "Command.hpp"
#include "Compiler.hpp"
#include "Diag.hpp"
#include "Git2.hpp"
#include "Logger.hpp"
#include "Manifest.hpp"
#include "Parallelism.hpp"
#include "Semver.hpp"
Expand All @@ -23,6 +23,7 @@
#include <ostream>
#include <queue>
#include <ranges>
#include <spdlog/spdlog.h>
#include <sstream>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -447,7 +448,7 @@ BuildConfig::containsTestCode(const std::string& sourceFile) const {
// file.
const bool containsTest = src != testSrc;
if (containsTest) {
logger::trace("Found test code: {}", sourceFile);
spdlog::trace("Found test code: {}", sourceFile);
}
return Ok(containsTest);
}
Expand Down Expand Up @@ -622,7 +623,7 @@ BuildConfig::setVariables() {
commitShortHash = commitHash.substr(0, git2::SHORT_HASH_LEN);
commitDate = git2::Commit().lookup(repo, oid).time().toString();
} catch (const git2::Exception& e) {
logger::trace("No git repository found");
spdlog::trace("No git repository found");
}

// Variables Cabin sets for the user.
Expand Down Expand Up @@ -883,15 +884,15 @@ BuildConfig::configureBuild() {
std::string srcs;
for (const fs::path& sourceFilePath : sourceFilePaths) {
if (sourceFilePath != mainSource && isMainSource(sourceFilePath)) {
logger::warn(
Diag::warn(
"source file `{}` is named `main` but is not located directly in the "
"`src/` directory. "
"This file will not be treated as the program's entry point. "
"Move it directly to 'src/' if intended as such.",
sourceFilePath.string()
);
} else if (sourceFilePath != libSource && isLibSource(sourceFilePath)) {
logger::warn(
Diag::warn(
"source file `{}` is named `lib` but is not located directly in the "
"`src/` directory. "
"This file will not be treated as a hasLibraryTarget. "
Expand Down Expand Up @@ -982,16 +983,16 @@ emitMakefile(
bool buildProj = false;
bool buildCompDb = false;
if (config.makefileIsUpToDate()) {
logger::debug("Makefile is up to date");
spdlog::debug("Makefile is up to date");
} else {
logger::debug("Makefile is NOT up to date");
spdlog::debug("Makefile is NOT up to date");
buildProj = true;
}
if (profile.compDb) {
if (config.compdbIsUpToDate()) {
logger::debug("compile_commands.json is up to date");
spdlog::debug("compile_commands.json is up to date");
} else {
logger::debug("compile_commands.json is NOT up to date");
spdlog::debug("compile_commands.json is NOT up to date");
buildCompDb = true;
}
}
Expand Down Expand Up @@ -1024,10 +1025,10 @@ emitCompdb(
Try(config.installDeps(includeDevDeps));

if (config.compdbIsUpToDate()) {
logger::debug("compile_commands.json is up to date");
spdlog::debug("compile_commands.json is up to date");
return Ok(config.outBasePath);
}
logger::debug("compile_commands.json is NOT up to date");
spdlog::debug("compile_commands.json is NOT up to date");

Try(config.configureBuild());
std::ofstream ofs(config.outBasePath / "compile_commands.json");
Expand Down
27 changes: 25 additions & 2 deletions src/Cabin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
#include "Algos.hpp"
#include "Cli.hpp"
#include "Cmd.hpp"
#include "Logger.hpp"
#include "Diag.hpp"
#include "Rustify/Result.hpp"
#include "TermColor.hpp"

#include <cstdlib>
#include <spdlog/cfg/env.h>
#include <string>
#include <utility>

Expand Down Expand Up @@ -76,12 +78,33 @@ colorizeAnyhowError(std::string s) {
return s;
}

static void
warnUnusedLogEnv() {
#if SPDLOG_VERSION > 11500
if (std::getenv("SPDLOG_LEVEL")) {
logger::warn("SPDLOG_LEVEL is set but not used. Use CABIN_LOG instead.");
}
#else
if (std::getenv("CABIN_LOG")) {
Diag::warn("CABIN_LOG is set but not used. Use SPDLOG_LEVEL instead.");
}
#endif
}

Result<void, void>
cabinMain(int argc, char* argv[]) noexcept { // NOLINT(*-avoid-c-arrays)
// Set up logger
spdlog::cfg::load_env_levels(
#if SPDLOG_VERSION > 11500
"CABIN_LOG"
#endif
);
warnUnusedLogEnv();

return getCli()
.parseArgs(argc, argv)
.map_err([](const auto& e) { return colorizeAnyhowError(e->what()); })
.map_err([](std::string e) { logger::error("{}", std::move(e)); });
.map_err([](std::string e) { Diag::error("{}", std::move(e)); });
}

} // namespace cabin
8 changes: 4 additions & 4 deletions src/Cli.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Cli.hpp"

#include "Algos.hpp"
#include "Logger.hpp"
#include "Diag.hpp"
#include "Rustify/Result.hpp"
#include "TermColor.hpp"

Expand Down Expand Up @@ -387,13 +387,13 @@ Cli::handleGlobalOpts(
return getCli().printHelp({}).map([] { return Return; });
}
} else if (arg == "-v" || arg == "--verbose") {
logger::setLevel(logger::Level::Debug);
setDiagLevel(DiagLevel::Verbose);
return Ok(Continue);
} else if (arg == "-vv") {
logger::setLevel(logger::Level::Trace);
setDiagLevel(DiagLevel::VeryVerbose);
return Ok(Continue);
} else if (arg == "-q" || arg == "--quiet") {
logger::setLevel(logger::Level::Off);
setDiagLevel(DiagLevel::Off);
return Ok(Continue);
} else if (arg == "--color") {
Ensure(itr + 1 < end, "missing argument for `--color`");
Expand Down
8 changes: 4 additions & 4 deletions src/Cmd/Add.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Add.hpp"

#include "../Cli.hpp"
#include "../Logger.hpp"
#include "../Diag.hpp"
#include "../Manifest.hpp"
#include "../Rustify/Result.hpp"

Expand Down Expand Up @@ -59,7 +59,7 @@ handleDependency(
std::unordered_set<std::string_view>& newDeps, const std::string_view dep
) {
if (newDeps.contains(dep)) {
logger::warn("The dependency `{}` is already in the cabin.toml", dep);
Diag::warn("The dependency `{}` is already in the cabin.toml", dep);
return;
}
newDeps.insert(dep);
Expand All @@ -70,7 +70,7 @@ getDependencyGitUrl(const std::string_view dep) {
if (dep.find("://") == std::string_view::npos) {
// Check if at least in "user/repo" format.
if (dep.find('/') == std::string_view::npos) {
logger::error("Invalid dependency: {}", dep);
Diag::error("Invalid dependency: {}", dep);
return "";
}

Expand Down Expand Up @@ -160,7 +160,7 @@ addDependencyToManifest(
std::ofstream ofs(manifestPath);
ofs << data;

logger::info("Added", "to the cabin.toml");
Diag::info("Added", "to the cabin.toml");
return Ok();
}

Expand Down
8 changes: 4 additions & 4 deletions src/Cmd/Build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "../BuildConfig.hpp"
#include "../Cli.hpp"
#include "../Command.hpp"
#include "../Logger.hpp"
#include "../Diag.hpp"
#include "../Manifest.hpp"
#include "../Parallelism.hpp"
#include "Common.hpp"
Expand Down Expand Up @@ -49,7 +49,7 @@ runBuildCommand(
ExitStatus exitStatus = Try(execCmd(checkUpToDateCmd));
if (!exitStatus.success()) {
// If `targetName` is not up-to-date, compile it.
logger::info(
Diag::info(
"Compiling", "{} v{} ({})", targetName,
manifest.package.version.toString(),
manifest.path.parent_path().string()
Expand Down Expand Up @@ -85,7 +85,7 @@ buildImpl(const Manifest& manifest, std::string& outDir, const bool isDebug) {
const Profile& profile =
isDebug ? manifest.profiles.at("dev") : manifest.profiles.at("release");

logger::info(
Diag::info(
"Finished", "`{}` profile [{}] target(s) in {:.2f}s",
modeToProfile(isDebug), profile.toString(), elapsed.count()
);
Expand Down Expand Up @@ -141,7 +141,7 @@ buildMain(const CliArgsView args) {
// Build compilation database
const std::string outDir =
Try(emitCompdb(manifest, isDebug, /*includeDevDeps=*/false));
logger::info("Generated", "{}/compile_commands.json", outDir);
Diag::info("Generated", "{}/compile_commands.json", outDir);
return Ok();
}

Expand Down
4 changes: 2 additions & 2 deletions src/Cmd/Clean.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Clean.hpp"

#include "../Cli.hpp"
#include "../Logger.hpp"
#include "../Diag.hpp"
#include "../Manifest.hpp"
#include "../Rustify/Result.hpp"

Expand Down Expand Up @@ -53,7 +53,7 @@ cleanMain(CliArgsView args) noexcept {
}

if (fs::exists(outDir)) {
logger::info("Removing", "{}", fs::canonical(outDir).string());
Diag::info("Removing", "{}", fs::canonical(outDir).string());
fs::remove_all(outDir);
}
return Ok();
Expand Down
Loading

0 comments on commit 4a0f803

Please sign in to comment.