![]() |
User Manual, Developers Guide and API Documentation |
![]() |
00001 /******************************************************************************* 00002 * This file is part of openWNS (open Wireless Network Simulator) 00003 * _____________________________________________________________________________ 00004 * 00005 * Copyright (C) 2004-2007 00006 * Chair of Communication Networks (ComNets) 00007 * Kopernikusstr. 16, D-52074 Aachen, Germany 00008 * phone: ++49-241-80-27910, 00009 * fax: ++49-241-80-22242 00010 * email: info@openwns.org 00011 * www: http://www.openwns.org 00012 * _____________________________________________________________________________ 00013 * 00014 * openWNS is free software; you can redistribute it and/or modify it under the 00015 * terms of the GNU Lesser General Public License version 2 as published by the 00016 * Free Software Foundation; 00017 * 00018 * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY 00019 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 00020 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00021 * details. 00022 * 00023 * You should have received a copy of the GNU Lesser General Public License 00024 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00025 * 00026 ******************************************************************************/ 00027 00028 #include <WNS/logger/ConsoleFormat.hpp> 00029 #include <iomanip> 00030 #include <cstdlib> 00031 00032 using namespace wns::logger; 00033 00034 STATIC_FACTORY_REGISTER_WITH_CREATOR( 00035 ConsoleFormat, 00036 FormatStrategy, 00037 "Console", 00038 wns::PyConfigViewCreator); 00039 00040 ConsoleFormat::ConsoleFormat(const pyconfig::View& pyco) : 00041 blackNWhite(false), 00042 module2color(), 00043 debugTimeWidth(pyco.get<int>("timeWidth")), 00044 debugTimePrecision(pyco.get<int>("timePrecision")), 00045 maxLocationLength(pyco.get<int>("maxLocationLength")), 00046 currentLocationLength(0) 00047 { 00048 for(int i=0; i<pyco.len("colorMap"); ++i) { 00049 pyconfig::View mcView = pyco.getView("colorMap", i); 00050 std::string name = mcView.get<std::string>("name"); 00051 std::string modifier = mcView.get<std::string>("modifier"); 00052 module2color[name] = modifier; 00053 } 00054 00055 if (pyco.get<int>("colors") == 0) { // Off 00056 blackNWhite = true; 00057 } else if (pyco.get<int>("colors") == 1) { // Auto 00058 if(!isatty(1) || !isatty(2)) { 00059 blackNWhite = true; 00060 } 00061 00062 char* p = getenv("TERM"); 00063 if(p && p == std::string("dumb")) { 00064 blackNWhite = true; 00065 } 00066 } 00067 } 00068 00069 std::string ConsoleFormat::highlightModuleName(const std::string& module) 00070 { 00071 if (blackNWhite) { 00072 return ""; 00073 } 00074 00075 std::map<std::string, std::string>::iterator itr = module2color.find(module); 00076 if(itr == module2color.end()) { 00077 return std::string("\033[00;00m"); 00078 } 00079 return itr->second; 00080 } 00081 00082 std::string ConsoleFormat::formatMessage(const RawMessage& m) 00083 { 00084 long int n; 00085 00086 std::stringstream formattedMessage; 00087 formattedMessage << "(" 00088 << std::resetiosflags(std::ios::fixed) 00089 << std::resetiosflags(std::ios::scientific) 00090 << std::resetiosflags(std::ios::right) 00091 << std::setiosflags(std::ios::right) 00092 << std::setiosflags(std::ios::fixed) 00093 << std::setiosflags(std::ios::dec) 00094 << std::setprecision(debugTimePrecision) 00095 << std::setw(debugTimeWidth) 00096 << m.time 00097 << ") " 00098 << highlightModuleName(m.module) 00099 << "[" << std::setw(5) << m.module.substr(0, 5) << "] " 00100 << m.location << " "; 00101 00102 if ((m.location.length() > currentLocationLength) && (m.location.length() <= maxLocationLength)) { 00103 currentLocationLength = m.location.length(); 00104 } 00105 formattedMessage << std::setw(currentLocationLength - m.location.length()) << ""; 00106 formattedMessage << " "; 00107 00108 n = m.message.length(); 00109 formattedMessage << m.message << highlightModuleName("NULL") << "\n"; 00110 00111 // insert spaces after each "\n" to have a nice output 00112 std::string ret = formattedMessage.str(); 00113 unsigned long int i = 0; 00114 while ((i = ret.find("\n", i) + 1) < ret.length()) 00115 { 00116 ret.insert(i, std::string(15 + debugTimeWidth + currentLocationLength, ' ')); 00117 } 00118 00119 return ret; 00120 } 00121 00122
1.5.5