1 /* 2 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License, version 2.0, 6 * as published by the Free Software Foundation. 7 * 8 * This program is also distributed with certain software (including 9 * but not limited to OpenSSL) that is licensed under separate terms, 10 * as designated in a particular file or component or in included license 11 * documentation. The authors of MySQL hereby grant you an additional 12 * permission to link the program and your derivative works with the 13 * separately licensed software that they have included with MySQL. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License, version 2.0, for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 23 */ 24 25 #ifndef PLUGIN_X_TESTS_DRIVER_FORMATTERS_CONSOLE_H_ 26 #define PLUGIN_X_TESTS_DRIVER_FORMATTERS_CONSOLE_H_ 27 28 #include <ostream> 29 #include <set> 30 #include <string> 31 #include <utility> 32 33 #include "plugin/x/client/mysqlxclient/xerror.h" 34 #include "plugin/x/client/mysqlxclient/xprotocol.h" 35 36 std::ostream &operator<<(std::ostream &os, const xcl::XError &err); 37 std::ostream &operator<<(std::ostream &os, const std::exception &exc); 38 std::ostream &operator<<(std::ostream &os, const std::set<int> &value); 39 std::ostream &operator<<(std::ostream &os, 40 const xcl::XProtocol::Message &message); 41 42 class Console { 43 public: 44 struct Options { 45 bool m_use_color{false}; 46 bool m_be_verbose{false}; 47 }; 48 49 public: 50 explicit Console(const Options &options); 51 Console(const Options &options, std::ostream *out, std::ostream *err); 52 53 template <typename T> print(const T & obj)54 void print(const T &obj) const { 55 (*m_out) << obj; 56 } 57 58 template <typename T, typename... R> print(const T & first,R &&...rest)59 void print(const T &first, R &&... rest) const { 60 print(first); 61 print(std::forward<R>(rest)...); 62 } 63 64 template <typename... T> print_verbose(T &&...args)65 void print_verbose(T &&... args) const { 66 if (m_options.m_be_verbose) print(std::forward<T>(args)...); 67 } 68 69 template <typename T> print_error(const T & obj)70 void print_error(const T &obj) const { 71 (*m_err) << obj; 72 } 73 74 template <typename T, typename... R> print_error(const T & first,R &&...rest)75 void print_error(const T &first, R &&... rest) const { 76 print_error(first); 77 print_error(std::forward<R>(rest)...); 78 } 79 80 template <typename... T> print_error_red(T &&...values)81 void print_error_red(T &&... values) const { 82 #ifndef _WIN32 83 if (m_options.m_use_color) 84 print_error(k_red, values..., k_clear); 85 else 86 #endif 87 print_error(values...); 88 } 89 90 private: 91 class Color { 92 public: Color(const char * const v)93 explicit Color(const char *const v) : m_value(v) {} 94 95 private: 96 const char *const m_value; 97 98 friend std::ostream &operator<<(std::ostream &os, const Color &c) { 99 return os << c.m_value; 100 } 101 }; 102 103 static const Color k_red; 104 static const Color k_clear; 105 const Options m_options; 106 std::ostream *m_out; 107 std::ostream *m_err; 108 }; 109 110 #endif // PLUGIN_X_TESTS_DRIVER_FORMATTERS_CONSOLE_H_ 111