From e486b10aa62bf270f31b48fe8abe5254c93b7982 Mon Sep 17 00:00:00 2001 From: redPanther Date: Sun, 26 Jun 2016 11:53:16 +0200 Subject: [PATCH] Logging leds (#52) * switch to new logger for folowing led devices: LedDeviceAtmo LedDeviceFactory LedDeviceFadeCandy LedDeviceHyperionUsbasp LedDeviceLightpack-hidapi LedDevicePiBlaster LedDeviceWS281x LedRs232Device leddevice base class defines logger already as protected member _log * migrate to new logger for led devices. still todo: LedDeviceWS2812b LedDeviceWs2812SPI LedDeviceTinkerforge LedDeviceLightpack LedDeviceMultiLightpack * migrate leddevice tinkerforge to new logger * migrate Lightpack and MultiLightpack to new logger --- libsrc/leddevice/LedDeviceLightpack.cpp | 34 ++++++++++---------- libsrc/leddevice/LedDeviceMultiLightpack.cpp | 26 +++++++-------- libsrc/leddevice/LedDeviceTinkerforge.cpp | 10 +++--- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/libsrc/leddevice/LedDeviceLightpack.cpp b/libsrc/leddevice/LedDeviceLightpack.cpp index 0deac582..70762ae3 100644 --- a/libsrc/leddevice/LedDeviceLightpack.cpp +++ b/libsrc/leddevice/LedDeviceLightpack.cpp @@ -71,12 +71,12 @@ int LedDeviceLightpack::open(const std::string & serialNumber) // initialize the usb context if ((error = libusb_init(&_libusbContext)) != LIBUSB_SUCCESS) { - std::cerr << "Error while initializing USB context(" << error << "): " << libusb_error_name(error) << std::endl; + Error(_log, "Error while initializing USB context(%s): %s", error, libusb_error_name(error)); _libusbContext = nullptr; return -1; } //libusb_set_debug(_libusbContext, 3); - std::cout << "USB context initialized" << std::endl; + Debug(_log, "USB context initialized"); // retrieve the list of usb devices libusb_device ** deviceList; @@ -102,11 +102,11 @@ int LedDeviceLightpack::open(const std::string & serialNumber) { if (_serialNumber.empty()) { - std::cerr << "No Lightpack device has been found" << std::endl; + Warning(_log, "No Lightpack device has been found"); } else { - std::cerr << "No Lightpack device has been found with serial " << _serialNumber << std::endl; + Error(_log,"No Lightpack device has been found with serial %s", _serialNumber.c_str()); } } @@ -119,14 +119,14 @@ int LedDeviceLightpack::testAndOpen(libusb_device * device, const std::string & int error = libusb_get_device_descriptor(device, &deviceDescriptor); if (error != LIBUSB_SUCCESS) { - std::cerr << "Error while retrieving device descriptor(" << error << "): " << libusb_error_name(error) << std::endl; + Error(_log, "Error while retrieving device descriptor(%s): %s", error, libusb_error_name(error)); return -1; } if ((deviceDescriptor.idVendor == USB_VENDOR_ID && deviceDescriptor.idProduct == USB_PRODUCT_ID) || (deviceDescriptor.idVendor == USB_OLD_VENDOR_ID && deviceDescriptor.idProduct == USB_OLD_PRODUCT_ID)) { - std::cout << "Found a lightpack device. Retrieving more information..." << std::endl; + Info(_log, "Found a lightpack device. Retrieving more information..."); // get the hardware address int busNumber = libusb_get_bus_number(device); @@ -142,12 +142,12 @@ int LedDeviceLightpack::testAndOpen(libusb_device * device, const std::string & } catch (int e) { - std::cerr << "unable to retrieve serial number from Lightpack device(" << e << "): " << libusb_error_name(e) << std::endl; + Error(_log, "unable to retrieve serial number from Lightpack device(%s): %s", e, libusb_error_name(e)); serialNumber = ""; } } - std::cout << "Lightpack device found: bus=" << busNumber << " address=" << addressNumber << " serial=" << serialNumber << std::endl; + Debug(_log,"Lightpack device found: bus=%d address=%d serial=%s", busNumber, addressNumber, serialNumber.c_str()); // check if this is the device we are looking for if (requestedSerialNumber.empty() || requestedSerialNumber == serialNumber) @@ -160,7 +160,7 @@ int LedDeviceLightpack::testAndOpen(libusb_device * device, const std::string & _busNumber = busNumber; _addressNumber = addressNumber; - std::cout << "Lightpack device successfully opened" << std::endl; + Info(_log, "Lightpack device successfully opened"); // get the firmware version uint8_t buffer[256]; @@ -173,7 +173,7 @@ int LedDeviceLightpack::testAndOpen(libusb_device * device, const std::string & buffer, sizeof(buffer), 1000); if (error < 3) { - std::cerr << "Unable to retrieve firmware version number from Lightpack device(" << error << "): " << libusb_error_name(error) << std::endl; + Error(_log, "Unable to retrieve firmware version number from Lightpack device(%s): %s", error, libusb_error_name(error)); } else { @@ -213,13 +213,13 @@ int LedDeviceLightpack::testAndOpen(libusb_device * device, const std::string & _ledBuffer[0] = CMD_UPDATE_LEDS; // return success - std::cout << "Lightpack device opened: bus=" << _busNumber << " address=" << _addressNumber << " serial=" << _serialNumber << " version=" << _firmwareVersion.majorVersion << "." << _firmwareVersion.minorVersion << std::endl; + Debug(_log, "Lightpack device opened: bus=%d address=%d serial=%s version=%s.%s.", _busNumber, _addressNumber, _serialNumber.c_str(), _firmwareVersion.majorVersion, _firmwareVersion.minorVersion ); return 0; } catch(int e) { _deviceHandle = nullptr; - std::cerr << "Unable to open Lightpack device. Searching for other device(" << e << "): " << libusb_error_name(e) << std::endl; + Warning(_log, "Unable to open Lightpack device. Searching for other device(%s): %s", e, libusb_error_name(e)); } } } @@ -289,7 +289,7 @@ int LedDeviceLightpack::writeBytes(uint8_t *data, int size) return 0; } - std::cerr << "Unable to write " << size << " bytes to Lightpack device(" << error << "): " << libusb_error_name(error) << std::endl; + Error(_log, "Unable to write %d bytes to Lightpack device(%s): %s", size, error, libusb_error_name(error)); return error; } @@ -302,11 +302,11 @@ int LedDeviceLightpack::disableSmoothing() libusb_device_handle * LedDeviceLightpack::openDevice(libusb_device *device) { libusb_device_handle * handle = nullptr; - + Logger * log = Logger::getInstance("LedDevice"); int error = libusb_open(device, &handle); if (error != LIBUSB_SUCCESS) { - std::cerr << "unable to open device(" << error << "): " << libusb_error_name(error) << std::endl; + Error(log, "unable to open device(%s): %s", error, libusb_error_name(error)); throw error; } @@ -316,7 +316,7 @@ libusb_device_handle * LedDeviceLightpack::openDevice(libusb_device *device) error = libusb_detach_kernel_driver(handle, LIGHTPACK_INTERFACE); if (error != LIBUSB_SUCCESS) { - std::cerr << "unable to detach kernel driver(" << error << "): " << libusb_error_name(error) << std::endl; + Error(log, "unable to detach kernel driver(%s): %s", error, libusb_error_name(error)); libusb_close(handle); throw error; } @@ -325,7 +325,7 @@ libusb_device_handle * LedDeviceLightpack::openDevice(libusb_device *device) error = libusb_claim_interface(handle, LIGHTPACK_INTERFACE); if (error != LIBUSB_SUCCESS) { - std::cerr << "unable to claim interface(" << error << "): " << libusb_error_name(error) << std::endl; + Error(log, "unable to claim interface(%s): %s", error, libusb_error_name(error)); libusb_attach_kernel_driver(handle, LIGHTPACK_INTERFACE); libusb_close(handle); throw error; diff --git a/libsrc/leddevice/LedDeviceMultiLightpack.cpp b/libsrc/leddevice/LedDeviceMultiLightpack.cpp index d6acc253..9a9fcbf4 100644 --- a/libsrc/leddevice/LedDeviceMultiLightpack.cpp +++ b/libsrc/leddevice/LedDeviceMultiLightpack.cpp @@ -42,7 +42,7 @@ int LedDeviceMultiLightpack::open() // open each lightpack device for (const std::string & serial : serialList) { - LedDeviceLightpack * device = new LedDeviceLightpack(); + LedDeviceLightpack * device = new LedDeviceLightpack(); int error = device->open(serial); if (error == 0) @@ -51,18 +51,18 @@ int LedDeviceMultiLightpack::open() } else { - std::cerr << "Error while creating Lightpack device with serial " << serial << std::endl; + Error(_log, "Error while creating Lightpack device with serial %s", serial.c_str()); delete device; } } if (_lightpacks.size() == 0) { - std::cerr << "No Lightpack devices were found" << std::endl; + Warning(_log, "No Lightpack devices were found"); } else { - std::cout << _lightpacks.size() << " Lightpack devices were found" << std::endl; + Info(_log, "%d Lightpack devices were found", _lightpacks.size()); } return _lightpacks.size() > 0 ? 0 : -1; @@ -86,7 +86,7 @@ int LedDeviceMultiLightpack::write(const std::vector &ledValues) } else { - std::cout << "Unable to write data to Lightpack device: no more led data available" << std::endl; + Warning(_log, "Unable to write data to Lightpack device: no more led data available"); } } @@ -106,20 +106,20 @@ int LedDeviceMultiLightpack::switchOff() std::list LedDeviceMultiLightpack::getLightpackSerials() { std::list serialList; - - std::cout << "Getting list of Lightpack serials" << std::endl; + Logger * log = Logger::getInstance("LedDevice"); + Debug(log, "Getting list of Lightpack serials"); // initialize the usb context libusb_context * libusbContext; int error = libusb_init(&libusbContext); if (error != LIBUSB_SUCCESS) { - std::cerr << "Error while initializing USB context(" << error << "): " << libusb_error_name(error) << std::endl; + Error(log,"Error while initializing USB context(%s): %s", error, libusb_error_name(error)); libusbContext = nullptr; return serialList; } //libusb_set_debug(_libusbContext, 3); - std::cout << "USB context initialized in multi Lightpack device" << std::endl; + Info(log, "USB context initialized in multi Lightpack device"); // retrieve the list of usb devices libusb_device ** deviceList; @@ -132,14 +132,14 @@ std::list LedDeviceMultiLightpack::getLightpackSerials() error = libusb_get_device_descriptor(deviceList[i], &deviceDescriptor); if (error != LIBUSB_SUCCESS) { - std::cerr << "Error while retrieving device descriptor(" << error << "): " << libusb_error_name(error) << std::endl; + Error(log, "Error while retrieving device descriptor(%s): %s", error, libusb_error_name(error)); continue; } if ((deviceDescriptor.idVendor == USB_VENDOR_ID && deviceDescriptor.idProduct == USB_PRODUCT_ID) || (deviceDescriptor.idVendor == USB_OLD_VENDOR_ID && deviceDescriptor.idProduct == USB_OLD_PRODUCT_ID)) { - std::cout << "Found a lightpack device. Retrieving serial..." << std::endl; + Info(log, "Found a lightpack device. Retrieving serial..."); // get the serial number std::string serialNumber; @@ -151,12 +151,12 @@ std::list LedDeviceMultiLightpack::getLightpackSerials() } catch (int e) { - std::cerr << "Unable to retrieve serial number(" << e << "): " << libusb_error_name(e) << std::endl; + Error(log,"Unable to retrieve serial number(%s): %s", e, libusb_error_name(e)); continue; } } - std::cout << "Lightpack device found with serial " << serialNumber << std::endl; + Error(log, "Lightpack device found with serial %s", serialNumber.c_str());; serialList.push_back(serialNumber); } } diff --git a/libsrc/leddevice/LedDeviceTinkerforge.cpp b/libsrc/leddevice/LedDeviceTinkerforge.cpp index 83367042..9bfa0778 100644 --- a/libsrc/leddevice/LedDeviceTinkerforge.cpp +++ b/libsrc/leddevice/LedDeviceTinkerforge.cpp @@ -40,7 +40,7 @@ int LedDeviceTinkerforge::open() // Check if connection is already createds if (_ipConnection != nullptr) { - std::cout << "Attempt to open existing connection; close before opening" << std::endl; + Error(_log, "Attempt to open existing connection; close before opening"); return -1; } @@ -51,7 +51,7 @@ int LedDeviceTinkerforge::open() int connectionStatus = ipcon_connect(_ipConnection, _host.c_str(), _port); if (connectionStatus < 0) { - std::cerr << "Attempt to connect to master brick (" << _host << ":" << _port << ") failed with status " << connectionStatus << std::endl; + Warning(_log, "Attempt to connect to master brick (%s:%d) failed with status %d", _host.c_str(), _port, connectionStatus); return -1; } @@ -62,7 +62,7 @@ int LedDeviceTinkerforge::open() int frameStatus = led_strip_set_frame_duration(_ledStrip, _interval); if (frameStatus < 0) { - std::cerr << "Attempt to connect to led strip bricklet (led_strip_set_frame_duration()) failed with status " << frameStatus << std::endl; + Error(_log,"Attempt to connect to led strip bricklet (led_strip_set_frame_duration()) failed with status %d", frameStatus); return -1; } @@ -75,7 +75,7 @@ int LedDeviceTinkerforge::write(const std::vector &ledValues) if (nrLedValues > MAX_NUM_LEDS) { - std::cerr << "Invalid attempt to write led values. Not more than " << MAX_NUM_LEDS << " leds are allowed." << std::endl; + Error(_log,"Invalid attempt to write led values. Not more than %d leds are allowed.", MAX_NUM_LEDS); return -1; } @@ -134,7 +134,7 @@ int LedDeviceTinkerforge::transferLedData(LEDStrip *ledStrip, unsigned index, un const int status = led_strip_set_rgb_values(ledStrip, i, copyLength, reds, greens, blues); if (status != E_OK) { - std::cerr << "Setting led values failed with status " << status << std::endl; + Warning(_log, "Setting led values failed with status %d", status); return status; } }