Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nextapp #10

Merged
merged 9 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.24)

if (NOT DEFINED MYSQLPOOL_VERSION)
set(MYSQLPOOL_VERSION 0.0.1)
set(MYSQLPOOL_VERSION 0.0.2)
endif()

project(mysqlpool-cpp
Expand All @@ -19,19 +19,21 @@ option(MYSQLPOOL_WITH_EXAMPLES "Compile examples" ON)
option(MYSQLPOOL_EMBEDDED "Do not try to install dependencies" OFF)

set(MYSQLPOOL_LOGGER "clog" CACHE STRING "Log system to use. One of 'clog', 'internal', 'logfault', 'boost' or 'none'")
set(MYSQLPOOL_LOG_LEVEL_STR "info" CACHE STRING "Minimum log level to enable")
set(MYSQLPOOL_LOG_LEVEL_STR "info" CACHE STRING "Minimum log level to enable. One of 'none', error', 'warn', 'info', 'debug', 'trace'")

set(MYSQLPOOL_DBUSER "MYSQLPOOL_DBUSER" CACHE STRING "Environment variable to get login user name name from")
set(MYSQLPOOL_DBPASSW "MYSQLPOOL_DBPASSW" CACHE STRING "Environment variable to get user login password from")
set(MYSQLPOOL_DATABASE "MYSQLPOOL_DATABASE" CACHE STRING "Environment variable to get database name from")
set(MYSQLPOOL_HOST "MYSQLPOOL_HOST" CACHE STRING "Environment variable to get the dbservers hostname or IP address from")
set(MYSQLPOOL_PORT "MYSQLPOOL_PORT" CACHE STRING "Environment variable to get the dbservers port number from")
set(MYSQLPOOL_DBHOST "MYSQLPOOL_DBHOST" CACHE STRING "Environment variable to get the dbservers hostname or IP address from")
set(MYSQLPOOL_DBPORT "MYSQLPOOL_DBPORT" CACHE STRING "Environment variable to get the dbservers port number from")
set(MYSQLPOOL_DB_TLS_MODE "MYSQLPOOL_DB_TLS_MODE" CACHE STRING "Environment variable to get the TLS mode from . One of: 'disable', 'enable', 'require'")

set(DEFAULT_MYSQLPOOL_DBUSER "" CACHE STRING "Default db user")
set(DEFAULT_MYSQLPOOL_DBPASSW "" CACHE STRING "Default db password")
set(DEFAULT_MYSQLPOOL_DATABASE "" CACHE STRING "Default database name")
set(DEFAULT_MYSQLPOOL_HOST "localhost" CACHE STRING "Default port to the server")
set(DEFAULT_MYSQLPOOL_HOST "localhost" CACHE STRING "Default host for the server")
set(DEFAULT_MYSQLPOOL_PORT 3306 CACHE STRING "Default port to the server")
set(DEFAULT_MYSQLPOOL_TLS_MODE "enable" CACHE STRING "TLS requirements. One of 'disable', 'enable', 'require'")

if (MYSQLPOOL_LOGGER STREQUAL "clog")
set(MYSQLPOOL_LOG_WITH_CLOG ON)
Expand Down Expand Up @@ -88,6 +90,19 @@ set(MYSQLPOOL_ROOT ${CMAKE_CURRENT_SOURCE_DIR})

message(STATUS "Using ${CMAKE_CXX_COMPILER}")

if (MYSQLPOOL_WITH_CONAN)
if (MYSQLPOOL_LOG_WITH_LOGFAULT)
message("Conan is in use. Will use that to get logfault")
#find_package (Logfault REQUIRED)
message("CMAKE_INCLUDE_PATH: ${CMAKE_INCLUDE_PATH}")
endif()
elseif (MYSQLPOOL_EMBEDDED)
message("Using dependencies as they are installed on the system.")
else()
include(cmake/3rdparty.cmake)
endif()


find_package(Threads REQUIRED)
find_package(ZLIB REQUIRED)
find_package(OpenSSL REQUIRED)
Expand Down Expand Up @@ -128,12 +143,6 @@ if (MYSQLPOOL_WITH_TESTS)
add_subdirectory(tests)
endif()

if (MYSQLPOOL_EMBEDDED)
message("Using dependencies as they are installed on the system.")
else()
include(cmake/3rdparty.cmake)
endif()

add_subdirectory(src)

# We create a configuration file so that other code that include our header files gets the correct configuration.
Expand Down
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,38 @@ Logging in C++ is something that many people have strong opinions about. My opin
that it must be possible. Mysqlpool-cpp offer several compile time alternatives.

**Loggers**
- **clog** Just log to std::clog with simple formatting.
- **clog** Just log to std::clog with simple formatting. Note that this logger does not support any runtime log-level. It will use the log-level set in `MYSQLPOOL_LOG_LEVEL_STR`.
- **internal** Allows you to forward the log events from the library to whatever log system you use.
- **logfault** Use the [logfault](https://github.com/jgaa/logfault) logger. This require that this logger is used project wide, which is unusual. Except for my own projects.
- **boost** Uses Boost.log, via `BOOST_LOG_TRIVIAL`. This require that this logger is used project wide.
- **none** Does not log anything.

You can specify your chosen logger via the `MYSQLPOOL_LOGGER` CMake variable. For example `cmake .. -CMYSQLPOOL_LOGGER=boost`.

If you use the **internal** logger, here is an example on how to use it. In the lamda
passed to SetHandler, you can bridge it to the logger you use in your application:

```C++
void BridgeLogger(jgaa::mysqlpool::LogLevel llevel = jgaa::mysqlpool::LogLevel::INFO) {

jgaa::mysqlpool::Logger::Instance().SetLogLevel(llevel);

jgaa::mysqlpool::Logger::Instance().SetHandler([](jgaa::mysqlpool::LogLevel level,
const std::string& msg) {
static constexpr auto levels = std::to_array<std::string_view>({"NONE", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"});

const auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());

std::clog << std::put_time(std::localtime(&now), "%c") << ' '
<< levels.at(static_cast<size_t>(level))
<< ' ' << std::this_thread::get_id() << ' '
<< msg << std::endl;
});

MYSQLPOOL_LOG_INFO_("Logging on level " << level << ". Boost version is " << BOOST_LIB_VERSION);
}
```

**Log levels**
When you develop your code, trace level logging can be useful. For example, the logging of SQL statements
happens on **trace** log level. In production code you will probably never user trace level. Probably not
Expand All @@ -119,6 +143,29 @@ For example: `cmake .. -CMYSQLPOOL_LOG_LEVEL_STR=info` will remove all trace and
the code. Even if you run your application with trace log level, mysqlpool-cpp will only show messages
with level **info**, **warning** and **error**.

## CMake options and variables
| name | type | explanation |
|------|------|-------------|
| DEFAULT_MYSQLPOOL_DATABASE | string | Default database name |
| DEFAULT_MYSQLPOOL_DBPASSW | string | Default db password |
| DEFAULT_MYSQLPOOL_DBUSER | string | Default db user |
| DEFAULT_MYSQLPOOL_HOST | string | Default host for the server |
| DEFAULT_MYSQLPOOL_PORT | string | Default port to the server |
| DEFAULT_MYSQLPOOL_TLS_MODE | string | TLS requirements |
| LOGFAULT_DIR | string | Path to the Logfault header only library. If used, CMake will not install it from *cmake/3rdparty.cmake*" |
| MYSQLPOOL_DBHOST | string | Environment variable to get the dbservers hostname or IP address from |
| MYSQLPOOL_DBPASSW | string | Environment variable to get user login password from. |
| MYSQLPOOL_DBPORT | string | Environment variable to get the dbservers port number from |
| MYSQLPOOL_DBUSER | string | Environment variable to get login user name name from. |
| MYSQLPOOL_DB_TLS_MODE | string | Environment variable to get the TLS mode from . One of: 'disable', 'enable', 'require' |
| MYSQLPOOL_EMBEDDED | bool | Tells CMake not to install dependencies from *cmake/3rdparty.cmake* |
| MYSQLPOOL_LOGGER | string | Log system to use. One of 'clog', 'internal', 'logfault', 'boost' or 'none'. |
| MYSQLPOOL_LOG_LEVEL_STR | string | Minimum log level to enable. Minimum log level to enable. One of 'none', error', 'warn', 'info', 'debug', 'trace'. |
| MYSQLPOOL_WITH_CONAN | bool | Tells CMake that conan is in charge." |
| MYSQLPOOL_WITH_EXAMPLES | bool | Builds the example |
| MYSQLPOOL_WITH_INTGRATION_TESTS | bool | Enables integration tests |
| MYSQLPOOL_WITH_TESTS | bool | Enables unit tests |

## Use

When you use the library, you can ask for a handle to a database connection. The connection
Expand Down
4 changes: 2 additions & 2 deletions cmake/3rdparty.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if (MYSQLPOOL_LOG_WITH_LOGFAULT)
# Assume that we are using a library that might not be available yet
message("LOGFAULT_ROOT: ${LOGFAULT_ROOT}")
set(LOGFAULT_DIR ${LOGFAULT_ROOT})
include_directories(${LOGFAULT_DIR})
#include_directories(${LOGFAULT_DIR})
else()
find_path(LOGFAULT_DIR NAMES logfault.h PATH_SUFFIXES logfault)
endif()
Expand All @@ -22,7 +22,7 @@ if (MYSQLPOOL_LOG_WITH_LOGFAULT)
if (NOT LOGFAULT_DIR STREQUAL "LOGFAULT_DIR-NOTFOUND" )
message ("Using existing logfault at: ${LOGFAULT_DIR}")
add_library(logfault INTERFACE IMPORTED)

set(LOGFAULT_INCLUDEDIR ${LOGFAULT_DIR})
else()
message ("Embedding logfault header only library")
set(MYSQLPOOL_LOGDEP ${RESTC_CPP_EXTERNAL_DEPS} embeddedLogfault)
Expand Down
41 changes: 29 additions & 12 deletions conanfile.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout, CMakeDeps
from conan.tools.build import check_max_cppstd, check_min_cppstd

class mysqlpoolRecipe(ConanFile):
name = "mysqlpool"
version = "0.1.0"
version = "0.0.2"

# Optional metadata
license = " BSL-1.0"
Expand All @@ -15,31 +15,45 @@ class mysqlpoolRecipe(ConanFile):

# Binary configuration
settings = "os", "compiler", "build_type", "arch"
options = {"shared": [True, False], "fPIC": [True, False], "logger": ["clog", "internal", "boost", "none"], "log_level": ["trace", "debug", "info", "warn"]}
default_options = {"shared": False, "fPIC": True, "logger": "clog", "log_level": "info"}
options = {"logger": ["logfault", "clog", "internal", "boost", "none"],
"log_level": ["trace", "debug", "info", "warn"],
"integration_tests": [True, False],
"env_prefix": ["ANY"]}
default_options = {"logger": "clog",
"log_level": "info",
"integration_tests": False,
"env_prefix": "MYSQLPOOL"}

# Sources are located in the same place as this recipe, copy them to the recipe
exports_sources = "config.h.template", "CMakeLists.txt", "src/*", "include/*", "tests/*", "cmake/*"

#def config_options(self):
exports_sources = "config.h.template", "CMakeLists.txt", "src/*", "include/*", "tests/*", "cmake/*", "examples/*"

def layout(self):
cmake_layout(self)

def generate(self):
tc = CMakeToolchain(self)
deps = CMakeDeps(self)
deps.generate()

tc = CMakeToolchain(self, generator="Ninja")

tc.variables["MYSQLPOOL_LOGGER"] = self.options.logger
tc.variables["MYSQLPOOL_LOG_LEVEL_STR"] = self.options.log_level
tc.variables["MYSQLPOOL_WITH_CONAN"] = True
tc.variables["MYSQLPOOL_WITH_TESTS"] = not self.conf.get("tools.build:skip_test", default=False)
tc.variables["MYSQLPOOL_WITH_INTEGRATION_TESTS"] = self.options.integration_tests
tc.variables["MYSQLPOOL_DBUSER"] = str(self.options.env_prefix) + "_DBUSER"
tc.variables["MYSQLPOOL_DBPASSW"] = str(self.options.env_prefix) + "_DBPASS"
tc.variables["MYSQLPOOL_DBHOST"] = str(self.options.env_prefix) + "_DBHOST"
tc.variables["MYSQLPOOL_DBPORT"] = str(self.options.env_prefix) + "_DBPORT"
tc.variables["MYSQLPOOL_DATABASE"] = str(self.options.env_prefix) + "_DATABASE"
tc.variables["MYSQLPOOL_TLS_MODE"] = str(self.options.env_prefix) + "_TLS_MODE"

tc.generate()

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
# if not self.conf.get("tools.build:skip_test", default=False):
# self.run(os.path.join(test_folder, "unit_tests"))
if not self.conf.get("tools.build:skip_test", default=False):
cmake.test()

Expand All @@ -49,15 +63,18 @@ def package(self):

def package_info(self):
self.cpp_info.libs = ["mysqlpool"]
self.cpp_info.set_property("cmake_target_name", "mysqlpool::mysqlpool")

def validate(self):
check_min_cppstd(self, "20")

def requirements(self):
#self.requires("boost/[>=1.84.0]")
self.requires("zlib/[~1.3]")
self.requires("openssl/[~3]")
self.requires("boost/[>=1.83.0]")
self.test_requires("gtest/[>=1.14]")
self.requires("logfault/[>=0.5.0]")
if not self.conf.get("tools.build:skip_test", default=False):
self.test_requires("gtest/[>=1.14]")

# def test(self):
# if can_run(self):
Expand Down
7 changes: 5 additions & 2 deletions config.h.template
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
#define MYSQLPOOL_DBPASSW "${MYSQLPOOL_DBPASSW}"
#define MYSQLPOOL_DATABASE "${MYSQLPOOL_DATABASE}"
#define MYSQLPOOL_DBUSER "${MYSQLPOOL_DBUSER}"
#define MYSQLPOOL_PORT "${MYSQLPOOL_PORT}"
#define MYSQLPOOL_HOST "${MYSQLPOOL_HOST}"
#define MYSQLPOOL_DBPORT "${MYSQLPOOL_DBPORT}"
#define MYSQLPOOL_DBHOST "${MYSQLPOOL_DBHOST}"
#define DEFAULT_MYSQLPOOL_DBUSER "${DEFAULT_MYSQLPOOL_DBUSER}"
#define DEFAULT_MYSQLPOOL_DBPASSW "${DEFAULT_MYSQLPOOL_DBPASSW}"
#define DEFAULT_MYSQLPOOL_DATABASE "${DEFAULT_MYSQLPOOL_DATABASE}"
#define DEFAULT_MYSQLPOOL_HOST "${DEFAULT_MYSQLPOOL_HOST}"
#define DEFAULT_MYSQLPOOL_PORT "${DEFAULT_MYSQLPOOL_PORT}"
#define MYSQLPOOL_VERSION "${MYSQLPOOL_VERSION}"

#define MYSQLPOOL_DB_TLS_MODE "${MYSQLPOOL_DB_TLS_MODE}"
#define DEFAULT_MYSQLPOOL_TLS_MODE "${DEFAULT_MYSQLPOOL_TLS_MODE}"
20 changes: 20 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM mariadb:latest

# Copy certificates

COPY server-cert.pem /etc/mysql/server-cert.pem
COPY server-key.pem /etc/mysql/server-key.pem
COPY ca.pem /etc/mysql/cacert.pem

RUN chmod 0444 /etc/mysql/cacert.pem \
/etc/mysql/server-key.pem \
/etc/mysql/server-cert.pem

#Enable TLS in the MariaDB configuration
RUN echo '\n\
[mariadb]\n\
ssl-ca = /etc/mysql/cacert.pem\n\
ssl-cert = /etc/mysql/server-cert.pem\n\
ssl-key = /etc/mysql/server-key.pem\n\
require-secure-transport = on\n\
' >> /etc/mysql/my.cnf
23 changes: 23 additions & 0 deletions docker/create_certs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh

echo "Creating self sighed certs with 10 years validity..."

root_subj="/C=EU/O=The Last Viking LTD/OU=Root CA/CN=rootca.localhost"
server_subj="/C=EU/O=The Last Viking LTD/CN=localhost"


openssl genrsa 2048 > ca-key.pem

openssl req -new -x509 -nodes -days 365000 -key ca-key.pem -out ca.pem -subj "$root_subj"

#openssl req -newkey rsa:2048 -days 365000 -nodes -keyout server-key.pem -out server-req.pem -subj "$server_subj"
openssl req -newkey rsa:2048 -nodes -keyout server-key.pem -out server-req.pem -subj "$server_subj"
#openssl req -newkey rsa:2048 -x509 -days 365000 -nodes -keyout server-key.pem -out server-req.pem -subj "$server_subj"

openssl rsa -in server-key.pem -out server-key.pem

openssl x509 -req -in server-req.pem -days 365000 -CA ca.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem

openssl verify -CAfile ca.pem server-cert.pem

echo "Done";
4 changes: 4 additions & 0 deletions examples/simple/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ project(simple_sql_app
LANGUAGES CXX
)

message("examples: LOGFAULT_INCLUDEDIR=${LOGFAULT_INCLUDEDIR}")

add_executable(${PROJECT_NAME} main.cpp fun_with_sql.cpp)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 20)
target_link_libraries(${PROJECT_NAME} mysqlpool)

target_include_directories(${PROJECT_NAME} PRIVATE
${CMAKE_INCLUDE_PATH}
$<BUILD_INTERFACE:${Boost_INCLUDE_DIR}>
$<BUILD_INTERFACE:${MYSQLPOOL_ROOT}/include>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/generated-include>
Expand All @@ -17,5 +20,6 @@ target_include_directories(${PROJECT_NAME} PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$Boost_INCLUDE_DIR
${LOGFAULT_INCLUDEDIR}
)

Loading
Loading