LLVM OpenMP 22.0.0git
Logging.h
Go to the documentation of this file.
1//===- Logging.h - General logging class ------------------------*- 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/// Provides ompTest-tailored logging, with log-levels and formatting/coloring.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef OPENMP_TOOLS_OMPTEST_INCLUDE_LOGGING_H
15#define OPENMP_TOOLS_OMPTEST_INCLUDE_LOGGING_H
16
17#include "OmptAssertEvent.h"
18
19#include <iostream>
20#include <map>
21#include <mutex>
22#include <set>
23#include <sstream>
24#include <string>
25
26namespace omptest {
27namespace logging {
28
29enum class Level : uint32_t {
30 // Levels (Note: DEBUG may already be reserved)
32 Info = 20,
33 Warning = 30,
34 Error = 40,
36
37 // Types used for formatting options
42
43 // Suppress all prints
44 Silent = 0xFFFFFFFF
45};
46
47enum class FormatOption : uint32_t {
48 // General options
49 // Note: Bold is actually "BRIGHT" -- But it will be perceived as 'bold' font
50 // It is implicitly switching colors to the 'Light' variant
51 // Thus, it has -NO EFFECT- when already using a Light* color
52 None = 0,
53 Bold = 1,
54 Dim = 2,
56 Blink = 5,
58 Hidden = 8,
59 // Foreground colors
77 // Background colors
95};
96
97/// Returns a string representation of the given logging level.
98const char *to_string(Level LogLevel);
99
100/// Returns the format options as escaped sequence, for the given logging level
101std::string getFormatSequence(Level LogLevel = Level::Default);
102
103/// Format the given message with the provided option(s) and return it.
104/// Here formatting is only concerning control sequences using <Esc> character
105/// which can be obtained using '\e' (on console), '\033' or '\x1B'.
106std::string format(const std::string &Message, FormatOption Option);
107std::string format(const std::string &Message, std::set<FormatOption> Options);
108
109class Logger {
110public:
111 Logger(Level LogLevel = Level::Warning, std::ostream &OutStream = std::cerr,
112 bool FormatOutput = true);
113 ~Logger();
114
115 /// Log the given message to the output.
116 void log(const std::string &Message, Level LogLevel) const;
117
118 /// Log a single event mismatch.
119 void logEventMismatch(const std::string &Message,
121 Level LogLevel = Level::Error) const;
122
123 /// Log an event-pair mismatch.
124 void logEventMismatch(const std::string &Message,
127 Level LogLevel = Level::Error) const;
128
129 /// Set if output is being formatted (e.g. colored).
130 void setFormatOutput(bool Enabled);
131
132 /// Return the current (minimum) Logging Level.
133 Level getLoggingLevel() const;
134
135 /// Set the (minimum) Logging Level.
136 void setLoggingLevel(Level LogLevel);
137
138private:
139 /// The minimum logging level that is considered by the logger instance.
140 Level LoggingLevel;
141
142 /// The output stream used by the logger instance.
143 std::ostream &OutStream;
144
145 /// Determine if log messages are formatted using control sequences.
146 bool FormatOutput;
147
148 /// Mutex to ensure serialized logging
149 mutable std::mutex LogMutex;
150};
151
152} // namespace logging
153} // namespace omptest
154
155#endif
Contains assertion event constructors, for generally all observable events.
void log(const std::string &Message, Level LogLevel) const
Log the given message to the output.
Definition Logging.cpp:95
Logger(Level LogLevel=Level::Warning, std::ostream &OutStream=std::cerr, bool FormatOutput=true)
Definition Logging.cpp:19
void setFormatOutput(bool Enabled)
Set if output is being formatted (e.g. colored).
Definition Logging.cpp:177
Level getLoggingLevel() const
Return the current (minimum) Logging Level.
Definition Logging.cpp:179
void logEventMismatch(const std::string &Message, const omptest::OmptAssertEvent &OffendingEvent, Level LogLevel=Level::Error) const
Log a single event mismatch.
Definition Logging.cpp:110
void setLoggingLevel(Level LogLevel)
Set the (minimum) Logging Level.
Definition Logging.cpp:181
std::string getFormatSequence(Level LogLevel=Level::Default)
Returns the format options as escaped sequence, for the given logging level.
Definition Logging.cpp:61
const char * to_string(Level LogLevel)
Returns a string representation of the given logging level.
Definition Logging.cpp:41
std::string format(const std::string &Message, FormatOption Option)
Format the given message with the provided option(s) and return it.
Definition Logging.cpp:77
Assertion event struct, provides statically callable CTORs.