Logger.cpp
Go to the documentation of this file.
00001 /****************************************************************
00002  *
00003  * Copyright (c) 2011
00004  * All rights reserved.
00005  *
00006  * Hochschule Bonn-Rhein-Sieg
00007  * University of Applied Sciences
00008  * Computer Science Department
00009  *
00010  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00011  *
00012  * Author:
00013  * Jan Paulus, Nico Hochgeschwender, Michael Reckhaus, Azamat Shakhimardanov
00014  * Supervised by:
00015  * Gerhard K. Kraetzschmar
00016  *
00017  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00018  *
00019  * This sofware is published under a dual-license: GNU Lesser General Public
00020  * License LGPL 2.1 and BSD license. The dual-license implies that users of this
00021  * code may choose which terms they prefer.
00022  *
00023  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00024  *
00025  * Redistribution and use in source and binary forms, with or without
00026  * modification, are permitted provided that the following conditions are met:
00027  *
00028  *     * Redistributions of source code must retain the above copyright
00029  *       notice, this list of conditions and the following disclaimer.
00030  *     * Redistributions in binary form must reproduce the above copyright
00031  *       notice, this list of conditions and the following disclaimer in the
00032  *       documentation and/or other materials provided with the distribution.
00033  *     * Neither the name of the Hochschule Bonn-Rhein-Sieg nor the names of its
00034  *       contributors may be used to endorse or promote products derived from
00035  *       this software without specific prior written permission.
00036  *
00037  * This program is free software: you can redistribute it and/or modify
00038  * it under the terms of the GNU Lesser General Public License LGPL as
00039  * published by the Free Software Foundation, either version 2.1 of the
00040  * License, or (at your option) any later version or the BSD license.
00041  *
00042  * This program is distributed in the hope that it will be useful,
00043  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00044  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00045  * GNU Lesser General Public License LGPL and the BSD license for more details.
00046  *
00047  * You should have received a copy of the GNU Lesser General Public
00048  * License LGPL and BSD license along with this program.
00049  *
00050  ****************************************************************/
00051 
00052 #include <youbot_driver/generic/Logger.hpp>
00053 
00054 namespace youbot
00055 {
00056 
00057 bool Logger::toConsole = true;
00058 bool Logger::toFile = false;
00059 bool Logger::toROS = false;
00060 severity_level Logger::logginLevel = info;
00061 
00062 Logger::Logger(const std::string &funcName, const int &lineNo, const std::string &fileName, severity_level level)
00063 {
00064 #ifndef USE_ROS_LOGGING
00065   if (toROS)
00066   {
00067     toConsole = true;
00068   }
00069 #endif
00070 
00071   this->level = level;
00072   if (toConsole || toFile)
00073   {
00074     if (level >= logginLevel)
00075     {
00076       print = true;
00077 
00078       switch (level)
00079       {
00080         case trace:
00081           out << "Trace" << ": ";
00082           break;
00083         case debug:
00084           out << "Debug" << ": ";
00085           break;
00086         case info:
00087           out << "Info" << ": ";
00088           break;
00089         case warning:
00090           out << "Warning" << ": ";
00091           break;
00092         case error:
00093           out << "Error" << ": ";
00094           break;
00095         case fatal:
00096           out << "Fatal" << ": ";
00097           break;
00098         default:
00099           break;
00100       }
00101       //  out << "function " << funcName << ": ";
00102       //  out << "line " << lineNo << ": ";
00103       //  out << "fileName " << fileName << ": ";
00104       //  out << "time " << boost::posix_time::microsec_clock::local_time() << ": ";
00105     }
00106     else
00107     {
00108       print = false;
00109     }
00110   }
00111   else
00112   {
00113     print = false;
00114   }
00115 
00116 }
00117 
00118 Logger::~Logger()
00119 {
00120   //end of message
00121   if (toConsole && print)
00122   {
00123     printf("%s\n", out.str().c_str());
00124     //  std::cout << out.str() << std::endl;
00125   }
00126 
00127   if (toFile && print)
00128   {
00129     std::fstream filestr;
00130     filestr.open("log.txt", std::fstream::out | std::fstream::app);
00131     filestr << out.str() << std::endl;
00132     filestr.close();
00133   }
00134 #ifdef USE_ROS_LOGGING
00135   if (toROS)
00136   {
00137     switch (level)
00138     {
00139       case trace:
00140       ROS_DEBUG("%s",out.str().c_str());
00141       break;
00142       case debug:
00143       ROS_DEBUG("%s",out.str().c_str());
00144       break;
00145       case info:
00146       ROS_INFO("%s",out.str().c_str());
00147       break;
00148       case warning:
00149       ROS_WARN("%s",out.str().c_str());
00150       break;
00151       case error:
00152       ROS_ERROR("%s",out.str().c_str());
00153       break;
00154       case fatal:
00155       ROS_FATAL("%s",out.str().c_str());
00156       break;
00157       default:
00158       break;
00159     }
00160   }
00161 #endif
00162 }
00163 
00164 } // namespace youbot


youbot_driver
Author(s): Jan Paulus
autogenerated on Mon Oct 6 2014 09:08:01