diff options
author | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2019-10-13 22:22:04 +0300 |
---|---|---|
committer | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2019-10-15 16:49:55 +0300 |
commit | f391048a7b987a149d0ba5421846b9b5ab916338 (patch) | |
tree | de82297ab5c83cc530d51bd9e76040f6d0b909f3 /src | |
parent | ce739e616c42105e5c6f22bf4df5dc19b5e8c8f9 (diff) |
libcamera: utils: Add hex stream output helper
Add a utils::hex() function that simplifies writing hexadecimal values
to an ostream. The function handles the '0x' prefix, the field width and
the fill character automatically. Use it through the libcamera code
base, and add a test.
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/android/camera_device.cpp | 2 | ||||
-rw-r--r-- | src/libcamera/camera_sensor.cpp | 6 | ||||
-rw-r--r-- | src/libcamera/controls.cpp | 6 | ||||
-rw-r--r-- | src/libcamera/include/utils.h | 40 | ||||
-rw-r--r-- | src/libcamera/stream.cpp | 7 | ||||
-rw-r--r-- | src/libcamera/utils.cpp | 39 | ||||
-rw-r--r-- | src/libcamera/v4l2_subdevice.cpp | 6 | ||||
-rw-r--r-- | src/libcamera/v4l2_videodevice.cpp | 6 |
8 files changed, 91 insertions, 21 deletions
diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index bf991d59..c7c9b3fd 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -640,7 +640,7 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) << ", direction: " << stream->stream_type << ", width: " << stream->width << ", height: " << stream->height - << ", format: " << std::hex << stream->format; + << ", format: " << utils::hex(stream->format); } /* Hardcode viewfinder role, collecting sizes from the stream config. */ diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp index 9e8b44a2..1b8e8c0e 100644 --- a/src/libcamera/camera_sensor.cpp +++ b/src/libcamera/camera_sensor.cpp @@ -14,6 +14,7 @@ #include <math.h> #include "formats.h" +#include "utils.h" #include "v4l2_subdevice.h" /** @@ -79,9 +80,8 @@ int CameraSensor::init() if (entity_->function() != MEDIA_ENT_F_CAM_SENSOR) { LOG(CameraSensor, Error) - << "Invalid sensor function 0x" - << std::hex << std::setfill('0') << std::setw(8) - << entity_->function(); + << "Invalid sensor function " + << utils::hex(entity_->function()); return -EINVAL; } diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp index e46aa438..6a0301f3 100644 --- a/src/libcamera/controls.cpp +++ b/src/libcamera/controls.cpp @@ -549,8 +549,7 @@ const ControlValue &ControlList::get(unsigned int id) const const auto ctrl = idmap_->find(id); if (ctrl == idmap_->end()) { LOG(Controls, Error) - << std::hex << std::setfill('0') - << "Control 0x" << std::setw(8) << id + << "Control " << utils::hex(id) << " is not supported"; return zero; } @@ -579,8 +578,7 @@ void ControlList::set(unsigned int id, const ControlValue &value) const auto ctrl = idmap_->find(id); if (ctrl == idmap_->end()) { LOG(Controls, Error) - << std::hex << std::setfill('0') - << "Control 0x" << std::setw(8) << id + << "Control 0x" << utils::hex(id) << " is not supported"; return; } diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h index 52eee8ac..3efb11c1 100644 --- a/src/libcamera/include/utils.h +++ b/src/libcamera/include/utils.h @@ -10,6 +10,7 @@ #include <algorithm> #include <chrono> #include <memory> +#include <ostream> #include <string> #include <sys/time.h> @@ -63,6 +64,45 @@ using time_point = std::chrono::steady_clock::time_point; struct timespec duration_to_timespec(const duration &value); std::string time_point_to_string(const time_point &time); +#ifndef __DOXYGEN__ +struct _hex { + uint64_t v; + unsigned int w; +}; + +std::basic_ostream<char, std::char_traits<char>> & +operator<<(std::basic_ostream<char, std::char_traits<char>> &stream, const _hex &h); +#endif + +template<typename T> +_hex hex(T value, unsigned int width = 0); + +#ifndef __DOXYGEN__ +template<> +inline _hex hex<int32_t>(int32_t value, unsigned int width) +{ + return { static_cast<uint64_t>(value), width ? width : 8 }; +} + +template<> +inline _hex hex<uint32_t>(uint32_t value, unsigned int width) +{ + return { static_cast<uint64_t>(value), width ? width : 8 }; +} + +template<> +inline _hex hex<int64_t>(int64_t value, unsigned int width) +{ + return { static_cast<uint64_t>(value), width ? width : 16 }; +} + +template<> +inline _hex hex<uint64_t>(uint64_t value, unsigned int width) +{ + return { static_cast<uint64_t>(value), width ? width : 16 }; +} +#endif + } /* namespace utils */ } /* namespace libcamera */ diff --git a/src/libcamera/stream.cpp b/src/libcamera/stream.cpp index c28b4cd6..610920d1 100644 --- a/src/libcamera/stream.cpp +++ b/src/libcamera/stream.cpp @@ -16,6 +16,7 @@ #include <libcamera/request.h> #include "log.h" +#include "utils.h" /** * \file stream.h @@ -367,11 +368,7 @@ StreamConfiguration::StreamConfiguration(const StreamFormats &formats) std::string StreamConfiguration::toString() const { std::stringstream ss; - - ss.fill(0); - ss << size.toString() << "-0x" << std::hex << std::setw(8) - << pixelFormat; - + ss << size.toString() << "-" << utils::hex(pixelFormat); return ss.str(); } diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp index 928db254..d632f6e6 100644 --- a/src/libcamera/utils.cpp +++ b/src/libcamera/utils.cpp @@ -143,6 +143,45 @@ std::string time_point_to_string(const time_point &time) return ossTimestamp.str(); } +std::basic_ostream<char, std::char_traits<char>> & +operator<<(std::basic_ostream<char, std::char_traits<char>> &stream, const _hex &h) +{ + stream << "0x"; + + std::ostream::fmtflags flags = stream.setf(std::ios_base::hex, + std::ios_base::basefield); + std::streamsize width = stream.width(h.w); + char fill = stream.fill('0'); + + stream << h.v; + + stream.flags(flags); + stream.width(width); + stream.fill(fill); + + return stream; +} + +/** + * \fn hex(T value, unsigned int width) + * \brief Write an hexadecimal value to an output string + * \param value The value + * \param width The width + * + * Return an object of unspecified type such that, if \a os is the name of an + * output stream of type std::ostream, and T is an integer type, then the + * expression + * + * \code{.cpp} + * os << utils::hex(value) + * \endcode + * + * will output the \a value to the stream in hexadecimal form with the base + * prefix and the filling character set to '0'. The field width is set to \a + * width if specified to a non-zero value, or to the native width of type T + * otherwise. The \a os stream configuration is not modified. + */ + } /* namespace utils */ } /* namespace libcamera */ diff --git a/src/libcamera/v4l2_subdevice.cpp b/src/libcamera/v4l2_subdevice.cpp index a188298d..f2bcd7f7 100644 --- a/src/libcamera/v4l2_subdevice.cpp +++ b/src/libcamera/v4l2_subdevice.cpp @@ -21,6 +21,7 @@ #include "log.h" #include "media_device.h" #include "media_object.h" +#include "utils.h" /** * \file v4l2_subdevice.h @@ -76,10 +77,7 @@ LOG_DECLARE_CATEGORY(V4L2) const std::string V4L2SubdeviceFormat::toString() const { std::stringstream ss; - - ss.fill(0); - ss << size.toString() << "-0x" << std::hex << std::setw(4) << mbus_code; - + ss << size.toString() << "-" << utils::hex(mbus_code, 4); return ss.str(); } diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index eb4e44de..208ab541 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -23,6 +23,7 @@ #include "log.h" #include "media_device.h" #include "media_object.h" +#include "utils.h" /** * \file v4l2_videodevice.h @@ -239,10 +240,7 @@ LOG_DECLARE_CATEGORY(V4L2) const std::string V4L2DeviceFormat::toString() const { std::stringstream ss; - - ss.fill(0); - ss << size.toString() << "-0x" << std::hex << std::setw(8) << fourcc; - + ss << size.toString() << "-" << utils::hex(fourcc); return ss.str(); } |