Skip to content

Commit

Permalink
Merge pull request #317 from Laguna1989/feature/cpp20sourcelocation
Browse files Browse the repository at this point in the history
Use std::source_location
  • Loading branch information
Laguna1989 authored Jun 1, 2024
2 parents af4ec91 + f66320e commit d227d73
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 66 deletions.
8 changes: 7 additions & 1 deletion impl/jamtemplate/common/log/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ void jt::Console::doDraw() const
ImGui::Checkbox("Level", &m_drawLevel);
ImGui::SameLine();
ImGui::Checkbox("Tags", &m_drawTag);
ImGui::SameLine();
ImGui::Checkbox("Source", &m_drawSource);

// Display contents in a scrolling region
ImGui::TextColored(ImVec4(1, 1, 0, 1), "");
Expand Down Expand Up @@ -148,7 +150,11 @@ void jt::Console::renderOneLogEntry(jt::LogEntry const& entry) const
levelText = "";
}

std::string text = timeText + levelText + tagText + entry.message;
std::string sourceText;
if (m_drawSource)
sourceText = std::string { entry.source.file_name() } + ":"
+ std::to_string(entry.source.line()) + " [" + entry.source.function_name() + "]";
std::string text = timeText + levelText + tagText + entry.message + sourceText;

std::string filterString = m_inputBufferFilter.data();
strutil::trim(filterString);
Expand Down
1 change: 1 addition & 0 deletions impl/jamtemplate/common/log/console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Console : public jt::GameObject {
mutable bool m_drawLevel { true };
mutable bool m_drawTag { true };
mutable bool m_drawTime { false };
mutable bool m_drawSource { false };

void doCreate() override;
void doUpdate(float const /*elapsed*/) override;
Expand Down
2 changes: 2 additions & 0 deletions impl/jamtemplate/common/log/log_entry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define JAMTEMPLATE_LOG_ENTRY_HPP

#include <log/log_level.hpp>
#include <source_location>
#include <string>
#include <vector>

Expand All @@ -13,6 +14,7 @@ struct LogEntry {
std::string time { "" };
LogLevel level { LogLevel::Off };
std::vector<std::string> tags {};
std::source_location source {};
};

} // namespace jt
Expand Down
5 changes: 4 additions & 1 deletion impl/jamtemplate/common/log/log_target_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ jt::LogTargetFile::LogTargetFile()

void jt::LogTargetFile::doLog(jt::LogEntry const& entry)
{
m_file << entry.time << ": " << entry.message << std::endl;
std::string sourceText = std::string { entry.source.file_name() } + ":"
+ std::to_string(entry.source.line()) + " [" + entry.source.function_name() + "]";

m_file << entry.time << ": " << entry.message << " " << sourceText << std::endl;
}
34 changes: 20 additions & 14 deletions impl/jamtemplate/common/log/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,59 @@
#include <sstream>
#include <stdexcept>

void jt::Logger::action(std::string const& string)
void jt::Logger::action(std::string const& string, std::source_location source)
{
addLogEntry(LogEntry { string, "", LogLevel::Action, {} });
addLogEntry(LogEntry { string, "", LogLevel::Action, {}, source });
}

void jt::Logger::fatal(std::string const& string, std::vector<std::string> const& tags)
void jt::Logger::fatal(
std::string const& string, std::vector<std::string> const& tags, std::source_location source)
{
if (m_logLevel >= LogLevel::Fatal) {
addLogEntry(LogEntry { string, "", LogLevel::Fatal, tags });
addLogEntry(LogEntry { string, "", LogLevel::Fatal, tags, source });
}
}

void jt::Logger::error(std::string const& string, std::vector<std::string> const& tags)
void jt::Logger::error(
std::string const& string, std::vector<std::string> const& tags, std::source_location source)
{
if (m_logLevel >= LogLevel::Error) {
addLogEntry(LogEntry { string, "", LogLevel::Error, tags });
addLogEntry(LogEntry { string, "", LogLevel::Error, tags, source });
}
}

void jt::Logger::warning(std::string const& string, std::vector<std::string> const& tags)
void jt::Logger::warning(
std::string const& string, std::vector<std::string> const& tags, std::source_location source)
{
if (m_logLevel >= LogLevel::Warning) {
addLogEntry(LogEntry { string, "", LogLevel::Warning, tags });
addLogEntry(LogEntry { string, "", LogLevel::Warning, tags, source });
}
}

void jt::Logger::info(std::string const& string, std::vector<std::string> const& tags)
void jt::Logger::info(
std::string const& string, std::vector<std::string> const& tags, std::source_location source)
{
if (m_logLevel >= LogLevel::Info) {
addLogEntry(LogEntry { string, "", LogLevel::Info, tags });
addLogEntry(LogEntry { string, "", LogLevel::Info, tags, source });
}
}

void jt::Logger::debug(std::string const& string, std::vector<std::string> const& tags)
void jt::Logger::debug(
std::string const& string, std::vector<std::string> const& tags, std::source_location source)
{
#ifdef JT_ENABLE_DEBUG
if (m_logLevel >= LogLevel::Debug) {
addLogEntry(LogEntry { string, "", LogLevel::Debug, tags });
addLogEntry(LogEntry { string, "", LogLevel::Debug, tags, source });
}
#endif
}

void jt::Logger::verbose(std::string const& string, std::vector<std::string> const& tags)
void jt::Logger::verbose(
std::string const& string, std::vector<std::string> const& tags, std::source_location source)
{
#ifdef JT_ENABLE_DEBUG
if (m_logLevel >= LogLevel::Verbose) {
addLogEntry(LogEntry { string, "", LogLevel::Verbose, tags });
addLogEntry(LogEntry { string, "", LogLevel::Verbose, tags, source });
}
#endif
}
Expand Down
21 changes: 14 additions & 7 deletions impl/jamtemplate/common/log/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ class LogTargetInterface;

class Logger : public jt::LoggerInterface {
public:
void action(std::string const& string) override;
void fatal(std::string const& string, std::vector<std::string> const& tags = {}) override;
void error(std::string const& string, std::vector<std::string> const& tags = {}) override;
void warning(std::string const& string, std::vector<std::string> const& tags = {}) override;
void info(std::string const& string, std::vector<std::string> const& tags = {}) override;
void debug(std::string const& string, std::vector<std::string> const& tags = {}) override;
void verbose(std::string const& string, std::vector<std::string> const& tags = {}) override;
void action(
std::string const& string, std::source_location = std::source_location::current()) override;
void fatal(std::string const& string, std::vector<std::string> const& tags = {},
std::source_location = std::source_location::current()) override;
void error(std::string const& string, std::vector<std::string> const& tags = {},
std::source_location = std::source_location::current()) override;
void warning(std::string const& string, std::vector<std::string> const& tags = {},
std::source_location = std::source_location::current()) override;
void info(std::string const& string, std::vector<std::string> const& tags = {},
std::source_location = std::source_location::current()) override;
void debug(std::string const& string, std::vector<std::string> const& tags = {},
std::source_location = std::source_location::current()) override;
void verbose(std::string const& string, std::vector<std::string> const& tags = {},
std::source_location = std::source_location::current()) override;

void addLogTarget(std::shared_ptr<LogTargetInterface> target) override;
void setLogLevel(LogLevel level) override;
Expand Down
29 changes: 22 additions & 7 deletions impl/jamtemplate/common/log/logger_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <log/log_level.hpp>
#include <memory>
#include <source_location>
#include <string>
#include <vector>

Expand All @@ -15,37 +16,51 @@ class LoggerInterface {
/// Log a fatal message
/// \param string the log message
/// \param tags the message tags
virtual void fatal(std::string const& string, std::vector<std::string> const& tags = {}) = 0;
virtual void fatal(std::string const& string, std::vector<std::string> const& tags = {},
std::source_location = std::source_location::current())
= 0;

/// Log a error message
/// \param string the log message
/// \param tags the message tags
virtual void error(std::string const& string, std::vector<std::string> const& tags = {}) = 0;
virtual void error(std::string const& string, std::vector<std::string> const& tags = {},
std::source_location = std::source_location::current())
= 0;

/// Log a warning message
/// \param string the log message
/// \param tags the message tags
virtual void warning(std::string const& string, std::vector<std::string> const& tags = {}) = 0;
virtual void warning(std::string const& string, std::vector<std::string> const& tags = {},
std::source_location = std::source_location::current())
= 0;

/// Log a info message
/// \param string the log message
/// \param tags the message tags
virtual void info(std::string const& string, std::vector<std::string> const& tags = {}) = 0;
virtual void info(std::string const& string, std::vector<std::string> const& tags = {},
std::source_location = std::source_location::current())
= 0;

/// Log a debug message
/// \param string the log message
/// \param tags the message tags
virtual void debug(std::string const& string, std::vector<std::string> const& tags = {}) = 0;
virtual void debug(std::string const& string, std::vector<std::string> const& tags = {},
std::source_location = std::source_location::current())
= 0;

/// Log a verbose message
/// \param string the log message
/// \param tags the message tags
virtual void verbose(std::string const& string, std::vector<std::string> const& tags = {}) = 0;
virtual void verbose(std::string const& string, std::vector<std::string> const& tags = {},
std::source_location = std::source_location::current())
= 0;

/// Log a action message (from the console
/// \param string the log message
/// \param tags the message tags
virtual void action(std::string const& string) = 0;
virtual void action(
std::string const& string, std::source_location = std::source_location::current())
= 0;

/// Add a log target to the logger
/// \param target the target to be added
Expand Down
36 changes: 23 additions & 13 deletions impl/jamtemplate/common/log/logger_null.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
#include "logger_null.hpp"
#include <log/log_target_interface.hpp>

void jt::null_objects::LoggerNull::action(std::string const& /*string*/) { }
void jt::null_objects::LoggerNull::action(
std::string const& /*string*/, std::source_location /*source*/)
{
}

void jt::null_objects::LoggerNull::fatal(
std::string const& /*string*/, std::vector<std::string> const& /*tags*/)
void jt::null_objects::LoggerNull::fatal(std::string const& /*string*/,
std::vector<std::string> const& /*tags*/, std::source_location /*source*/)
{
}
void jt::null_objects::LoggerNull::error(
std::string const& /*string*/, std::vector<std::string> const& /*tags*/)

void jt::null_objects::LoggerNull::error(std::string const& /*string*/,
std::vector<std::string> const& /*tags*/, std::source_location /*source*/)
{
}
void jt::null_objects::LoggerNull::warning(
std::string const& /*string*/, std::vector<std::string> const& /*tags*/)

void jt::null_objects::LoggerNull::warning(std::string const& /*string*/,
std::vector<std::string> const& /*tags*/, std::source_location /*source*/)
{
}
void jt::null_objects::LoggerNull::info(
std::string const& /*string*/, std::vector<std::string> const& /*tags*/)

void jt::null_objects::LoggerNull::info(std::string const& /*string*/,
std::vector<std::string> const& /*tags*/, std::source_location /*source*/)
{
}
void jt::null_objects::LoggerNull::debug(
std::string const& /*string*/, std::vector<std::string> const& /*tags*/)

void jt::null_objects::LoggerNull::debug(std::string const& /*string*/,
std::vector<std::string> const& /*tags*/, std::source_location /*source*/)
{
}
void jt::null_objects::LoggerNull::verbose(
std::string const& /*string*/, std::vector<std::string> const& /*tags*/)

void jt::null_objects::LoggerNull::verbose(std::string const& /*string*/,
std::vector<std::string> const& /*tags*/, std::source_location /*source*/)
{
}

void jt::null_objects::LoggerNull::addLogTarget(std::shared_ptr<LogTargetInterface> /*target*/) { }

void jt::null_objects::LoggerNull::setLogLevel(LogLevel /*level*/) { }
21 changes: 14 additions & 7 deletions impl/jamtemplate/common/log/logger_null.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ namespace null_objects {

class LoggerNull : public jt::LoggerInterface {
public:
void action(std::string const& string) override;
void fatal(std::string const& string, std::vector<std::string> const& tags = {}) override;
void error(std::string const& string, std::vector<std::string> const& tags = {}) override;
void warning(std::string const& string, std::vector<std::string> const& tags = {}) override;
void info(std::string const& string, std::vector<std::string> const& tags = {}) override;
void debug(std::string const& string, std::vector<std::string> const& tags = {}) override;
void verbose(std::string const& string, std::vector<std::string> const& tags = {}) override;
void action(
std::string const& string, std::source_location = std::source_location::current()) override;
void fatal(std::string const& string, std::vector<std::string> const& tags = {},
std::source_location = std::source_location::current()) override;
void error(std::string const& string, std::vector<std::string> const& tags = {},
std::source_location = std::source_location::current()) override;
void warning(std::string const& string, std::vector<std::string> const& tags = {},
std::source_location = std::source_location::current()) override;
void info(std::string const& string, std::vector<std::string> const& tags = {},
std::source_location = std::source_location::current()) override;
void debug(std::string const& string, std::vector<std::string> const& tags = {},
std::source_location = std::source_location::current()) override;
void verbose(std::string const& string, std::vector<std::string> const& tags = {},
std::source_location = std::source_location::current()) override;
void addLogTarget(std::shared_ptr<LogTargetInterface> target) override;
void setLogLevel(LogLevel level) override;
};
Expand Down
18 changes: 17 additions & 1 deletion test/integration/demo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,24 @@ int main(int /*argc*/, char* /*argv*/[])
keyboard->listenForKey(jt::KeyCode::C);
keyboard->listenForKey(jt::KeyCode::V);
keyboard->listenForKey(jt::KeyCode::I);


keyboard->listenForKey(jt::KeyCode::Num0);
keyboard->listenForKey(jt::KeyCode::Num1);
keyboard->listenForKey(jt::KeyCode::Num2);
keyboard->listenForKey(jt::KeyCode::Num3);
keyboard->listenForKey(jt::KeyCode::Num4);
keyboard->listenForKey(jt::KeyCode::Num5);
keyboard->listenForKey(jt::KeyCode::Num6);
keyboard->listenForKey(jt::KeyCode::Num7);
keyboard->listenForKey(jt::KeyCode::Num8);
keyboard->listenForKey(jt::KeyCode::Num9);

keyboard->listenForKey(jt::KeyCode::F10);
keyboard->listenForKey(jt::KeyCode::F1);

keyboard->listenForKey(jt::KeyCode::Home);
keyboard->listenForKey(jt::KeyCode::End);

keyboard->listenForKey(jt::KeyCode::Escape);
keyboard->listenForKey(jt::KeyCode::Space);

Expand Down
Loading

0 comments on commit d227d73

Please sign in to comment.