diff options
Diffstat (limited to 'apps')
-rw-r--r-- | apps/CMakeLists.txt | 21 | ||||
-rw-r--r-- | apps/apps.cpp | 23 | ||||
-rw-r--r-- | apps/apps.h | 16 | ||||
-rw-r--r-- | apps/log4cplus-test.cpp | 49 | ||||
-rw-r--r-- | apps/sm-get-value.cpp | 13 |
5 files changed, 118 insertions, 4 deletions
diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 792e339..ab9c529 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -3,10 +3,15 @@ set(APPS sm-db-insert sm-db-select sm-get-value) -set(shared_sources SoilMoisture.cpp) + +set(APPS ${APPS} log4cplus-test) + +set(shared_sources + SoilMoisture.cpp + apps.cpp) # Boost -find_package(Boost COMPONENTS system log thread program_options REQUIRED) +find_package(Boost COMPONENTS system program_options REQUIRED) # Bluez pkg_check_modules(BLUEZ bluez REQUIRED) @@ -16,6 +21,16 @@ find_package(Threads REQUIRED) pkg_check_modules(PQXX libpqxx REQUIRED) +find_path(LOG4CPLUS_INCLUDE_DIRECTORIES log4cplus/logger.h) +if(LOG4CPLUS_INCLUDE_DIRECTORIES MATCHES NOTFOUND) + message(FATAL_ERROR "Could not find log4cplus header files") +endif() + +find_library(LOG4CPLUS_LIBRARIES log4cplus) +if(LOG4CPLUS_LIBRARIES MATCHES NOTFOUND) + message(FATAL_ERROR "Could not find log4cplus library files") +endif() + include(ExternalProject) ExternalProject_Add( JSON @@ -37,5 +52,7 @@ foreach(app ${APPS}) target_link_libraries(${app} ${Boost_LIBRARIES}) target_link_libraries(${app} ${BLUEZ_LIBRARIES}) target_link_libraries(${app} ${PQXX_LIBRARIES}) + include_directories("${LOG4CPLUS_INCLUDE_DIRECTORIES}") + target_link_libraries(${app} ${LOG4CPLUS_LIBRARIES}) target_link_libraries(${app} ${CMAKE_THREAD_LIBS_INIT}) endforeach(app) diff --git a/apps/apps.cpp b/apps/apps.cpp new file mode 100644 index 0000000..21c4fe6 --- /dev/null +++ b/apps/apps.cpp @@ -0,0 +1,23 @@ +#include "apps.h" +#include <log4cplus/logger.h> +#include <log4cplus/configurator.h> + +namespace trygvis { +namespace apps { + +using namespace log4cplus; +namespace po = boost::program_options; + +const po::options_description logging_options() { + po::options_description desc; + + return desc; +} + +void setup_logging(po::variables_map vm) { + BasicConfigurator config; + config.configure(); +} + +} +} diff --git a/apps/apps.h b/apps/apps.h new file mode 100644 index 0000000..f1cdb8a --- /dev/null +++ b/apps/apps.h @@ -0,0 +1,16 @@ +#ifndef UTILS_H +#define UTILS_H + +#include <boost/program_options.hpp> + +namespace trygvis { +namespace apps { + +const boost::program_options::options_description logging_options(); + +void setup_logging(boost::program_options::variables_map vm); + +} +} + +#endif diff --git a/apps/log4cplus-test.cpp b/apps/log4cplus-test.cpp new file mode 100644 index 0000000..f53569f --- /dev/null +++ b/apps/log4cplus-test.cpp @@ -0,0 +1,49 @@ +#include <log4cplus/logger.h> +#include <log4cplus/configurator.h> +#include <iomanip> + +namespace trygvis { +using namespace log4cplus; + +class LogSetup { +public: + LogSetup(std::string name) : logger(Logger::getInstance(LOG4CPLUS_TEXT(name))) { + } + +protected: + Logger logger; +}; + +class MyService : LogSetup { +public: + MyService() : LogSetup("trygvis.MyService") { + } + + void launchMissiles() { + LOG4CPLUS_DEBUG(logger, "some debug message"); + LOG4CPLUS_INFO(logger, "some info message"); + LOG4CPLUS_WARN(logger, "some warning message"); + } +}; +} + +using namespace std; +using namespace log4cplus; +using namespace trygvis; + +int main() { + BasicConfigurator config; + config.configure(); + + cout << "Hello world!" << endl; + + Logger l = Logger::getRoot(); + + l.setLogLevel(INFO_LOG_LEVEL); + + MyService myService; + + myService.launchMissiles(); + + return EXIT_SUCCESS; +} diff --git a/apps/sm-get-value.cpp b/apps/sm-get-value.cpp index 14a90de..417fac7 100644 --- a/apps/sm-get-value.cpp +++ b/apps/sm-get-value.cpp @@ -1,17 +1,22 @@ #include <iostream> #include <iomanip> #include <chrono> -#include <boost/uuid/uuid_io.hpp> -#include <boost/optional.hpp> +#include <boost/log/core/core.hpp> +#include <boost/log/sinks.hpp> +#include <boost/log/trivial.hpp> #include <boost/program_options.hpp> +#include <boost/utility/empty_deleter.hpp> +#include <boost/uuid/uuid_io.hpp> #include <thread> #include "ble/Bluetooth.h" #include "SoilMoisture.h" #include "json.hpp" +#include "apps.h" // I'm lazy using namespace std; using namespace std::chrono; +using namespace trygvis::apps; using namespace trygvis::bluetooth; using namespace trygvis::soil_moisture; using json = nlohmann::json; @@ -129,6 +134,8 @@ int main(int argc, char *argv[]) { "How long to sleep in seconds between each poll. If not give, it will exit after first poll") ("format", po::value<Format>(&format)->default_value(Format::PLAIN), "Output format"); + desc.add(logging_options()); + po::variables_map vm; auto parsed = po::parse_command_line(argc, argv, desc); po::store(parsed, vm); @@ -146,6 +153,8 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } + setup_logging(vm); + __attribute__((unused)) BluetoothSystem bluetoothSystem; |