diff --git a/premake5.lua b/premake5.lua index fbb6264..d5b94b8 100644 --- a/premake5.lua +++ b/premake5.lua @@ -65,6 +65,8 @@ project "omnetpp-federate-LIBRARY" , "src/mgmt/*.cc" , PROTO_CC_PATH .. "/ClientServerChannel.h" , PROTO_CC_PATH .. "/ClientServerChannel.cc" + , "src/util/Log.h" + , "src/util/Log.cc" , PROTO_CC_PATH .. "/ClientServerChannelMessages.pb.h" , PROTO_CC_PATH .. "/ClientServerChannelMessages.pb.cc" } diff --git a/src/node/MosaicProxyApp.cc b/src/node/MosaicProxyApp.cc index 6926ebe..a5406de 100644 --- a/src/node/MosaicProxyApp.cc +++ b/src/node/MosaicProxyApp.cc @@ -149,8 +149,6 @@ void MosaicProxyApp::sendDelayedToUDP(omnetpp::cPacket *msg, int srcPort, const * triggered from MosaicScenarioManager and hence from Mosaic. */ void MosaicProxyApp::sendPacket(omnetpp::cMessage *msg) { - EV << "MosaicUDP send packet" << std::endl; - auto *packet = inet::check_and_cast(msg); auto destAddr = packet->getDestAddr(); double delay = dblrand() * maxProcDelay; @@ -167,7 +165,7 @@ void MosaicProxyApp::receivePacket(omnetpp::cMessage *msg) { auto udp_packet = inet::check_and_cast(msg); auto cPacket = udp_packet->popAtBack().get()->getPacket(); auto packet = omnetpp::check_and_cast(cPacket); - EV << "MosaicUDP: srcNodeId " << packet->getNodeId() << ", msgId " << packet->getMsgId() << std::endl; + EV << "srcNodeId " << packet->getNodeId() << ", msgId " << packet->getMsgId() << std::endl; packet->setNodeId(m_externalId); send(packet->dup(), gate("fedOut")); diff --git a/src/omnetpp.ini b/src/omnetpp.ini index baec4b7..0b43357 100644 --- a/src/omnetpp.ini +++ b/src/omnetpp.ini @@ -10,17 +10,22 @@ simtime-resolution=1ns # -------------- # scheduler-class and debugging option for more verbose logging scheduler-class = "omnetpp_federate::MosaicEventScheduler" -mosaiceventscheduler-debug = true +mosaiceventscheduler-debug = false # connection settings, when omnetpp-federate is started manually mosaiceventscheduler-host = "localhost" mosaiceventscheduler-port = 4998 +# ClientServerChannel +# ------------------- +# OMNeT++ log level names are valid, same hierarchy is used +clientserverchannel-log-level = warn + # RecordingModi # ------------- record-eventlog = false cmdenv-express-mode = false cmdenv-event-banners = false -cmdenv-log-prefix = "%t: " +cmdenv-log-prefix = "%t: [%M] " # random numbers # ------------- @@ -29,12 +34,19 @@ Simulation.mobility.rng-0 = 1 Simulation.wlan[*].mac.rng-0 = 2 # general logging output -# -------------- -#*.mgmt.cmdenv-ev-output = false -*.mgmt.debug = false -#*.veh[*].**.cmdenv-ev-output = false -#*.rsu[*].**.cmdenv-ev-output = false - +# ---------------------- + +# These are setting suggestions for verbosity levels +# which are equivalent to MOSAIC's log level INFO and DEBUG +# +# MOSAIC log level equivalent: INFO DEBUG +#--------------------------------------------------- +**.tx.cmdenv-log-level = info # = info +**.rx.cmdenv-log-level = info # = info +**.proxyApp.cmdenv-log-level = info # = info +Simulation.mgmt.cmdenv-log-level = info # = info + +**.cmdenv-log-level = warn # = info # This sets everything to level INFO ########### application settings ############ #Simulation.rsu[*].udpApp.maxProcDelay = 1e-3 diff --git a/src/util/ClientServerChannel.cc b/src/util/ClientServerChannel.cc index f6e36c9..5d6e6be 100644 --- a/src/util/ClientServerChannel.cc +++ b/src/util/ClientServerChannel.cc @@ -31,17 +31,20 @@ #include #include #include +#include #include #include #include #include -#ifdef USE_OMNET_CLOG_H -#include -#define LOG_DEBUG EV_DEBUG -#else -#define LOG_DEBUG std::cout -#endif +#include "Log.h" +#include + +LOG_COMPONENT_DEFINE("ClientServerChannel"); + +Register_GlobalConfigOption(CFGID_CLIENTSERVERCHANNEL_LOG_LEVEL, "clientserverchannel-log-level", + CFG_CUSTOM, "warn", "Log level of ClientServerChannel."); + namespace std { ostream& operator<< ( ostream& out, ClientServerChannelSpace::CMD cmd ) { @@ -121,6 +124,9 @@ std::string uint32_to_ip ( const unsigned int ip ) { ClientServerChannel::ClientServerChannel() { servsock = INVALID_SOCKET; sock = INVALID_SOCKET; + omnetpp::LogLevel level = omnetpp::cLog::resolveLogLevel( + omnetpp::cSimulation::getActiveEnvir()->getConfig()->getAsCustom(CFGID_CLIENTSERVERCHANNEL_LOG_LEVEL)); + set_log_level(level); } /** @@ -209,18 +215,21 @@ ClientServerChannel::~ClientServerChannel() { // Public read-methods //##################################################### -void debug_byte_array ( const char* buffer, const size_t buffer_size ) { -#ifdef USE_OMNET_CLOG_H - using namespace omnetpp; -#endif - LOG_DEBUG << std::dec << "DEBUG: debug_byte_array buffer_size: " << buffer_size << std::endl; +/** + * Formats buffer as string, each byte is formatted as decimal. + */ +std::string debug_byte_array ( const char* buffer, const size_t buffer_size ) { + std::stringstream array; + array << std::dec << "size: " << buffer_size << ", bytes: "; + if(buffer_size > 16) + array << std::endl; // begin multiline print in new line for ( size_t i=0; i < buffer_size; i++ ) { const char c = buffer[i]; - LOG_DEBUG << std::dec << static_cast(c); - LOG_DEBUG << (((i + 1) % 16 == 0) ? '\n' : ' '); + array << std::dec << static_cast(c); + array << (((i + 1) % 16 == 0) ? '\n' : ' '); } - LOG_DEBUG << std::endl; - + array << std::endl; + return array.str(); } /** @@ -231,21 +240,18 @@ void debug_byte_array ( const char* buffer, const size_t buffer_size ) { * TODO: return type should be maybe */ CMD ClientServerChannel::readCommand() { -#ifdef USE_OMNET_CLOG_H - using namespace omnetpp; -#endif - LOG_DEBUG << "readCommand" << std::endl; + LOG_FUNCTION(this); //Read the mandatory prefixed size const std::shared_ptr < uint32_t > message_size = readVarintPrefix ( sock ); - if ( !message_size || *message_size < 0 ) { + if ( !message_size || *message_size < 0 ) { std::cerr << "ERROR: reading of mandatory message size failed!" << std::endl; return CMD_UNDEF; } - LOG_DEBUG << "DEBUG: read command announced message size: " << *message_size << std::endl; + LOG_LOGIC("read command announced message size: " << *message_size); //Allocate a fitting buffer and read message from stream char message_buffer[*message_size]; size_t res = recv ( sock, message_buffer, *message_size, MSG_WAITALL ); - LOG_DEBUG << "DEBUG: readCommand recv result: " << res << std::endl; + LOG_LOGIC("readCommand recv result: " << res); if ( *message_size > 0 && res != *message_size ) { std::cerr << "ERROR: expected " << *message_size << " bytes, but red " << res << " bytes. poll ... " << std::endl; struct pollfd socks[1]; @@ -256,11 +262,11 @@ CMD ClientServerChannel::readCommand() { int retries = 3; do { poll_res = poll(socks, 1, 1000); - LOG_DEBUG << "poll res: " << poll_res << std::endl; + LOG_LOGIC("poll res: " << poll_res); retries--; if ( retries == 0) { break; } sleep(1); - LOG_DEBUG << "poll ..." << std::endl; + LOG_LOGIC("poll ..."); } while ( poll_res < 1 ); res = recv ( sock, message_buffer, *message_size, MSG_WAITALL ); if ( retries != 3 && res < 1 ) { @@ -272,15 +278,8 @@ CMD ClientServerChannel::readCommand() { std::cerr << "ERROR: reading of message body failed! Socket not ready." << std::endl; return CMD_UNDEF; } -// LOG_DEBUG << "readCommand message:" << std::endl; -// for (size_t i=0; i < *message_size; i++) { -// const char c = message_buffer[i]; -// LOG_DEBUG << std::dec << static_cast(c); -// LOG_DEBUG << (((i + 1) % 16 == 0) ? '\n' : ' '); -// } -// LOG_DEBUG << std::endl; if ( *message_size > 0 ) { - debug_byte_array ( message_buffer, *message_size ); + LOG_LOGIC("message buffer as byte array: " << debug_byte_array ( message_buffer, *message_size )); //Create the streams that can parse the received data into the protobuf class google::protobuf::io::ArrayInputStream arrayIn ( message_buffer, *message_size ); google::protobuf::io::CodedInputStream codedIn ( &arrayIn ); @@ -289,7 +288,7 @@ CMD ClientServerChannel::readCommand() { commandMessage.ParseFromCodedStream(&codedIn); //parse message //pick the needed data from the protobuf message class and return it const CMD cmd = protoCMDToCMD(commandMessage.command_type()); - LOG_DEBUG << "DEBUG: read command: " << cmd << std::endl; + LOG_INFO("read command: " << cmd); return cmd; } return CMD_UNDEF; @@ -302,16 +301,13 @@ CMD ClientServerChannel::readCommand() { * @return 0 if successful */ int ClientServerChannel::readInit ( CSC_init_return &return_value ) { -#ifdef USE_OMNET_CLOG_H - using namespace omnetpp; -#endif - LOG_DEBUG << "readInit" << std::endl; + LOG_FUNCTION(this); const std::shared_ptr < uint32_t > message_size = readVarintPrefix(sock); if ( !message_size ) { return -1; } - LOG_DEBUG << "DEBUG: read init announced message size: " << *message_size << std::endl; + LOG_LOGIC("read init announced message size: " << *message_size); char message_buffer[*message_size]; const size_t count = recv ( sock, message_buffer, *message_size, MSG_WAITALL ); - LOG_DEBUG << "DEBUG: read init received message size: " << count << std::endl; + LOG_LOGIC("read init received message size: " << count); google::protobuf::io::ArrayInputStream arrayIn ( message_buffer, *message_size ); google::protobuf::io::CodedInputStream codedIn ( &arrayIn); @@ -322,8 +318,8 @@ int ClientServerChannel::readInit ( CSC_init_return &return_value ) { return_value.start_time = init_message.start_time(); return_value.end_time = init_message.end_time(); - LOG_DEBUG << "DEBUG: read init start time: " << return_value.start_time << std::endl; - LOG_DEBUG << "DEBUG: read init end time: " << return_value.end_time << std::endl; + LOG_INFO("read init start time: " << return_value.start_time); + LOG_INFO("read init end time: " << return_value.end_time); return 0; } @@ -335,20 +331,17 @@ int ClientServerChannel::readInit ( CSC_init_return &return_value ) { * @return 0 if successful */ int ClientServerChannel::readUpdateNode ( CSC_update_node_return &return_value ) { -#ifdef USE_OMNET_CLOG_H - using namespace omnetpp; -#endif - LOG_DEBUG << "readUpdateNode" << std::endl; + LOG_FUNCTION(this); const std::shared_ptr < uint32_t > message_size = readVarintPrefix ( sock ); if ( !message_size ) { return -1; } - LOG_DEBUG << "DEBUG: read update note announced message size: " << *message_size << std::endl; + LOG_LOGIC("read update note announced message size: " << *message_size); if ( *message_size < 0 ) { return 0; } char message_buffer[*message_size]; const size_t count = recv ( sock, message_buffer, *message_size, MSG_WAITALL ); - LOG_DEBUG << "DEBUG: read update node received message size: " << count << std::endl; + LOG_LOGIC("read update node received message size: " << count); if ( *message_size != count ) { std::cerr << "ERROR: expected " << *message_size << " bytes, but red " << count << " bytes!" << std::endl; @@ -370,10 +363,10 @@ int ClientServerChannel::readUpdateNode ( CSC_update_node_return &return_value ) std::cerr << "ERROR: update type unknown: " << update_message.update_type() << std::endl; return_value.type = (UPDATE_NODE_TYPE)0; return 1; //1 signals an error } - LOG_DEBUG << "DEBUG: read update message update type " << return_value.type << std::endl; + LOG_INFO("read update message update type " << return_value.type); return_value.time = update_message.time(); - LOG_DEBUG << "DEBUG: read update message update time " << return_value.time << std::endl; + LOG_INFO("read update message update time " << return_value.time); for ( size_t i = 0; i < update_message.properties_size(); i++ ) { //fill the update messages into our struct UpdateNode_NodeData node_data = update_message.properties(i); @@ -383,10 +376,10 @@ int ClientServerChannel::readUpdateNode ( CSC_update_node_return &return_value ) returned_node_data.x = node_data.x(); returned_node_data.y = node_data.y(); - LOG_DEBUG << "DEBUG: read update message update node index " << i << std::endl; - LOG_DEBUG << "DEBUG: read update message update node id " << returned_node_data.id << std::endl; - LOG_DEBUG << "DEBUG: read update message update node x " << returned_node_data.x << std::endl; - LOG_DEBUG << "DEBUG: read update message update node y " << returned_node_data.y << std::endl; + LOG_INFO("read update message update node index=" << i + << " id=" << returned_node_data.id + << " x=" << returned_node_data.x + << " y=" << returned_node_data.y); return_value.properties.push_back(returned_node_data); } @@ -400,17 +393,14 @@ int ClientServerChannel::readUpdateNode ( CSC_update_node_return &return_value ) * @return the read time as an int64_t */ int64_t ClientServerChannel::readTimeMessage() { -#ifdef USE_OMNET_CLOG_H - using namespace omnetpp; -#endif - LOG_DEBUG << "readTimeMessage" << std::endl; + LOG_FUNCTION(this); const std::shared_ptr < uint32_t > message_size = readVarintPrefix(sock); if ( !message_size ) { return -1; } - LOG_DEBUG << "DEBUG: read time announced message size: " << *message_size << std::endl; + LOG_LOGIC("read time announced message size: " << *message_size); char message_buffer[*message_size]; const size_t count = recv ( sock, message_buffer, *message_size, MSG_WAITALL ); - LOG_DEBUG << "DEBUG: read time received message size: " << count << std::endl; + LOG_LOGIC("read time received message size: " << count); google::protobuf::io::ArrayInputStream arrayIn ( message_buffer, *message_size ); google::protobuf::io::CodedInputStream codedIn ( &arrayIn ); @@ -419,7 +409,7 @@ int64_t ClientServerChannel::readTimeMessage() { time_message.ParseFromCodedStream ( &codedIn ); int64_t time = time_message.time(); - LOG_DEBUG << "DEBUG: read time message: " << time << std::endl; + LOG_INFO("read time message: " << time); return time; } @@ -430,17 +420,14 @@ int64_t ClientServerChannel::readTimeMessage() { * @return 0 if successful */ int ClientServerChannel::readConfigurationMessage(CSC_config_message &return_value) { -#ifdef USE_OMNET_CLOG_H - using namespace omnetpp; -#endif - LOG_DEBUG << "readConfigurationMessage" << std::endl; + LOG_FUNCTION(this); const std::shared_ptr < uint32_t > message_size = readVarintPrefix ( sock ); if ( !message_size ) { return -1; } - LOG_DEBUG << "DEBUG: read config announced message size: " << *message_size << std::endl; + LOG_LOGIC("read config announced message size: " << *message_size); char message_buffer[*message_size]; const size_t count = recv ( sock, message_buffer, *message_size, MSG_WAITALL ); - LOG_DEBUG << "DEBUG: read config received message size: " << count << std::endl; + LOG_LOGIC("read config received message size: " << count); google::protobuf::io::ArrayInputStream arrayIn ( message_buffer, *message_size ); google::protobuf::io::CodedInputStream codedIn ( &arrayIn ); @@ -452,9 +439,9 @@ int ClientServerChannel::readConfigurationMessage(CSC_config_message &return_val return_value.msg_id = conf_message.message_id(); return_value.node_id = conf_message.external_id(); - LOG_DEBUG << "DEBUG: read config message time: " << return_value.time << std::endl; - LOG_DEBUG << "DEBUG: read config message msg id: " << return_value.msg_id << std::endl; - LOG_DEBUG << "DEBUG: read config message node id: " << return_value.node_id << std::endl; + LOG_INFO("read config message time: " << return_value.time); + LOG_INFO("read config message msg id: " << return_value.msg_id); + LOG_INFO("read config message node id: " << return_value.node_id); if ( conf_message.radio_number() == ConfigureRadioMessage_RadioNumber_SINGLE_RADIO ) { return_value.num_radios = SINGLE_RADIO; @@ -463,7 +450,7 @@ int ClientServerChannel::readConfigurationMessage(CSC_config_message &return_val } else if ( conf_message.radio_number() == ConfigureRadioMessage_RadioNumber_NO_RADIO ) { return_value.num_radios = NO_RADIO; } - LOG_DEBUG << "DEBUG: read config message num_radios: " << return_value.num_radios << std::endl; + LOG_INFO("read config message num_radios: " << return_value.num_radios); if ( return_value.num_radios == SINGLE_RADIO || return_value.num_radios == DUAL_RADIO ) { return_value.primary_radio.turnedOn = conf_message.primary_radio_configuration().receiving_messages(); @@ -472,16 +459,16 @@ int ClientServerChannel::readConfigurationMessage(CSC_config_message &return_val return_value.primary_radio.tx_power = conf_message.primary_radio_configuration().transmission_power(); return_value.primary_radio.primary_channel = protoChannelToChannel ( conf_message.primary_radio_configuration().primary_radio_channel() ); - LOG_DEBUG << "DEBUG: read config message primary radio turned on: " - << std::boolalpha << return_value.primary_radio.turnedOn << std::endl; - LOG_DEBUG << "DEBUG: read config message primary radio ip address: " - << uint32_to_ip ( return_value.primary_radio.ip_address ) << std::endl; - LOG_DEBUG << "DEBUG: read config message primary radio subnet: " - << uint32_to_ip ( return_value.primary_radio.subnet ) << std::endl; - LOG_DEBUG << "DEBUG: read config message primary radio tx_power: " - << return_value.primary_radio.tx_power << std::endl; - LOG_DEBUG << "DEBUG: read config message primary radio primary channel: " - << return_value.primary_radio.primary_channel << std::endl; + LOG_INFO("read config message primary radio turned on: " + << std::boolalpha << return_value.primary_radio.turnedOn); + LOG_INFO("read config message primary radio ip address: " + << uint32_to_ip ( return_value.primary_radio.ip_address )); + LOG_INFO("read config message primary radio subnet: " + << uint32_to_ip ( return_value.primary_radio.subnet )); + LOG_INFO("read config message primary radio tx_power: " + << return_value.primary_radio.tx_power); + LOG_INFO("read config message primary radio primary channel: " + << return_value.primary_radio.primary_channel); if ( conf_message.primary_radio_configuration().radio_mode() == ConfigureRadioMessage_RadioConfiguration_RadioMode_SINGLE_CHANNEL ) { @@ -492,12 +479,12 @@ int ClientServerChannel::readConfigurationMessage(CSC_config_message &return_val return_value.primary_radio.secondary_channel = protoChannelToChannel ( conf_message.primary_radio_configuration().secondary_radio_channel() ); } - LOG_DEBUG << "DEBUG: read config message primary radio channel mode: " - << return_value.primary_radio.channelmode << std::endl; + LOG_INFO("read config message primary radio channel mode: " + << return_value.primary_radio.channelmode); if ( conf_message.primary_radio_configuration().radio_mode() == ConfigureRadioMessage_RadioConfiguration_RadioMode_DUAL_CHANNEL) { - LOG_DEBUG << "DEBUG: read config message primary radio secondary channel: " - << return_value.primary_radio.secondary_channel << std::endl; + LOG_INFO("read config message primary radio secondary channel: " + << return_value.primary_radio.secondary_channel); } } @@ -508,16 +495,16 @@ int ClientServerChannel::readConfigurationMessage(CSC_config_message &return_val return_value.secondary_radio.tx_power = conf_message.secondary_radio_configuration().transmission_power(); return_value.secondary_radio.primary_channel = protoChannelToChannel ( conf_message.secondary_radio_configuration().primary_radio_channel() ); - LOG_DEBUG << "DEBUG: read config message secondary radio turned on: " - << std::boolalpha << return_value.secondary_radio.turnedOn << std::endl; - LOG_DEBUG << "DEBUG: read config message secondary radio ip address: " - << uint32_to_ip ( return_value.secondary_radio.ip_address ) << std::endl; - LOG_DEBUG << "DEBUG: read config message secondary radio subnet: " - << uint32_to_ip ( return_value.secondary_radio.subnet ) << std::endl; - LOG_DEBUG << "DEBUG: read config message secondary radio tx_power: " - << return_value.secondary_radio.tx_power << std::endl; - LOG_DEBUG << "DEBUG: read config message secondary radio primary channel: " - << return_value.secondary_radio.primary_channel << std::endl; + LOG_INFO("read config message secondary radio turned on: " + << std::boolalpha << return_value.secondary_radio.turnedOn); + LOG_INFO("read config message secondary radio ip address: " + << uint32_to_ip ( return_value.secondary_radio.ip_address )); + LOG_INFO("read config message secondary radio subnet: " + << uint32_to_ip ( return_value.secondary_radio.subnet )); + LOG_INFO("read config message secondary radio tx_power: " + << return_value.secondary_radio.tx_power); + LOG_INFO("read config message secondary radio primary channel: " + << return_value.secondary_radio.primary_channel); if ( conf_message.secondary_radio_configuration().radio_mode() == ConfigureRadioMessage_RadioConfiguration_RadioMode_SINGLE_CHANNEL) { @@ -528,12 +515,12 @@ int ClientServerChannel::readConfigurationMessage(CSC_config_message &return_val return_value.secondary_radio.secondary_channel = protoChannelToChannel(conf_message.secondary_radio_configuration().secondary_radio_channel()); } - LOG_DEBUG << "DEBUG: read config message secondary radio channel mode: " - << return_value.secondary_radio.channelmode << std::endl; + LOG_INFO("read config message secondary radio channel mode: " + << return_value.secondary_radio.channelmode); if ( conf_message.primary_radio_configuration().radio_mode() == ConfigureRadioMessage_RadioConfiguration_RadioMode_DUAL_CHANNEL) { - LOG_DEBUG << "DEBUG: read config message secondary radio secondary channel: " - << return_value.secondary_radio.secondary_channel << std::endl; + LOG_INFO("read config message secondary radio secondary channel: " + << return_value.secondary_radio.secondary_channel); } } writeCommand(CMD_SUCCESS); @@ -548,17 +535,14 @@ int ClientServerChannel::readConfigurationMessage(CSC_config_message &return_val * @return 0 if successful */ int ClientServerChannel::readSendMessage ( CSC_send_message &return_value ) { -#ifdef USE_OMNET_CLOG_H - using namespace omnetpp; -#endif - LOG_DEBUG << "readSendMessage" << std::endl; + LOG_FUNCTION(this); std::shared_ptr < uint32_t > message_size = readVarintPrefix ( sock ); if ( !message_size ) { return -1; } - LOG_DEBUG << "DEBUG: read send announced message size: " << *message_size << std::endl; + LOG_LOGIC("read send announced message size: " << *message_size); char message_buffer[*message_size]; const size_t count = recv ( sock, message_buffer, *message_size, MSG_WAITALL ); - LOG_DEBUG << "DEBUG: read send received message size: " << count << std::endl; + LOG_LOGIC("read send received message size: " << count); google::protobuf::io::ArrayInputStream arrayIn ( message_buffer, *message_size ); google::protobuf::io::CodedInputStream codedIn ( &arrayIn ); @@ -569,32 +553,32 @@ int ClientServerChannel::readSendMessage ( CSC_send_message &return_value ) { return_value.time = send_message.time(); return_value.node_id = send_message.node_id(); - LOG_DEBUG << "DEBUG: read send message time: " << return_value.time << std::endl; - LOG_DEBUG << "DEBUG: read send message node id: " << return_value.node_id << std::endl; + LOG_INFO("read send message time: " << return_value.time); + LOG_INFO("read send message node id: " << return_value.node_id); return_value.channel_id = protoChannelToChannel(send_message.channel_id()); return_value.message_id = send_message.message_id(); return_value.length = send_message.length(); - LOG_DEBUG << "DEBUG: read send message channel id: " << return_value.channel_id << std::endl; - LOG_DEBUG << "DEBUG: read send message message id: " << return_value.message_id << std::endl; - LOG_DEBUG << "DEBUG: read send message length: " << return_value.length << std::endl; + LOG_INFO("read send message channel id: " << return_value.channel_id); + LOG_INFO("read send message message id: " << return_value.message_id); + LOG_INFO("read send message length: " << return_value.length); if (send_message.has_topo_address() ) { return_value.topo_address.ip_address = send_message.topo_address().ip_address(); return_value.topo_address.ttl = send_message.topo_address().ttl(); - LOG_DEBUG << "DEBUG: read send message topo address ip: " << return_value.topo_address.ip_address << std::endl; - LOG_DEBUG << "DEBUG: read send message topo address ttl: " << return_value.topo_address.ttl << std::endl; + LOG_INFO("read send message topo address ip: " << return_value.topo_address.ip_address); + LOG_INFO("read send message topo address ttl: " << return_value.topo_address.ttl); } else if (send_message.has_rectangle_address() ) { //Not yet implemented return_value.topo_address.ip_address = send_message.rectangle_address().ip_address(); return_value.topo_address.ttl = 10; - LOG_DEBUG << "DEBUG: read send message topo address ip: " << return_value.topo_address.ip_address << std::endl; - LOG_DEBUG << "DEBUG: read send message topo address ttl: " << return_value.topo_address.ttl << std::endl; + LOG_INFO("read send message topo address ip: " << return_value.topo_address.ip_address); + LOG_INFO("read send message topo address ttl: " << return_value.topo_address.ttl); } else if (send_message.has_circle_address() ) { //Not yet implemented return_value.topo_address.ip_address = send_message.circle_address().ip_address(); return_value.topo_address.ttl = 10; - LOG_DEBUG << "DEBUG: read send message topo address ip: " << return_value.topo_address.ip_address << std::endl; - LOG_DEBUG << "DEBUG: read send message topo address ttl: " << return_value.topo_address.ttl << std::endl; + LOG_INFO("read send message topo address ip: " << return_value.topo_address.ip_address); + LOG_INFO("read send message topo address ttl: " << return_value.topo_address.ttl); } writeCommand(CMD_SUCCESS); @@ -612,17 +596,13 @@ int ClientServerChannel::readSendMessage ( CSC_send_message &return_value ) { * @param cmd command to be written to ambassador */ void ClientServerChannel::writeCommand(CMD cmd) { -#ifdef USE_OMNET_CLOG_H - using namespace omnetpp; -#endif - LOG_DEBUG << "writeCommand" << std::endl; + LOG_FUNCTION(this << cmd); CommandMessage commandMessage; - LOG_DEBUG << "DEBUG: write command: " << cmd << std::endl; commandMessage.set_command_type(cmdToProtoCMD(cmd)); int varintsize = google::protobuf::io::CodedOutputStream::VarintSize32(commandMessage.ByteSize()); - LOG_DEBUG << "DEBUG: write command varint size: " << varintsize << std::endl; + LOG_LOGIC("write command varint size: " << varintsize); int buffer_size = varintsize+commandMessage.ByteSize(); - LOG_DEBUG << "DEBUG: write command buffer size: " << buffer_size << std::endl; + LOG_LOGIC("write command buffer size: " << buffer_size); char message_buffer[buffer_size]; google::protobuf::io::ArrayOutputStream arrayOut ( message_buffer, buffer_size ); @@ -631,7 +611,7 @@ void ClientServerChannel::writeCommand(CMD cmd) { codedOut.WriteVarint32(commandMessage.ByteSize()); commandMessage.SerializeToCodedStream(&codedOut); const size_t count = send ( sock, message_buffer, buffer_size, 0 ); - LOG_DEBUG << "DEBUG: write command send bytes: " << count << std::endl; + LOG_LOGIC("write command send bytes: " << count); } /** @@ -644,10 +624,7 @@ void ClientServerChannel::writeCommand(CMD cmd) { * @param rssi the rssi during the receive event */ void ClientServerChannel::writeReceiveMessage(uint64_t time, int node_id, int message_id, RADIO_CHANNEL channel, int rssi) { -#ifdef USE_OMNET_CLOG_H - using namespace omnetpp; -#endif - LOG_DEBUG << "writeReceiveMessage" << std::endl; + LOG_FUNCTION(this << time << node_id << message_id << channel << rssi); ReceiveMessage receive_message; receive_message.set_time(time); receive_message.set_node_id(node_id); @@ -655,9 +632,9 @@ void ClientServerChannel::writeReceiveMessage(uint64_t time, int node_id, int me receive_message.set_channel_id(channelToProtoChannel(channel)); receive_message.set_rssi(rssi); int varintsize = google::protobuf::io::CodedOutputStream::VarintSize32(receive_message.ByteSize()); - LOG_DEBUG << "DEBUG: write receive message varint size: " << varintsize << std::endl; + LOG_LOGIC("write receive message varint size: " << varintsize); int buffer_size = varintsize+receive_message.ByteSize(); - LOG_DEBUG << "DEBUG: write receive message buffer size: " << buffer_size << std::endl; + LOG_LOGIC("write receive message buffer size: " << buffer_size); char message_buffer[buffer_size]; google::protobuf::io::ArrayOutputStream arrayOut ( message_buffer, buffer_size ); @@ -666,7 +643,7 @@ void ClientServerChannel::writeReceiveMessage(uint64_t time, int node_id, int me codedOut.WriteVarint32 ( receive_message.ByteSize() ); receive_message.SerializeToCodedStream ( &codedOut ); const size_t count = send ( sock, message_buffer, buffer_size, 0 ); - LOG_DEBUG << "DEBUG: write receive message send bytes: " << count << std::endl; + LOG_LOGIC("write receive message send bytes: " << count); } /** @@ -675,16 +652,13 @@ void ClientServerChannel::writeReceiveMessage(uint64_t time, int node_id, int me * @param time the time to write */ void ClientServerChannel::writeTimeMessage(int64_t time) { -#ifdef USE_OMNET_CLOG_H - using namespace omnetpp; -#endif - LOG_DEBUG << "DEBUG: write time message: " << time << std::endl; + LOG_FUNCTION(this << time); TimeMessage time_message; time_message.set_time ( time ); int varintsize = google::protobuf::io::CodedOutputStream::VarintSize32 ( time_message.ByteSize() ); - LOG_DEBUG << "DEBUG: write time message varint size: " << varintsize << std::endl; + LOG_LOGIC("write time message varint size: " << varintsize); int buffer_size = varintsize+time_message.ByteSize(); - LOG_DEBUG << "DEBUG: write time message buffer size: " << buffer_size << std::endl; + LOG_LOGIC("write time message buffer size: " << buffer_size); char message_buffer[buffer_size]; google::protobuf::io::ArrayOutputStream arrayOut ( message_buffer, buffer_size ); @@ -693,7 +667,7 @@ void ClientServerChannel::writeTimeMessage(int64_t time) { codedOut.WriteVarint32 ( time_message.ByteSize() ); time_message.SerializeToCodedStream ( &codedOut ); const size_t count = send ( sock, message_buffer, buffer_size, 0 ); - LOG_DEBUG << "DEBUG: write time message send bytes: " << count << std::endl; + LOG_LOGIC("write time message send bytes: " << count); } /** @@ -702,17 +676,14 @@ void ClientServerChannel::writeTimeMessage(int64_t time) { * @param port port */ void ClientServerChannel::writePort(uint32_t port) { -#ifdef USE_OMNET_CLOG_H - using namespace omnetpp; -#endif - LOG_DEBUG << "writePort port: " << port << std::endl; + LOG_FUNCTION(this << port); PortExchange port_exchange; port_exchange.set_port_number ( port ); - LOG_DEBUG << "DEBUG: write port exchange: " << port_exchange.port_number() << std::endl; + LOG_LOGIC("write port exchange: " << port_exchange.port_number()); int varintsize = google::protobuf::io::CodedOutputStream::VarintSize32(port_exchange.ByteSize()); - LOG_DEBUG << "DEBUG: write port message varint size: " << varintsize << std::endl; + LOG_LOGIC("write port message varint size: " << varintsize); int buffer_size = varintsize+port_exchange.ByteSize(); - LOG_DEBUG << "DEBUG: write port message buffer size: " << buffer_size << std::endl; + LOG_LOGIC("write port message buffer size: " << buffer_size); char message_buffer[buffer_size]; google::protobuf::io::ArrayOutputStream arrayOut ( message_buffer, buffer_size ); @@ -721,7 +692,7 @@ void ClientServerChannel::writePort(uint32_t port) { codedOut.WriteVarint32(port_exchange.ByteSize()); port_exchange.SerializeToCodedStream(&codedOut); const size_t count = send ( sock, message_buffer, buffer_size, 0 ); - LOG_DEBUG << "DEBUG: write port message send bytes: " << count << std::endl; + LOG_LOGIC("write port message send bytes: " << count); } //##################################################### @@ -737,9 +708,7 @@ void ClientServerChannel::writePort(uint32_t port) { * */ std::shared_ptr < uint32_t > ClientServerChannel::readVarintPrefix(SOCKET sock) { -#ifdef USE_OMNET_CLOG_H - using namespace omnetpp; -#endif + LOG_FUNCTION(this << sock); int num_bytes=0; char current_byte; @@ -760,7 +729,7 @@ std::shared_ptr < uint32_t > ClientServerChannel::readVarintPrefix(SOCKET sock) } return_value |= ( current_byte & 0x7F ) << ( 7 * (num_bytes - 1 ) ); //Add the next 7 bits } - LOG_DEBUG << "DEBUG: read VarintPrefix value: " << return_value << std::endl; + LOG_LOGIC("readVarintPrefix return value: " << return_value); return std::make_shared < uint32_t > ( return_value ); } diff --git a/src/util/Log.cc b/src/util/Log.cc new file mode 100644 index 0000000..8956e9b --- /dev/null +++ b/src/util/Log.cc @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2021 Fraunhofer FOKUS and others. All rights reserved. + * Copyright (c) 2006,2007 INRIA + * + * Contact: mosaic@fokus.fraunhofer.de + * + * This code is developed for the MOSAIC-OMNeT++ coupling. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + + #include "Log.h" + #include + + +void set_log_level(omnetpp::LogLevel omnetpp_level) { + // map OMNeT++ log level to LogLevel + switch (omnetpp_level) { + case omnetpp::LOGLEVEL_TRACE: + g_log_level = LOG_LEVEL_LOGIC; + break; + case omnetpp::LOGLEVEL_DEBUG: + g_log_level = LOG_LEVEL_FUNCTION; + break; + case omnetpp::LOGLEVEL_DETAIL: + g_log_level = LOG_LEVEL_INFO; + break; + case omnetpp::LOGLEVEL_INFO: + g_log_level = LOG_LEVEL_DEBUG; + break; + case omnetpp::LOGLEVEL_WARN: + g_log_level = LOG_LEVEL_WARN; + break; + case omnetpp::LOGLEVEL_ERROR: + case omnetpp::LOGLEVEL_FATAL: + g_log_level = LOG_LEVEL_ERROR; + break; + case omnetpp::LOGLEVEL_OFF: + g_log_level = LOG_NONE; + } +} + +bool is_LOG_enabled(int level) { + if (g_log_level & level) + return true; + else + return false; +} + + +// copied from ns-3.34, file: core/log.cc +ParameterLogger::ParameterLogger (std::ostream &os) + : m_first (true), + m_os (os) +{} + +// copied from ns-3.34, file: core/log.cc +template<> +ParameterLogger & +ParameterLogger::operator<< (const std::string param) +{ + if (m_first) + { + m_os << "\"" << param << "\""; + m_first = false; + } + else + { + m_os << ", \"" << param << "\""; + } + return *this; +} + +// copied from ns-3.34, file: core/log.cc +template<> +ParameterLogger & +ParameterLogger::operator<< (const char * param) +{ + (*this) << std::string (param); + return *this; +} \ No newline at end of file diff --git a/src/util/Log.h b/src/util/Log.h new file mode 100644 index 0000000..aae26d7 --- /dev/null +++ b/src/util/Log.h @@ -0,0 +1,200 @@ +/* + * Copyright (c) 2021 Fraunhofer FOKUS and others. All rights reserved. + * Copyright (c) 2006,2007 INRIA + * + * Contact: mosaic@fokus.fraunhofer.de + * + * This code is developed for the MOSAIC-OMNeT++ coupling. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + + +#ifndef __NS3LOG_H__ +#define __NS3LOG_H__ + +#include +#include +#include + + #include + +// default log stream +#ifndef LOG_OUT +#define LOG_OUT std::cout +#endif + + +// enum copied from ns-3.34, file: core/log.h +/** + * Logging severity classes and levels. + */ +enum LogLevel +{ + LOG_NONE = 0x00000000, //!< No logging. + + LOG_ERROR = 0x00000001, //!< Serious error messages only. + LOG_LEVEL_ERROR = 0x00000001, //!< LOG_ERROR and above. + + LOG_WARN = 0x00000002, //!< Warning messages. + LOG_LEVEL_WARN = 0x00000003, //!< LOG_WARN and above. + + LOG_DEBUG = 0x00000004, //!< Rare ad-hoc debug messages. + LOG_LEVEL_DEBUG = 0x00000007, //!< LOG_DEBUG and above. + + LOG_INFO = 0x00000008, //!< Informational messages (e.g., banners). + LOG_LEVEL_INFO = 0x0000000f, //!< LOG_INFO and above. + + LOG_FUNCTION = 0x00000010, //!< Function tracing. + LOG_LEVEL_FUNCTION = 0x0000001f, //!< LOG_FUNCTION and above. + + LOG_LOGIC = 0x00000020, //!< Control flow tracing within functions. + LOG_LEVEL_LOGIC = 0x0000003f, //!< LOG_LOGIC and above. + + LOG_ALL = 0x0fffffff, //!< Print everything. + LOG_LEVEL_ALL = LOG_ALL, //!< Print everything. + + LOG_PREFIX_FUNC = 0x80000000, //!< Prefix all trace prints with function. + LOG_PREFIX_TIME = 0x40000000, //!< Prefix all trace prints with simulation time. + LOG_PREFIX_NODE = 0x20000000, //!< Prefix all trace prints with simulation node. + LOG_PREFIX_LEVEL = 0x10000000, //!< Prefix all trace prints with log level (severity). + LOG_PREFIX_ALL = 0xf0000000 //!< All prefixes. +}; + +// class copied from ns-3.34, file: core/log.h +/** + * Insert `, ` when streaming function arguments. + */ +class ParameterLogger +{ + bool m_first; //!< First argument flag, doesn't get `, `. + std::ostream &m_os; //!< Underlying output stream. + +public: + /** + * Constructor. + * + * \param [in] os Underlying output stream. + */ + ParameterLogger (std::ostream &os); + + /** + * Write a function parameter on the output stream, + * separating parameters after the first by `,` strings. + * + * \param [in] param The function parameter. + * \return This ParameterLogger, so it's chainable. + */ + template + ParameterLogger& operator<< (T param); + + /** + * Overload for vectors, to print each element. + * + * \param [in] vector The vector of parameters + * \return This ParameterLogger, so it's chainable. + */ + template + ParameterLogger& operator<< (std::vector vector); + +}; + +// derived from ns-3.34, file: core/log.cc +template +ParameterLogger & +ParameterLogger::operator<< (const T param) +{ + if (m_first) + { + m_os << (param); + m_first = false; + } + else + { + m_os << ", " << (param); + } + return *this; +} + + +// define log level with default value or use defined preprocessor symbol LOG_LEVEL +#ifdef LOG_LEVEL +static int g_log_level = LOG_LEVEL; +#else +static int g_log_level = LOG_LEVEL_WARN; +#endif + +void set_log_level (omnetpp::LogLevel omnetpp_level); + +bool is_LOG_enabled (int level); + +// define simplified LOG makros (compare to official ns3 logging macros) + +#define LOG_CONDITION + +#define LOG_COMPONENT_DEFINE(name) \ + static std::string g_LOG_component_name = name; + +// simplified prefix +#define LOG_APPEND_PREFIX \ + LOG_OUT << g_LOG_component_name << ": "; + +// copied from ns-3.34, file: core/log.h +#define LOG_ERROR(msg) \ + NS_LOG (LOG_ERROR, msg) + +// copied from ns-3.34, file: core/log.h +#define LOG_WARN(msg) \ + NS_LOG (LOG_WARN, msg) + +// copied from ns-3.34, file: core/log.h +#define LOG_DEBUG(msg) \ + NS_LOG (LOG_DEBUG, msg) + +// copied from ns-3.34, file: core/log.h +#define LOG_INFO(msg) \ + NS_LOG (LOG_INFO, msg) + +// copied from ns-3.34, file: core/log.h +#define LOG_LOGIC(msg) \ + NS_LOG (LOG_LOGIC, msg) + +// derived from ns-3.34, file: core/log.h +#define NS_LOG(level, msg) \ + LOG_CONDITION \ + do { \ + if (is_LOG_enabled (level)) \ + { \ + LOG_APPEND_PREFIX; \ + LOG_OUT << msg << std::endl; \ + } \ + } while (false) + +// derived from ns-3.34, file: core/log.h +#define LOG_FUNCTION(parameters) \ + LOG_CONDITION \ + do \ + { \ + if (is_LOG_enabled (LOG_FUNCTION)) \ + { \ + LOG_APPEND_PREFIX; \ + LOG_OUT << __FUNCTION__ << "("; \ + ParameterLogger (LOG_OUT) << parameters; \ + LOG_OUT << ")" << std::endl; \ + } \ + } \ + while (false) + +#endif \ No newline at end of file