RAUL  0.8.0
log.hpp
1 /* This file is part of Raul.
2  * Copyright (C) 2009 David Robillard <http://drobilla.net>
3  *
4  * Raul is free software; you can redistribute it and/or modify it under the
5  * terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17 
18 #ifndef RAUL_LOG_HPP
19 #define RAUL_LOG_HPP
20 
21 #include <iostream>
22 #include <sstream>
23 #include <string>
24 
25 namespace Raul {
26 
30 class LogBuffer : public std::streambuf
31 {
32 public:
33  enum Colour {
34  DEFAULT = 0,
35  RED = 31,
36  GREEN,
37  YELLOW,
38  BLUE,
39  MAGENTA,
40  CYAN,
41  WHITE
42  };
43 
44  LogBuffer(const char* prefix="", Colour colour=DEFAULT)
45  : _prefix(prefix)
46  , _colour(colour)
47  , _out(std::cout)
48  {}
49 
51  std::string colour(Colour c);
52 
54  std::string plain();
55 
56 protected:
57  int_type overflow(int_type c) {
58  if (c == '\n')
59  emit();
60  else if (c != traits_type::eof())
61  _line += c;
62 
63  return c;
64  }
65 
66  int sync() {
67  if (!_line.empty())
68  emit();
69  return 0;
70  }
71 
72 private:
73  void emit();
74 
75  const char* _prefix;
76  Colour _colour;
77  std::string _line;
78  std::ostream& _out;
79 };
80 
81 
82 class NullBuffer : public std::streambuf
83 {
84 protected:
85  int_type overflow(int_type c) { return c; }
86  int sync() { return 0; }
87 };
88 
89 
90 extern std::ostream info;
91 extern std::ostream warn;
92 extern std::ostream error;
93 extern std::ostream debug;
94 
95 
96 } // namespace Raul
97 
98 #endif // RAUL_LOG_HPP