diff --git a/.gitmodules b/.gitmodules index cf6f4f6e..aabaee5b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -8,11 +8,6 @@ url = https://github.com/libuv/libuv.git branch = v1.x -[submodule "deps/spdlog"] - path = deps/spdlog - url = https://github.com/gabime/spdlog.git - branch = v1.x - [submodule "deps/toml"] path = deps/toml url = https://github.com/marzer/tomlplusplus.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d4eb5dc..2b63586e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,7 +63,6 @@ target_link_libraries(clice-core PUBLIC # clice executable add_subdirectory(${CMAKE_SOURCE_DIR}/deps/libuv) -add_subdirectory(${CMAKE_SOURCE_DIR}/deps/spdlog) set(CLICE_SERVER_SOURCES ${CMAKE_SOURCE_DIR}/src/main.cpp) file(GLOB_RECURSE SRC_FILES "${CMAKE_SOURCE_DIR}/src/Server/*.cpp") @@ -73,9 +72,8 @@ add_executable(clice ${CLICE_SERVER_SOURCES}) target_include_directories(clice PRIVATE "${CMAKE_SOURCE_DIR}/deps/toml/include" "${CMAKE_SOURCE_DIR}/deps/libuv/include" - "${CMAKE_SOURCE_DIR}/deps/spdlog/include" ) -target_link_libraries(clice PRIVATE clice-core uv spdlog::spdlog) +target_link_libraries(clice PRIVATE clice-core uv) # clice tests @@ -87,13 +85,11 @@ if(CLICE_ENABLE_TEST) add_executable(clice-tests ${CLICE_TEST_SOURCES}) target_include_directories(clice-tests PRIVATE - "${CMAKE_SOURCE_DIR}/deps/spdlog/include" "${CMAKE_SOURCE_DIR}/deps/googletest/googletest/include" ) target_link_libraries(clice-tests PRIVATE clice-core - gtest_main - spdlog::spdlog + gtest_main ) endif() diff --git a/deps/spdlog b/deps/spdlog deleted file mode 160000 index a3a0c9d6..00000000 --- a/deps/spdlog +++ /dev/null @@ -1 +0,0 @@ -Subproject commit a3a0c9d66386962fcaf0178fcae03ac77c1e0257 diff --git a/include/Server/Logger.h b/include/Server/Logger.h index fe86a1ca..c3af42b2 100644 --- a/include/Server/Logger.h +++ b/include/Server/Logger.h @@ -1,46 +1,50 @@ #pragma once -#include -#include -#include - #include #include #include -namespace clice::logger { +namespace clice::log { -inline auto init(std::string_view type, std::string_view filepath) { - if(type == "file") { - llvm::SmallString<128> temp; - temp.append(path::parent_path(path::parent_path(filepath))); - path::append(temp, "logs"); +enum class Level { + INFO, + ERROR, + WARN, + DEBUG, + TRACE, +}; - auto error = llvm::sys::fs::make_absolute(temp); - auto now = std::chrono::system_clock::now(); - std::time_t now_c = std::chrono::system_clock::to_time_t(now); - std::tm now_tm = *std::localtime(&now_c); - - std::ostringstream timeStream; - timeStream << std::put_time(&now_tm, "%Y-%m-%d_%H-%M-%S"); - - std::string logFileName = "clice_" + timeStream.str() + ".log"; - path::append(temp, logFileName); - - auto logger = spdlog::basic_logger_mt("clice", temp.c_str()); - logger->flush_on(spdlog::level::trace); - spdlog::set_default_logger(logger); - } else if(type == "console") { - auto logger = spdlog::stdout_color_mt("clice"); - logger->flush_on(spdlog::level::trace); - spdlog::set_default_logger(logger); - } +template +void log(Level level, std::string_view fmt, Args&&... args) { + namespace chrono = std::chrono; + auto now = chrono::floor(chrono::system_clock::now()); + auto time = chrono::zoned_time(chrono::current_zone(), now); + auto tag = [&] { + switch(level) { + case Level::INFO: return "\033[32mINFO\033[0m"; // Green + case Level::ERROR: return "\033[31mERROR\033[0m"; // Red + case Level::WARN: return "\033[33mWARN\033[0m"; // Yellow + case Level::DEBUG: return "\033[36mDEBUG\033[0m"; // Cyan + case Level::TRACE: return "\033[35mTRACE\033[0m"; // Magenta + } + }(); + llvm::errs() << std::format("[{0:%Y-%m-%d %H:%M:%S}] [{1}] ", time, tag) + << std::vformat(fmt, std::make_format_args(args...)) << "\n"; } -using spdlog::info; -using spdlog::error; -using spdlog::warn; -using spdlog::debug; -using spdlog::trace; +template +void info(std::format_string fmt, Args&&... args) { + log::log(Level::INFO, fmt.get(), std::forward(args)...); +} -} // namespace clice::logger +template +void error(std::format_string fmt, Args&&... args) { + log::log(Level::ERROR, fmt.get(), std::forward(args)...); +} + +template +void warn(std::format_string fmt, Args&&... args) { + log::log(Level::WARN, fmt.get(), std::forward(args)...); +} + +} // namespace clice::log diff --git a/src/Server/Async.cpp b/src/Server/Async.cpp index c21d1067..96b5a724 100644 --- a/src/Server/Async.cpp +++ b/src/Server/Async.cpp @@ -35,7 +35,7 @@ public: if(str.consume_front("Content-Length: ") && !str.consumeInteger(10, length) && str.consume_front("\r\n\r\n") && str.size() >= length) { auto result = str.substr(0, length); - pos = str.end() - buffer.begin(); + pos = result.end() - buffer.begin(); return result; } return {}; diff --git a/src/Server/Server.cpp b/src/Server/Server.cpp index 2d272f1a..101f825b 100644 --- a/src/Server/Server.cpp +++ b/src/Server/Server.cpp @@ -41,8 +41,6 @@ Server::Server() { } void Server::run(int argc, const char** argv) { - logger::init("console", argv[0]); - auto dispatch = [this](json::Value value) -> async::promise { assert(value.kind() == json::Value::Object); auto object = value.getAsObject(); @@ -52,18 +50,18 @@ void Server::run(int argc, const char** argv) { auto params = object->get("params"); if(auto id = object->get("id")) { if(auto iter = requests.find(name); iter != requests.end()) { - logger::info("Request: {0}", name.str()); + log::info("Request: {0}", name.str()); co_await iter->second(std::move(*id), params ? std::move(*params) : json::Value(nullptr)); } else { - logger::error("Unknown request: {0}", name.str()); + log::error("Unknown request: {0}", name.str()); } } else { if(auto iter = notifications.find(name); iter != notifications.end()) { - logger::info("Notification: {0}", name.str()); + log::info("Notification: {0}", name.str()); co_await iter->second(params ? std::move(*params) : json::Value(nullptr)); } else { - logger::error("Unknown notification: {0}", name.str()); + log::error("Unknown notification: {0}", name.str()); } } } @@ -245,7 +243,7 @@ async::promise Server::onRangeFormatting(json::Value id, async::promise Server::updatePCH(llvm::StringRef filepath, llvm::StringRef content, llvm::ArrayRef args) { - logger::info("Start building PCH for {0}", filepath.str()); + log::info("Start building PCH for {0}", filepath.str()); clang::PreambleBounds bounds = {0, 0}; std::string outpath = "/home/ykiko/C++/clice2/build/cache/xxx.pch"; co_await async::schedule_task([&] { @@ -255,7 +253,7 @@ async::promise Server::updatePCH(llvm::StringRef filepath, compiler.generatePCH(outpath, bounds.Size, bounds.PreambleEndsAtStartOfLine); } }); - logger::info("Build PCH success"); + log::info("Build PCH success"); auto preamble2 = content.substr(0, bounds.Size).str(); if(bounds.PreambleEndsAtStartOfLine) { @@ -284,7 +282,7 @@ async::promise Server::buildAST(llvm::StringRef filepath, llvm::StringRef auto& pch = pchs.at(filepath); - logger::info("Start building AST for {0}", filepath.str()); + log::info("Start building AST for {0}", filepath.str()); auto compiler = co_await async::schedule_task([&] { std::uint32_t boundSize = pch.preamble.size(); bool endAtStart = false; @@ -299,7 +297,7 @@ async::promise Server::buildAST(llvm::StringRef filepath, llvm::StringRef compiler->buildAST(); return compiler; }); - logger::info("Build AST success"); + log::info("Build AST success"); auto& unit = units[filepath]; unit.state = TranslationUnit::State::Ready; diff --git a/unittests/Test.h b/unittests/Test.h index f2601a04..b15ac3a8 100644 --- a/unittests/Test.h +++ b/unittests/Test.h @@ -3,7 +3,6 @@ #include #include -#include #include #include