Loading [MathJax]/extensions/tex2jax.js
CIRCT 21.0.0git
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 "fmt/color.h"
19#include "fmt/core.h"
20
21#include <iostream>
22#include <mutex>
23
24using namespace esi;
25
26void TSLogger::log(Level level, const std::string &subsystem,
27 const std::string &msg,
28 const std::map<std::string, std::any> *details) {
29 std::scoped_lock<std::mutex> lock(mutex);
30 logImpl(level, subsystem, msg, details);
31}
32
34 : TSLogger(minLevel <= Level::Debug, minLevel <= Level::Trace),
35 minLevel(minLevel), outStream(std::cout), errorStream(std::cerr) {}
36
37void StreamLogger::logImpl(Level level, const std::string &subsystem,
38 const std::string &msg,
39 const std::map<std::string, std::any> *details) {
40 if (level < minLevel)
41 return;
42 std::ostream &os = level == Level::Error ? errorStream : outStream;
43 unsigned indentSpaces = 0;
44
45 switch (level) {
46 case Level::Error:
47 os << "[ ERROR] ";
48 indentSpaces = 8;
49 break;
50 case Level::Warning:
51 os << "[WARNING] ";
52 indentSpaces = 10;
53 break;
54 case Level::Info:
55 os << "[ INFO] ";
56 indentSpaces = 7;
57 break;
58 case Level::Debug:
59 os << "[ DEBUG] ";
60 indentSpaces = 8;
61 break;
62 case Level::Trace:
63 os << "[ TRACE] ";
64 indentSpaces = 8;
65 break;
66 }
67
68 if (!subsystem.empty()) {
69 os << "[" << subsystem << "] ";
70 indentSpaces += subsystem.size() + 3;
71 }
72 os << msg << std::endl;
73
74 if (details) {
75 std::string indent(indentSpaces, ' ');
76 for (const auto &detail : *details)
77 os << indent << detail.first << ": " << toString(detail.second) << "\n";
78 }
79 os.flush();
80}
81
83 : TSLogger(minLevel <= Level::Debug, minLevel <= Level::Trace),
84 minLevel(minLevel) {}
85
86void ConsoleLogger::logImpl(Level level, const std::string &subsystem,
87 const std::string &msg,
88 const std::map<std::string, std::any> *details) {
89 if (level < minLevel)
90 return;
91 FILE *os = level == Level::Error ? stderr : stdout;
92 unsigned indentSpaces = 0;
93
94 switch (level) {
95 case Level::Error:
96 fmt::print(os, fmt::fg(fmt::color::red), "[ ERROR] ");
97 indentSpaces = 8;
98 break;
99 case Level::Warning:
100 fmt::print(os, fmt::fg(fmt::color::yellow), "[WARNING] ");
101 indentSpaces = 10;
102 break;
103 case Level::Info:
104 fmt::print(os, fmt::fg(fmt::color::dark_green), "[ INFO] ");
105 indentSpaces = 7;
106 break;
107 case Level::Debug:
108 fmt::print(os, fmt::fg(fmt::color::beige), "[ DEBUG] ");
109 indentSpaces = 8;
110 break;
111 case Level::Trace:
112 fmt::print(os, fmt::fg(fmt::color::burly_wood), "[ TRACE] ");
113 indentSpaces = 8;
114 break;
115 }
116
117 if (!subsystem.empty()) {
118 fmt::print(os, "[{}]", subsystem);
119 indentSpaces += subsystem.size() + 3;
120 }
121 fmt::print(os, " {}", msg);
122 fmt::print(os, "\n");
123
124 if (details) {
125 std::string indent(indentSpaces, ' ');
126 for (const auto &detail : *details)
127 fmt::print(os, "{} {}: {}\n", indent, detail.first,
128 toString(detail.second));
129 }
130}
131
132std::string esi::toString(const std::any &value) {
133 if (value.type() == typeid(std::string))
134 return std::any_cast<std::string>(value);
135 if (value.type() == typeid(int))
136 return std::to_string(std::any_cast<int>(value));
137 if (value.type() == typeid(long))
138 return std::to_string(std::any_cast<long>(value));
139 if (value.type() == typeid(unsigned))
140 return std::to_string(std::any_cast<unsigned>(value));
141 if (value.type() == typeid(unsigned long))
142 return std::to_string(std::any_cast<unsigned long>(value));
143 if (value.type() == typeid(bool))
144 return std::any_cast<bool>(value) ? "true" : "false";
145 if (value.type() == typeid(double))
146 return std::to_string(std::any_cast<double>(value));
147 if (value.type() == typeid(float))
148 return std::to_string(std::any_cast<float>(value));
149 if (value.type() == typeid(const char *))
150 return std::string(std::any_cast<const char *>(value));
151 if (value.type() == typeid(char))
152 return std::string(1, std::any_cast<char>(value));
153 if (value.type() == typeid(MessageData))
154 return std::any_cast<MessageData>(value).toHex();
155 return "<unknown>";
156}
ConsoleLogger(Level minLevel)
Create a stream logger that logs to stdout, stderr.
Definition Logging.cpp:82
Level minLevel
The minimum log level to emit.
Definition Logging.h:215
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:86
A logical chunk of data representing serialized data.
Definition Common.h:103
Level minLevel
The minimum log level to emit.
Definition Logging.h:196
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:37
std::ostream & outStream
Everything except errors goes here.
Definition Logging.h:199
std::ostream & errorStream
Just for errors.
Definition Logging.h:201
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:185
A thread-safe logger which calls functions implemented by subclasses.
Definition Logging.h:162
std::mutex mutex
Mutex to protect the stream from interleaved logging writes.
Definition Logging.h:177
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:26
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:132