CIRCT 21.0.0git
Loading...
Searching...
No Matches
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(minLevel),
35 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 }
63
64 if (!subsystem.empty()) {
65 os << "[" << subsystem << "] ";
66 indentSpaces += subsystem.size() + 3;
67 }
68 os << msg << std::endl;
69
70 if (details) {
71 std::string indent(indentSpaces, ' ');
72 for (const auto &detail : *details)
73 os << indent << detail.first << ": " << toString(detail.second) << "\n";
74 }
75 os.flush();
76}
77
79 : TSLogger(minLevel == Level::Debug), minLevel(minLevel) {}
80
81void ConsoleLogger::logImpl(Level level, const std::string &subsystem,
82 const std::string &msg,
83 const std::map<std::string, std::any> *details) {
84 if (level < minLevel)
85 return;
86 FILE *os = level == Level::Error ? stderr : stdout;
87 unsigned indentSpaces = 0;
88
89 switch (level) {
90 case Level::Error:
91 fmt::print(os, fmt::fg(fmt::color::red), "[ ERROR] ");
92 indentSpaces = 8;
93 break;
94 case Level::Warning:
95 fmt::print(os, fmt::fg(fmt::color::yellow), "[WARNING] ");
96 indentSpaces = 10;
97 break;
98 case Level::Info:
99 fmt::print(os, fmt::fg(fmt::color::dark_green), "[ INFO] ");
100 indentSpaces = 7;
101 break;
102 case Level::Debug:
103 fmt::print(os, fmt::fg(fmt::color::beige), "[ DEBUG] ");
104 indentSpaces = 8;
105 break;
106 }
107
108 if (!subsystem.empty()) {
109 fmt::print(os, "[{}]", subsystem);
110 indentSpaces += subsystem.size() + 3;
111 }
112 fmt::print(os, " {}", msg);
113 fmt::print(os, "\n");
114
115 if (details) {
116 std::string indent(indentSpaces, ' ');
117 for (const auto &detail : *details)
118 fmt::print(os, "{} {}: {}\n", indent, detail.first,
119 toString(detail.second));
120 }
121}
122
123std::string esi::toString(const std::any &value) {
124 if (value.type() == typeid(std::string))
125 return std::any_cast<std::string>(value);
126 if (value.type() == typeid(int))
127 return std::to_string(std::any_cast<int>(value));
128 if (value.type() == typeid(long))
129 return std::to_string(std::any_cast<long>(value));
130 if (value.type() == typeid(unsigned))
131 return std::to_string(std::any_cast<unsigned>(value));
132 if (value.type() == typeid(unsigned long))
133 return std::to_string(std::any_cast<unsigned long>(value));
134 if (value.type() == typeid(bool))
135 return std::any_cast<bool>(value) ? "true" : "false";
136 if (value.type() == typeid(double))
137 return std::to_string(std::any_cast<double>(value));
138 if (value.type() == typeid(float))
139 return std::to_string(std::any_cast<float>(value));
140 if (value.type() == typeid(const char *))
141 return std::string(std::any_cast<const char *>(value));
142 if (value.type() == typeid(char))
143 return std::string(1, std::any_cast<char>(value));
144 if (value.type() == typeid(MessageData))
145 return std::any_cast<MessageData>(value).toHex();
146 return "<unknown>";
147}
ConsoleLogger(Level minLevel)
Create a stream logger that logs to stdout, stderr.
Definition Logging.cpp:78
Level minLevel
The minimum log level to emit.
Definition Logging.h:179
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:81
A logical chunk of data representing serialized data.
Definition Common.h:103
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:37
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: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:123