• Main Page
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

log.hpp

00001 /* This file is part of Raul.
00002  * Copyright (C) 2009 David Robillard <http://drobilla.net>
00003  *
00004  * Raul is free software; you can redistribute it and/or modify it under the
00005  * terms of the GNU General Public License as published by the Free Software
00006  * Foundation; either version 2 of the License, or (at your option) any later
00007  * version.
00008  *
00009  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
00010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
00012  *
00013  * You should have received a copy of the GNU General Public License along
00014  * with this program; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00016  */
00017 
00018 #ifndef RAUL_LOG_HPP
00019 #define RAUL_LOG_HPP
00020 
00021 #include <iostream>
00022 #include <sstream>
00023 
00024 namespace Raul {
00025 
00026 class LogBuffer : public std::streambuf
00027 {
00028 public:
00029     enum Colour {
00030         DEFAULT = 0,
00031         RED = 31,
00032         GREEN,
00033         YELLOW,
00034         BLUE,
00035         MAGENTA,
00036         CYAN,
00037         WHITE
00038     };
00039 
00040     LogBuffer(const char* prefix="", Colour colour=DEFAULT)
00041         : _prefix(prefix)
00042         , _colour(colour)
00043         , _out(std::cout)
00044     {}
00045 
00047     std::string colour(Colour c);
00048 
00050     std::string plain();
00051 
00052 protected:
00053     int_type overflow(int_type c) {
00054         if (c == '\n')
00055             emit();
00056         else if (c != traits_type::eof())
00057             _line += c;
00058 
00059         return c;
00060     }
00061 
00062     int sync() {
00063         if (!_line.empty())
00064             emit();
00065         return 0;
00066     }
00067 
00068 private:
00069     void emit();
00070 
00071     const char*   _prefix;
00072     Colour        _colour;
00073     std::string   _line;
00074     std::ostream& _out;
00075 };
00076 
00077 
00078 class NullBuffer : public std::streambuf
00079 {
00080 protected:
00081     int_type overflow(int_type c) { return c; }
00082     int      sync()               { return 0; }
00083 };
00084 
00085 
00086 extern std::ostream info;
00087 extern std::ostream warn;
00088 extern std::ostream error;
00089 extern std::ostream debug;
00090 
00091 
00092 } // namespace Raul
00093 
00094 #endif // RAUL_LOG_HPP

Generated on Wed Sep 29 2010 for RAUL by  doxygen 1.7.1