Skip to content

Commit

Permalink
Implement logger interface
Browse files Browse the repository at this point in the history
- Add an interface for logger
- Define Macro for each log level in the interface
- Add an implementation from the logger interface
- Add the spdlog dependency to the CMakeLists.txt

Change-Id: Ibf1426c86e6e28903619cba328bd6998d4c4f74d
  • Loading branch information
jcralmeida committed Jul 26, 2022
1 parent 591b83e commit 60dd060
Show file tree
Hide file tree
Showing 14 changed files with 1,304 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@ if(CMAKE_BUILD_TYPE STREQUAL "Release")
add_compile_definitions(NDEBUG)
endif()

add_compile_definitions(FMT_HEADER_ONLY)

include(ExternalProject)

message("Using spdlog from https://github.com/gabime/spdlog on tag v1.10.0")
ExternalProject_Add(
dep_spdlog
GIT_REPOSITORY "https://github.com/gabime/spdlog"
GIT_TAG "v1.10.0"
GIT_SHALLOW 1
UPDATE_COMMAND ""
PATCH_COMMAND ""
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/dep_spdlog-prefix/src/dep_spdlog-install
-DSPDLOG_BUILD_EXAMPLES=OFF
-DSPDLOG_BUILD_BENCH=OFF
-DSPDLOG_BUILD_TESTS=OFF
TEST_COMMAND ""
)

include_directories(${CMAKE_CURRENT_BINARY_DIR}/dep_spdlog-prefix/src/dep_spdlog/include)

# Fetch and include GTest
# Adapted from Google's documentation: https://google.github.io/googletest/quickstart-cmake.html#set-up-a-project
include(FetchContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,41 @@
*/

#include "flight_sql_connection.h"
#include "odbcabstraction/utils.h"
#include <odbcabstraction/spd_logger.h>
#include <odbcabstraction/platform.h>
#include <odbcabstraction/utils.h>
#include <flight_sql/flight_sql_driver.h>


#define DEFAULT_MAXIMUM_FILE_SIZE 16777216

namespace driver {
namespace flight_sql {

using odbcabstraction::Connection;
using odbcabstraction::OdbcVersion;
using odbcabstraction::LogLevel;
using odbcabstraction::SPDLogger;

namespace {
LogLevel ToLogLevel(int64_t level) {
switch (level) {
case 0:
return LogLevel::TRACE;
case 1:
return LogLevel::DEBUG;
case 2:
return LogLevel::INFO;
case 3:
return LogLevel::WARN;
case 4:
return LogLevel::ERROR;
default:
return LogLevel::OFF;
}
}
}

FlightSqlDriver::FlightSqlDriver()
: diagnostics_("Apache Arrow", "Flight SQL", OdbcVersion::V_3),
Expand All @@ -31,5 +58,52 @@ odbcabstraction::Diagnostics &FlightSqlDriver::GetDiagnostics() {
void FlightSqlDriver::SetVersion(std::string version) {
version_ = std::move(version);
}

void FlightSqlDriver::RegisterLog(const std::string &configFileName) {
odbcabstraction::PropertyMap propertyMap;
driver::odbcabstraction::ReadConfigFile(propertyMap, configFileName);

auto log_enable_iterator = propertyMap.find(SPDLogger::LOG_ENABLED);

auto log_enabled = log_enable_iterator != propertyMap.end() ?
odbcabstraction::AsBool(log_enable_iterator->second) : false;

auto log_path_iterator = propertyMap.find(SPDLogger::LOG_PATH);

auto log_path =
log_path_iterator != propertyMap.end() ? log_path_iterator->second : "";

if (*log_enabled && !log_path.empty()) {
auto log_level_iterator = propertyMap.find(SPDLogger::LOG_LEVEL);

auto log_level =
log_level_iterator != propertyMap.end() ? std::stoi(log_level_iterator->second) : 1;

auto maximum_file_size_iterator = propertyMap.find(SPDLogger::MAXIMUM_FILE_SIZE);

auto maximum_file_size = maximum_file_size_iterator != propertyMap.end() ?
std::stoi(maximum_file_size_iterator->second) : DEFAULT_MAXIMUM_FILE_SIZE;

auto maximum_file_quantity_iterator = propertyMap.
find(SPDLogger::FILE_QUANTITY);

auto maximum_file_quantity =
maximum_file_quantity_iterator != propertyMap.end() ? std::stoi(
maximum_file_quantity_iterator->second) : 1;

std::unique_ptr<odbcabstraction::SPDLogger> logger (new odbcabstraction::SPDLogger());

logger->init(maximum_file_quantity, maximum_file_size,
log_path, ToLogLevel(log_level));
odbcabstraction::Logger::SetInstance(std::move(logger));
} else {
std::unique_ptr<odbcabstraction::SPDLogger> logger (new odbcabstraction::SPDLogger());

logger->init(1, 1,
log_path, LogLevel::OFF);
odbcabstraction::Logger::SetInstance(std::move(logger));
}
}

} // namespace flight_sql
} // namespace driver
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class FlightSqlDriver : public odbcabstraction::Driver {
odbcabstraction::Diagnostics &GetDiagnostics() override;

void SetVersion(std::string version) override;

void RegisterLog(const std::string &configFileName) override;
};

}; // namespace flight_sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ add_library(odbcabstraction
include/odbcabstraction/diagnostics.h
include/odbcabstraction/error_codes.h
include/odbcabstraction/exceptions.h
include/odbcabstraction/logger.h
include/odbcabstraction/platform.h
include/odbcabstraction/spd_logger.h
include/odbcabstraction/types.h
include/odbcabstraction/utils.h
include/odbcabstraction/odbc_impl/AttributeUtils.h
Expand All @@ -32,7 +34,11 @@ add_library(odbcabstraction
diagnostics.cc
encoding.cc
exceptions.cc
logger.cc
spd_logger.cc
utils.cc
whereami.h
whereami.cc
odbc_impl/ODBCConnection.cc
odbc_impl/ODBCDescriptor.cc
odbc_impl/ODBCEnvironment.cc
Expand All @@ -45,4 +51,7 @@ set_target_properties(odbcabstraction
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>/lib
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>/lib
)

add_dependencies(odbcabstraction dep_spdlog)

target_link_libraries(odbcabstraction)
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2020-2022 Dremio Corporation
*
* See "LICENSE" for license information.
*/

#pragma once

#include <cstdint>
#include <map>
#include <string>

#include <boost/algorithm/string.hpp>
#include <spdlog/fmt/bundled/format.h>

#include <odbcabstraction/spi/connection.h>

#define LOG_DEBUG(...) driver::odbcabstraction::Logger::GetInstance()->log(driver::odbcabstraction::LogLevel::DEBUG, __VA_ARGS__);
#define LOG_INFO(...) driver::odbcabstraction::Logger::GetInstance()->log(driver::odbcabstraction::LogLevel::INFO, __VA_ARGS__);
#define LOG_ERROR(...) driver::odbcabstraction::Logger::GetInstance()->log(driver::odbcabstraction::LogLevel::ERROR, __VA_ARGS__);
#define LOG_TRACE(...) driver::odbcabstraction::Logger::GetInstance()->log(driver::odbcabstraction::LogLevel::TRACE, __VA_ARGS__);
#define LOG_WARN(...) driver::odbcabstraction::Logger::GetInstance()->log(driver::odbcabstraction::LogLevel::WARN, __VA_ARGS__);

namespace driver {
namespace odbcabstraction {

enum LogLevel { TRACE, DEBUG, INFO, WARN, ERROR, OFF };

class Logger {
protected:
Logger() = default;

public:
static Logger *GetInstance();
static void SetInstance(std::unique_ptr<Logger> logger);

virtual ~Logger() = default;

virtual void log(LogLevel level, const std::string &message) = 0;

virtual bool checkLogLevel(LogLevel called) = 0;

template <typename... Args>
void log(LogLevel level, fmt::format_string<Args...> fmt, Args &&... args) {
if(checkLogLevel(level)) {
log(level, fmt::format(fmt, args...));
}
};
};

} // namespace odbcabstraction
} // namespace driver
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2020-2022 Dremio Corporation
*
* See "LICENSE" for license information.
*/

#pragma once

#include "odbcabstraction/logger.h"

#include <cstdint>
#include <string>

#include <spdlog/spdlog.h>

namespace driver {
namespace odbcabstraction {

class SPDLogger : public Logger {
protected:
std::shared_ptr<spdlog::logger> logger_;

public:
static const std::string LOG_LEVEL;
static const std::string LOG_PATH;
static const std::string MAXIMUM_FILE_SIZE;
static const std::string FILE_QUANTITY;
static const std::string LOG_ENABLED;

SPDLogger() = default;
~SPDLogger();
SPDLogger(SPDLogger &other) = delete;

void operator=(const SPDLogger &) = delete;
void init(int64_t fileQuantity, int64_t maxFileSize,
const std::string &fileNamePrefix, LogLevel level);

void log(LogLevel level, const std::string &message) override;

bool checkLogLevel(LogLevel called) override;
};

} // namespace odbcabstraction
} // namespace driver
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class Driver {

/// \brief Sets the driver version.
virtual void SetVersion(std::string version) = 0;

/// \brief Register a log to be used by the system.
virtual void RegisterLog(const std::string &configFileName) = 0;
};

} // namespace odbcabstraction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
#pragma once

#include <string>
#include <odbcabstraction/logger.h>
#include <odbcabstraction/spi/connection.h>

namespace driver {
namespace odbcabstraction {

typedef std::map<std::string, std::string>
PropertyMap;

using driver::odbcabstraction::Connection;

/// Parse a string value to a boolean.
Expand All @@ -36,5 +40,8 @@ boost::optional<bool> AsBool(const Connection::ConnPropertyMap& connPropertyMap,
/// \exception std::out_of_range exception from \link std::stoi \endlink
boost::optional<int32_t> AsInt32(int32_t min_value, const Connection::ConnPropertyMap& connPropertyMap,
const std::string& property_name);


void ReadConfigFile(PropertyMap &properties, const std::string &configFileName);
} // namespace odbcabstraction
} // namespace driver
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2020-2022 Dremio Corporation
*
* See "LICENSE" for license information.
*/


#include <odbcabstraction/logger.h>

namespace driver {
namespace odbcabstraction {

static std::unique_ptr<Logger> odbc_logger_ = nullptr;

Logger *Logger::GetInstance() {
return odbc_logger_.get();
}

void Logger::SetInstance(std::unique_ptr<Logger>logger) {
odbc_logger_ = std::move(logger);
}

}
}
Loading

0 comments on commit 60dd060

Please sign in to comment.