Files
llvm-project/llvm/lib/Support/LSP/Logging.cpp
Bertik23 a3a25996b1 [LLVM][MLIR] Move LSP server support library from MLIR into LLVM (#157885)
This is a second PR on this patch (first #155572), that fixes the
linking problem for `flang-aarch64-dylib` test.

The SupportLSP library was made a component library.

---

This PR moves the generic Language Server Protocol (LSP) server support
code that was copied from clangd into MLIR, into the LLVM tree so it can
be reused by multiple subprojects.

Centralizing the generic LSP support in LLVM lowers the barrier to
building new LSP servers across the LLVM ecosystem and avoids each
subproject maintaining its own copy.

The code originated in clangd and was copied into MLIR for its LSP
server. MLIR had this code seperate to be reused by all of their LSP
server. This PR relocates the MLIR copy into LLVM as a shared component
into LLVM/Support. If this is not a suitable place, please suggest a
better one.

A follow up to this move could be deduplication with the original clangd
implementation and converge on a single shared LSP support library used
by clangd, MLIR, and future servers.
What changes

mlir/include/mlir/Tools/lsp-server-support/{Logging, Protocol,
Transport}.h moved to llvm/include/llvm/Support/LSP
mlir/lib/Tools/lsp-server-support/{Logging, Protocol, Transport}.cpp
moved to llvm/lib/Support/LSP

and their namespace was changed from mlir to llvm

I ran clang-tidy --fix and clang-format on the whole moved files (last
two commits), as they are basically new files and should hold up to the
code style used by LLVM.

MLIR LSP servers where updated to include these files from their new
location and account for the namespace change.

This PR is made as part of the LLVM IR LSP project
([RFC](https://discourse.llvm.org/t/rfc-ir-visualization-with-vs-code-extension-using-an-lsp-server/87773))
2025-09-11 17:17:52 +00:00

42 lines
1.3 KiB
C++

//===- Logging.cpp --------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/LSP/Logging.h"
#include "llvm/Support/Chrono.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
using namespace llvm::lsp;
void Logger::setLogLevel(Level LogLevel) { get().LogLevel = LogLevel; }
Logger &Logger::get() {
static Logger Logger;
return Logger;
}
void Logger::log(Level LogLevel, const char *Fmt,
const llvm::formatv_object_base &Message) {
Logger &Logger = get();
// Ignore messages with log levels below the current setting in the logger.
if (LogLevel < Logger.LogLevel)
return;
// An indicator character for each log level.
const char *LogLevelIndicators = "DIE";
// Format the message and print to errs.
llvm::sys::TimePoint<> Timestamp = std::chrono::system_clock::now();
std::lock_guard<std::mutex> LogGuard(Logger.Mutex);
llvm::errs() << llvm::formatv(
"{0}[{1:%H:%M:%S.%L}] {2}\n",
LogLevelIndicators[static_cast<unsigned>(LogLevel)], Timestamp, Message);
llvm::errs().flush();
}