stream.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Data Differential YATL (i.e. libtest) library
  4. *
  5. * Copyright (C) 2012 Data Differential, http://datadifferential.com/
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * * The names of its contributors may not be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. #pragma once
  37. #include <iostream>
  38. #include <cassert>
  39. #include <sstream>
  40. #include <ctime>
  41. #include <ostream>
  42. #include <sys/types.h>
  43. #include <unistd.h>
  44. namespace libtest {
  45. namespace stream {
  46. namespace detail {
  47. template<class Ch, class Tr, class A>
  48. class channel {
  49. private:
  50. public:
  51. typedef std::basic_ostringstream<Ch, Tr, A> stream_buffer;
  52. public:
  53. void operator()(const stream_buffer& s, std::ostream& _out,
  54. const char* filename, int line_number, const char* func)
  55. {
  56. if (filename)
  57. {
  58. _out
  59. << filename
  60. << ":"
  61. << line_number
  62. << ": in "
  63. << func << "() pid("
  64. << getpid() << ") "
  65. << s.str()
  66. << std::endl;
  67. }
  68. else
  69. {
  70. _out
  71. << s.str()
  72. << std::endl;
  73. }
  74. }
  75. };
  76. template<class Ch, class Tr, class A>
  77. class channelln {
  78. private:
  79. public:
  80. typedef std::basic_ostringstream<Ch, Tr, A> stream_buffer;
  81. public:
  82. void operator()(const stream_buffer& s, std::ostream& _out,
  83. const char* filename, int line_number, const char* func)
  84. {
  85. if (filename)
  86. {
  87. _out
  88. << std::endl
  89. << filename
  90. << ":"
  91. << line_number
  92. << ": in "
  93. << func << "() pid("
  94. << getpid() << ") "
  95. << s.str()
  96. << std::endl;
  97. }
  98. else
  99. {
  100. _out
  101. << std::endl
  102. << s.str()
  103. << std::endl;
  104. }
  105. }
  106. };
  107. template<template <class Ch, class Tr, class A> class OutputPolicy, class Ch = char, class Tr = std::char_traits<Ch>, class A = std::allocator<Ch> >
  108. class log {
  109. private:
  110. typedef OutputPolicy<Ch, Tr, A> output_policy;
  111. private:
  112. std::ostream& _out;
  113. const char *_filename;
  114. int _line_number;
  115. const char *_func;
  116. public:
  117. log(std::ostream& out_arg, const char* filename, int line_number, const char* func) :
  118. _out(out_arg),
  119. _filename(filename),
  120. _line_number(line_number),
  121. _func(func)
  122. { }
  123. virtual ~log()
  124. {
  125. output_policy()(arg, _out, _filename, _line_number, _func);
  126. }
  127. public:
  128. template<class T>
  129. log &operator<<(const T &x)
  130. {
  131. arg << x;
  132. return *this;
  133. }
  134. private:
  135. typename output_policy::stream_buffer arg;
  136. private:
  137. log( const log& );
  138. const log& operator=( const log& );
  139. };
  140. } // namespace detail
  141. class make_cerr : public detail::log<detail::channelln> {
  142. public:
  143. make_cerr(const char* filename, int line_number, const char* func) :
  144. detail::log<detail::channelln>(std::cerr, filename, line_number, func)
  145. { }
  146. private:
  147. make_cerr( const make_cerr& );
  148. const make_cerr& operator=( const make_cerr& );
  149. };
  150. class cerr : public detail::log<detail::channel> {
  151. public:
  152. cerr(const char* filename, int line_number, const char* func) :
  153. detail::log<detail::channel>(std::cout, filename, line_number, func)
  154. { }
  155. private:
  156. cerr( const cerr& );
  157. const cerr& operator=( const cerr& );
  158. };
  159. class clog : public detail::log<detail::channel> {
  160. public:
  161. clog(const char* filename, int line_number, const char* func) :
  162. detail::log<detail::channel>(std::clog, filename, line_number, func)
  163. { }
  164. private:
  165. clog( const clog& );
  166. const clog& operator=( const clog& );
  167. };
  168. class make_cout : public detail::log<detail::channelln> {
  169. public:
  170. make_cout(const char* filename, int line_number, const char* func) :
  171. detail::log<detail::channelln>(std::cout, filename, line_number, func)
  172. { }
  173. private:
  174. make_cout( const make_cout& );
  175. const make_cout& operator=( const make_cout& );
  176. };
  177. class cout : public detail::log<detail::channel> {
  178. public:
  179. cout(const char* filename, int line_number, const char* func) :
  180. detail::log<detail::channel>(std::cout, filename, line_number, func)
  181. { }
  182. private:
  183. cout( const cout& );
  184. const cout& operator=( const cout& );
  185. };
  186. } // namespace stream
  187. #define Error stream::cerr(__FILE__, __LINE__, __func__)
  188. #define Out stream::cout(NULL, __LINE__, __func__)
  189. #define Outn() stream::cout(NULL, __LINE__, __func__) << " "
  190. #define Log stream::clog(NULL, __LINE__, __func__)
  191. #define Logn() stream::clog(NULL, __LINE__, __func__) << " "
  192. } // namespace libtest