Logger.h

Go to the documentation of this file.
00001 /************************************************************************
00002 * Verve                                                                 *
00003 * Copyright (C) 2004-2006                                               *
00004 * Tyler Streeter  tylerstreeter@gmail.com                               *
00005 * All rights reserved.                                                  *
00006 * Web: http://verve-agents.sourceforge.net                              *
00007 *                                                                       *
00008 * This library is free software; you can redistribute it and/or         *
00009 * modify it under the terms of EITHER:                                  *
00010 *   (1) The GNU Lesser General Public License as published by the Free  *
00011 *       Software Foundation; either version 2.1 of the License, or (at  *
00012 *       your option) any later version. The text of the GNU Lesser      *
00013 *       General Public License is included with this library in the     *
00014 *       file license-LGPL.txt.                                          *
00015 *   (2) The BSD-style license that is included with this library in     *
00016 *       the file license-BSD.txt.                                       *
00017 *                                                                       *
00018 * This library is distributed in the hope that it will be useful,       *
00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
00021 * license-LGPL.txt and license-BSD.txt for more details.                *
00022 ************************************************************************/
00023 
00024 #ifndef VERVE_LOGGER_H
00025 #define VERVE_LOGGER_H
00026 
00027 #include <iomanip>
00028 #include <stdlib.h>
00029 #include <string>
00030 #include <ostream>
00031 #include <iostream>
00032 #include <map>
00033 #include <iosfwd>
00034 #include <stdexcept>
00035 
00038 #define VERVE_LOGGER Logger::instance().stream
00039 
00040 namespace verve
00041 {
00050         class Logger
00051         {
00052         public:
00054                 static Logger& VERVE_CALL instance()
00055                 {
00056                         static Logger mSelf;
00057                         return mSelf;
00058                 }
00059 
00061                 void VERVE_CALL setStream(const std::string& name, 
00062                         std::ostream *stream, const std::string& prefix="", 
00063                         char mark='\0')
00064                 {
00065                         Logger::Stream s;
00066                         s.mark = mark;
00067                         s.silent = false;
00068                         s.stream = stream;
00069                         s.prefix = prefix;
00070                         mStreams[name] = s;
00071                 }
00072 
00074                 std::ostream& VERVE_CALL stream(const std::string& name)
00075                 {
00076                         Logger::Stream s = mStreams[name];
00077 
00078                         if(s.mark != '\0')
00079                         {
00080                                 std::cout << s.mark;
00081                                 std::cout.flush();
00082                         }
00083 
00084                         if (!s.prefix.empty())
00085                         {
00086                                 *s.stream << s.prefix;
00087                         }
00088 
00089                         return *(s.stream);
00090                 }
00091 
00092         private:
00093                 Logger()
00094                 {
00095                         setStream("debug", &std::cout, "[Verve debug] ");
00096                         setStream("warning", &std::cout, "[Verve warning] ");
00097                         setStream("error", &std::cout, "[Verve error] ");
00098                 }
00099 
00100                 ~Logger()
00101                 {
00102                 }
00103 
00105                 struct Stream
00106                 {
00107                         std::ostream *nullStream;
00108                         std::ostream *stream;
00109                         bool silent;
00110                         char mark;
00111                         std::string prefix;
00112                 };
00113 
00115                 std::map<std::string, Stream> mStreams;
00116         };
00117 }
00118 
00119 #endif

Generated on Tue Jan 24 21:46:37 2006 for Verve by  doxygen 1.4.6-NO