aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/LSP/Logging.cpp
blob: b36621ae1c6c65e2a6c4aecbf53f5430075d9be7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//===- 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();
}