LLVM OpenMP
Logging.cpp
Go to the documentation of this file.
1//===- Logging.cpp - General logging class implementation -------*- C++ -*-===//
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/// \file
10/// Implements ompTest-tailored logging.
11///
12//===----------------------------------------------------------------------===//
13
14#include "Logging.h"
15#include "EnvHelper.h"
16
17using namespace omptest;
18using namespace logging;
19
20Logger::Logger(Level LogLevel, std::ostream &OutStream, bool FormatOutput)
21 : LoggingLevel(LogLevel), OutStream(OutStream), FormatOutput(FormatOutput) {
22 if (auto EnvVal = getBoolEnvironmentVariable("OMPTEST_LOG_COLORED");
23 EnvVal.has_value())
24 FormatOutput = EnvVal.value();
25 // Flush any buffered output
26 OutStream << std::flush;
27}
28
30 // Flush any buffered output
31 OutStream << std::flush;
32}
33
44
45const char *logging::to_string(Level LogLevel) {
46 switch (LogLevel) {
48 return "Diagnostic";
49 case Level::Info:
50 return "Info";
51 case Level::Warning:
52 return "Warning";
53 case Level::Error:
54 return "Error";
55 case Level::Critical:
56 return "Critical";
57 case Level::Silent:
58 return "Silent";
59 default:
60 assert(false && "Requested string representation for unknown LogLevel");
61 return "UNKNOWN";
62 }
63}
64
65std::string logging::getFormatSequence(Level LogLevel) {
66 auto Options = AggregatedFormatOptions[LogLevel];
67 std::stringstream SS{"\033["};
68 SS << "\033[";
69 if (!Options.empty()) {
70 for (auto &Option : AggregatedFormatOptions[LogLevel])
71 SS << int(Option) << ';';
72 SS.seekp(-1, SS.cur);
73 SS << 'm';
74 } else {
75 // Fallback to None / reset formatting
76 SS << "0m";
77 }
78 return SS.str();
79}
80
81std::string logging::format(const std::string &Message, FormatOption Option) {
82 std::stringstream SS{"\033["};
83 SS << "\033[";
84 SS << int(Option) << 'm' << Message << "\033[0m";
85 return SS.str();
86}
87
88std::string logging::format(const std::string &Message,
89 std::set<FormatOption> Options) {
90 std::stringstream SS{"\033["};
91 SS << "\033[";
92 for (auto &Option : Options)
93 SS << int(Option) << ';';
94 SS.seekp(-1, SS.cur);
95 SS << 'm' << Message << "\033[0m";
96 return SS.str();
97}
98
99void Logger::log(const std::string &Message, Level LogLevel) const {
100 // Serialize logging
101 std::lock_guard<std::mutex> Lock(LogMutex);
102
103 if (LoggingLevel > LogLevel)
104 return;
105
106 if (FormatOutput) {
107 OutStream << getFormatSequence(LogLevel) << '[' << to_string(LogLevel)
108 << "] " << Message << getFormatSequence() << std::endl;
109 } else {
110 OutStream << '[' << to_string(LogLevel) << "] " << Message << std::endl;
111 }
112}
113
114void Logger::logEventMismatch(const std::string &Message,
116 Level LogLevel) const {
117 // Serialize logging
118 std::lock_guard<std::mutex> Lock(LogMutex);
119 if (LoggingLevel > LogLevel)
120 return;
121
122 if (FormatOutput) {
123 OutStream << getFormatSequence(LogLevel) << '[' << to_string(LogLevel)
124 << "] " << getFormatSequence()
125 << format(Message, AggregatedFormatOptions[LogLevel])
126 << "\n\tOffending event name='"
127 << format(OffendingEvent.getEventName(),
129 << "'\n\tOffending='"
130 << format(OffendingEvent.toString(),
132 << '\'' << std::endl;
133 } else {
134 OutStream << '[' << to_string(LogLevel) << "] " << Message
135 << "\n\tOffending event name='" << OffendingEvent.getEventName()
136 << "'\n\tOffending='" << OffendingEvent.toString() << '\''
137 << std::endl;
138 }
139}
140
141void Logger::logEventMismatch(const std::string &Message,
144 Level LogLevel) const {
145 // Serialize logging
146 std::lock_guard<std::mutex> Lock(LogMutex);
147 if (LoggingLevel > LogLevel)
148 return;
149
150 if (FormatOutput) {
151 OutStream << getFormatSequence(LogLevel) << '[' << to_string(LogLevel)
152 << "] " << Message << getFormatSequence()
153 << "\n\tExpected event name='"
154 << format(ExpectedEvent.getEventName(),
156 << "' observe='"
157 << format(to_string(ExpectedEvent.getEventExpectedState()),
159 << "'\n\tObserved event name='"
160 << format(ObservedEvent.getEventName(),
162 << "'\n\tExpected='"
163 << format(ExpectedEvent.toString(),
165 << "'\n\tObserved='"
166 << format(ObservedEvent.toString(),
168 << '\'' << std::endl;
169 } else {
170 OutStream << '[' << to_string(LogLevel) << "] " << Message
171 << "\n\tExpected event name='" << ExpectedEvent.getEventName()
172 << "' observe='"
173 << to_string(ExpectedEvent.getEventExpectedState())
174 << "'\n\tObserved event name='" << ObservedEvent.getEventName()
175 << "'\n\tExpected='" << ExpectedEvent.toString()
176 << "'\n\tObserved='" << ObservedEvent.toString() << '\''
177 << std::endl;
178 }
179}
180
181void Logger::setFormatOutput(bool Enabled) { FormatOutput = Enabled; }
182
183Level Logger::getLoggingLevel() const { return LoggingLevel; }
184
185void Logger::setLoggingLevel(Level LogLevel) { LoggingLevel = LogLevel; }
Provides environment helpers shared between a couple of places.
std::map< Level, std::set< FormatOption > > AggregatedFormatOptions
Definition Logging.cpp:34
Provides ompTest-tailored logging, with log-levels and formatting/coloring.
void log(const std::string &Message, Level LogLevel) const
Log the given message to the output.
Definition Logging.cpp:99
Logger(Level LogLevel=Level::Warning, std::ostream &OutStream=std::cerr, bool FormatOutput=true)
Definition Logging.cpp:20
void setFormatOutput(bool Enabled)
Set if output is being formatted (e.g. colored).
Definition Logging.cpp:181
Level getLoggingLevel() const
Return the current (minimum) Logging Level.
Definition Logging.cpp:183
void logEventMismatch(const std::string &Message, const omptest::OmptAssertEvent &OffendingEvent, Level LogLevel=Level::Error) const
Log a single event mismatch.
Definition Logging.cpp:114
void setLoggingLevel(Level LogLevel)
Set the (minimum) Logging Level.
Definition Logging.cpp:185
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_string_handle unsigned long long ITT_FORMAT lu const __itt_domain __itt_id __itt_string_handle __itt_metadata_type size_t void ITT_FORMAT p const __itt_domain __itt_id __itt_string_handle const wchar_t size_t ITT_FORMAT lu const __itt_domain __itt_id __itt_relation __itt_id ITT_FORMAT p const wchar_t int ITT_FORMAT __itt_group_mark d int
std::string getFormatSequence(Level LogLevel=Level::Default)
Returns the format options as escaped sequence, for the given logging level.
Definition Logging.cpp:65
const char * to_string(Level LogLevel)
Returns a string representation of the given logging level.
Definition Logging.cpp:45
std::string format(const std::string &Message, FormatOption Option)
Format the given message with the provided option(s) and return it.
Definition Logging.cpp:81
std::optional< bool > getBoolEnvironmentVariable(const char *VariableName)
Load the value of a given boolean environmental variable.
Definition EnvHelper.h:23
Assertion event struct, provides statically callable CTORs.