MLIR  20.0.0git
Logging.cpp
Go to the documentation of this file.
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 
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  "{0}[{1:%H:%M:%S.%L}] {2}\n",
40  logLevelIndicators[static_cast<unsigned>(logLevel)], timestamp, message);
41  llvm::errs().flush();
42 }
This class represents the main interface for logging, and allows for filtering logging based on diffe...
Definition: Logging.h:23
Level
The level of significance for a log message.
Definition: Logging.h:26
static void setLogLevel(Level logLevel)
Set the severity level of the logger.
Definition: Logging.cpp:17
Include the generated interface declarations.