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 "generic/Logger.hpp" 00053 00054 namespace youbot { 00055 00056 bool Logger::toConsole = true; 00057 bool Logger::toFile = false; 00058 bool Logger::toROS = false; 00059 severity_level Logger::logginLevel = info; 00060 00061 Logger::Logger(const std::string &funcName, const int &lineNo, const std::string &fileName, severity_level level) { 00062 #ifndef USE_ROS_LOGGING 00063 if (toROS) { 00064 toConsole = true; 00065 } 00066 #endif 00067 00068 this->level = level; 00069 if (toConsole || toFile) { 00070 if (level >= logginLevel) { 00071 print = true; 00072 00073 switch (level) { 00074 case trace: 00075 out << "Trace" << ": "; 00076 break; 00077 case debug: 00078 out << "Debug" << ": "; 00079 break; 00080 case info: 00081 out << "Info" << ": "; 00082 break; 00083 case warning: 00084 out << "Warning" << ": "; 00085 break; 00086 case error: 00087 out << "Error" << ": "; 00088 break; 00089 case fatal: 00090 out << "Fatal" << ": "; 00091 break; 00092 default: 00093 break; 00094 } 00095 // out << "function " << funcName << ": "; 00096 // out << "line " << lineNo << ": "; 00097 // out << "fileName " << fileName << ": "; 00098 // out << "time " << boost::posix_time::microsec_clock::local_time() << ": "; 00099 } else { 00100 print = false; 00101 } 00102 } else { 00103 print = false; 00104 } 00105 00106 } 00107 00108 Logger::~Logger() { 00109 //end of message 00110 if (toConsole && print) { 00111 printf("%s\n", out.str().c_str()); 00112 // std::cout << out.str() << std::endl; 00113 } 00114 00115 if (toFile && print) { 00116 std::fstream filestr; 00117 filestr.open("log.txt", std::fstream::out | std::fstream::app); 00118 filestr << out.str() << std::endl; 00119 filestr.close(); 00120 } 00121 #ifdef USE_ROS_LOGGING 00122 if (toROS) { 00123 switch (level) { 00124 case trace: 00125 ROS_DEBUG(out.str().c_str()); 00126 break; 00127 case debug: 00128 ROS_DEBUG(out.str().c_str()); 00129 break; 00130 case info: 00131 ROS_INFO(out.str().c_str()); 00132 break; 00133 case warning: 00134 ROS_WARN(out.str().c_str()); 00135 break; 00136 case error: 00137 ROS_ERROR(out.str().c_str()); 00138 break; 00139 case fatal: 00140 ROS_FATAL(out.str().c_str()); 00141 break; 00142 default: 00143 break; 00144 } 00145 } 00146 #endif 00147 } 00148 00149 } // namespace youbot