Skip to content

Commit

Permalink
Added Logger::Intent - now stray lines/appends depend on current inte…
Browse files Browse the repository at this point in the history
…nt and are easily ignored if need be; Customization points for intent styles; Improved prefices; Other minor improvements
  • Loading branch information
Epixu committed Sep 10, 2024
1 parent 245ff5a commit 69626cd
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 140 deletions.
7 changes: 6 additions & 1 deletion source/HTML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ void ToHTML::NewLine() const noexcept {
Write("<br>");
Write(Instance.TimeStampStyle);
Write(GetSimpleTime());
Write("|");
if (Instance.CurrentIntent != Intent::Ignore)
Write(Instance.IntentStyle[int(Instance.CurrentIntent)].prefix);
else
Write(" ");
Write("| ");

auto tabs = Instance.GetTabs();
Expand Down Expand Up @@ -214,7 +219,7 @@ void ToHTML::WriteHeader() const {

/// Write file footer - just the official shutdown timestamp
void ToHTML::WriteFooter() const {
Write("<h2>Log ended - ");
Write("</strong></em></u></blink></del></span><h2>Log ended - ");
Write(GetAdvancedTime());
Write("</h2></code></body></html>");
}
93 changes: 78 additions & 15 deletions source/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ ScopedTabs::~ScopedTabs() noexcept {
}

/// Logger construction
Interface::Interface() {
mStyleStack.push(DefaultStyle);
}
Interface::Interface() {}

/// Logger copy-construction
Interface::Interface(const Interface& other)
Expand Down Expand Up @@ -109,6 +107,9 @@ Text Logger::A::Interface::GetSimpleTime() noexcept {
/// Write a string view to stdout
/// @param stdString - the text view to write
void Interface::Write(const TextView& stdString) const noexcept {
if (CurrentIntent == Intent::Ignore)
return;

// Dispatch to redirectors
if (not mRedirectors.empty()) {
for (auto attachment : mRedirectors)
Expand All @@ -129,6 +130,9 @@ void Interface::Write(const TextView& stdString) const noexcept {
/// Change the style
/// @param s - the style
void Interface::Write(Style s) const noexcept {
if (CurrentIntent == Intent::Ignore)
return;

// Dispatch to redirectors
if (not mRedirectors.empty()) {
for (auto attachment : mRedirectors)
Expand All @@ -147,6 +151,9 @@ void Interface::Write(Style s) const noexcept {

/// Add a new line, tabulating properly, but continuing the previous style
void Interface::NewLine() const noexcept {
if (CurrentIntent == Intent::Ignore)
return;

// Dispatch to redirectors
if (not mRedirectors.empty()) {
for (auto attachment : mRedirectors)
Expand All @@ -159,7 +166,11 @@ void Interface::NewLine() const noexcept {
// Clear formatting, add new line, simple time stamp, and tabs
fmt::print("\n");
FmtPrintStyle(TimeStampStyle);
fmt::print("{}| ", GetSimpleTime());

if (CurrentIntent == Intent::Ignore)
fmt::print("{}| | ", GetSimpleTime());
else
fmt::print("{}|{}| ", GetSimpleTime(), IntentStyle[int(CurrentIntent)].prefix);

if (mTabulator) {
auto tabs = mTabulator;
Expand All @@ -170,6 +181,11 @@ void Interface::NewLine() const noexcept {
}
}

if (mStyleStack.empty()) {
const_cast<decltype(mStyleStack)&>(mStyleStack)
.push(GetCurrentStyle());
}

FmtPrintStyle(mStyleStack.top());

// Dispatch to duplicators
Expand Down Expand Up @@ -215,7 +231,11 @@ void Interface::RunCommand(Command c) noexcept {
case Command::Reset:
while (not mStyleStack.empty())
mStyleStack.pop();
mStyleStack.push(DefaultStyle);

if (CurrentIntent == Intent::Ignore)
CurrentIntent = DefaultIntent;

mStyleStack.push(GetCurrentStyle());
Write(mStyleStack.top());
break;
case Command::Time:
Expand All @@ -225,19 +245,27 @@ void Interface::RunCommand(Command c) noexcept {
Write(GetAdvancedTime());
break;
case Command::Pop:
if (mStyleStack.size() > 1)
if (not mStyleStack.empty())
mStyleStack.pop();

if (mStyleStack.empty())
mStyleStack.push(GetCurrentStyle());

Write(mStyleStack.top());
break;
case Command::Push:
mStyleStack.push(mStyleStack.top());
break;
case Command::PopAndPush:
if (mStyleStack.size() > 1)
if (not mStyleStack.empty())
mStyleStack.pop();

mStyleStack.push(mStyleStack.top());
break;
case Command::Stylize:
if (mStyleStack.empty())
mStyleStack.push(GetCurrentStyle());

Write(mStyleStack.top());
break;
case Command::Tab:
Expand All @@ -250,10 +278,13 @@ void Interface::RunCommand(Command c) noexcept {
}
}

/// Change the foreground/background color
/// Change the foreground/background color by modifying the current style
/// @param c_with_flags - the color with optional mixing flags
/// @return the last style, with coloring applied
const Style& Interface::SetColor(Color c_with_flags) noexcept {
auto Interface::SetColor(Color c_with_flags) noexcept -> const Style& {
if (mStyleStack.empty())
mStyleStack.push(GetCurrentStyle());

if (static_cast<unsigned>(c_with_flags)
& static_cast<unsigned>(Color::PreviousColor)) {
// We have to pop
Expand Down Expand Up @@ -309,21 +340,40 @@ const Style& Interface::SetColor(Color c_with_flags) noexcept {
return style;
}

/// Change the emphasis
/// @param c - the color
const Style& Interface::SetEmphasis(Emphasis e) noexcept {
/// Change the emphasis by modifying the current style
/// @param e - the emphasis
auto Interface::SetEmphasis(Emphasis e) noexcept -> const Style& {
if (mStyleStack.empty())
mStyleStack.push(GetCurrentStyle());

auto& style = mStyleStack.top();
style |= static_cast<fmt::emphasis>(e);
return style;
}

/// Change the style
/// Change the style by overwriting the current one
/// @param s - the style
const Style& Interface::SetStyle(Style s) noexcept {
mStyleStack.top() = s;
auto Interface::SetStyle(Style s) noexcept -> const Style& {
if (mStyleStack.empty())
mStyleStack.emplace(s);
else
mStyleStack.top() = s;
return mStyleStack.top();
}

/// Get the current style
/// @returns either the top of the style stack, or the style of the current
/// intent (or a default style if current intent is Ignore)
auto Interface::GetCurrentStyle() const noexcept -> Style {
if (mStyleStack.empty()) {
if (CurrentIntent != Intent::Ignore)
return IntentStyle[int(CurrentIntent)].style;
else
return {};
}
else return mStyleStack.top();
}

/// Attach another logger, if no redirectors are attached, any logging
/// will be duplicated to the provided interface
/// @attention the logger doesn't have ownership of the attachment
Expand Down Expand Up @@ -411,6 +461,19 @@ Logger::A::Interface& Logger::A::Interface::operator << (::std::nullptr_t) noexc
return *this;
}

/// Sets the current intent, and sylizes accordingly, unles Intent::Ignore
/// @return a reference to the logger for chaining
Logger::A::Interface& Logger::A::Interface::operator << (Intent i) noexcept {
if (i != Intent::Counter)
Instance.CurrentIntent = i;

if (i < Intent::Counter) {
Instance.SetStyle(Instance.IntentStyle[int(i)].style);
Instance.RunCommand(Command::Stylize);
}
return *this;
}

/// Push a number of tabs
/// @param t - the tabs to push
/// @return a reference to the logger for chaining
Expand Down
65 changes: 57 additions & 8 deletions source/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,33 @@ namespace Langulus::Logger
/// GCC equates templates with enum types as their underlying type, so we
/// are forced to define these anums as enum class, and then do using enum
using enum Command;

/// Types of predefined messages, each with its unique style and search
/// patterns.
enum class Intent {
FatalError = 0,
Error,
Warning,
Verbose,
Info,
Message,
Special,
Flow,
Input,
Network,
OS,
Prompt,

Counter,
Ignore
};

/// Can be used to specify each intent's style and search patterns
struct IntentProperties {
TextView prefix;
Style style;
bool silenced = false;
};

/// Tabulation marker (can be pushed to log)
struct Tabs {
Expand Down Expand Up @@ -319,6 +346,7 @@ namespace Langulus::Logger
LANGULUS_API(LOGGER) Interface& operator << (Emphasis) noexcept;
LANGULUS_API(LOGGER) Interface& operator << (Style) noexcept;
LANGULUS_API(LOGGER) Interface& operator << (::std::nullptr_t) noexcept;
LANGULUS_API(LOGGER) Interface& operator << (Intent) noexcept;

LANGULUS_API(LOGGER) Interface& operator << (const Tabs&) noexcept;
LANGULUS_API(LOGGER) ScopedTabs operator << (Tabs&&) noexcept;
Expand Down Expand Up @@ -351,11 +379,31 @@ namespace Langulus::Logger
::std::list<A::Interface*> mDuplicators;

public:
// Tabulator color and formatting
static constexpr Style DefaultStyle = {};
static constexpr Style TabStyle = fmt::fg(fmt::terminal_color::bright_black);
static constexpr Style TimeStampStyle = TabStyle;
static constexpr TextView TabString = "| ";
// Current intent
Intent CurrentIntent = Intent::Info;

// Intent style customization point
IntentProperties IntentStyle[int(Intent::Counter)] = {
{"F", fmt::fg(fmt::terminal_color::red)}, // FatalError
{"E", fmt::fg(fmt::terminal_color::bright_red)}, // Error
{"W", fmt::fg(fmt::terminal_color::yellow)}, // Warning
{"V", fmt::fg(fmt::terminal_color::bright_black)}, // Verbose
{"I", fmt::fg(fmt::terminal_color::white)}, // Info
{"M", fmt::fg(fmt::terminal_color::bright_white)}, // Message
{"S", fmt::fg(fmt::terminal_color::bright_magenta)}, // Special
{"L", fmt::fg(fmt::terminal_color::cyan)}, // Flow
{"N", fmt::fg(fmt::terminal_color::bright_blue)}, // Input
{"T", fmt::fg(fmt::terminal_color::bright_yellow)}, // Network
{"O", fmt::fg(fmt::terminal_color::blue)}, // OS
{"P", fmt::fg(fmt::terminal_color::bright_green)} // Prompt
};

// Tabulator color and formatting customization
Intent DefaultIntent = Intent::Info;
Style TabStyle = fmt::fg(fmt::terminal_color::bright_black);
Style TimeStampStyle = TabStyle;
TextView TabString = "| ";

size_t GetTabs() const noexcept { return mTabulator; }

LANGULUS_API(LOGGER) Interface();
Expand All @@ -374,9 +422,10 @@ namespace Langulus::Logger
/// State changers
///
LANGULUS_API(LOGGER) void RunCommand(Command) noexcept;
LANGULUS_API(LOGGER) const Style& SetStyle(Style) noexcept;
LANGULUS_API(LOGGER) const Style& SetColor(Color) noexcept;
LANGULUS_API(LOGGER) const Style& SetEmphasis(Emphasis) noexcept;
LANGULUS_API(LOGGER) auto GetCurrentStyle() const noexcept -> Style;
LANGULUS_API(LOGGER) auto SetStyle(Style) noexcept -> const Style&;
LANGULUS_API(LOGGER) auto SetColor(Color) noexcept -> const Style&;
LANGULUS_API(LOGGER) auto SetEmphasis(Emphasis) noexcept -> const Style&;

///
/// Attachments
Expand Down
Loading

0 comments on commit 69626cd

Please sign in to comment.