1+ // ===--- ompTest/include/Logging.h - ompTest 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+ #ifndef OFFLOAD_TEST_OMPTEST_INCLUDE_LOGGING_H
9+ #define OFFLOAD_TEST_OMPTEST_INCLUDE_LOGGING_H
10+
11+ #include " OmptAssertEvent.h"
12+
13+ #include < iostream>
14+ #include < map>
15+ #include < set>
16+ #include < sstream>
17+ #include < string>
18+
19+ namespace omptest {
20+ namespace logging {
21+
22+ enum class Level : uint32_t {
23+ // Levels (Note: DEBUG may already be reserved)
24+ DIAGNOSTIC = 10 ,
25+ INFO = 20 ,
26+ WARNING = 30 ,
27+ ERROR = 40 ,
28+ CRITICAL = 50 ,
29+
30+ // Types used for formatting options
31+ Default,
32+ ExpectedEvent,
33+ ObservedEvent,
34+ OffendingEvent
35+ };
36+
37+ enum class FormatOption : uint32_t {
38+ // General options
39+ // Note: BOLD is actually "BRIGHT" -- But it will be perceived as 'bold' font
40+ // It is implicitly switching colors to the 'Light' variant
41+ // Thus, it has -NO EFFECT- when already using a Light* color
42+ NONE = 0 ,
43+ BOLD = 1 ,
44+ DIM = 2 ,
45+ UNDERLINED = 4 ,
46+ BLINK = 5 ,
47+ INVERTED = 7 ,
48+ HIDDEN = 8 ,
49+ // Foreground colors
50+ COLOR_Default = 39 ,
51+ COLOR_Black = 30 ,
52+ COLOR_Red = 31 ,
53+ COLOR_Green = 32 ,
54+ COLOR_Yellow = 33 ,
55+ COLOR_Blue = 34 ,
56+ COLOR_Magenta = 35 ,
57+ COLOR_Cyan = 36 ,
58+ COLOR_LightGray = 37 ,
59+ COLOR_DarkGray = 90 ,
60+ COLOR_LightRed = 91 ,
61+ COLOR_LightGreen = 92 ,
62+ COLOR_LightYellow = 93 ,
63+ COLOR_LightBlue = 94 ,
64+ COLOR_LightMagenta = 95 ,
65+ COLOR_LightCyan = 96 ,
66+ COLOR_White = 97 ,
67+ // Background colors
68+ COLOR_BG_Default = 49 ,
69+ COLOR_BG_Black = 40 ,
70+ COLOR_BG_Red = 41 ,
71+ COLOR_BG_Green = 42 ,
72+ COLOR_BG_Yellow = 43 ,
73+ COLOR_BG_Blue = 44 ,
74+ COLOR_BG_Magenta = 45 ,
75+ COLOR_BG_Cyan = 46 ,
76+ COLOR_BG_LightGray = 47 ,
77+ COLOR_BG_DarkGray = 100 ,
78+ COLOR_BG_LightRed = 101 ,
79+ COLOR_BG_LightGreen = 102 ,
80+ COLOR_BG_LightYellow = 103 ,
81+ COLOR_BG_LightBlue = 104 ,
82+ COLOR_BG_LightMagenta = 105 ,
83+ COLOR_BG_LightCyan = 106 ,
84+ COLOR_BG_White = 107
85+ };
86+
87+ // / Returns a string representation of the given logging level.
88+ const char *to_string (Level LogLevel);
89+
90+ // / Returns the format options as escaped sequence, for the given logging level
91+ std::string getFormatSequence (Level LogLevel = Level::Default);
92+
93+ // / Format the given message with the provided option(s) and return it.
94+ // / Here formatting is only concerning control sequences using <Esc> character
95+ // / which can be obtained using '\e' (on console), '\033' or '\x1B'.
96+ std::string format (const std::string &Message, FormatOption Option);
97+ std::string format (const std::string &Message, std::set<FormatOption> Options);
98+
99+ class Logger {
100+ public:
101+ ~Logger ();
102+
103+ // / Retrieve the singleton logger (and initialize, if not done already)
104+ static Logger &get (Level LogLevel = Level::WARNING,
105+ std::ostream &OutStream = std::cerr,
106+ bool FormatOutput = true );
107+
108+ // / Log the given message to the output.
109+ void log (Level LogLevel, const std::string &Message) const ;
110+
111+ // / Log a single event mismatch.
112+ void eventMismatch (const omptest::OmptAssertEvent &OffendingEvent,
113+ const std::string &Message,
114+ Level LogLevel = Level::ERROR) const ;
115+
116+ // / Log an event-pair mismatch.
117+ void eventMismatch (const omptest::OmptAssertEvent &ExpectedEvent,
118+ const omptest::OmptAssertEvent &ObservedEvent,
119+ const std::string &Message,
120+ Level LogLevel = Level::ERROR) const ;
121+
122+ // / Set if output is being formatted.
123+ void setFormatOutput (bool Enabled);
124+
125+ // / Return the current (minimum) Logging Level.
126+ Level getLoggingLevel () const ;
127+
128+ // / Set the (minimum) Logging Level.
129+ void setLoggingLevel (Level LogLevel);
130+
131+ private:
132+ Logger (Level LogLevel = Level::WARNING, std::ostream &OutStream = std::cerr,
133+ bool FormatOutput = true );
134+
135+ // / The minimum logging level that is considered by the logger instance.
136+ Level LoggingLevel;
137+
138+ // / The output stream used by the logger instance.
139+ std::ostream &OutStream;
140+
141+ // / Determine if log messages are formatted using control sequences.
142+ bool FormatOutput;
143+ };
144+
145+ } // namespace logging
146+ } // namespace omptest
147+
148+ // Pointer to global logger
149+ extern omptest::logging::Logger *Log;
150+
151+ #endif
0 commit comments