Refactored debug() and log() in CDev

These functions used vprintf which is not available on all platforms.
They also do not enable line and file debug output.

Changed to macros that preserve the output format.  Uses new macro that
can be used to implement per object, runtime selectable logging

Signed-off-by: Mark Charlebois <charlebm@gmail.com>
This commit is contained in:
Mark Charlebois
2015-08-11 12:07:06 -07:00
parent be58c6abd3
commit a589d15c52
47 changed files with 202 additions and 261 deletions
+1 -28
View File
@@ -38,6 +38,7 @@
*/
#include "device.h"
#include "px4_log.h"
#include <nuttx/arch.h>
#include <stdio.h>
@@ -156,34 +157,6 @@ Device::interrupt(void *context)
interrupt_disable();
}
void
Device::log(const char *fmt, ...)
{
va_list ap;
printf("[%s] ", _name);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
fflush(stdout);
}
void
Device::debug(const char *fmt, ...)
{
va_list ap;
if (_debug_enabled) {
printf("<%s> ", _name);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
fflush(stdout);
}
}
static int
register_interrupt(int irq, Device *owner)
{
+3 -16
View File
@@ -54,6 +54,9 @@
#include <nuttx/fs/fs.h>
#define DEVICE_LOG(FMT, ...) PX4_LOG_NAMED(_name, FMT, ##__VA_ARGS__)
#define DEVICE_DEBUG(FMT, ...) PX4_LOG_NAMED_COND(_name, _debug_enabled, FMT, ##__VA_ARGS__)
/**
* Namespace encapsulating all device framework classes, functions and data.
*/
@@ -197,22 +200,6 @@ protected:
sem_post(&_lock);
}
/**
* Log a message.
*
* The message is prefixed with the driver name, and followed
* by a newline.
*/
void log(const char *fmt, ...);
/**
* Print a debug message.
*
* The message is prefixed with the driver name, and followed
* by a newline.
*/
void debug(const char *fmt, ...);
private:
int _irq;
bool _irq_attached;
-28
View File
@@ -78,34 +78,6 @@ Device::init()
return ret;
}
void
Device::log(const char *fmt, ...)
{
va_list ap;
PX4_INFO("[%s] ", _name);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
fflush(stdout);
}
void
Device::debug(const char *fmt, ...)
{
va_list ap;
if (_debug_enabled) {
printf("<%s> ", _name);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
fflush(stdout);
}
}
int
Device::dev_read(unsigned offset, void *data, unsigned count)
{
+7 -7
View File
@@ -90,7 +90,7 @@ I2C::set_bus_clock(unsigned bus, unsigned clock_hz)
}
if (_bus_clocks[index] > 0) {
// debug("overriding clock of %u with %u Hz\n", _bus_clocks[index], clock_hz);
// DEVICE_DEBUG("overriding clock of %u with %u Hz\n", _bus_clocks[index], clock_hz);
}
_bus_clocks[index] = clock_hz;
@@ -107,7 +107,7 @@ I2C::init()
_dev = up_i2cinitialize(_bus);
if (_dev == nullptr) {
debug("failed to init I2C");
DEVICE_DEBUG("failed to init I2C");
ret = -ENOENT;
goto out;
}
@@ -121,7 +121,7 @@ I2C::init()
if (_bus_clocks[bus_index] > _frequency) {
(void)up_i2cuninitialize(_dev);
_dev = nullptr;
log("FAIL: too slow for bus #%u: %u KHz, device max: %u KHz)",
DEVICE_LOG("FAIL: too slow for bus #%u: %u KHz, device max: %u KHz)",
_bus, _bus_clocks[bus_index] / 1000, _frequency / 1000);
ret = -EINVAL;
goto out;
@@ -148,7 +148,7 @@ I2C::init()
ret = probe();
if (ret != OK) {
debug("probe failed");
DEVICE_DEBUG("probe failed");
goto out;
}
@@ -156,12 +156,12 @@ I2C::init()
ret = CDev::init();
if (ret != OK) {
debug("cdev init failed");
DEVICE_DEBUG("cdev init failed");
goto out;
}
// tell the world where we are
log("on I2C bus %d at 0x%02x (bus: %u KHz, max: %u KHz)",
DEVICE_LOG("on I2C bus %d at 0x%02x (bus: %u KHz, max: %u KHz)",
_bus, _address, _bus_clocks[bus_index] / 1000, _frequency / 1000);
out:
@@ -188,7 +188,7 @@ I2C::transfer(const uint8_t *send, unsigned send_len, uint8_t *recv, unsigned re
unsigned retry_count = 0;
do {
// debug("transfer out %p/%u in %p/%u", send, send_len, recv, recv_len);
// DEVICE_DEBUG("transfer out %p/%u in %p/%u", send, send_len, recv, recv_len);
msgs = 0;
+3 -3
View File
@@ -100,13 +100,13 @@ I2C::init()
ret = VDev::init();
if (ret != PX4_OK) {
debug("VDev::init failed");
DEVICE_DEBUG("VDev::init failed");
return ret;
}
_fd = px4_open(get_devname(), PX4_F_RDONLY | PX4_F_WRONLY);
if (_fd < 0) {
debug("px4_open failed of device %s", get_devname());
DEVICE_DEBUG("px4_open failed of device %s", get_devname());
return PX4_ERROR;
}
@@ -150,7 +150,7 @@ I2C::transfer(const uint8_t *send, unsigned send_len, uint8_t *recv, unsigned re
}
do {
// debug("transfer out %p/%u in %p/%u", send, send_len, recv, recv_len);
// DEVICE_DEBUG("transfer out %p/%u in %p/%u", send, send_len, recv, recv_len);
msgs = 0;
if (send_len > 0) {
+4 -4
View File
@@ -98,7 +98,7 @@ SPI::init()
_dev = up_spiinitialize(_bus);
if (_dev == nullptr) {
debug("failed to init SPI");
DEVICE_DEBUG("failed to init SPI");
ret = -ENOENT;
goto out;
}
@@ -110,7 +110,7 @@ SPI::init()
ret = probe();
if (ret != OK) {
debug("probe failed");
DEVICE_DEBUG("probe failed");
goto out;
}
@@ -118,12 +118,12 @@ SPI::init()
ret = CDev::init();
if (ret != OK) {
debug("cdev init failed");
DEVICE_DEBUG("cdev init failed");
goto out;
}
/* tell the workd where we are */
log("on SPI bus %d at %d (%u KHz)", _bus, _device, _frequency / 1000);
DEVICE_LOG("on SPI bus %d at %d (%u KHz)", _bus, _device, _frequency / 1000);
out:
return ret;
+5 -18
View File
@@ -51,6 +51,9 @@
#include <stdint.h>
#include <semaphore.h>
#define DEVICE_LOG(FMT, ...) PX4_LOG_NAMED(_name, FMT, ##__VA_ARGS__)
#define DEVICE_DEBUG(FMT, ...) PX4_LOG_NAMED_COND(_name, _debug_enabled, FMT, ##__VA_ARGS__)
/**
* Namespace encapsulating all device framework classes, functions and data.
*/
@@ -176,7 +179,7 @@ protected:
* Note that we must loop as the wait may be interrupted by a signal.
*/
void lock() {
debug("lock");
DEVICE_DEBUG("lock");
do {} while (sem_wait(&_lock) != 0);
}
@@ -184,26 +187,10 @@ protected:
* Release the driver lock.
*/
void unlock() {
debug("unlock");
DEVICE_DEBUG("unlock");
sem_post(&_lock);
}
/**
* Log a message.
*
* The message is prefixed with the driver name, and followed
* by a newline.
*/
void log(const char *fmt, ...);
/**
* Print a debug message.
*
* The message is prefixed with the driver name, and followed
* by a newline.
*/
void debug(const char *fmt, ...);
private:
sem_t _lock;