FileOutput.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 FileOutput.cpp
00024  * @Description FileOutput implementation
00025  * @Status Finished
00026  * @Version $Id: FileOutput.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 using namespace mermaid::support::logging;
00035 
00036 FileOutput::FileOutput (std::string fileName) :
00037     fileName_ (fileName),
00038     fileStream_ (fileName.c_str(), std::ios::app)
00039 {
00040   fileStream_.fill ('0');
00041 }
00042 
00043 shared_ptr<FileOutput> FileOutput::build (std::string fileName)
00044 {
00045   return shared_ptr<FileOutput> (new FileOutput (fileName));
00046 }
00047 
00048 void FileOutput::outputMessage (const LogMessage& logMessage)
00049 {
00050   // time
00051   time_t timep = logMessage.getTime();
00052   struct tm * t = gmtime (&timep);
00053   fileStream_.width (2);
00054   fileStream_ << t->tm_mday << "/";
00055   fileStream_.width (2);
00056   fileStream_ << t->tm_mon << "/";
00057   fileStream_.width (4);
00058   fileStream_ << 1900 + t->tm_year << "-";
00059   fileStream_.width (2);
00060   fileStream_ << t->tm_hour << ":";
00061   fileStream_.width (2);
00062   fileStream_ << t->tm_min << ":";
00063   fileStream_.width (2);
00064   fileStream_ << t->tm_sec << " ";
00065   
00066   // prefix
00067   if (logMessage.getPrefix().size() > 0) {
00068     fileStream_ << logMessage.getPrefix() << ": ";
00069   }
00070   
00071   // level
00072   switch (logMessage.getLogLevel()) {
00073     case LOG_ERROR:
00074       fileStream_ << "ERROR: ";
00075       break;
00076     case LOG_WARN:
00077       fileStream_ << "WARN: ";
00078       break;
00079     case LOG_INFO:
00080       fileStream_ << "INFO: ";
00081       break;
00082     case LOG_BEGIN:
00083       for (int i = 0; i < logMessage.getIndentation(); i++) {
00084         fileStream_ << "| ";
00085       }
00086       fileStream_ << "/---------- ";
00087       break;
00088     case LOG_SEPARATOR:
00089       if (logMessage.getIndentation() > 0) {
00090         for (int i = 0; i < (logMessage.getIndentation() - 1); i++) {
00091           fileStream_ << "| ";
00092         }
00093         fileStream_ << "+-";
00094       }
00095       fileStream_ << "--------- ";
00096       break;
00097     case LOG_STEP:
00098       for (int i = 0; i < logMessage.getIndentation(); i++) {
00099         fileStream_ << "| ";
00100       }
00101       break;
00102     case LOG_END:
00103       for (int i = 0; i < logMessage.getIndentation(); i++) {
00104         fileStream_ << "| ";
00105       }
00106       fileStream_ << "\\---------- ";
00107       break;
00108   }
00109   
00110   // message
00111   fileStream_ << logMessage.getMessage();
00112   
00113   // epilogue
00114   fileStream_ << std::endl;
00115   fileStream_.flush();
00116 }
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3