Skip to content

Commit

Permalink
Prefixes for all message styles; Plain-text logger redirector/duplica…
Browse files Browse the repository at this point in the history
…tor; Combining colors in different ways for less redundant pushing/popping; Bug fix
  • Loading branch information
Epixu committed Jul 17, 2024
1 parent 3763f26 commit 73d5dea
Show file tree
Hide file tree
Showing 8 changed files with 374 additions and 93 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fetch_external_module(
add_library(LangulusLogger ${LANGULUS_LIBRARY_TYPE}
source/Logger.cpp
source/HTML.cpp
source/TXT.cpp
)

target_compile_definitions(LangulusLogger
Expand Down
11 changes: 9 additions & 2 deletions source/HTML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ToHTML::ToHTML(const TextView& filename) : mFilename {filename} {
}

ToHTML::~ToHTML() {
WriteFooter();
mFile.close();
}

Expand All @@ -33,7 +34,7 @@ void ToHTML::Write(const TextView& text) const noexcept {

/// Apply some style
/// @param style - the style to set
void ToHTML::Write(const Style& style) const noexcept {
void ToHTML::Write(Style style) const noexcept {
// Always reset before a style change
Write(" \n</code></strong></em></u></blink></del></span><code>");

Expand Down Expand Up @@ -179,7 +180,6 @@ void ToHTML::Write(const Style& style) const noexcept {

/// Remove formatting, add a new line, add a timestamp and tabulate
/// @attention top of the style stack is not applied
/// @param timestamp - whether to insert a timestamp
void ToHTML::NewLine() const noexcept {
Write("<br>");
Write(Instance.TimeStampStyle);
Expand Down Expand Up @@ -211,3 +211,10 @@ void ToHTML::WriteHeader() const {
Write(GetAdvancedTime());
Write("</h2><code>\n");
}

/// Write file footer - just the official shutdown timestamp
void ToHTML::WriteFooter() const {
Write("<h2>Log ended - ");
Write(GetAdvancedTime());
Write("</h2></code></body></html>");
}
75 changes: 51 additions & 24 deletions source/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void Interface::Write(const TextView& stdString) const noexcept {

/// Change the style
/// @param s - the style
void Interface::Write(const Style& s) const noexcept {
void Interface::Write(Style s) const noexcept {
// Dispatch to redirectors
if (not mRedirectors.empty()) {
for (auto attachment : mRedirectors)
Expand Down Expand Up @@ -200,7 +200,7 @@ void Interface::Clear() const noexcept {

/// Execute a logger command
/// @param c - the command to execute
void Interface::RunCommand(const Command& c) noexcept {
void Interface::RunCommand(Command c) noexcept {
switch (c) {
case Command::Clear:
Clear();
Expand Down Expand Up @@ -232,6 +232,11 @@ void Interface::RunCommand(const Command& c) noexcept {
case Command::Push:
mStyleStack.push(mStyleStack.top());
break;
case Command::PopAndPush:
if (mStyleStack.size() > 1)
mStyleStack.pop();
mStyleStack.push(mStyleStack.top());
break;
case Command::Stylize:
Write(mStyleStack.top());
break;
Expand All @@ -246,9 +251,31 @@ void Interface::RunCommand(const Command& c) noexcept {
}

/// Change the foreground/background color
/// @param c - the color
/// @param c_with_flags - the color with optional mixing flags
/// @return the last style, with coloring applied
const Style& Interface::SetColor(const Color& c) noexcept {
const Style& Interface::SetColor(Color c_with_flags) noexcept {
if (static_cast<unsigned>(c_with_flags)
& static_cast<unsigned>(Color::PreviousColor)) {
// We have to pop
if (mStyleStack.size() > 1)
mStyleStack.pop();
}

if (static_cast<unsigned>(c_with_flags)
& static_cast<unsigned>(Color::NextColor)) {
// We have to push
mStyleStack.push(mStyleStack.top());
}

// Strip the mixing bits from the color
const Color c = static_cast<Color>(
static_cast<unsigned>(c_with_flags) & (~(
static_cast<unsigned>(Color::PreviousColor)
| static_cast<unsigned>(Color::NextColor)
))
);

// Mix...
auto& style = mStyleStack.top();
const auto oldStyle = style;
if (c == Color::NoForeground) {
Expand Down Expand Up @@ -284,15 +311,15 @@ const Style& Interface::SetColor(const Color& c) noexcept {

/// Change the emphasis
/// @param c - the color
const Style& Interface::SetEmphasis(const Emphasis& e) noexcept {
const Style& Interface::SetEmphasis(Emphasis e) noexcept {
auto& style = mStyleStack.top();
style |= static_cast<fmt::emphasis>(e);
return style;
}

/// Change the style
/// @param s - the style
const Style& Interface::SetStyle(const Style& s) noexcept {
const Style& Interface::SetStyle(Style s) noexcept {
mStyleStack.top() = s;
return mStyleStack.top();
}
Expand Down Expand Up @@ -337,15 +364,15 @@ Logger::A::Interface& Logger::A::Interface::operator << (Logger::A::Interface&)
/// Push a command
/// @param c - the command to push
/// @return a reference to the logger for chaining
Logger::A::Interface& Logger::A::Interface::operator << (const Command& c) noexcept {
Logger::A::Interface& Logger::A::Interface::operator << (Command c) noexcept {
Instance.RunCommand(c);
return *this;
}

/// Push a foreground color
/// @param c - the command to push
/// @return a reference to the logger for chaining
Logger::A::Interface& Logger::A::Interface::operator << (const Color& c) noexcept {
Logger::A::Interface& Logger::A::Interface::operator << (Color c) noexcept {
Instance.SetColor(c);
Instance.RunCommand(Command::Stylize);
return *this;
Expand All @@ -354,7 +381,7 @@ Logger::A::Interface& Logger::A::Interface::operator << (const Color& c) noexcep
/// Push an emphasis
/// @param e - the emphasis to push
/// @return a reference to the logger for chaining
Logger::A::Interface& Logger::A::Interface::operator << (const Emphasis& e) noexcept {
Logger::A::Interface& Logger::A::Interface::operator << (Emphasis e) noexcept {
Instance.SetEmphasis(e);
Instance.RunCommand(Command::Stylize);
return *this;
Expand All @@ -363,25 +390,12 @@ Logger::A::Interface& Logger::A::Interface::operator << (const Emphasis& e) noex
/// Push a foreground and background color
/// @param c - the state to push
/// @return a reference to the logger for chaining
Logger::A::Interface& Logger::A::Interface::operator << (const Style& c) noexcept {
Logger::A::Interface& Logger::A::Interface::operator << (Style c) noexcept {
Instance.SetStyle(c);
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
Logger::A::Interface& Logger::A::Interface::operator << (const Tabs& t) noexcept {
auto tabs = ::std::max(1, t.mTabs);
while (tabs) {
Instance.RunCommand(Command::Tab);
--tabs;
}

return *this;
}

/// Write string views
/// @param t - text to write
/// @return a reference to the logger for chaining
Expand All @@ -392,11 +406,24 @@ Logger::A::Interface& Logger::A::Interface::operator << (const TextView& t) noex

/// Write a nullptr as "null"
/// @return a reference to the logger for chaining
Logger::A::Interface& Logger::A::Interface::operator << (const ::std::nullptr_t&) noexcept {
Logger::A::Interface& Logger::A::Interface::operator << (::std::nullptr_t) noexcept {
Instance.Write("null");
return *this;
}

/// Push a number of tabs
/// @param t - the tabs to push
/// @return a reference to the logger for chaining
Logger::A::Interface& Logger::A::Interface::operator << (const Tabs& t) noexcept {
auto tabs = ::std::max(1, t.mTabs);
while (tabs) {
Instance.RunCommand(Command::Tab);
--tabs;
}

return *this;
}

/// Push a number of tabs
/// Keeps track of the number of tabs that have been pushed, and then
/// automatically untabs when the Tabs object is destroyed
Expand Down
Loading

0 comments on commit 73d5dea

Please sign in to comment.