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 "llvm/Support/LSP/Logging.h"
10#include "llvm/Support/Chrono.h"
11#include "llvm/Support/raw_ostream.h"
12
13using namespace llvm;
14using namespace llvm::lsp;
15
16void Logger::setLogLevel(Level LogLevel) { get().LogLevel = LogLevel; }
17
18Logger &Logger::get() {
19 static Logger Logger;
20 return Logger;
21}
22
23void Logger::log(Level LogLevel, const char *Fmt,
24 const llvm::formatv_object_base &Message) {
25 Logger &Logger = get();
26
27 // Ignore messages with log levels below the current setting in the logger.
28 if (LogLevel < Logger.LogLevel)
29 return;
30
31 // An indicator character for each log level.
32 const char *LogLevelIndicators = "DIE";
33
34 // Format the message and print to errs.
35 llvm::sys::TimePoint<> Timestamp = std::chrono::system_clock::now();
36 std::lock_guard<std::mutex> LogGuard(Logger.Mutex);
37 llvm::errs() << llvm::formatv(
38 Fmt: "{0}[{1:%H:%M:%S.%L}] {2}\n",
39 Vals: LogLevelIndicators[static_cast<unsigned>(LogLevel)], Vals&: Timestamp, Vals: Message);
40 llvm::errs().flush();
41}
42