[libcamera-devel,v3,06/13] libcamera: stream: Add operator<<() to print StreamRole as a string
diff mbox series

Message ID 20221024000356.29521-7-laurent.pinchart@ideasonboard.com
State Accepted
Headers show
Series
  • Add Bayer format support for RkISP1
Related show

Commit Message

Laurent Pinchart Oct. 24, 2022, 12:03 a.m. UTC
libcamera prints stream role values in log messages. To be more
user-friendly, add a specialization of operator<<() to print the role
name as a string instead of a numerical value.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 include/libcamera/stream.h |  3 +++
 src/libcamera/stream.cpp   | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+)

Comments

Nicolas Dufresne via libcamera-devel Oct. 24, 2022, 7:40 a.m. UTC | #1
On Mon, Oct 24, 2022 at 03:03:49AM +0300, Laurent Pinchart wrote:
> libcamera prints stream role values in log messages. To be more
> user-friendly, add a specialization of operator<<() to print the role
> name as a string instead of a numerical value.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>

> ---
>  include/libcamera/stream.h |  3 +++
>  src/libcamera/stream.cpp   | 19 +++++++++++++++++++
>  2 files changed, 22 insertions(+)
> 
> diff --git a/include/libcamera/stream.h b/include/libcamera/stream.h
> index f0ae7e62e0a3..efec695ad317 100644
> --- a/include/libcamera/stream.h
> +++ b/include/libcamera/stream.h
> @@ -9,6 +9,7 @@
>  
>  #include <map>
>  #include <memory>
> +#include <ostream>
>  #include <string>
>  #include <vector>
>  
> @@ -70,6 +71,8 @@ enum StreamRole {
>  
>  using StreamRoles = std::vector<StreamRole>;
>  
> +std::ostream &operator<<(std::ostream &out, StreamRole role);
> +
>  class Stream
>  {
>  public:
> diff --git a/src/libcamera/stream.cpp b/src/libcamera/stream.cpp
> index 686e693bccba..67f308157fbf 100644
> --- a/src/libcamera/stream.cpp
> +++ b/src/libcamera/stream.cpp
> @@ -417,6 +417,25 @@ std::string StreamConfiguration::toString() const
>   * acceptable.
>   */
>  
> +/**
> + * \brief Insert a text representation of a StreamRole into an output stream
> + * \param[in] out The output stream
> + * \param[in] role The StreamRole
> + * \return The output stream \a out
> + */
> +std::ostream &operator<<(std::ostream &out, StreamRole role)
> +{
> +	static constexpr std::array<const char *, 4> names{
> +		"Raw",
> +		"StillCapture",
> +		"VideoRecording",
> +		"Viewfinder",
> +	};
> +
> +	out << names[static_cast<std::underlying_type_t<StreamRole>>(role)];
> +	return out;
> +}
> +
>  /**
>   * \typedef StreamRoles
>   * \brief A vector of StreamRole
> -- 
> Regards,
> 
> Laurent Pinchart
>
Jacopo Mondi Oct. 26, 2022, 3:07 p.m. UTC | #2
Hi Laurent

On Mon, Oct 24, 2022 at 03:03:49AM +0300, Laurent Pinchart via libcamera-devel wrote:
> libcamera prints stream role values in log messages. To be more
> user-friendly, add a specialization of operator<<() to print the role
> name as a string instead of a numerical value.
>
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> ---
>  include/libcamera/stream.h |  3 +++
>  src/libcamera/stream.cpp   | 19 +++++++++++++++++++
>  2 files changed, 22 insertions(+)
>
> diff --git a/include/libcamera/stream.h b/include/libcamera/stream.h
> index f0ae7e62e0a3..efec695ad317 100644
> --- a/include/libcamera/stream.h
> +++ b/include/libcamera/stream.h
> @@ -9,6 +9,7 @@
>
>  #include <map>
>  #include <memory>
> +#include <ostream>
>  #include <string>
>  #include <vector>
>
> @@ -70,6 +71,8 @@ enum StreamRole {
>
>  using StreamRoles = std::vector<StreamRole>;
>
> +std::ostream &operator<<(std::ostream &out, StreamRole role);
> +
>  class Stream
>  {
>  public:
> diff --git a/src/libcamera/stream.cpp b/src/libcamera/stream.cpp
> index 686e693bccba..67f308157fbf 100644
> --- a/src/libcamera/stream.cpp
> +++ b/src/libcamera/stream.cpp
> @@ -417,6 +417,25 @@ std::string StreamConfiguration::toString() const
>   * acceptable.
>   */
>
> +/**
> + * \brief Insert a text representation of a StreamRole into an output stream
> + * \param[in] out The output stream
> + * \param[in] role The StreamRole
> + * \return The output stream \a out
> + */
> +std::ostream &operator<<(std::ostream &out, StreamRole role)
> +{
> +	static constexpr std::array<const char *, 4> names{
> +		"Raw",
> +		"StillCapture",
> +		"VideoRecording",
> +		"Viewfinder",
> +	};

My only concern is about the enum and the list of names going out of
sync. A map might help ?

> +
> +	out << names[static_cast<std::underlying_type_t<StreamRole>>(role)];
> +	return out;
> +}
> +
>  /**
>   * \typedef StreamRoles
>   * \brief A vector of StreamRole
> --
> Regards,
>
> Laurent Pinchart
>
Laurent Pinchart Oct. 30, 2022, 5:19 p.m. UTC | #3
Hi Jacopo,

On Wed, Oct 26, 2022 at 05:07:03PM +0200, Jacopo Mondi wrote:
> On Mon, Oct 24, 2022 at 03:03:49AM +0300, Laurent Pinchart via libcamera-devel wrote:
> > libcamera prints stream role values in log messages. To be more
> > user-friendly, add a specialization of operator<<() to print the role
> > name as a string instead of a numerical value.
> >
> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> 
> > ---
> >  include/libcamera/stream.h |  3 +++
> >  src/libcamera/stream.cpp   | 19 +++++++++++++++++++
> >  2 files changed, 22 insertions(+)
> >
> > diff --git a/include/libcamera/stream.h b/include/libcamera/stream.h
> > index f0ae7e62e0a3..efec695ad317 100644
> > --- a/include/libcamera/stream.h
> > +++ b/include/libcamera/stream.h
> > @@ -9,6 +9,7 @@
> >
> >  #include <map>
> >  #include <memory>
> > +#include <ostream>
> >  #include <string>
> >  #include <vector>
> >
> > @@ -70,6 +71,8 @@ enum StreamRole {
> >
> >  using StreamRoles = std::vector<StreamRole>;
> >
> > +std::ostream &operator<<(std::ostream &out, StreamRole role);
> > +
> >  class Stream
> >  {
> >  public:
> > diff --git a/src/libcamera/stream.cpp b/src/libcamera/stream.cpp
> > index 686e693bccba..67f308157fbf 100644
> > --- a/src/libcamera/stream.cpp
> > +++ b/src/libcamera/stream.cpp
> > @@ -417,6 +417,25 @@ std::string StreamConfiguration::toString() const
> >   * acceptable.
> >   */
> >
> > +/**
> > + * \brief Insert a text representation of a StreamRole into an output stream
> > + * \param[in] out The output stream
> > + * \param[in] role The StreamRole
> > + * \return The output stream \a out
> > + */
> > +std::ostream &operator<<(std::ostream &out, StreamRole role)
> > +{
> > +	static constexpr std::array<const char *, 4> names{
> > +		"Raw",
> > +		"StillCapture",
> > +		"VideoRecording",
> > +		"Viewfinder",
> > +	};
> 
> My only concern is about the enum and the list of names going out of
> sync. A map might help ?

I'm not very concerned by the risk of reordering values, I don't think
we will do that. There is a risk of forgetting to update this code when
adding values though, but I haven't found a goog way to catch this at
compilation time.

> > +
> > +	out << names[static_cast<std::underlying_type_t<StreamRole>>(role)];
> > +	return out;
> > +}
> > +
> >  /**
> >   * \typedef StreamRoles
> >   * \brief A vector of StreamRole

Patch
diff mbox series

diff --git a/include/libcamera/stream.h b/include/libcamera/stream.h
index f0ae7e62e0a3..efec695ad317 100644
--- a/include/libcamera/stream.h
+++ b/include/libcamera/stream.h
@@ -9,6 +9,7 @@ 
 
 #include <map>
 #include <memory>
+#include <ostream>
 #include <string>
 #include <vector>
 
@@ -70,6 +71,8 @@  enum StreamRole {
 
 using StreamRoles = std::vector<StreamRole>;
 
+std::ostream &operator<<(std::ostream &out, StreamRole role);
+
 class Stream
 {
 public:
diff --git a/src/libcamera/stream.cpp b/src/libcamera/stream.cpp
index 686e693bccba..67f308157fbf 100644
--- a/src/libcamera/stream.cpp
+++ b/src/libcamera/stream.cpp
@@ -417,6 +417,25 @@  std::string StreamConfiguration::toString() const
  * acceptable.
  */
 
+/**
+ * \brief Insert a text representation of a StreamRole into an output stream
+ * \param[in] out The output stream
+ * \param[in] role The StreamRole
+ * \return The output stream \a out
+ */
+std::ostream &operator<<(std::ostream &out, StreamRole role)
+{
+	static constexpr std::array<const char *, 4> names{
+		"Raw",
+		"StillCapture",
+		"VideoRecording",
+		"Viewfinder",
+	};
+
+	out << names[static_cast<std::underlying_type_t<StreamRole>>(role)];
+	return out;
+}
+
 /**
  * \typedef StreamRoles
  * \brief A vector of StreamRole