Skip to content

Commit

Permalink
Reapply "Implement logger interface"
Browse files Browse the repository at this point in the history
This reverts commit bb95d6d: Implement logger interface

- 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

In addition to that this commit this also:

- Fixes build on Windows
- Changes log strategy to be lazy, so we can log strings without impact
  performance if logging is disabled

Change-Id: If58d9c1ee7ea11390d858bd9815e6b9b11a9bee9
  • Loading branch information
Rafael Telles authored and alinaliBQ committed Dec 12, 2023
1 parent 37b43ef commit 509afd1
Show file tree
Hide file tree
Showing 15 changed files with 1,300 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ inline std::string GetCerts() {

#endif

const std::set<std::string, Connection::CaseInsensitiveComparator> BUILT_IN_PROPERTIES = {
const std::set<std::string, odbcabstraction::CaseInsensitiveComparator> BUILT_IN_PROPERTIES = {
FlightSqlConnection::HOST,
FlightSqlConnection::PORT,
FlightSqlConnection::USER,
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 <flight_sql/flight_sql_driver.h>


#define DEFAULT_MAXIMUM_FILE_SIZE 16777216
#define CONFIG_FILE_NAME "arrow-odbc.ini"

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::LogLevel_TRACE;
case 1:
return LogLevel::LogLevel_DEBUG;
case 2:
return LogLevel::LogLevel_INFO;
case 3:
return LogLevel::LogLevel_WARN;
case 4:
return LogLevel::LogLevel_ERROR;
default:
return LogLevel::LogLevel_OFF;
}
}
}

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

void FlightSqlDriver::RegisterLog() {
odbcabstraction::PropertyMap propertyMap;
driver::odbcabstraction::ReadConfigFile(propertyMap, CONFIG_FILE_NAME);

auto log_enable_iterator = propertyMap.find(SPDLogger::LOG_ENABLED);
auto log_enabled = log_enable_iterator != propertyMap.end() ?
odbcabstraction::AsBool(log_enable_iterator->second) : false;
if (!log_enabled) {
return;
}

auto log_path_iterator = propertyMap.find(SPDLogger::LOG_PATH);
auto log_path =
log_path_iterator != propertyMap.end() ? log_path_iterator->second : "";
if (log_path.empty()) {
return;
}

auto log_level_iterator = propertyMap.find(SPDLogger::LOG_LEVEL);
auto log_level =
ToLogLevel(log_level_iterator != propertyMap.end() ? std::stoi(log_level_iterator->second) : 1);
if (log_level == odbcabstraction::LogLevel_OFF) {
return;
}

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, log_level);
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() override;
};

}; // namespace flight_sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)

include_directories(include)

# Ensure fmt is loaded as header only
add_compile_definitions(FMT_HEADER_ONLY)

add_library(odbcabstraction
include/odbcabstraction/calendar_utils.h
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 +37,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 +54,21 @@ set_target_properties(odbcabstraction
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>/lib
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>/lib
)
target_link_libraries(odbcabstraction)

include(FetchContent)
FetchContent_Declare(
spdlog
URL https://github.com/gabime/spdlog/archive/76fb40d95455f249bd70824ecfcae7a8f0930fa3.zip
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
)
FetchContent_GetProperties(spdlog)
if(NOT spdlog_POPULATED)
FetchContent_Populate(spdlog)
endif()

add_library(spdlog INTERFACE)
target_include_directories(spdlog INTERFACE ${spdlog_SOURCE_DIR}/include)

add_dependencies(odbcabstraction spdlog)
target_include_directories(odbcabstraction PUBLIC ${spdlog_SOURCE_DIR}/include)
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2020-2022 Dremio Corporation
*
* See "LICENSE" for license information.
*/

#pragma once

#include <functional>
#include <string>

#include <spdlog/fmt/bundled/format.h>

#define __LAZY_LOG(LEVEL, ...) do { \
driver::odbcabstraction::Logger *logger = driver::odbcabstraction::Logger::GetInstance(); \
if (logger) { \
logger->log(driver::odbcabstraction::LogLevel::LogLevel_##LEVEL, [&]() { \
return fmt::format(__VA_ARGS__); \
}); \
} \
} while(0)
#define LOG_DEBUG(...) __LAZY_LOG(DEBUG, __VA_ARGS__)
#define LOG_INFO(...) __LAZY_LOG(INFO, __VA_ARGS__)
#define LOG_ERROR(...) __LAZY_LOG(ERROR, __VA_ARGS__)
#define LOG_TRACE(...) __LAZY_LOG(TRACE, __VA_ARGS__)
#define LOG_WARN(...) __LAZY_LOG(WARN, __VA_ARGS__)

namespace driver {
namespace odbcabstraction {

enum LogLevel {
LogLevel_TRACE,
LogLevel_DEBUG,
LogLevel_INFO,
LogLevel_WARN,
LogLevel_ERROR,
LogLevel_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::function<std::string(void)> &build_message) = 0;
};

} // namespace odbcabstraction
} // namespace driver
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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::function<std::string(void)> &build_message) override;
};

} // namespace odbcabstraction
} // namespace driver
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
namespace driver {
namespace odbcabstraction {

/// \brief Case insensitive comparator
struct CaseInsensitiveComparator
: std::binary_function<std::string, std::string, bool> {
bool operator()(const std::string &s1, const std::string &s2) const {
return boost::lexicographical_compare(s1, s2, boost::is_iless());
}
};

// PropertyMap is case-insensitive for keys.
typedef std::map<std::string, std::string, CaseInsensitiveComparator> PropertyMap;

class Statement;

/// \brief High-level representation of an ODBC connection.
Expand All @@ -38,20 +49,9 @@ class Connection {
PACKET_SIZE, // uint32_t - The Packet Size
};

/// \brief Case insensitive comparator
struct CaseInsensitiveComparator
: std::binary_function<std::string, std::string, bool> {
bool operator()(const std::string &s1, const std::string &s2) const {
return boost::lexicographical_compare(s1, s2, boost::is_iless());
}
};

typedef boost::variant<std::string, void*, uint64_t, uint32_t> Attribute;
typedef std::string Property;
typedef boost::variant<std::string, uint32_t, uint16_t> Info;
// ConnPropertyMap is case-insensitive for keys.
typedef std::map<std::string, Property, CaseInsensitiveComparator>
ConnPropertyMap;
typedef PropertyMap ConnPropertyMap;

/// \brief Establish the connection.
/// \param properties[in] properties used to establish the connection.
Expand Down
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() = 0;
};

} // namespace odbcabstraction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

#pragma once

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

namespace driver {
Expand Down Expand Up @@ -36,5 +38,9 @@ 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 509afd1

Please sign in to comment.