Skip to content

Commit

Permalink
Merge pull request #72 from Klebert-Engineering/feature/spdlog
Browse files Browse the repository at this point in the history
Extended Logging
  • Loading branch information
josephbirkner committed Jan 25, 2022
2 parents b8892ea + 6d444e4 commit c04676e
Show file tree
Hide file tree
Showing 13 changed files with 320 additions and 104 deletions.
22 changes: 15 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ if (NOT TARGET wheel)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/deps/python-cmake-wheel")
include(python-wheel)
set(WHEEL_DEPLOY_DIRECTORY "${ZSWAG_DEPLOY_DIR}/wheel")
endif ()
endif()

if (NOT TARGET yaml-cpp)
add_subdirectory(deps/yaml-cpp)
endif ()
endif()

if (NOT TARGET stx)
FetchContent_Declare(stx
Expand All @@ -55,30 +55,38 @@ if (NOT TARGET speedyj)
FetchContent_MakeAvailable(speedyj)
endif()

if (NOT TARGET spdlog)
FetchContent_Declare(spdlog
GIT_REPOSITORY "https://github.com/gabime/spdlog.git"
GIT_TAG "v1.x"
GIT_SHALLOW ON)
FetchContent_MakeAvailable(spdlog)
endif()

if (NOT TARGET Catch2)
add_subdirectory(deps/Catch2)
endif ()
endif()

if (NOT TARGET httplib)
add_subdirectory(deps/cpp-httplib)
target_compile_definitions(httplib
INTERFACE
CPPHTTPLIB_OPENSSL_SUPPORT)
target_link_libraries(httplib INTERFACE OpenSSL::SSL)
endif ()
endif()

if (NOT TARGET keychain)
add_subdirectory(deps/keychain)
endif ()
endif()

if (NOT TARGET pybind11)
add_subdirectory(deps/pybind11)
endif ()
endif()

if (NOT TARGET ZserioCppRuntime)
add_subdirectory(deps/zserio/compiler/extensions/cpp/runtime/src)
set(ZSERIO_REPO_ROOT "${CMAKE_CURRENT_LIST_DIR}/deps/zserio")
endif ()
endif()

if (NOT TARGET zserio-cmake-helper)
add_subdirectory(deps/zserio-cmake-helper)
Expand Down
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ struct Response {
};
service MyService {
Response my_api(Request);
Response myApi(Request);
};
```

Expand Down Expand Up @@ -440,8 +440,9 @@ int main (int argc, char* argv[])
// Create the request object
auto request = myapp_services::services::Request(2);

// Invoke the REST endpoint
auto response = myServiceClient.my_api(request);
// Invoke the REST endpoint. Mind that your method-
// name from the schema is appended with a "...Method" suffix.
auto response = myServiceClient.myApiMethod(request);

// Print the response
std::cout << "Got " << response.getValue() << std::endl;
Expand All @@ -458,6 +459,20 @@ int main (int argc, char* argv[])
config from being considered at all, set `HTTP_SETTINGS_FILE` to empty,
e.g. via `setenv`.

## Client Environment Settings

Both the Python and C++ Clients can be configured using the following
environment variables:

| Variable Name | Details |
| ------------- | --------- |
| `HTTP_SETTINGS_FILE` | Path to settings file for HTTP proxies and authentication, see [next section](#persistent-http-headers-proxy-cookie-and-authentication) |
| `HTTP_LOG_LEVEL` | Verbosity level for console/log output. Set to `debug` for detailed output. |
| `HTTP_LOG_FILE` | Logfile-path (including filename) to redirect console output. The log will rotate with three files (`HTTP_LOG_FILE`, `HTTP_LOG_FILE-1`, `HTTP_LOG_FILE-2`). |
| `HTTP_LOG_FILE_MAXSIZE` | Maximum size of the logfile, in bytes. Defaults to 1GB. |
| `HTTP_TIMEOUT` | Timeout for HTTP requests (connection+transfer) in seconds. Defaults to 60s. |
| `HTTP_SSL_STRICT` | Set to any nonempty value for strict SSL certificate validation. |

## Persistent HTTP Headers, Proxy, Cookie and Authentication

Both the Python `OAClient` and C++ `HttpLibHttpClient` read a YAML file
Expand Down
2 changes: 1 addition & 1 deletion deps/zserio-cmake-helper
5 changes: 4 additions & 1 deletion libs/httpcl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ add_library(httpcl STATIC
include/httpcl/http-client.hpp
include/httpcl/http-settings.hpp
include/httpcl/uri.hpp
include/httpcl/log.hpp
src/http-client.cpp
src/http-settings.cpp
src/uri.cpp)
src/uri.cpp
src/log.cpp)

target_compile_features(httpcl
INTERFACE
Expand All @@ -19,6 +21,7 @@ target_link_libraries(httpcl
PRIVATE
stx
PUBLIC
spdlog
keychain
httplib::httplib
yaml-cpp
Expand Down
10 changes: 9 additions & 1 deletion libs/httpcl/include/httpcl/http-client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "http-settings.hpp"
#include "uri.hpp"
#include "log.hpp"

namespace httpcl
{
Expand All @@ -36,7 +37,9 @@ class IHttpClient
Error(Result result, std::string const& message)
: std::runtime_error(message)
, result(std::move(result))
{}
{
log().error(message);
}
};

virtual ~IHttpClient() = default;
Expand All @@ -60,6 +63,8 @@ class IHttpClient
class HttpLibHttpClient : public IHttpClient
{
public:
HttpLibHttpClient();

Result get(const std::string& uri,
const Config& config) override;
Result post(const std::string& uri,
Expand All @@ -74,6 +79,9 @@ class HttpLibHttpClient : public IHttpClient
Result patch(const std::string& uri,
const OptionalBodyAndContentType& body,
const Config& config) override;
private:
time_t timeoutSecs_ = 60.;
bool sslCertStrict_ = false;
};

class MockHttpClient : public IHttpClient
Expand Down
28 changes: 28 additions & 0 deletions libs/httpcl/include/httpcl/log.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include "spdlog/spdlog.h"

namespace httpcl
{

/**
* Obtain global logger, which is initialized from
* the following environment variables:
* - HTTP_LOG_LEVEL
* - HTTP_LOG_FILE
* - HTTP_LOG_FILE_MAXSIZE
*/
spdlog::logger& log();

/**
* Log a runtime error and return the throwable object.
* @param what Runtime error message.
* @return std::runtime_error to throw.
*/
template<typename error_t = std::runtime_error>
error_t logRuntimeError(std::string const& what) {
log().error(what);
return error_t(what);
}

}
37 changes: 26 additions & 11 deletions libs/httpcl/src/http-client.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "http-client.hpp"
#include "uri.hpp"

#include "httpcl/http-settings.hpp"

#include <httplib.h>

namespace
Expand All @@ -20,12 +18,16 @@ void applyQuery(httpcl::URIComponents& uri, httpcl::Config const& config) {
uri.addQuery(key, value);
}

auto makeClientAndApplyQuery(httpcl::URIComponents& uri, httpcl::Config const& config)
auto makeClientAndApplyQuery(
httpcl::URIComponents& uri,
httpcl::Config const& config,
time_t const& timeoutSecs,
bool const& sslCertStrict)
{
auto client = std::make_unique<httplib::Client>(uri.buildHost().c_str());
client->enable_server_certificate_verification(false);
client->set_connection_timeout(60000);
client->set_read_timeout(60000);
client->enable_server_certificate_verification(sslCertStrict);
client->set_connection_timeout(timeoutSecs);
client->set_read_timeout(timeoutSecs);
config.apply(*client);

applyQuery(uri, config);
Expand All @@ -39,11 +41,24 @@ namespace httpcl

using Result = HttpLibHttpClient::Result;

HttpLibHttpClient::HttpLibHttpClient() {
if (auto timeoutStr = std::getenv("HTTP_TIMEOUT")) {
try {
timeoutSecs_ = std::stoll(timeoutStr);
}
catch (std::exception& e) {
std::cerr << "Could not parse value of HTTP_TIMEOUT." << std::endl;
}
}
if (auto sslStrictFlagStr = std::getenv("HTTP_SSL_STRICT"))
sslCertStrict_ = !std::string(sslStrictFlagStr).empty();
}

Result HttpLibHttpClient::get(const std::string& uriStr,
const Config& config)
{
auto uri = URIComponents::fromStrRfc3986(uriStr);
return makeResult(makeClientAndApplyQuery(uri, config)->Get(
return makeResult(makeClientAndApplyQuery(uri, config, timeoutSecs_, sslCertStrict_)->Get(
uri.buildPath().c_str()));
}

Expand All @@ -52,7 +67,7 @@ Result HttpLibHttpClient::post(const std::string& uriStr,
const Config& config)
{
auto uri = URIComponents::fromStrRfc3986(uriStr);
return makeResult(makeClientAndApplyQuery(uri, config)->Post(
return makeResult(makeClientAndApplyQuery(uri, config, timeoutSecs_, sslCertStrict_)->Post(
uri.buildPath().c_str(),
body ? body->body : std::string(),
body ? body->contentType.c_str() : nullptr));
Expand All @@ -63,7 +78,7 @@ Result HttpLibHttpClient::put(const std::string& uriStr,
const Config& config)
{
auto uri = URIComponents::fromStrRfc3986(uriStr);
return makeResult(makeClientAndApplyQuery(uri, config)->Put(
return makeResult(makeClientAndApplyQuery(uri, config, timeoutSecs_, sslCertStrict_)->Put(
uri.buildPath().c_str(),
body ? body->body : std::string(),
body ? body->contentType.c_str() : nullptr));
Expand All @@ -74,7 +89,7 @@ Result HttpLibHttpClient::del(const std::string& uriStr,
const Config& config)
{
auto uri = URIComponents::fromStrRfc3986(uriStr);
return makeResult(makeClientAndApplyQuery(uri, config)->Delete(
return makeResult(makeClientAndApplyQuery(uri, config, timeoutSecs_, sslCertStrict_)->Delete(
uri.buildPath().c_str(),
body ? body->body : std::string(),
body ? body->contentType.c_str() : nullptr));
Expand All @@ -85,7 +100,7 @@ Result HttpLibHttpClient::patch(const std::string& uriStr,
const Config& config)
{
auto uri = URIComponents::fromStrRfc3986(uriStr);
return makeResult(makeClientAndApplyQuery(uri, config)->Patch(
return makeResult(makeClientAndApplyQuery(uri, config, timeoutSecs_, sslCertStrict_)->Patch(
uri.buildPath().c_str(),
body ? body->body : std::string(),
body ? body->contentType.c_str() : nullptr));
Expand Down
Loading

0 comments on commit c04676e

Please sign in to comment.