From f05ce53b919ee4b4ec202ee69bdd4046f8bc19ce Mon Sep 17 00:00:00 2001 From: Carlos O'Ryan Date: Wed, 20 Jan 2021 20:18:49 +0000 Subject: [PATCH] refactor: use Boost.Log in some examples These examples will be used to create testing examples, i.e., show our users how to write unit, integration, and system tests. In those, we will want to capture the log output. --- examples/CMakeLists.txt | 3 ++- .../site/hello_world_pubsub/CMakeLists.txt | 22 +++++++++++++++++++ .../hello_world_pubsub/hello_world_pubsub.cc | 3 ++- examples/site/hello_world_pubsub/vcpkg.json | 1 + .../site/hello_world_storage/CMakeLists.txt | 22 +++++++++++++++++++ .../hello_world_storage.cc | 15 ++++++++----- examples/site/hello_world_storage/vcpkg.json | 1 + vcpkg.json | 3 ++- 8 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 examples/site/hello_world_pubsub/CMakeLists.txt create mode 100644 examples/site/hello_world_storage/CMakeLists.txt diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index ed6d6845..956ee5f7 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -17,6 +17,7 @@ find_package(storage_client REQUIRED) find_package(pubsub_client REQUIRED) find_package(fmt REQUIRED) +find_package(Boost REQUIRED COMPONENTS log) add_library( functions_framework_examples # cmake-format: sort @@ -66,7 +67,7 @@ if (BUILD_TESTING) target_link_libraries( ${target} PRIVATE functions_framework_examples googleapis_functions_framework - GTest::gmock_main GTest::gmock GTest::gtest) + Boost::log GTest::gmock_main GTest::gmock GTest::gtest) add_test(NAME ${target} COMMAND ${target}) endforeach () endif () diff --git a/examples/site/hello_world_pubsub/CMakeLists.txt b/examples/site/hello_world_pubsub/CMakeLists.txt new file mode 100644 index 00000000..a6201dca --- /dev/null +++ b/examples/site/hello_world_pubsub/CMakeLists.txt @@ -0,0 +1,22 @@ +# ~~~ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ~~~ + +find_package(googleapis_functions_framework) +find_package(Boost REQUIRED COMPONENTS log) + +add_library(functions_framework_cpp_function hello_world_pubsub.cc) +target_link_libraries(functions_framework_cpp_function + googleapis-c++::functions_framework Boost::log) diff --git a/examples/site/hello_world_pubsub/hello_world_pubsub.cc b/examples/site/hello_world_pubsub/hello_world_pubsub.cc index 6dca085c..2e7ed0d2 100644 --- a/examples/site/hello_world_pubsub/hello_world_pubsub.cc +++ b/examples/site/hello_world_pubsub/hello_world_pubsub.cc @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -31,7 +32,7 @@ void hello_world_pubsub(gcf::CloudEvent event) { // NOLINT } auto const payload = nlohmann::json::parse(event.data().value_or("{}")); auto name = decode_base64(payload["message"]["data"].get()); - std::cerr << "Hello " << (name.empty() ? "World" : name) << "\n"; + BOOST_LOG_TRIVIAL(info) << "Hello " << (name.empty() ? "World" : name); } // [END functions_helloworld_pubsub] diff --git a/examples/site/hello_world_pubsub/vcpkg.json b/examples/site/hello_world_pubsub/vcpkg.json index c05e5e04..5c802fe0 100644 --- a/examples/site/hello_world_pubsub/vcpkg.json +++ b/examples/site/hello_world_pubsub/vcpkg.json @@ -2,6 +2,7 @@ "name": "hello-world-pubsub", "version-string": "unversioned", "dependencies": [ + "boost-log", "boost-serialization", "functions-framework-cpp", "nlohmann-json" diff --git a/examples/site/hello_world_storage/CMakeLists.txt b/examples/site/hello_world_storage/CMakeLists.txt new file mode 100644 index 00000000..8ec293a7 --- /dev/null +++ b/examples/site/hello_world_storage/CMakeLists.txt @@ -0,0 +1,22 @@ +# ~~~ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ~~~ + +find_package(googleapis_functions_framework) +find_package(Boost REQUIRED COMPONENTS log) + +add_library(functions_framework_cpp_function hello_world_storage.cc) +target_link_libraries(functions_framework_cpp_function + googleapis-c++::functions_framework Boost::log) diff --git a/examples/site/hello_world_storage/hello_world_storage.cc b/examples/site/hello_world_storage/hello_world_storage.cc index c1a5b513..fddbb887 100644 --- a/examples/site/hello_world_storage/hello_world_storage.cc +++ b/examples/site/hello_world_storage/hello_world_storage.cc @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -27,11 +28,13 @@ void hello_world_storage(gcf::CloudEvent event) { // NOLINT return; } auto const payload = nlohmann::json::parse(event.data().value_or("{}")); - std::cout << "Event: " << event.id() << "\nEvent Type: " << event.type() - << "\nBucket: " << payload.value("bucket", "") - << "\nFile: " << payload.value("name", "") - << "\nMetageneration: " << payload.value("metageneration", "") - << "\nCreated: " << payload.value("timeCreated", "") - << "\nUpdated: " << payload.value("updated", "") << "\n"; + BOOST_LOG_TRIVIAL(info) << "Event: " << event.id(); + BOOST_LOG_TRIVIAL(info) << "Event Type: " << event.type(); + BOOST_LOG_TRIVIAL(info) << "Bucket: " << payload.value("bucket", ""); + BOOST_LOG_TRIVIAL(info) << "File: " << payload.value("name", ""); + BOOST_LOG_TRIVIAL(info) << "Metageneration: " + << payload.value("metageneration", ""); + BOOST_LOG_TRIVIAL(info) << "Created: " << payload.value("timeCreated", ""); + BOOST_LOG_TRIVIAL(info) << "Updated: " << payload.value("updated", ""); } // [END functions_helloworld_storage] diff --git a/examples/site/hello_world_storage/vcpkg.json b/examples/site/hello_world_storage/vcpkg.json index 2b89f33e..4ec411d6 100644 --- a/examples/site/hello_world_storage/vcpkg.json +++ b/examples/site/hello_world_storage/vcpkg.json @@ -2,6 +2,7 @@ "name": "hello-world-storage", "version-string": "unversioned", "dependencies": [ + "boost-log", "boost-serialization", "functions-framework-cpp", "nlohmann-json" diff --git a/vcpkg.json b/vcpkg.json index df6b47c5..70fd3124 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -1,13 +1,14 @@ { "name": "functions-framework-cpp-dev", "version-string": "0.3.0-dev", - "port-version": 1, + "port-version": 2, "homepage": "https://github.com/GoogleCloudPlatform/functions-framework-cpp/", "description": "Functions Framework for C++ -- Development", "dependencies": [ "abseil", "boost-beast", "boost-filesystem", + "boost-log", "boost-process", "boost-program-options", "boost-property-tree",