Patch Detail
Show a patch.
GET /api/patches/2181/?format=api
{ "id": 2181, "url": "https://patchwork.libcamera.org/api/patches/2181/?format=api", "web_url": "https://patchwork.libcamera.org/patch/2181/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20191013192346.8270-1-laurent.pinchart@ideasonboard.com>", "date": "2019-10-13T19:23:46", "name": "[libcamera-devel] libcamera: utils: Add hex stream output helper", "commit_ref": "f391048a7b987a149d0ba5421846b9b5ab916338", "pull_url": null, "state": "accepted", "archived": false, "hash": "af99d881b7a135193a1b8738e39ab8cfec7b025f", "submitter": { "id": 2, "url": "https://patchwork.libcamera.org/api/people/2/?format=api", "name": "Laurent Pinchart", "email": "laurent.pinchart@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/2181/mbox/", "series": [ { "id": 533, "url": "https://patchwork.libcamera.org/api/series/533/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=533", "date": "2019-10-13T19:23:46", "name": "[libcamera-devel] libcamera: utils: Add hex stream output helper", "version": 1, "mbox": "https://patchwork.libcamera.org/series/533/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/2181/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/2181/checks/", "tags": {}, "headers": { "Return-Path": "<laurent.pinchart@ideasonboard.com>", "Received": [ "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id BD3E5600F9\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 13 Oct 2019 21:23:52 +0200 (CEST)", "from pendragon.bb.dnainternet.fi\n\t(dfj612yhrgyx302h3jwwy-3.rev.dnainternet.fi\n\t[IPv6:2001:14ba:21f5:5b00:ce28:277f:58d7:3ca4])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 3EDA9A46\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 13 Oct 2019 21:23:52 +0200 (CEST)" ], "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1570994632;\n\tbh=vw9g9xOpkn0qNHOLKRDozEq+H/58HRU/YfrPG4xHtLs=;\n\th=From:To:Subject:Date:From;\n\tb=gNbs80YS5n2yYwmHHBP5xnCl0/cO9lyot2gfuzaJZqK7QcjzDbXMy1CzjDGQgNB85\n\t6Pj0bOZdbGHpU0db1rGX044ExQRXP4/4x5QUWX82URSdG0pVm8EIEIPc9fjXRU6K6Q\n\thVqzQMis3eZLOh2qD1GbMNdeB0Xx6/hb4jcBcplk=", "From": "Laurent Pinchart <laurent.pinchart@ideasonboard.com>", "To": "libcamera-devel@lists.libcamera.org", "Date": "Sun, 13 Oct 2019 22:23:46 +0300", "Message-Id": "<20191013192346.8270-1-laurent.pinchart@ideasonboard.com>", "X-Mailer": "git-send-email 2.21.0", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "Subject": "[libcamera-devel] [PATCH] libcamera: utils: Add hex stream output\n\thelper", "X-BeenThere": "libcamera-devel@lists.libcamera.org", "X-Mailman-Version": "2.1.29", "Precedence": "list", "List-Id": "<libcamera-devel.lists.libcamera.org>", "List-Unsubscribe": "<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>", "List-Archive": "<https://lists.libcamera.org/pipermail/libcamera-devel/>", "List-Post": "<mailto:libcamera-devel@lists.libcamera.org>", "List-Help": "<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>", "List-Subscribe": "<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>", "X-List-Received-Date": "Sun, 13 Oct 2019 19:23:52 -0000" }, "content": "Add a utils::hex() function that simplifies writing hexadecimal values\nto an ostream. The function handles the '0x' prefix, the field width and\nthe fill character automatically. Use it through the libcamera code\nbase, and add a test.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/android/camera_device.cpp | 2 +-\n src/libcamera/camera_sensor.cpp | 6 ++--\n src/libcamera/controls.cpp | 6 ++--\n src/libcamera/include/utils.h | 40 ++++++++++++++++++++++\n src/libcamera/stream.cpp | 7 ++--\n src/libcamera/utils.cpp | 39 ++++++++++++++++++++++\n src/libcamera/v4l2_subdevice.cpp | 6 ++--\n src/libcamera/v4l2_videodevice.cpp | 6 ++--\n test/camera-sensor.cpp | 3 +-\n test/meson.build | 1 +\n test/utils.cpp | 53 ++++++++++++++++++++++++++++++\n 11 files changed, 147 insertions(+), 22 deletions(-)\n create mode 100644 test/utils.cpp", "diff": "diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp\nindex bf991d5933cd..c7c9b3fd1724 100644\n--- a/src/android/camera_device.cpp\n+++ b/src/android/camera_device.cpp\n@@ -640,7 +640,7 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list)\n \t\t\t << \", direction: \" << stream->stream_type\n \t\t\t << \", width: \" << stream->width\n \t\t\t << \", height: \" << stream->height\n-\t\t\t << \", format: \" << std::hex << stream->format;\n+\t\t\t << \", format: \" << utils::hex(stream->format);\n \t}\n \n \t/* Hardcode viewfinder role, collecting sizes from the stream config. */\ndiff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp\nindex 9e8b44a23850..1b8e8c0e07da 100644\n--- a/src/libcamera/camera_sensor.cpp\n+++ b/src/libcamera/camera_sensor.cpp\n@@ -14,6 +14,7 @@\n #include <math.h>\n \n #include \"formats.h\"\n+#include \"utils.h\"\n #include \"v4l2_subdevice.h\"\n \n /**\n@@ -79,9 +80,8 @@ int CameraSensor::init()\n \n \tif (entity_->function() != MEDIA_ENT_F_CAM_SENSOR) {\n \t\tLOG(CameraSensor, Error)\n-\t\t\t<< \"Invalid sensor function 0x\"\n-\t\t\t<< std::hex << std::setfill('0') << std::setw(8)\n-\t\t\t<< entity_->function();\n+\t\t\t<< \"Invalid sensor function \"\n+\t\t\t<< utils::hex(entity_->function());\n \t\treturn -EINVAL;\n \t}\n \ndiff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp\nindex e46aa438a06e..6a0301f3a2ae 100644\n--- a/src/libcamera/controls.cpp\n+++ b/src/libcamera/controls.cpp\n@@ -549,8 +549,7 @@ const ControlValue &ControlList::get(unsigned int id) const\n \tconst auto ctrl = idmap_->find(id);\n \tif (ctrl == idmap_->end()) {\n \t\tLOG(Controls, Error)\n-\t\t\t<< std::hex << std::setfill('0')\n-\t\t\t<< \"Control 0x\" << std::setw(8) << id\n+\t\t\t<< \"Control \" << utils::hex(id)\n \t\t\t<< \" is not supported\";\n \t\treturn zero;\n \t}\n@@ -579,8 +578,7 @@ void ControlList::set(unsigned int id, const ControlValue &value)\n \tconst auto ctrl = idmap_->find(id);\n \tif (ctrl == idmap_->end()) {\n \t\tLOG(Controls, Error)\n-\t\t\t<< std::hex << std::setfill('0')\n-\t\t\t<< \"Control 0x\" << std::setw(8) << id\n+\t\t\t<< \"Control 0x\" << utils::hex(id)\n \t\t\t<< \" is not supported\";\n \t\treturn;\n \t}\ndiff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h\nindex 52eee8ac2804..3efb11c119c2 100644\n--- a/src/libcamera/include/utils.h\n+++ b/src/libcamera/include/utils.h\n@@ -10,6 +10,7 @@\n #include <algorithm>\n #include <chrono>\n #include <memory>\n+#include <ostream>\n #include <string>\n #include <sys/time.h>\n \n@@ -63,6 +64,45 @@ using time_point = std::chrono::steady_clock::time_point;\n struct timespec duration_to_timespec(const duration &value);\n std::string time_point_to_string(const time_point &time);\n \n+#ifndef __DOXYGEN__\n+struct _hex {\n+\tuint64_t v;\n+\tunsigned int w;\n+};\n+\n+std::basic_ostream<char, std::char_traits<char>> &\n+operator<<(std::basic_ostream<char, std::char_traits<char>> &stream, const _hex &h);\n+#endif\n+\n+template<typename T>\n+_hex hex(T value, unsigned int width = 0);\n+\n+#ifndef __DOXYGEN__\n+template<>\n+inline _hex hex<int32_t>(int32_t value, unsigned int width)\n+{\n+\treturn { static_cast<uint64_t>(value), width ? width : 8 };\n+}\n+\n+template<>\n+inline _hex hex<uint32_t>(uint32_t value, unsigned int width)\n+{\n+\treturn { static_cast<uint64_t>(value), width ? width : 8 };\n+}\n+\n+template<>\n+inline _hex hex<int64_t>(int64_t value, unsigned int width)\n+{\n+\treturn { static_cast<uint64_t>(value), width ? width : 16 };\n+}\n+\n+template<>\n+inline _hex hex<uint64_t>(uint64_t value, unsigned int width)\n+{\n+\treturn { static_cast<uint64_t>(value), width ? width : 16 };\n+}\n+#endif\n+\n } /* namespace utils */\n \n } /* namespace libcamera */\ndiff --git a/src/libcamera/stream.cpp b/src/libcamera/stream.cpp\nindex c28b4cd669b2..610920d1e5b3 100644\n--- a/src/libcamera/stream.cpp\n+++ b/src/libcamera/stream.cpp\n@@ -16,6 +16,7 @@\n #include <libcamera/request.h>\n \n #include \"log.h\"\n+#include \"utils.h\"\n \n /**\n * \\file stream.h\n@@ -367,11 +368,7 @@ StreamConfiguration::StreamConfiguration(const StreamFormats &formats)\n std::string StreamConfiguration::toString() const\n {\n \tstd::stringstream ss;\n-\n-\tss.fill(0);\n-\tss << size.toString() << \"-0x\" << std::hex << std::setw(8)\n-\t << pixelFormat;\n-\n+\tss << size.toString() << \"-\" << utils::hex(pixelFormat);\n \treturn ss.str();\n }\n \ndiff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp\nindex 928db254ec67..d632f6e66638 100644\n--- a/src/libcamera/utils.cpp\n+++ b/src/libcamera/utils.cpp\n@@ -143,6 +143,45 @@ std::string time_point_to_string(const time_point &time)\n \treturn ossTimestamp.str();\n }\n \n+std::basic_ostream<char, std::char_traits<char>> &\n+operator<<(std::basic_ostream<char, std::char_traits<char>> &stream, const _hex &h)\n+{\n+\tstream << \"0x\";\n+\n+\tstd::ostream::fmtflags flags = stream.setf(std::ios_base::hex,\n+\t\t\t\t\t\t std::ios_base::basefield);\n+\tstd::streamsize width = stream.width(h.w);\n+\tchar fill = stream.fill('0');\n+\n+\tstream << h.v;\n+\n+\tstream.flags(flags);\n+\tstream.width(width);\n+\tstream.fill(fill);\n+\n+\treturn stream;\n+}\n+\n+/**\n+ * \\fn hex(T value, unsigned int width)\n+ * \\brief Write an hexadecimal value to an output string\n+ * \\param value The value\n+ * \\param width The width\n+ *\n+ * Return an object of unspecified type such that, if \\a os is the name of an\n+ * output stream of type std::ostream, and T is an integer type, then the\n+ * expression\n+ *\n+ * \\code{.cpp}\n+ * os << utils::hex(value)\n+ * \\endcode\n+ *\n+ * will output the \\a value to the stream in hexadecimal form with the base\n+ * prefix and the filling character set to '0'. The field width is set to \\a\n+ * width if specified to a non-zero value, or to the native width of type T\n+ * otherwise. The \\a os stream configuration is not modified.\n+ */\n+\n } /* namespace utils */\n \n } /* namespace libcamera */\ndiff --git a/src/libcamera/v4l2_subdevice.cpp b/src/libcamera/v4l2_subdevice.cpp\nindex a188298de34c..f2bcd7f73c5c 100644\n--- a/src/libcamera/v4l2_subdevice.cpp\n+++ b/src/libcamera/v4l2_subdevice.cpp\n@@ -21,6 +21,7 @@\n #include \"log.h\"\n #include \"media_device.h\"\n #include \"media_object.h\"\n+#include \"utils.h\"\n \n /**\n * \\file v4l2_subdevice.h\n@@ -76,10 +77,7 @@ LOG_DECLARE_CATEGORY(V4L2)\n const std::string V4L2SubdeviceFormat::toString() const\n {\n \tstd::stringstream ss;\n-\n-\tss.fill(0);\n-\tss << size.toString() << \"-0x\" << std::hex << std::setw(4) << mbus_code;\n-\n+\tss << size.toString() << \"-\" << utils::hex(mbus_code, 4);\n \treturn ss.str();\n }\n \ndiff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\nindex eb4e44deb4a5..208ab54199b1 100644\n--- a/src/libcamera/v4l2_videodevice.cpp\n+++ b/src/libcamera/v4l2_videodevice.cpp\n@@ -23,6 +23,7 @@\n #include \"log.h\"\n #include \"media_device.h\"\n #include \"media_object.h\"\n+#include \"utils.h\"\n \n /**\n * \\file v4l2_videodevice.h\n@@ -239,10 +240,7 @@ LOG_DECLARE_CATEGORY(V4L2)\n const std::string V4L2DeviceFormat::toString() const\n {\n \tstd::stringstream ss;\n-\n-\tss.fill(0);\n-\tss << size.toString() << \"-0x\" << std::hex << std::setw(8) << fourcc;\n-\n+\tss << size.toString() << \"-\" << utils::hex(fourcc);\n \treturn ss.str();\n }\n \ndiff --git a/test/camera-sensor.cpp b/test/camera-sensor.cpp\nindex 9fe59cc98d79..27c190fe7ace 100644\n--- a/test/camera-sensor.cpp\n+++ b/test/camera-sensor.cpp\n@@ -13,6 +13,7 @@\n #include \"camera_sensor.h\"\n #include \"device_enumerator.h\"\n #include \"media_device.h\"\n+#include \"utils.h\"\n #include \"v4l2_subdevice.h\"\n \n #include \"test.h\"\n@@ -91,7 +92,7 @@ protected:\n \t\tif (format.mbus_code != MEDIA_BUS_FMT_SBGGR10_1X10 ||\n \t\t format.size != Size(4096, 2160)) {\n \t\t\tcerr << \"Failed to get a suitable format, expected 4096x2160-0x\"\n-\t\t\t << std::hex << MEDIA_BUS_FMT_SBGGR10_1X10\n+\t\t\t << utils::hex(MEDIA_BUS_FMT_SBGGR10_1X10)\n \t\t\t << \", got \" << format.toString() << endl;\n \t\t\treturn TestFail;\n \t\t}\ndiff --git a/test/meson.build b/test/meson.build\nindex 84722cceb35d..cf5eb84d20b2 100644\n--- a/test/meson.build\n+++ b/test/meson.build\n@@ -30,6 +30,7 @@ internal_tests = [\n ['threads', 'threads.cpp'],\n ['timer', 'timer.cpp'],\n ['timer-thread', 'timer-thread.cpp'],\n+ ['utils', 'utils.cpp'],\n ]\n \n foreach t : public_tests\ndiff --git a/test/utils.cpp b/test/utils.cpp\nnew file mode 100644\nindex 000000000000..9fe0d4775b73\n--- /dev/null\n+++ b/test/utils.cpp\n@@ -0,0 +1,53 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * Copyright (C) 2018, Google Inc.\n+ *\n+ * utils.cpp - Miscellaneous utility tests\n+ */\n+\n+#include <iostream>\n+#include <sstream>\n+\n+#include \"test.h\"\n+#include \"utils.h\"\n+\n+using namespace std;\n+using namespace libcamera;\n+\n+class UtilsTest : public Test\n+{\n+protected:\n+\tint run()\n+\t{\n+\t\tstd::ostringstream os;\n+\t\tstd::string ref;\n+\n+\t\tos << utils::hex(static_cast<int32_t>(0x42)) << \" \";\n+\t\tref += \"0x00000042 \";\n+\t\tos << utils::hex(static_cast<uint32_t>(0x42)) << \" \";\n+\t\tref += \"0x00000042 \";\n+\t\tos << utils::hex(static_cast<int64_t>(0x42)) << \" \";\n+\t\tref += \"0x0000000000000042 \";\n+\t\tos << utils::hex(static_cast<uint64_t>(0x42)) << \" \";\n+\t\tref += \"0x0000000000000042 \";\n+\t\tos << utils::hex(static_cast<int32_t>(0x42), 4) << \" \";\n+\t\tref += \"0x0042 \";\n+\t\tos << utils::hex(static_cast<uint32_t>(0x42), 1) << \" \";\n+\t\tref += \"0x42 \";\n+\t\tos << utils::hex(static_cast<int64_t>(0x42), 4) << \" \";\n+\t\tref += \"0x0042 \";\n+\t\tos << utils::hex(static_cast<uint64_t>(0x42), 1) << \" \";\n+\t\tref += \"0x42 \";\n+\n+\t\tstd::string s = os.str();\n+\t\tif (s != ref) {\n+\t\t\tcerr << \"utils::hex() test failed, expected '\" << ref\n+\t\t\t << \"', got '\" << s << \"'\";\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\treturn TestPass;\n+\t}\n+};\n+\n+TEST_REGISTER(UtilsTest)\n", "prefixes": [ "libcamera-devel" ] }