Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(405)

Unified Diff: mojo/common/logging.h

Issue 1519673002: Add helpers for logging mojom objects. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« mojo/common/BUILD.gn ('K') | « mojo/common/BUILD.gn ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/common/logging.h
diff --git a/mojo/common/logging.h b/mojo/common/logging.h
new file mode 100644
index 0000000000000000000000000000000000000000..6cb69c6fba1a96ac03574548cd19b4a948f5404e
--- /dev/null
+++ b/mojo/common/logging.h
@@ -0,0 +1,81 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MOJO_COMMON_LOGGING_H_
+#define MOJO_COMMON_LOGGING_H_
+
+#include <ostream>
vardhan 2015/12/11 00:12:20 nit: blank line after this include
+#include "mojo/public/cpp/bindings/array.h"
+#include "mojo/public/cpp/bindings/interface_ptr.h"
+#include "mojo/public/cpp/bindings/map.h"
+
+// Mojom Logging helpers.
vardhan 2015/12/11 00:12:20 Probably "mojom debug string" is more clarifying;
+
+namespace mojo {
+
+// Support output formatting of Mojo structs by value or by reference
+// assuming there exists an operator<< overload that accepts a const reference.
+template <typename T, typename = typename T::Data_>
vardhan 2015/12/11 00:12:20 Hm, I was hoping you could use IsStructPtr<> in bi
+auto operator<<(std::ostream& os, const T* value) -> decltype(os << *value) {
vardhan 2015/12/11 00:12:20 What do you think about defining: template <Mojom
+ return value ? os << *value : os << "null";
+}
+
+template <typename T>
+std::ostream& operator<<(std::ostream& os, const mojo::StructPtr<T>& value) {
+ return os << value.get();
+}
+
+template <typename T>
+std::ostream& operator<<(std::ostream& os,
+ const mojo::InlinedStructPtr<T>& value) {
+ return os << value.get();
+}
+
+template <typename T>
+std::ostream& operator<<(std::ostream& os, const mojo::InterfacePtr<T>& iface) {
+ return os << "{impl=" << iface.get() << ", bound=" << iface.is_bound() << "}";
+}
+
+template <typename T>
+std::ostream& operator<<(std::ostream& os, const mojo::Array<T>& array) {
+ if (array) {
+ os << "[";
+ bool first = true;
+ for (auto it = array.storage().cbegin(); it != array.storage().cend();
+ ++it) {
+ if (first)
+ first = false;
+ else
+ os << ", ";
+ os << *it;
+ }
+ os << "]";
+ } else {
+ os << "null";
+ }
+ return os;
+}
+
+template <typename Key, typename Value>
+std::ostream& operator<<(std::ostream& os, const mojo::Map<Key, Value>& map) {
+ if (map) {
+ os << "[";
+ bool first = true;
+ for (auto it = map.cbegin(); it != map.cend(); ++it) {
+ if (first)
+ first = false;
+ else
+ os << ", ";
+ os << "{" << it.GetKey() << ": " << it.GetValue() << "}";
+ }
+ os << "]";
+ } else {
+ os << "null";
+ }
+ return os;
+}
+
+} // namespace mojo
+
+#endif // MOJO_COMMON_LOGGING_H_
« mojo/common/BUILD.gn ('K') | « mojo/common/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698