| 1 | //===- Logging.cpp --------------------------------------------------------===// |
|---|---|
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "mlir/Tools/lsp-server-support/Logging.h" |
| 10 | #include "llvm/Support/Chrono.h" |
| 11 | #include "llvm/Support/ManagedStatic.h" |
| 12 | #include "llvm/Support/raw_ostream.h" |
| 13 | |
| 14 | using namespace mlir; |
| 15 | using namespace mlir::lsp; |
| 16 | |
| 17 | void Logger::setLogLevel(Level logLevel) { get().logLevel = logLevel; } |
| 18 | |
| 19 | Logger &Logger::get() { |
| 20 | static Logger logger; |
| 21 | return logger; |
| 22 | } |
| 23 | |
| 24 | void Logger::log(Level logLevel, const char *fmt, |
| 25 | const llvm::formatv_object_base &message) { |
| 26 | Logger &logger = get(); |
| 27 | |
| 28 | // Ignore messages with log levels below the current setting in the logger. |
| 29 | if (logLevel < logger.logLevel) |
| 30 | return; |
| 31 | |
| 32 | // An indicator character for each log level. |
| 33 | const char *logLevelIndicators = "DIE"; |
| 34 | |
| 35 | // Format the message and print to errs. |
| 36 | llvm::sys::TimePoint<> timestamp = std::chrono::system_clock::now(); |
| 37 | std::lock_guard<std::mutex> logGuard(logger.mutex); |
| 38 | llvm::errs() << llvm::formatv( |
| 39 | Fmt: "{0}[{1:%H:%M:%S.%L}] {2}\n", |
| 40 | Vals: logLevelIndicators[static_cast<unsigned>(logLevel)], Vals&: timestamp, Vals: message); |
| 41 | llvm::errs().flush(); |
| 42 | } |
| 43 |
