From e64d925b45ff4802fe924ea2e8108bb1932b4d01 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 1 Mar 2015 21:15:01 +0100 Subject: o Replacing boost::logging with log4cplus. --- apps/CMakeLists.txt | 21 +++++++++++++++-- apps/apps.cpp | 23 +++++++++++++++++++ apps/apps.h | 16 +++++++++++++ apps/log4cplus-test.cpp | 49 ++++++++++++++++++++++++++++++++++++++++ apps/sm-get-value.cpp | 13 +++++++++-- ble/Bluetooth.cpp | 16 ++++++------- ble/BluetoothImpl.h | 45 +++++++++++++++++++++++++++---------- ble/ByteBuffer.cpp | 1 - ble/LinuxBluetooth.cpp | 60 +++++++++++++++++++------------------------------ test/ByteBufferTest.cpp | 3 +-- 10 files changed, 183 insertions(+), 64 deletions(-) create mode 100644 apps/apps.cpp create mode 100644 apps/apps.h create mode 100644 apps/log4cplus-test.cpp 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 +#include + +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 + +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 +#include +#include + +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 #include #include -#include -#include +#include +#include +#include #include +#include +#include #include #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)->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; diff --git a/ble/Bluetooth.cpp b/ble/Bluetooth.cpp index 66f97b7..b672dba 100644 --- a/ble/Bluetooth.cpp +++ b/ble/Bluetooth.cpp @@ -97,7 +97,7 @@ void AttPdu::makeWrite(ByteBuffer &req, uint16_t handle, const ByteBuffer &bytes } vector AttPdu::parse(ByteBuffer &bytes, AttPduType type) { - DF << "bytes: " << bytes.toString(); + // cout << "bytes: " << bytes.toString(); AttPduType t = (AttPduType) bytes.read8(); @@ -114,15 +114,15 @@ vector AttPdu::parse(ByteBuffer &bytes, AttPduType type) { } uint8_t length = bytes.read8(); - D << "length=" << (int) length; + // cout << "length=" << (int) length; size_t count = (bytes.getSize() - 2) / length; - D << "count=" << count; + // cout << "count=" << count; vector values; for (int i = 0; i < count; i++) { auto data = bytes.view(length); - D << "data, size=" << data.getSize() << ", bytes=" << data.toString(); + // cout << "data, size=" << data.getSize() << ", bytes=" << data.toString(); bytes.skip(length); values.push_back(AttributeData::fromByteBuffer(data)); } @@ -233,10 +233,10 @@ void shutdown() { } uuid_t makeUuid(const uuid_t base, uint8_t a, uint8_t b) { - uuid_t copy = base; - copy.data[2] = a; - copy.data[3] = b; - return copy; + uuid_t c = base; + c.data[2] = a; + c.data[3] = b; + return c; } } diff --git a/ble/BluetoothImpl.h b/ble/BluetoothImpl.h index 967c2e7..204b051 100644 --- a/ble/BluetoothImpl.h +++ b/ble/BluetoothImpl.h @@ -2,9 +2,10 @@ #define BLUETOOTH_IMPL_H #include "ble/Bluetooth.h" -#include "log.h" #include #include +#include +#include #define BLUETOOTH_UUID_INITIALIZER \ { \ @@ -15,17 +16,36 @@ 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb \ }; +#define LOG_DEBUG(body) LOG4CPLUS_DEBUG(logger, body) +#define LOG_INFO(body) LOG4CPLUS_INFO(logger, body) +#define LOG_WARN(body) LOG4CPLUS_WARN(logger, body) + namespace trygvis { namespace bluetooth { +// Utility typedefs typedef boost::uuids::uuid uuid_t; template using o = boost::optional; +using namespace log4cplus; + +// Logging + +class LogSetup { +public: + LogSetup(std::string name) : logger(Logger::getInstance(LOG4CPLUS_TEXT(name))) { + } + +protected: + Logger logger; +}; + +// Shared classes -class DefaultBluetoothGattCharacteristic : public BluetoothGattCharacteristic { +class DefaultBluetoothGattCharacteristic : LogSetup, public BluetoothGattCharacteristic { public: DefaultBluetoothGattCharacteristic(BluetoothGattService &service, uint16_t handle, uuid_t uuid, uint8_t properties, uint16_t valueHandle) - : service(service), handle(handle), uuid(uuid), properties(properties), valueHandle(valueHandle) { + : LogSetup("DefaultBluetoothGattCharacteristic"), service(service), handle(handle), uuid(uuid), properties(properties), valueHandle(valueHandle) { } virtual ~DefaultBluetoothGattCharacteristic() { @@ -63,11 +83,9 @@ class DefaultBluetoothGattService : public BluetoothGattService { public: DefaultBluetoothGattService(BluetoothDevice &device, const uuid_t uuid, const uint16_t handle, const uint16_t endGroupHandle) : device(device), uuid(uuid), handle(handle), endGroupHandle(endGroupHandle) { - DF; } virtual ~DefaultBluetoothGattService() { - DF; removeCharacteristics(); } @@ -113,7 +131,6 @@ protected: vector characteristics; void removeCharacteristics() { - DF; for (auto &c: characteristics) { delete c; } @@ -122,7 +139,7 @@ protected: }; template -class DefaultBluetoothGatt : public BluetoothGatt { +class DefaultBluetoothGatt : protected LogSetup, public BluetoothGatt { public: virtual _D &getDevice() const { return device; @@ -147,12 +164,10 @@ public: } protected: - DefaultBluetoothGatt(_D &device) : device(device) { - DF; + DefaultBluetoothGatt(_D &device) : LogSetup("BluetoothGatt"), device(device) { } virtual ~DefaultBluetoothGatt() { - DF; removeServices(); } @@ -182,11 +197,9 @@ public: protected: DefaultBluetoothDevice(A &adapter, Mac &mac) : adapter(adapter), mac(mac) { - DF; } virtual ~DefaultBluetoothDevice() { - DF; removeServices(); } @@ -202,6 +215,14 @@ protected: vector services; }; +class DefaultBluetoothAdapter : protected LogSetup, public BluetoothAdapter { +public: +protected: + DefaultBluetoothAdapter() : + LogSetup("BluetoothAdapter") { + } +}; + BluetoothAdapter &getAdapterImpl(int hciDevice); void shutdownImpl(); diff --git a/ble/ByteBuffer.cpp b/ble/ByteBuffer.cpp index d1c923e..66f9270 100644 --- a/ble/ByteBuffer.cpp +++ b/ble/ByteBuffer.cpp @@ -3,7 +3,6 @@ #include #include #include -#include "log.h" using namespace std; diff --git a/ble/LinuxBluetooth.cpp b/ble/LinuxBluetooth.cpp index 435dd63..80e68cb 100644 --- a/ble/LinuxBluetooth.cpp +++ b/ble/LinuxBluetooth.cpp @@ -27,7 +27,7 @@ class LinuxBluetoothAdapter; class LinuxBluetoothManager; -class LinuxBluetoothAdapter : public BluetoothAdapter { +class LinuxBluetoothAdapter : public DefaultBluetoothAdapter { public: LinuxBluetoothAdapter(int hciDeviceId); @@ -141,7 +141,7 @@ LinuxBluetoothGatt::~LinuxBluetoothGatt() { void LinuxBluetoothGatt::connect() { struct sockaddr_l2 addr; - D << "connect: mac=" << device.getMac().str(); + LOG_DEBUG("connect: mac=" << device.getMac().str()); l2cap = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); if (l2cap < 0) { @@ -186,7 +186,7 @@ void LinuxBluetoothGatt::connect() { } void LinuxBluetoothGatt::disconnect() { - DF << "mac = " << device.getMac().str(); + LOG_DEBUG("mac = " << device.getMac().str()); close(l2cap); } @@ -227,9 +227,7 @@ uuid_t readUuid(BluetoothDevice *device, const ByteBuffer &bytes) { } void LinuxBluetoothGatt::writeValue(const BluetoothGattCharacteristic &c, const ByteBuffer &bytes) { - DF; - - D << "Writing to characteristic " << c.getUuid() << ": " << bytes.toString(); + LOG_DEBUG("Writing to characteristic " << c.getUuid() << ": " << bytes.toString()); shared_ptr buffer(new uint8_t[MAX_MTU]); ByteBuffer out = ByteBuffer(buffer, MAX_MTU); @@ -242,8 +240,6 @@ void LinuxBluetoothGatt::writeValue(const BluetoothGattCharacteristic &c, const } ByteBuffer LinuxBluetoothGatt::readValue(const BluetoothGattCharacteristic &c) { - DF; - shared_ptr buffer(new uint8_t[MAX_MTU]); ByteBuffer out = ByteBuffer(buffer, MAX_MTU); @@ -257,7 +253,7 @@ ByteBuffer LinuxBluetoothGatt::readValue(const BluetoothGattCharacteristic &c) { auto response = in.view(); - D << "Value of characteristic " << c.getUuid() << "=" << response.toString(); + LOG_DEBUG("Value of characteristic " << c.getUuid() << "=" << response.toString()); return response; } @@ -347,10 +343,10 @@ void LinuxBluetoothGatt::discoverServices() { } ByteBuffer LinuxBluetoothGatt::writeAndRead(ByteBuffer &out, shared_ptr buffer, size_t size) { - D << "pdu size=" << out.getCursor(); + LOG_DEBUG("pdu size=" << out.getCursor()); ssize_t written = write(l2cap, buffer.get(), out.getCursor()); - D << "written=" << written; + LOG_DEBUG("written=" << written); ssize_t r = read(l2cap, buffer.get(), size); @@ -360,14 +356,12 @@ ByteBuffer LinuxBluetoothGatt::writeAndRead(ByteBuffer &out, shared_ptr auto in = ByteBuffer(buffer, (size_t) r, (size_t) r); - D << "read: " << r << " bytes: " << in.toString(); + LOG_DEBUG("read: " << r << " bytes: " << in.toString()); return in; } vector LinuxBluetoothGatt::discoverServices(uint16_t startHandle) { - DF; - shared_ptr buffer(new uint8_t[MAX_MTU]); ByteBuffer out = ByteBuffer(buffer, MAX_MTU); @@ -377,14 +371,12 @@ vector LinuxBluetoothGatt::discoverServices(uint16_t startHandle) vector values = AttPdu::parseReadByGroupType(in); - D << "READ_BY_GROUP_TYPE response has " + to_string(values.size()) + " values"; + LOG_DEBUG("READ_BY_GROUP_TYPE response has " + to_string(values.size()) + " values"); return values; } vector LinuxBluetoothGatt::discoverCharacteristics(uint16_t startHandle, uint16_t endHandle) { - DF; - shared_ptr buffer(new uint8_t[MAX_MTU]); ByteBuffer out = ByteBuffer(buffer, MAX_MTU); @@ -394,7 +386,7 @@ vector LinuxBluetoothGatt::discoverCharacteristics(uint16_t start vector values = AttPdu::parseReadByType(in); - D << "READ_BY_TYPE response has " + to_string(values.size()) + " values"; + LOG_DEBUG("READ_BY_TYPE response has " + to_string(values.size()) + " values"); return values; } @@ -405,12 +397,12 @@ vector LinuxBluetoothGatt::discoverCharacteristics(uint16_t start LinuxBluetoothAdapter::LinuxBluetoothAdapter(int hciDeviceId) : scanning(false) { - DF << "hciDeviceId=" << hciDeviceId; + LOG_DEBUG("hciDeviceId=" << hciDeviceId); this->hciDeviceId = hciDeviceId; hciSocket = ::hci_open_dev(hciDeviceId); - D << "HCI socket: " << hciSocket; + LOG_DEBUG("HCI socket: " << hciSocket); if (hciSocket == -1) { throw BluetoothException(this, "Could not open HCI device " + to_string(hciDeviceId)); @@ -424,8 +416,6 @@ LinuxBluetoothAdapter::LinuxBluetoothAdapter(int hciDeviceId) : } LinuxBluetoothAdapter::~LinuxBluetoothAdapter() { - DF; - stopScan(); close(hciSocket); @@ -436,22 +426,20 @@ LinuxBluetoothAdapter::~LinuxBluetoothAdapter() { } void LinuxBluetoothAdapter::startScan() { - DF; - struct hci_dev_info di; if (hci_devinfo(hciDeviceId, &di) < 0) { throw BluetoothException(this, "HCI adapter is not up: " + to_string(hciDeviceId)); } - D << "hciDeviceId.dev_id=" << di.dev_id; - D << "hciDeviceId.bdaddr=" << parseMac(di.bdaddr).str(); - D << "hciDeviceId.flags=" << setw(8) << setfill('0') << hex << di.flags; - D << "hciDeviceId.flags RUNNING = " << hci_test_bit(HCI_RUNNING, &di.flags); - D << "hciDeviceId.flags UP = " << hci_test_bit(HCI_UP, &di.flags); - D << "hciDeviceId.flags PSCAN = " << hci_test_bit(HCI_PSCAN, &di.flags); - D << "hciDeviceId.flags ISCAN = " << hci_test_bit(HCI_ISCAN, &di.flags); - D << "hciDeviceId.name=" << di.name; + LOG_DEBUG("hciDeviceId.dev_id=" << di.dev_id); + LOG_DEBUG("hciDeviceId.bdaddr=" << parseMac(di.bdaddr).str()); + LOG_DEBUG("hciDeviceId.flags=" << setw(8) << setfill('0') << hex << di.flags); + LOG_DEBUG("hciDeviceId.flags RUNNING = " << hci_test_bit(HCI_RUNNING, &di.flags)); + LOG_DEBUG("hciDeviceId.flags UP = " << hci_test_bit(HCI_UP, &di.flags)); + LOG_DEBUG("hciDeviceId.flags PSCAN = " << hci_test_bit(HCI_PSCAN, &di.flags)); + LOG_DEBUG("hciDeviceId.flags ISCAN = " << hci_test_bit(HCI_ISCAN, &di.flags)); + LOG_DEBUG("hciDeviceId.name=" << di.name); int up = hci_test_bit(HCI_UP, &di.flags); @@ -471,8 +459,6 @@ void LinuxBluetoothAdapter::startScan() { } void LinuxBluetoothAdapter::stopScan() { - DF; - if (!scanning) { return; } @@ -480,7 +466,7 @@ void LinuxBluetoothAdapter::stopScan() { scanning = false; if (hci_le_set_scan_enable(hciSocket, 0, 0, 1000) < 0) { - W << "stopScan: hci_le_set_scan_enable: " << errnoAsString(); + LOG_WARN("stopScan: hci_le_set_scan_enable: " << errnoAsString()); } } @@ -516,7 +502,7 @@ void LinuxBluetoothAdapter::runScan(void (*callback)(BluetoothDevice &device)) { } if (selected == 0) { - D << "timeout"; + LOG_DEBUG("timeout"); // Timeout, just continue continue; } @@ -527,7 +513,7 @@ void LinuxBluetoothAdapter::runScan(void (*callback)(BluetoothDevice &device)) { evt_le_meta_event *metaEvent = (evt_le_meta_event *) (hciEventBuf + (1 + HCI_EVENT_HDR_SIZE)); len -= (1 + HCI_EVENT_HDR_SIZE); - D << "metaEvent->subevent = " << std::hex << (int) metaEvent->subevent; + LOG_DEBUG("metaEvent->subevent = " << std::hex << (int) metaEvent->subevent); if (metaEvent->subevent == EVT_LE_ADVERTISING_REPORT) { le_advertising_info *advertisingInfo = (le_advertising_info *) (metaEvent->data + 1); diff --git a/test/ByteBufferTest.cpp b/test/ByteBufferTest.cpp index fb31da3..5aad376 100644 --- a/test/ByteBufferTest.cpp +++ b/test/ByteBufferTest.cpp @@ -1,12 +1,11 @@ #include "ble/ByteBuffer.h" -#include "log.h" #define BOOST_TEST_MODULE "ByteBuffer" #include #define checkBuffer(buffer, capacity, size, cursor) \ - if(false) {D << "capacity=" << buffer.getCapacity() << ", size=" << buffer.getSize() << ", cursor=" << buffer.getCursor();} \ + if(false) {cout << "capacity=" << buffer.getCapacity() << ", size=" << buffer.getSize() << ", cursor=" << buffer.getCursor();} \ BOOST_CHECK_EQUAL(buffer.getSize(), size); \ BOOST_CHECK_EQUAL(buffer.getCapacity(), capacity); \ BOOST_CHECK_EQUAL(buffer.getCursor(), cursor) -- cgit v1.2.3