void ezlogger_simple_example()
{
        int i = 123;
        std::string somedata = "Hello World";

        //Simple usage with standard verbosity level
        EZLOGGERSTREAM << somedata << " " << i << std::endl;

        //Can use alternate stream
        EZLOGGERSTREAM2(std::cerr) << somedata << " next line " << i << std::endl;

        //Verbosity level logging example
        EZLOGGERVLSTREAM(axter::log_often) << somedata << " " << i << std::endl;

        //Complex extended data example
        EZLOGGERVLSTREAM(axter::levels(axter::log_often, axter::warn, __FUNCSIG__ /*or GNU PRETTY_FUNCTION*/, "Xyz Facility")) << somedata << " " << i << std::endl;
}
Beispiel #2
0
int main(int argc, char**argv)
{
    axter::ezlogger<>::set_verbosity_level_tolerance(axter::log_very_rarely);
    EZLOGGERFUNCTRACKER;
    int ReturnValue = 99;
    EZLOGGER_PRG_MAIN_ARG(argc, argv);
    EZDBGONLYLOGGER_PRG_MAIN_ARG(argc, argv);
    EZLOGGERVL_PRG_MAIN_ARG(axter::log_often, argc, argv);
    int i = 123;
    std::string somedata = "Hello World";
    EZLOGGER(i);
    EZDBGONLYLOGGER(i);
    EZLOGGERVL(axter::log_often)(i);

    EZLOGGERVAR(somedata);
    EZDBGONLYLOGGERVAR(somedata);
    EZLOGGERVLVAR(axter::log_often, somedata);

    bool SomeConditionVar = true;
    EZLOGGERVAR(SomeConditionVar == false);
    EZDBGONLYLOGGERVAR(SomeConditionVar == false);
    EZLOGGERVLVAR(axter::log_often, SomeConditionVar == true);

    EZLOGGERVLVARIFY(axter::log_often, SomeConditionVar == false);

    EZLOGGERSTREAM << somedata << " " << i << std::endl;
    EZLOGGERSTREAM << somedata << " next line " << i << std::endl;
    EZLOGGERSTREAM2(std::cerr) << somedata << " next line " << i << std::endl;
    EZDBGONLYLOGGERSTREAM << somedata << " " << i << std::endl;
    EZDBGONLYLOGGERSTREAM << somedata << " next line " << i << std::endl;
    EZLOGGERVLSTREAM(axter::log_often) << somedata << " " << i << std::endl;
    // EZLOGGERVLSTREAM(axter::levels(axter::log_often, axter::warn, __FUNCSIG__ /*or GNU PRETTY_FUNCTION*/,"Xyz Facility")) << somedata << " " << i << std::endl;

    EZLOGGERPRINT("i = %i and somedata = %s", i, somedata.c_str());
    EZDBGONLYLOGGERPRINT("i = %i and somedata = %s", i, somedata.c_str());
    EZLOGGERVLPRINT(axter::log_often)("i = %i and somedata = %s", i, somedata.c_str());
    //Alternative method
    EZLOGGERVL(axter::log_often).cprint("i = %i and somedata = %s", i, somedata.c_str());
    EZLOGGER.cprint("i = %i and somedata = %s", i, somedata.c_str());

    if (1)
    {
        EZLOGGERMARKER;
        EZDBGONLYLOGGERMARKER;
        EZLOGGERVLMARKER(axter::log_often);
    }

    some_func1();

    return EZLOGGERVAR(ReturnValue);
}
void CommandController::Process(std::string source, std::string message)
{
	// commands can perform an effect on the game via the command functors ...

	JSONValue *value = JSON::Parse(message.c_str());

	std::string id,target;
	if (value)
	{
		if (!value->IsObject())
		{
			EZLOGGERVLSTREAM(axter::log_always) << "Input from " << source << ": Object expected." <<  std::endl;
		}
		else
		{
			JSONObject object = value->AsObject();

			JSONValue* jsonID = (object.find(L"id") != object.end())?  object[L"id"] : NULL;
			JSONValue* jsonTarget = (object.find(L"target") != object.end())?  object[L"target"] : NULL;

			if (jsonID != NULL && jsonID->IsString())
			{
				std::wstring ws = jsonID->AsString();
				id  = std::string( ws.begin(), ws.end() );
			}
			else
			{
				EZLOGGERVLSTREAM(axter::log_always) << "Input from " << source << ": string id expected." <<  std::endl;
			}

			if (jsonTarget != NULL && jsonTarget->IsString())
			{
				std::wstring ws = jsonTarget->AsString();
				target  = std::string( ws.begin(), ws.end() );
			}
		}
	}
	
	delete value;

	bool toSend = true;
	auto clientCommandIter = clientCommands.find(source);

	if (clientCommandIter != clientCommands.end())
	{
		// there is a filter list for this client
		toSend = false;
		for (auto i = clientCommandIter->second.begin(); i != clientCommandIter->second.end(); i++)
		{
			if (id.compare(*i) == 0 )
			{
				toSend = true;
				break;
			}
		}
	}

	if (!toSend)
		return;

	auto commandIterator = commands.find(id);
	if (commandIterator != commands.end())
	{
		// call the behavior
		(commandIterator->second)(target);
	}

	// ... and they can also be routed to other clients
	auto configurationIter = commandConfigurations.find(id);
	if (configurationIter != commandConfigurations.end())
	{
		// a config exists, send to all in 'route'
		auto configuration = configurationIter->second;
		for (auto i = configuration.route.begin(); i != configuration.route.end(); i++)
		{
			auto client = *i;
			for (auto j = commanders.begin(); j != commanders.end(); j++)
			{
				auto clientConnection = *j;
				if (clientConnection->id.compare(client) == 0)
				{
					clientConnection->Send(message);
				}
			}
		}
	}



}
boost::shared_ptr<StateSubscriber>  NeuroSandCube::CreateSerialPort(std::string description)
{
	// i have moved this here temporarily since i dont know where to handle the creation right now.

	// instantiates appropriate network port based on device description
	// if its a com address -> serial port

	boost::shared_ptr<NetworkConnection> serialPort;
	auto subscriber = boost::shared_ptr<NetworkSubscriber>(new NetworkSubscriber);

	std::transform(description.begin(), description.end(),description.begin(), ::toupper);
	std::istringstream oss(description);
	std::string address, rest;
	getline(oss , address, ':'); 
	getline(oss , rest, ':'); 

	std::string sub = address.substr(0,3);
	
	if (sub.compare(std::string("COM")) == 0)
	{
		// init com
		
		int iBaudRate;
		SerialPort_Parity eParity;
		int iDataBits ;
		float fStopBits;


		std::string sBaudRate, sDataBits;;
		// yuck
		for (unsigned int i = 0; i < rest.size(); ++i)
		{
			char ch = rest[i];

			if (!isdigit(ch))
			{
				// done with baud rate - convert
				iBaudRate = atoi(sBaudRate.c_str());

				if (ch == 'N')
					eParity = SP_PARITY_NONE;
				else if (ch == 'O')
					eParity = SP_PARITY_ODD;
				else
					eParity = SP_PARITY_EVEN;

				char  sDataBits[2]  = { rest[i+1], '\0' };
				iDataBits = atoi(sDataBits); 
				const char* r = rest.c_str();
				fStopBits = atof(&(r[i+2]));
				break;
			}
			else
			{
				sBaudRate.insert(sBaudRate.size(),1,ch);
			}
		}


		if (iBaudRate == 0 || iDataBits == 0 || fStopBits == 0.0f)
		{
			//FAIL
			EZLOGGERVLSTREAM(axter::log_always) << "Failed to initialize serial port - " << description <<". Incorrect format specified!"<< std::endl;
		}

		serialPort = boost::shared_ptr<SerialPort>(new SerialPort(m_spIOService->m_IOService,address,iBaudRate,eParity,iDataBits,fStopBits));
		/*subscriber->m_Connection = serialPort;
		subscriber->id = description;*/
		m_spConnectionMediator->AddConnection(description, serialPort);
	}

	return subscriber;
}
void NeuroSandCube::Initialize(fpsent* player)
{
	m_spCommandController = boost::shared_ptr<CommandController>(new CommandController());
	m_spConnectionMediator = boost::shared_ptr<ConnectionMediator>(new ConnectionMediator(m_spCommandController));
	m_spIOService = boost::shared_ptr<IOService>(new IOService());
	m_spServer = boost::shared_ptr<TCPServer>(new TCPServer(m_spIOService->m_IOService,12345,*m_spConnectionMediator));
	m_spServer->Init();

	this->player = player;

	bRewardIssued = false;

	std::string configFile = "data/NSC/nsc_config.json";

	// others possibilites:

	// flag captured
	// velocity

	// faced now with either extending States to have two modes of tracking - 
	StateDistributor& distributor = StateDistributor::GetInstance();


	distributor.AddState("player_x",
	[player] ()
	{
		return State(player->newpos.x);
	}
	);

	distributor.AddState("player_y",
	[player] ()
	{
		return State(player->newpos.y);
	}
	);

	distributor.AddState("player_left_click",
	[player] ()
	{
		return State(player->attacking);
	}
	);
	distributor.AddState("player_right_click",
	[player] ()
	{
		return State(player->rightClick);
	}
	);

	distributor.AddState("level_restart",
	[player] () -> State
	{
		return State(player->levelRestart);

	}
	);

	distributor.AddState("player_angle",
	[player] ()
	{
		return State(player->yaw);
	}
	);

	distributor.AddState("distance_traveled",
	[player] () -> State
	{
		player->distance_traveled += player->deltapos.magnitude();
		return State( player->distance_traveled);
	}
	);

	distributor.AddState("distance_from_start",
	[player] ()
	{
		return State( player->newpos.dist(player->startingPosition));
	}
	);

	distributor.AddState("teleport",
	[player] () -> State
	{
		return State(player->teleported);
	}
	);

	distributor.AddState("reward_issued",
	[this] () -> State
	{
		return State(bRewardIssued);
	});

	distributor.AddEvent("trial_start",
	[player] () -> NSCEvent
	{
		NSCEvent e;
		e.triggered = player->levelStart;
		e.description = std::string(player->prevMap);
		return e;
	});

	distributor.AddState("correct_trial",
	[player] () -> State
	{
		return State(player->trigger == 1);
	});

	distributor.AddState("incorrect_trial",
	[player] () -> State
	{
		return State(player->trigger == 2);
	});


	StateConfigReader configReader;

	configReader.ReadConfig(configFile);
	int i=0;
	boost::shared_ptr<StateAttributes> attributes = configReader.GetStateAttribute(i);

	std::set<std::string> serialPortSet;
	while (attributes.get() != NULL)
	{

		if (distributor.AddDistribution(*attributes))
		{
			for (unsigned int j=0; j < attributes->consumers.size(); ++j)
			{
				auto consumer = std::string(attributes->consumers[j]);
				std::istringstream oss(consumer);

				std::string sub = consumer.substr(0,3);
				if (sub.compare(std::string("COM")) == 0)
				{
					serialPortSet.insert(consumer);
				}

			}

		}
		else
		{

			EZLOGGERVLSTREAM(axter::log_always) << "Attempting to add distribution of unsuported state - " << attributes->id <<"!"<< std::endl;
		}
		attributes = configReader.GetStateAttribute(++i);

	}

	for (auto iterator = serialPortSet.begin(); iterator != serialPortSet.end(); iterator++)
	{
		/*distributor.AddSubscriber(*/CreateSerialPort(*iterator)/*)*/;
	}


	// setup command attributes

	m_spCommandController->AddCommand("restart_map",
		[player] (std::string target) { game::startmap(player->prevMap); });
	m_spCommandController->AddCommand("reset_counter",
		[player] (std::string target) { StateDistributor::GetInstance().ResetCounter(target); });
	m_spCommandController->AddCommand("issue_reward",
		[this] (std::string target) { bRewardIssued = true; });

	

	i=0;
	auto commandAttribute = configReader.GetCommandAttribute(i);

	while (commandAttribute.get() != NULL)
	{

		m_spCommandController->AddConfiguration(*commandAttribute);
		commandAttribute = configReader.GetCommandAttribute(++i);
	}




}