Skip to content

Commit

Permalink
[offload][ompTest] Added dedicated logging
Browse files Browse the repository at this point in the history
This is a first iteration on dedicated logging for ompTest.

Added logger class
Added rudimentary support for log-levels (with colored output)
 * Can be turned ON via EnVar 'OMPTEST_LOG_COLORED'

Change-Id: I3426c45e5f19c1102ee7278f0732ccc85b9209e9
  • Loading branch information
mhalk authored and ronlieb committed Sep 10, 2024
1 parent ad7def2 commit 673c177
Show file tree
Hide file tree
Showing 19 changed files with 409 additions and 75 deletions.
5 changes: 3 additions & 2 deletions offload/plugins-nextgen/common/OMPT/OmptTracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ OMPT_API_ROUTINE ompt_set_result_t ompt_set_trace_ompt(ompt_device_t *Device,

int DeviceId = getDeviceId(Device);
if (DeviceId < 0) {
REPORT("Failed to set trace events for Device=%p (Unknown device)\n",
Device);
REPORT("Failed to set trace events for Device=%p (Unknown device) "
"[Enable=%d, EventTy=%d]\n",
Device, Enable, EventTy);
return ompt_set_never;
}

Expand Down
2 changes: 2 additions & 0 deletions offload/test/ompTest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(OMPTEST_HEADERS
./include/AssertMacros.h
./include/InternalEvent.h
./include/InternalEventCommon.h
./include/Logging.h
./include/OmptAliases.h
./include/OmptAsserter.h
./include/OmptAssertEvent.h
Expand All @@ -31,6 +32,7 @@ add_library(omptest
${OMPTEST_HEADERS}
./src/InternalEvent.cpp
./src/InternalEventOperators.cpp
./src/Logging.cpp
./src/OmptAsserter.cpp
./src/OmptAssertEvent.cpp
./src/OmptCallbackHandler.cpp
Expand Down
4 changes: 2 additions & 2 deletions offload/test/ompTest/include/AssertMacros.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef OPENMP_LIBOMPTARGET_TEST_OMPTEST_ASSERTMACROS_H
#define OPENMP_LIBOMPTARGET_TEST_OMPTEST_ASSERTMACROS_H
#ifndef OFFLOAD_TEST_OMPTEST_INCLUDE_ASSERTMACROS_H
#define OFFLOAD_TEST_OMPTEST_INCLUDE_ASSERTMACROS_H

#define OMPTEST_EXCLUDED_EVENT omptest::ObserveState::never
#define OMPTEST_REQUIRED_EVENT omptest::ObserveState::always
Expand Down
4 changes: 2 additions & 2 deletions offload/test/ompTest/include/InternalEvent.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef OPENMP_LIBOMPTARGET_TEST_OMPTEST_INTERNALEVENT_H
#define OPENMP_LIBOMPTARGET_TEST_OMPTEST_INTERNALEVENT_H
#ifndef OFFLOAD_TEST_OMPTEST_INCLUDE_INTERNALEVENT_H
#define OFFLOAD_TEST_OMPTEST_INCLUDE_INTERNALEVENT_H

#include "InternalEventCommon.h"

Expand Down
4 changes: 2 additions & 2 deletions offload/test/ompTest/include/InternalEventCommon.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef OPENMP_LIBOMPTARGET_TEST_OMPTEST_INTERNALEVENTCOMMON_H
#define OPENMP_LIBOMPTARGET_TEST_OMPTEST_INTERNALEVENTCOMMON_H
#ifndef OFFLOAD_TEST_OMPTEST_INCLUDE_INTERNALEVENTCOMMON_H
#define OFFLOAD_TEST_OMPTEST_INCLUDE_INTERNALEVENTCOMMON_H

#include "omp-tools.h"

Expand Down
151 changes: 151 additions & 0 deletions offload/test/ompTest/include/Logging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
//===--- ompTest/include/Logging.h - ompTest logging class ------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef OFFLOAD_TEST_OMPTEST_INCLUDE_LOGGING_H
#define OFFLOAD_TEST_OMPTEST_INCLUDE_LOGGING_H

#include "OmptAssertEvent.h"

#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>

namespace omptest {
namespace logging {

enum class Level : uint32_t {
// Levels (Note: DEBUG may already be reserved)
DIAGNOSTIC = 10,
INFO = 20,
WARNING = 30,
ERROR = 40,
CRITICAL = 50,

// Types used for formatting options
Default,
ExpectedEvent,
ObservedEvent,
OffendingEvent
};

enum class FormatOption : uint32_t {
// General options
// Note: BOLD is actually "BRIGHT" -- But it will be perceived as 'bold' font
// It is implicitly switching colors to the 'Light' variant
// Thus, it has -NO EFFECT- when already using a Light* color
NONE = 0,
BOLD = 1,
DIM = 2,
UNDERLINED = 4,
BLINK = 5,
INVERTED = 7,
HIDDEN = 8,
// Foreground colors
COLOR_Default = 39,
COLOR_Black = 30,
COLOR_Red = 31,
COLOR_Green = 32,
COLOR_Yellow = 33,
COLOR_Blue = 34,
COLOR_Magenta = 35,
COLOR_Cyan = 36,
COLOR_LightGray = 37,
COLOR_DarkGray = 90,
COLOR_LightRed = 91,
COLOR_LightGreen = 92,
COLOR_LightYellow = 93,
COLOR_LightBlue = 94,
COLOR_LightMagenta = 95,
COLOR_LightCyan = 96,
COLOR_White = 97,
// Background colors
COLOR_BG_Default = 49,
COLOR_BG_Black = 40,
COLOR_BG_Red = 41,
COLOR_BG_Green = 42,
COLOR_BG_Yellow = 43,
COLOR_BG_Blue = 44,
COLOR_BG_Magenta = 45,
COLOR_BG_Cyan = 46,
COLOR_BG_LightGray = 47,
COLOR_BG_DarkGray = 100,
COLOR_BG_LightRed = 101,
COLOR_BG_LightGreen = 102,
COLOR_BG_LightYellow = 103,
COLOR_BG_LightBlue = 104,
COLOR_BG_LightMagenta = 105,
COLOR_BG_LightCyan = 106,
COLOR_BG_White = 107
};

/// Returns a string representation of the given logging level.
const char *to_string(Level LogLevel);

/// Returns the format options as escaped sequence, for the given logging level
std::string getFormatSequence(Level LogLevel = Level::Default);

/// Format the given message with the provided option(s) and return it.
/// Here formatting is only concerning control sequences using <Esc> character
/// which can be obtained using '\e' (on console), '\033' or '\x1B'.
std::string format(const std::string &Message, FormatOption Option);
std::string format(const std::string &Message, std::set<FormatOption> Options);

class Logger {
public:
~Logger();

/// Retrieve the singleton logger (and initialize, if not done already)
static Logger &get(Level LogLevel = Level::WARNING,
std::ostream &OutStream = std::cerr,
bool FormatOutput = true);

/// Log the given message to the output.
void log(Level LogLevel, const std::string &Message) const;

/// Log a single event mismatch.
void eventMismatch(const omptest::OmptAssertEvent &OffendingEvent,
const std::string &Message,
Level LogLevel = Level::ERROR) const;

/// Log an event-pair mismatch.
void eventMismatch(const omptest::OmptAssertEvent &ExpectedEvent,
const omptest::OmptAssertEvent &ObservedEvent,
const std::string &Message,
Level LogLevel = Level::ERROR) const;

/// Set if output is being formatted.
void setFormatOutput(bool Enabled);

/// Return the current (minimum) Logging Level.
Level getLoggingLevel() const;

/// Set the (minimum) Logging Level.
void setLoggingLevel(Level LogLevel);

private:
Logger(Level LogLevel = Level::WARNING, std::ostream &OutStream = std::cerr,
bool FormatOutput = true);

/// The minimum logging level that is considered by the logger instance.
Level LoggingLevel;

/// The output stream used by the logger instance.
std::ostream &OutStream;

/// Determine if log messages are formatted using control sequences.
bool FormatOutput;
};

} // namespace logging
} // namespace omptest

// Pointer to global logger
extern omptest::logging::Logger *Log;

#endif
4 changes: 2 additions & 2 deletions offload/test/ompTest/include/OmptAliases.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef OPENMP_LIBOMPTARGET_TEST_OMPTEST_OMPTALIASES_H
#define OPENMP_LIBOMPTARGET_TEST_OMPTEST_OMPTALIASES_H
#ifndef OFFLOAD_TEST_OMPTEST_INCLUDE_OMPTALIASES_H
#define OFFLOAD_TEST_OMPTEST_INCLUDE_OMPTALIASES_H

#include <omp-tools.h>

Expand Down
6 changes: 4 additions & 2 deletions offload/test/ompTest/include/OmptAssertEvent.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef OPENMP_LIBOMPTARGET_TEST_OMPTEST_OMPTASSERTEVENT_H
#define OPENMP_LIBOMPTARGET_TEST_OMPTEST_OMPTASSERTEVENT_H
#ifndef OFFLOAD_TEST_OMPTEST_INCLUDE_OMPTASSERTEVENT_H
#define OFFLOAD_TEST_OMPTEST_INCLUDE_OMPTASSERTEVENT_H

#include "InternalEvent.h"
#include "omp-tools.h"
Expand All @@ -13,6 +13,8 @@ namespace omptest{

enum class ObserveState { generated, always, never };

const char *to_string(ObserveState State);

struct OmptAssertEvent {

static OmptAssertEvent AssertionSyncPoint(const std::string &Name,
Expand Down
12 changes: 2 additions & 10 deletions offload/test/ompTest/include/OmptAsserter.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef OPENMP_LIBOMPTARGET_TEST_OMPTEST_OMPTASSERTER_H
#define OPENMP_LIBOMPTARGET_TEST_OMPTEST_OMPTASSERTER_H
#ifndef OFFLOAD_TEST_OMPTEST_INCLUDE_OMPTASSERTER_H
#define OFFLOAD_TEST_OMPTEST_INCLUDE_OMPTASSERTER_H

#include "OmptAssertEvent.h"

Expand Down Expand Up @@ -83,14 +83,6 @@ class OmptAsserter : public OmptListener {
/// the notification.
virtual void notifyImpl(omptest::OmptAssertEvent &&AE) = 0;

/// Report an error for a single event.
void reportError(const omptest::OmptAssertEvent &OffendingEvent,
const std::string &Message);

void reportError(const omptest::OmptAssertEvent &AwaitedEvent,
const omptest::OmptAssertEvent &OffendingEvent,
const std::string &Message);

/// Get the number of currently remaining events, with: ObserveState::always.
virtual size_t getRemainingEventCount() = 0;

Expand Down
4 changes: 2 additions & 2 deletions offload/test/ompTest/include/OmptCallbackHandler.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef OPENMP_LIBOMPTARGET_TEST_OMPTEST_OMPTCALLBACKHANDLER_H
#define OPENMP_LIBOMPTARGET_TEST_OMPTEST_OMPTCALLBACKHANDLER_H
#ifndef OFFLOAD_TEST_OMPTEST_INCLUDE_OMPTCALLBACKHANDLER_H
#define OFFLOAD_TEST_OMPTEST_INCLUDE_OMPTCALLBACKHANDLER_H

#include "OmptAssertEvent.h"
#include "OmptAsserter.h"
Expand Down
5 changes: 3 additions & 2 deletions offload/test/ompTest/include/OmptTester.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#ifndef OPENMP_LIBOMPTARGET_TEST_OMPTEST_OMPTTESTER_H
#define OPENMP_LIBOMPTARGET_TEST_OMPTEST_OMPTTESTER_H
#ifndef OFFLOAD_TEST_OMPTEST_INCLUDE_OMPTTESTER_H
#define OFFLOAD_TEST_OMPTEST_INCLUDE_OMPTTESTER_H

#include "AssertMacros.h"
#include "Logging.h"
#include "OmptAliases.h"
#include "OmptAssertEvent.h"
#include "OmptAsserter.h"
Expand Down
4 changes: 2 additions & 2 deletions offload/test/ompTest/include/OmptTesterGlobals.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef OPENMP_LIBOMPTARGET_TEST_OMPTEST_OMPTTESTERGLOBALS_H
#define OPENMP_LIBOMPTARGET_TEST_OMPTEST_OMPTTESTERGLOBALS_H
#ifndef OFFLOAD_TEST_OMPTEST_INCLUDE_OMPTTESTERGLOBALS_H
#define OFFLOAD_TEST_OMPTEST_INCLUDE_OMPTTESTERGLOBALS_H

#include <omp-tools.h>

Expand Down
4 changes: 2 additions & 2 deletions offload/test/ompTest/include/OmptTesterGoogleTest.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef OPENMP_LIBOMPTARGET_TEST_OMPTEST_OMPTTESTERGOOGLETEST_H
#define OPENMP_LIBOMPTARGET_TEST_OMPTEST_OMPTTESTERGOOGLETEST_H
#ifndef OFFLOAD_TEST_OMPTEST_INCLUDE_OMPTTESTERGOOGLETEST_H
#define OFFLOAD_TEST_OMPTEST_INCLUDE_OMPTTESTERGOOGLETEST_H

#include "AssertMacros.h"
#include "OmptAliases.h"
Expand Down
4 changes: 2 additions & 2 deletions offload/test/ompTest/include/OmptTesterStandalone.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef OPENMP_LIBOMPTARGET_TEST_OMPTEST_OMPTTESTERSTANDALONE_H
#define OPENMP_LIBOMPTARGET_TEST_OMPTEST_OMPTTESTERSTANDALONE_H
#ifndef OFFLOAD_TEST_OMPTEST_INCLUDE_OMPTTESTERSTANDALONE_H
#define OFFLOAD_TEST_OMPTEST_INCLUDE_OMPTTESTERSTANDALONE_H

#include "OmptAssertEvent.h"
#include "OmptAsserter.h"
Expand Down
Loading

0 comments on commit 673c177

Please sign in to comment.