Debian Codegraph @

openmp/tools/omptest/include/Logging.h

raw
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
26 namespace omptest {
27 namespace logging {
28
29 enum class Level : uint32_t {
30 // Levels (Note: DEBUG may already be reserved)
31 Diagnostic = 10,
32 Info = 20,
33 Warning = 30,
34 Error = 40,
35 Critical = 50,
36
37 // Types used for formatting options
38 Default,
39 ExpectedEvent,
40 ObservedEvent,
41 OffendingEvent,
42
43 // Suppress all prints
44 Silent = 0xFFFFFFFF
45 };
46
47 enum 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,
55 Underlined = 4,
56 Blink = 5,
57 Inverted = 7,
58 Hidden = 8,
59 // Foreground colors
60 ColorDefault = 39,
61 ColorBlack = 30,
62 ColorRed = 31,
63 ColorGreen = 32,
64 ColorYellow = 33,
65 ColorBlue = 34,
66 ColorMagenta = 35,
67 ColorCyan = 36,
68 ColorLightGray = 37,
69 ColorDarkGray = 90,
70 ColorLightRed = 91,
71 ColorLightGreen = 92,
72 ColorLightYellow = 93,
73 ColorLightBlue = 94,
74 ColorLightMagenta = 95,
75 ColorLightCyan = 96,
76 ColorWhite = 97,
77 // Background colors
78 ColorBackgroundDefault = 49,
79 ColorBackgroundBlack = 40,
80 ColorBackgroundRed = 41,
81 ColorBackgroundGreen = 42,
82 ColorBackgroundYellow = 43,
83 ColorBackgroundBlue = 44,
84 ColorBackgroundMagenta = 45,
85 ColorBackgroundCyan = 46,
86 ColorBackgroundLightGray = 47,
87 ColorBackgroundDarkGray = 100,
88 ColorBackgroundLightRed = 101,
89 ColorBackgroundLightGreen = 102,
90 ColorBackgroundLightYellow = 103,
91 ColorBackgroundLightBlue = 104,
92 ColorBackgroundLightMagenta = 105,
93 ColorBackgroundLightCyan = 106,
94 ColorBackgroundWhite = 107
95 };
96
97 /// Returns a string representation of the given logging level.
98 const char *to_string(Level LogLevel);
99
100 /// Returns the format options as escaped sequence, for the given logging level
101 std::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'.
106 std::string format(const std::string &Message, FormatOption Option);
107 std::string format(const std::string &Message, std::set<FormatOption> Options);
108
109 class Logger {
110 public:
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,
120 const omptest::OmptAssertEvent &OffendingEvent,
121 Level LogLevel = Level::Error) const;
122
123 /// Log an event-pair mismatch.
124 void logEventMismatch(const std::string &Message,
125 const omptest::OmptAssertEvent &ExpectedEvent,
126 const omptest::OmptAssertEvent &ObservedEvent,
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
138 private:
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