Merge lp:~thomas-voss/biometryd/pretty-print-details-in-tracing-observer into lp:biometryd

Proposed by Thomas Voß
Status: Merged
Approved by: Thomas Voß
Approved revision: 18
Merged at revision: 18
Proposed branch: lp:~thomas-voss/biometryd/pretty-print-details-in-tracing-observer
Merge into: lp:biometryd
Diff against target: 144 lines (+86/-24)
1 file modified
src/biometry/tracing_operation_observer.h (+86/-24)
To merge this branch: bzr merge lp:~thomas-voss/biometryd/pretty-print-details-in-tracing-observer
Reviewer Review Type Date Requested Status
Ubuntu Phablet Team Pending
Review via email: mp+294875@code.launchpad.net

Commit message

Add pretty printing of actual values passed to TracingObserver<T>::*.

Description of the change

Add pretty printing of actual values passed to TracingObserver<T>::*.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/biometry/tracing_operation_observer.h'
2--- src/biometry/tracing_operation_observer.h 2016-05-11 14:39:41 +0000
3+++ src/biometry/tracing_operation_observer.h 2016-05-17 06:40:55 +0000
4@@ -20,13 +20,67 @@
5 #ifndef BIOMETRYD_TRACING_OPERATION_OBSERVER_H_
6 #define BIOMETRYD_TRACING_OPERATION_OBSERVER_H_
7
8+#include <biometry/dictionary.h>
9 #include <biometry/operation.h>
10+#include <biometry/progress.h>
11+#include <biometry/variant.h>
12+#include <biometry/void.h>
13
14 #include <iostream>
15 #include <string>
16
17 namespace biometry
18 {
19+namespace detail
20+{
21+// Printer helps in pretty printing a value with a given offset as in:
22+// [ offset * ' ' ]value;
23+// We rely on this customization to make it easy to provide nicely formatted output
24+// without the need to provide a full abstraction on top of a std::ostream while still being
25+// able to only have one TracingObserver<T> without type specializations.
26+template<typename T>
27+struct Printer
28+{
29+ static std::ostream& print(std::ostream& out, const T& value, std::size_t offset)
30+ {
31+ return out << std::string(offset, ' ') << value;
32+ }
33+};
34+
35+template<>
36+struct Printer<biometry::Dictionary>
37+{
38+ static std::ostream& print(std::ostream& out, const biometry::Dictionary& dict, std::size_t offset)
39+ {
40+ std::string indent(offset, ' ');
41+ for (const auto& pair : dict)
42+ out << indent << pair.first << " = " << pair.second << std::endl;
43+
44+ return out;
45+ }
46+};
47+
48+template<>
49+struct Printer<biometry::Progress>
50+{
51+ static std::ostream& print(std::ostream& out, const biometry::Progress& progress, std::size_t offset)
52+ {
53+ std::string indent(offset, ' ');
54+ out << indent << progress.percent << " complete, with details:" << std::endl;
55+ Printer<biometry::Dictionary>::print(out, progress.details, offset + 2);
56+ return out;
57+ }
58+};
59+
60+template<>
61+struct Printer<biometry::Void>
62+{
63+ static std::ostream& print(std::ostream& out, const biometry::Void&, std::size_t)
64+ {
65+ return out;
66+ }
67+};
68+}
69 template<typename T>
70 class TracingObserver : public biometry::Operation<T>::Observer
71 {
72@@ -34,40 +88,48 @@
73 typedef typename biometry::Operation<T>::Observer Super;
74
75 TracingObserver(std::size_t offset = 0, std::ostream& out = std::cout)
76- : indent(offset, ' '),
77+ : offset{offset},
78+ indent(offset, ' '),
79 out{out}
80 {
81 }
82
83- void on_started()
84- {
85- out << indent << __FUNCTION__ << std::endl;
86- }
87-
88- void on_progress(const typename Super::Progress&)
89- {
90- out << indent << __FUNCTION__ << std::endl;
91- }
92-
93- void on_canceled(const typename Super::Reason&)
94- {
95- out << indent << __FUNCTION__ << std::endl;
96- }
97-
98- void on_failed(const typename Super::Error&)
99- {
100- out << indent << __FUNCTION__ << std::endl;
101- }
102-
103- void on_succeeded(const typename Super::Result&)
104- {
105- out << indent << __FUNCTION__ << std::endl;
106+ void on_started() override
107+ {
108+ out << indent << __FUNCTION__ << std::endl;
109+ }
110+
111+ void on_progress(const typename Super::Progress& progress) override
112+ {
113+ out << indent << __FUNCTION__<< ": " << std::endl;
114+ detail::Printer<typename Super::Progress>::print(out, progress, offset + 2) << std::endl;
115+ }
116+
117+ void on_canceled(const typename Super::Reason& reason) override
118+ {
119+ out << indent << __FUNCTION__ << ": " << std::endl;
120+ detail::Printer<typename Super::Reason>::print(out, reason, offset + 2) << std::endl;
121+ }
122+
123+ void on_failed(const typename Super::Error& error) override
124+ {
125+ out << indent << __FUNCTION__ << ": " << std::endl;
126+ detail::Printer<typename Super::Error>::print(out, error, offset + 2) << std::endl;
127+ }
128+
129+ void on_succeeded(const typename Super::Result& result) override
130+ {
131+ out << indent << __FUNCTION__ << std::endl;
132+ detail::Printer<typename Super::Result>::print(out, result, offset + 2) << std::endl;
133 }
134
135 private:
136+ std::size_t offset;
137 std::string indent;
138 std::ostream& out;
139 };
140+
141+
142 }
143
144 #endif // BIOMETRYD_TRACING_OPERATION_OBSERVER_H_

Subscribers

People subscribed via source and target branches