Note: Goby version 1 (shown here) is now considered obsolete. Please use version 2 for new projects, and consider upgrading old projects.

Goby Underwater Autonomy Project  Series: 1.1, revision: 163, released on 2013-02-06 14:23:27 -0500
util/liblogger/flex_ostreambuf.h
00001 // copyright 2009 t. schneider tes@mit.edu
00002 //
00003 // this file is part of flex-ostream, a terminal display library
00004 // that provides an ostream with both terminal display and file logging
00005 //
00006 // This program is free software: you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation, either version 3 of the License, or
00009 // (at your option) any later version.
00010 //
00011 // This software is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU General Public License
00017 // along with this software.  If not, see <http://www.gnu.org/licenses/>.
00018 
00019 #ifndef FlexOStreamBuf20091110H
00020 #define FlexOStreamBuf20091110H
00021 
00022 #include <iostream>
00023 #include <sstream>
00024 #include <vector>
00025 #include <deque>
00026 #include <cstdio>
00027 
00028 #include <boost/thread.hpp>
00029 #include <boost/shared_ptr.hpp>
00030 
00031 #include "term_color.h"
00032 
00033 class Group;
00034 
00035 namespace goby
00036 {
00037     namespace util
00038     {
00039 
00040         class FlexNCurses;
00041 
00043         struct Logger
00044         {
00045             static boost::mutex mutex;
00046             enum Verbosity { quiet, warn, verbose, debug, gui };
00047         };        
00048         
00049         
00051         class FlexOStreamBuf : public std::streambuf
00052         {
00053           public:
00054             FlexOStreamBuf();
00055             ~FlexOStreamBuf();
00056             
00058             int sync();
00059 
00061             int overflow(int c = EOF);
00062             
00064             void name(const std::string & s)
00065             { name_ = s; }
00066 
00068             void add_stream(Logger::Verbosity verbosity, std::ostream* os);            
00069 
00071             bool is_quiet()
00072             { return is_quiet_; }    
00073 
00075             bool is_gui()
00076             { return is_gui_; }
00077             
00079             void group_name(const std::string & s)
00080             { group_name_ = s; }
00081 
00083             void set_die_flag(bool b)
00084             { die_flag_ = b; }
00085 
00087             void set_debug_flag(bool b)
00088             { debug_flag_ = b; }
00089 
00091             void set_warn_flag(bool b)
00092             { warn_flag_ = b; }
00093 
00095             void add_group(const std::string& name, Group g);
00096 
00098             std::string color2esc_code(Colors::Color color)
00099             { return TermColor::esc_code_from_col(color); }
00100 
00102             void refresh();
00103           private:
00104             void display(std::string& s);    
00105             void strip_escapes(std::string& s);
00106             
00107           private:
00108             std::deque<std::string> buffer_;
00109             
00110             class StreamConfig
00111             {
00112               public:
00113                 StreamConfig(std::ostream* os, Logger::Verbosity verbosity)
00114                     : os_(os),
00115                     verbosity_(verbosity)
00116                     { }
00117                 
00118                 void set_verbosity(Logger::Verbosity verbosity) {  verbosity_ = verbosity; }
00119 
00120                 std::ostream* os() const { return os_; }
00121                 Logger::Verbosity verbosity() const { return verbosity_; }
00122                 
00123               private:
00124                 std::ostream* os_;
00125                 Logger::Verbosity verbosity_;
00126             };
00127             
00128             std::string name_;
00129             std::string group_name_;
00130     
00131             std::map<std::string, Group> groups_;
00132 
00133             bool die_flag_;
00134             bool warn_flag_;
00135             bool debug_flag_;
00136 
00137 #if HAS_NCURSES
00138             FlexNCurses* curses_;
00139 #endif            
00140             boost::shared_ptr<boost::thread> input_thread_;
00141 
00142             boost::posix_time::ptime start_time_;
00143 
00144             std::vector<StreamConfig> streams_;
00145 
00146             bool is_quiet_;
00147             bool is_gui_;
00148         };
00149     }
00150 }
00151 #endif
00152 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends