Skip to content

Commit

Permalink
fix: refactor code style (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrogTheFrog authored May 7, 2024
1 parent a4e4921 commit 8fa9d69
Show file tree
Hide file tree
Showing 11 changed files with 297 additions and 297 deletions.
52 changes: 26 additions & 26 deletions src/common/include/displaydevice/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ namespace display_device {
*
* @note A lazy-evaluated, correctly-destroyed, thread-safe singleton pattern is used here (https://stackoverflow.com/a/1008289).
*/
class logger_t {
class Logger {
public:
/**
* @brief Defines the possible log levels.
* @note Level implicitly includes all other levels below it.
* @note All levels are in lower-case on purpose to fit the "BOOST_LOG(info)" style.
*/
enum class log_level_e {
enum class LogLevel {
verbose = 0,
debug,
info,
Expand All @@ -32,18 +32,18 @@ namespace display_device {
/**
* @brief Defines the callback type for log data re-routing.
*/
using callback_t = std::function<void(const log_level_e, std::string)>;
using Callback = std::function<void(LogLevel, std::string)>;

/**
* @brief Get the singleton instance.
* @returns Singleton instance for the class.
*
* EXAMPLES:
* ```cpp
* logger_t& logger { logger_t::get() };
* Logger& logger { Logger::get() };
* ```
*/
static logger_t &
static Logger &
get();

/**
Expand All @@ -52,11 +52,11 @@ namespace display_device {
*
* EXAMPLES:
* ```cpp
* logger_t::get().set_log_level(logger_t::log_level_e::Info);
* Logger::get().setLogLevel(Logger::LogLevel::Info);
* ```
*/
void
set_log_level(log_level_e log_level);
setLogLevel(LogLevel log_level);

/**
* @brief Check if log level is currently enabled.
Expand All @@ -65,25 +65,25 @@ namespace display_device {
*
* EXAMPLES:
* ```cpp
* const bool is_enabled { logger_t::get().is_log_level_enabled(logger_t::log_level_e::Info) };
* const bool is_enabled { Logger::get().isLogLevelEnabled(Logger::LogLevel::Info) };
* ```
*/
[[nodiscard]] bool
is_log_level_enabled(log_level_e log_level) const;
isLogLevelEnabled(LogLevel log_level) const;

/**
* @brief Set custom callback for writing the logs.
* @param callback New callback to be used or nullptr to reset to the default.
*
* EXAMPLES:
* ```cpp
* logger_t::get().set_custom_callback([](const log_level_e level, std::string value){
* Logger::get().setCustomCallback([](const LogLevel level, std::string value){
* // write to file or something
* });
* ```
*/
void
set_custom_callback(callback_t callback);
setCustomCallback(Callback callback);

/**
* @brief Write the string to the output (via callback) if the log level is enabled.
Expand All @@ -92,65 +92,65 @@ namespace display_device {
*
* EXAMPLES:
* ```cpp
* logger_t::get().write(logger_t::log_level_e::Info, "Hello World!");
* Logger::get().write(Logger::LogLevel::Info, "Hello World!");
* ```
*/
void
write(log_level_e log_level, std::string value);
write(LogLevel log_level, std::string value);

/**
* @brief A deleted copy constructor for singleton pattern.
* @note Public to ensure better compiler error message.
*/
logger_t(logger_t const &) = delete;
Logger(Logger const &) = delete;

/**
* @brief A deleted assignment operator for singleton pattern.
* @note Public to ensure better compiler error message.
*/
void
operator=(logger_t const &) = delete;
operator=(Logger const &) = delete;

private:
/**
* @brief A private constructor to ensure the singleton pattern.
*/
explicit logger_t();
explicit Logger();

log_level_e m_enabled_log_level; /**< The currently enabled log level. */
callback_t m_custom_callback; /**< Custom callback to pass log data to. */
LogLevel m_enabled_log_level; /**< The currently enabled log level. */
Callback m_custom_callback; /**< Custom callback to pass log data to. */
};

/**
* @brief A helper class for accumulating output via the stream operator and then writing it out at once.
*/
class log_writer_t {
class LogWriter {
public:
/**
* @brief Constructor scoped writer utility.
* @param log_level Level to be used when writing out the output.
*/
explicit log_writer_t(logger_t::log_level_e log_level);
explicit LogWriter(Logger::LogLevel log_level);

/**
* @brief Write out the accumulated output.
*/
virtual ~log_writer_t();
virtual ~LogWriter();

/**
* @brief Stream value to the buffer.
* @param value Arbitrary value to be written to the buffer.
* @returns Reference to the writer utility for chaining the operators.
*/
template <class T>
log_writer_t &
LogWriter &
operator<<(T &&value) {
m_buffer << std::forward<T>(value);
return *this;
}

private:
logger_t::log_level_e m_log_level; /**< Log level to be used. */
Logger::LogLevel m_log_level; /**< Log level to be used. */
std::ostringstream m_buffer; /**< Buffer to hold all the output. */
};
} // namespace display_device
Expand All @@ -164,6 +164,6 @@ namespace display_device {
* DD_LOG(error) << "OH MY GAWD!";
* ```
*/
#define DD_LOG(level) \
for (bool is_enabled { display_device::logger_t::get().is_log_level_enabled(display_device::logger_t::log_level_e::level) }; is_enabled; is_enabled = false) \
display_device::log_writer_t(display_device::logger_t::log_level_e::level)
#define DD_LOG(level) \
for (bool is_enabled { display_device::Logger::get().isLogLevelEnabled(display_device::Logger::LogLevel::level) }; is_enabled; is_enabled = false) \
display_device::LogWriter(display_device::Logger::LogLevel::level)
40 changes: 20 additions & 20 deletions src/common/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@
#include <mutex>

namespace display_device {
logger_t &
logger_t::get() {
static logger_t instance;
Logger &
Logger::get() {
static Logger instance;
return instance;
}

void
logger_t::set_log_level(const log_level_e log_level) {
Logger::setLogLevel(LogLevel log_level) {
m_enabled_log_level = log_level;
}

bool
logger_t::is_log_level_enabled(const log_level_e log_level) const {
const auto log_level_v { static_cast<std::underlying_type_t<log_level_e>>(log_level) };
const auto enabled_log_level_v { static_cast<std::underlying_type_t<log_level_e>>(m_enabled_log_level) };
Logger::isLogLevelEnabled(LogLevel log_level) const {
const auto log_level_v { static_cast<std::underlying_type_t<LogLevel>>(log_level) };
const auto enabled_log_level_v { static_cast<std::underlying_type_t<LogLevel>>(m_enabled_log_level) };
return log_level_v >= enabled_log_level_v;
}

void
logger_t::set_custom_callback(callback_t callback) {
Logger::setCustomCallback(Callback callback) {
m_custom_callback = std::move(callback);
}

void
logger_t::write(const log_level_e log_level, std::string value) {
if (!is_log_level_enabled(log_level)) {
Logger::write(const LogLevel log_level, std::string value) {
if (!isLogLevelEnabled(log_level)) {
return;
}

Expand Down Expand Up @@ -70,19 +70,19 @@ namespace display_device {

// Log level
switch (log_level) {
case log_level_e::verbose:
case LogLevel::verbose:
stream << "VERBOSE: ";
break;
case log_level_e::debug:
case LogLevel::debug:
stream << "DEBUG: ";
break;
case log_level_e::info:
case LogLevel::info:
stream << "INFO: ";
break;
case log_level_e::warning:
case LogLevel::warning:
stream << "WARNING: ";
break;
case log_level_e::error:
case LogLevel::error:
stream << "ERROR: ";
break;
default:
Expand All @@ -98,14 +98,14 @@ namespace display_device {
std::cout << stream.rdbuf() << std::endl;
}

logger_t::logger_t():
m_enabled_log_level { log_level_e::info } {
Logger::Logger():
m_enabled_log_level { LogLevel::info } {
}

log_writer_t::log_writer_t(const logger_t::log_level_e log_level):
LogWriter::LogWriter(const Logger::LogLevel log_level):
m_log_level { log_level } {}

log_writer_t::~log_writer_t() {
logger_t::get().write(m_log_level, m_buffer.str());
LogWriter::~LogWriter() {
Logger::get().write(m_log_level, m_buffer.str());
}
} // namespace display_device
26 changes: 13 additions & 13 deletions src/windows/include/displaydevice/windows/winapilayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ namespace display_device {
*/
class WinApiLayer: public WinApiLayerInterface {
public:
/** For details @see WinApiLayerInterface::get_error_string */
/** For details @see WinApiLayerInterface::getErrorString */
[[nodiscard]] std::string
get_error_string(LONG error_code) const override;
getErrorString(LONG error_code) const override;

/** For details @see WinApiLayerInterface::query_display_config */
[[nodiscard]] std::optional<path_and_mode_data_t>
query_display_config(query_type_e type) const override;
/** For details @see WinApiLayerInterface::queryDisplayConfig */
[[nodiscard]] std::optional<PathAndModeData>
queryDisplayConfig(QueryType type) const override;

/** For details @see WinApiLayerInterface::get_device_id */
/** For details @see WinApiLayerInterface::getDeviceId */
[[nodiscard]] std::string
get_device_id(const DISPLAYCONFIG_PATH_INFO &path) const override;
getDeviceId(const DISPLAYCONFIG_PATH_INFO &path) const override;

/** For details @see WinApiLayerInterface::get_monitor_device_path */
/** For details @see WinApiLayerInterface::getMonitorDevicePath */
[[nodiscard]] std::string
get_monitor_device_path(const DISPLAYCONFIG_PATH_INFO &path) const override;
getMonitorDevicePath(const DISPLAYCONFIG_PATH_INFO &path) const override;

/** For details @see WinApiLayerInterface::get_friendly_name */
/** For details @see WinApiLayerInterface::getFriendlyName */
[[nodiscard]] std::string
get_friendly_name(const DISPLAYCONFIG_PATH_INFO &path) const override;
getFriendlyName(const DISPLAYCONFIG_PATH_INFO &path) const override;

/** For details @see WinApiLayerInterface::get_display_name */
/** For details @see WinApiLayerInterface::getDisplayName */
[[nodiscard]] std::string
get_display_name(const DISPLAYCONFIG_PATH_INFO &path) const override;
getDisplayName(const DISPLAYCONFIG_PATH_INFO &path) const override;
};
} // namespace display_device
Loading

0 comments on commit 8fa9d69

Please sign in to comment.