00001
00002 #include "log_stream_manager.hpp"
00003 #include "simulator.hpp"
00004 #include "packet.hpp"
00005 #include "node.hpp"
00006
00007 LogStreamManagerPtr LogStreamManager::m_instance;
00008
00009 LogStreamManager::LogStreamManager()
00010 {
00011
00012
00013
00014
00015
00016 m_pktSendStream.reset(new ostream(cout.rdbuf()));
00017 m_pktRecvStream.reset(new ostream(cout.rdbuf()));
00018 m_energyStream.reset(new ostream(cout.rdbuf()));
00019 m_newSenseEventStream.reset(new ostream(cout.rdbuf()));
00020 m_sensedEventStream.reset(new ostream(cout.rdbuf()));
00021 m_statsStream.reset(new ostream(cout.rdbuf()));
00022 m_userDefinedStream.reset(new ostream(cout.rdbuf()));
00023 m_debugStream.reset(new ostream(cout.rdbuf()));
00024
00025 }
00026
00027 LogStreamManager::~LogStreamManager()
00028 {
00029
00030 }
00031
00032 void LogStreamManager::logPktSendItem(const NodeId& nodeId,
00033 const CommunicationLayer::Types& layer, const Packet& sendPkt)
00034 {
00035 assert(m_pktSendStream != 0);
00036 *m_pktSendStream <<
00037 eventString(LogStreamManager::LogEvents_PktSend) <<
00038 " " << currentTimeString() << " " <<
00039 nodeIdString(nodeId) << " " <<
00040 layerString(layer) << " " << sendPkt << endl;
00041 }
00042
00043 void LogStreamManager::logPktRecvItem(const NodeId& nodeId,
00044 const CommunicationLayer::Types& layer, const Packet& recvPkt)
00045 {
00046 assert(m_pktRecvStream != 0);
00047 *m_pktRecvStream <<
00048 eventString(LogStreamManager::LogEvents_PktRecv) <<
00049 " " << currentTimeString() << " " <<
00050 nodeIdString(nodeId) << " " <<
00051 layerString(layer) << " " << recvPkt << endl;
00052 }
00053
00054 void LogStreamManager::logStatsItem(const string& statsKeyString,
00055 const string& statsValueString)
00056 {
00057 assert(m_statsStream != 0);
00058 *m_statsStream <<
00059 eventString(LogStreamManager::LogEvents_Stats) <<
00060 " " << currentTimeString() << " " <<
00061 "-globalStat -" << statsKeyString <<
00062 " " << statsValueString << endl;
00063 }
00064
00065 void LogStreamManager::logStatsItem(const NodeId& nodeId,
00066 const string& statsKeyString, const string& statsValueString)
00067 {
00068 assert(m_statsStream != 0);
00069 *m_statsStream <<
00070 eventString(LogStreamManager::LogEvents_Stats) <<
00071 " " << currentTimeString() << " " <<
00072 nodeIdString(nodeId) << " -" << statsKeyString <<
00073 " " << statsValueString << endl;
00074 }
00075
00076 void LogStreamManager::logUserDefinedItem(const string& userString)
00077 {
00078 assert(m_userDefinedStream != 0);
00079 *m_userDefinedStream <<
00080 eventString(LogStreamManager::LogEvents_UserDefined) <<
00081 " " << currentTimeString() << " " <<
00082 userString << endl;
00083 }
00084
00085 void LogStreamManager::logDebugItem(const string& debugString)
00086 {
00087 assert(m_debugStream != 0);
00088 *m_debugStream <<
00089 eventString(LogStreamManager::LogEvents_Debug) <<
00090 " " << currentTimeString() << " " <<
00091 debugString << endl;
00092 }
00093
00094 string LogStreamManager::eventString(
00095 const LogStreamManager::LogEvents& event) const
00096 {
00097 ostringstream eventStream;
00098 eventStream << "-event " << event;
00099 return eventStream.str();
00100 }
00101
00102 string LogStreamManager::currentTimeString() const
00103 {
00104 ostringstream curTimeStream;
00105 curTimeStream << "-time " << setprecision(12) << fixed <<
00106 Simulator::instance()->currentTime();
00107 return curTimeStream.str();
00108 }
00109
00110 string LogStreamManager::nodeIdString(const NodeId& nodeId) const
00111 {
00112 ostringstream nodeIdStream;
00113 nodeIdStream << "-nodeId " << nodeId;
00114 return nodeIdStream.str();
00115 }
00116
00117 string LogStreamManager::layerString(
00118 const CommunicationLayer::Types& layer) const
00119 {
00120 ostringstream layerStream;
00121 layerStream << "-layer " << layer;
00122 return layerStream.str();
00123 }
00124