TerminalOutput.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright 2007, 2008, 2009, 2010, 2011 Instituto de Sistemas e Robotica, Instituto Superior Tecnico
00003 
00004 This file is part of MeRMaID.
00005 
00006 MeRMaID is free software: you can redistribute it and/or modify
00007 it under the terms of the GNU Lesser 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 MeRMaID 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 Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License
00017 along with MeRMaID.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 
00021 
00022 /**
00023  * @Filename TerminalOutput.cpp
00024  * @Description TerminalOutput implementation
00025  * @Status Finished
00026  * @Version $Id: TerminalOutput.cpp 1 2011-03-04 18:13:18Z jreis $
00027  * @Maintainer Joao Reis (joaocgreis@gmail.com)
00028  */
00029 
00030 #include "config.h"
00031 
00032 #include "Logger.hpp"
00033 
00034 #include <iostream>
00035 
00036 using namespace mermaid::support::logging;
00037 
00038 using std::cerr;
00039 
00040 namespace
00041 {
00042   int to_init_flag = 0;
00043 }
00044 
00045 shared_ptr<ThreadSafeOutput> TerminalOutput::instance_;
00046 
00047 shared_ptr<ThreadSafeOutput> TerminalOutput::build()
00048 {
00049   if (to_init_flag == 0) {
00050     instance_ = ThreadSafeOutput::build (shared_ptr<TerminalOutput> (new TerminalOutput()));
00051     to_init_flag = 1;
00052   }
00053   return instance_;
00054 }
00055 
00056 void TerminalOutput::outputMessage (const LogMessage& logMessage)
00057 {
00058   // time
00059   time_t timep = logMessage.getTime();
00060   struct tm * t = gmtime (&timep);
00061   char prevFill = cerr.fill();
00062   cerr.fill ('0');
00063   cerr.width (2);
00064   cerr << t->tm_hour << ":";
00065   cerr.width (2);
00066   cerr << t->tm_min << ":";
00067   cerr.width (2);
00068   cerr << t->tm_sec << " ";
00069   cerr.fill (prevFill);
00070   
00071   // prefix
00072   if (logMessage.getPrefix().size() > 0) {
00073     cerr << logMessage.getPrefix() << ": ";
00074   }
00075   
00076   // level
00077   switch (logMessage.getLogLevel()) {
00078     case LOG_ERROR:
00079       cerr << "ERROR: ";
00080       break;
00081     case LOG_WARN:
00082       cerr << "WARN: ";
00083       break;
00084     case LOG_INFO:
00085       cerr << "INFO: ";
00086       break;
00087     case LOG_BEGIN:
00088       for (int i = 0; i < logMessage.getIndentation(); i++) {
00089         cerr << "| ";
00090       }
00091       cerr << "/---------- ";
00092       break;
00093     case LOG_SEPARATOR:
00094       if (logMessage.getIndentation() > 0) {
00095         for (int i = 0; i < (logMessage.getIndentation() - 1); i++) {
00096           cerr << "| ";
00097         }
00098         cerr << "+-";
00099       }
00100       cerr << "--------- ";
00101       break;
00102     case LOG_STEP:
00103       for (int i = 0; i < logMessage.getIndentation(); i++) {
00104         cerr << "| ";
00105       }
00106       break;
00107     case LOG_END:
00108       for (int i = 0; i < logMessage.getIndentation(); i++) {
00109         cerr << "| ";
00110       }
00111       cerr << "\\---------- ";
00112       break;
00113   }
00114   
00115   // message
00116   cerr << logMessage.getMessage();
00117   
00118   // epilogue
00119   cerr << std::endl;
00120   cerr.flush();
00121 }
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3