CIRCT  20.0.0git
Logging.cpp
Go to the documentation of this file.
1 //===- Logging.cpp - ESI logging system API implementation ----------------===//
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 // DO NOT EDIT!
10 // This file is distributed as part of an ESI package. The source for this file
11 // should always be modified within CIRCT (lib/dialect/ESI/runtime/cpp/).
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "esi/Logging.h"
16 #include "esi/Common.h"
17 
18 #include <iostream>
19 #include <mutex>
20 
21 using namespace esi;
22 
23 void TSLogger::log(Level level, const std::string &subsystem,
24  const std::string &msg,
25  const std::map<std::string, std::any> *details) {
26  std::scoped_lock<std::mutex> lock(mutex);
27  logImpl(level, subsystem, msg, details);
28 }
29 
31  : TSLogger(minLevel == Level::Debug), minLevel(minLevel),
32  outStream(std::cout), errorStream(std::cerr) {}
33 
34 void StreamLogger::logImpl(Level level, const std::string &subsystem,
35  const std::string &msg,
36  const std::map<std::string, std::any> *details) {
37  if (level < minLevel)
38  return;
39  std::ostream &os = level == Level::Error ? errorStream : outStream;
40  unsigned indentSpaces = 0;
41 
42  switch (level) {
43  case Level::Error:
44  os << "[ERROR] ";
45  indentSpaces = 8;
46  break;
47  case Level::Warning:
48  os << "[WARNING] ";
49  indentSpaces = 10;
50  break;
51  case Level::Info:
52  os << "[INFO] ";
53  indentSpaces = 7;
54  break;
55  case Level::Debug:
56  os << "[DEBUG] ";
57  indentSpaces = 8;
58  break;
59  }
60 
61  if (!subsystem.empty()) {
62  os << "[" << subsystem << "] ";
63  indentSpaces += subsystem.size() + 3;
64  }
65  os << msg << std::endl;
66 
67  if (!details)
68  return;
69  std::string indent(indentSpaces, ' ');
70  for (const auto &detail : *details)
71  os << indent << detail.first << ": " << toString(detail.second) << "\n";
72 }
73 
74 std::string esi::toString(const std::any &value) {
75  if (value.type() == typeid(std::string))
76  return std::any_cast<std::string>(value);
77  if (value.type() == typeid(int))
78  return std::to_string(std::any_cast<int>(value));
79  if (value.type() == typeid(long))
80  return std::to_string(std::any_cast<long>(value));
81  if (value.type() == typeid(unsigned))
82  return std::to_string(std::any_cast<unsigned>(value));
83  if (value.type() == typeid(unsigned long))
84  return std::to_string(std::any_cast<unsigned long>(value));
85  if (value.type() == typeid(bool))
86  return std::any_cast<bool>(value) ? "true" : "false";
87  if (value.type() == typeid(double))
88  return std::to_string(std::any_cast<double>(value));
89  if (value.type() == typeid(float))
90  return std::to_string(std::any_cast<float>(value));
91  if (value.type() == typeid(const char *))
92  return std::string(std::any_cast<const char *>(value));
93  if (value.type() == typeid(char))
94  return std::string(1, std::any_cast<char>(value));
95  if (value.type() == typeid(MessageData))
96  return std::any_cast<MessageData>(value).toHex();
97  return "<unknown>";
98 }
A logical chunk of data representing serialized data.
Definition: Common.h:92
Level minLevel
The minimum log level to emit.
Definition: Logging.h:160
void logImpl(Level level, const std::string &subsystem, const std::string &msg, const std::map< std::string, std::any > *details) override
Subclasses must implement this method to log messages.
Definition: Logging.cpp:34
std::ostream & outStream
Everything except errors goes here.
Definition: Logging.h:163
std::ostream & errorStream
Just for errors.
Definition: Logging.h:165
StreamLogger(Level minLevel, std::ostream &out, std::ostream &error)
Create a stream logger that logs to the given output stream and error output stream.
Definition: Logging.h:149
A thread-safe logger which calls functions implemented by subclasses.
Definition: Logging.h:126
std::mutex mutex
Mutex to protect the stream from interleaved logging writes.
Definition: Logging.h:141
virtual void logImpl(Level level, const std::string &subsystem, const std::string &msg, const std::map< std::string, std::any > *details)=0
Subclasses must implement this method to log messages.
void log(Level level, const std::string &subsystem, const std::string &msg, const std::map< std::string, std::any > *details) override final
Grabs the lock and calls logImpl.
Definition: Logging.cpp:23
Definition: esi.py:1
std::string toString(const std::any &a)
'Stringify' a std::any. This is used to log std::any values by some loggers.
Definition: Logging.cpp:74