Example #1
0
bool is_process_exist(int pID)
{
	VLOG(2) << "Checking  process " << pID << " exist.";
#if defined(_WIN32)
	HANDLE handle = OpenProcess(PROCESS_QUERY_INFORMATION, false, pID);
	if (!handle)
	{
		VLOG(2)<<"The process "<<pID<<" is not exist.";
		return false;
	}
	else
	{
		DWORD _status=0;
		bool _result=GetExitCodeProcess(handle,&_status)!=FALSE;
		CloseHandle(handle);
		VLOG_IF(2,_result)<<"The process "<<pID<<" is   exist.";
		VLOG_IF(2,!_result)<<"The process "<<pID<<" is not   exist.";
		return _status==STILL_ACTIVE;
	}
#else
	if (kill(pID, 0) != 0 && errno != EPERM)
	{
		VLOG(2) << "The process " << pID << " is not  exist.";
		return false;
	}
	else
	{
		VLOG(2) << "The process " << pID << " is  exist.";
		return true;
	}
#endif
}
bool IMPL_CLASS::MClose()
{
	if (!MIsOpen())
	{
		VLOG(2) << "The shared client is not opened.";
		return false;
	}
	VLOG(2) << "Close client " << FMyInfo->FInfo.FId;
	VLOG_IF(2,!MIsConnected()) << "Does not connected.";
	if (MIsConnected())
		MDisconnect();


	FIsOpened = false;

	VLOG(2) << "Deallocate memory";
	FServerSignalEvent.FSignalEvent.MFree();
	FServerSignalEvent.FSignalSem.MFree();
	if(FMyInfo->FInfo.FId.FUniqueID==0)//the server is handled disconnect
		deallocate_object(FSharedMemory.MGetAllocator(), FMyInfo);

	FMyInfo = NULL;
	if(MFreeBase())
	{
		FServerSignalEvent.FSignalEvent.MUnlink();
		FServerSignalEvent.FSignalSem.MUnlink();
	}
	return true;
}
Example #3
0
void AsyncServerSocket::getAddress(SocketAddress* addressReturn) const {
  CHECK(sockets_.size() >= 1);
  VLOG_IF(2, sockets_.size() > 1)
    << "Warning: getAddress() called and multiple addresses available ("
    << sockets_.size() << "). Returning only the first one.";

  addressReturn->setFromLocalAddress(sockets_[0].socket_);
}
Example #4
0
/* bool CCondvar::MTimedwait(CMutex *aMutex, double const aTime)
{
	struct timespec _time =
	{ 0, 0 };
	add(&_time, aTime);
	return MTimedwait(aMutex, &_time);
}*/
bool CCondvar::MTimedwait(CMutex *aMutex, double const aTime)
{
	VLOG_IF(2,aTime>=0.0)
			<< "Condvar is Waited for  Mutex " << aTime << " sec.";
	VLOG_IF(2,aTime<0)
			<< "Condvar is Waited for  Mutex an infinitely long time";
//	aMutex->FIsForCondvar = true;
#ifdef __QNX__
	if (aMutex->MGetMutexType() != CMutex::MUTEX_NORMAL)
	{
		std::cerr << "The Condvar is not  working with recursive mutex."
				<< std::endl;
		LOG(FATAL)<<"The Condvar is not  working with recursive mutex.";
		return false;
	}
#endif
	int _status=-1;
	pthread_cleanup_push(CImpl::MConditionCleanup, aMutex->MGetPtr())
		;
		if (aTime > 0.0)
		{
			struct timespec tm;
			if (clock_gettime(CLOCK_REALTIME, &tm) < 0)
			{
				LOG(DFATAL)<<"Clock reatime error "<<errno;
				return false;
			}
			VLOG(2)<<"Wait for from "<<tm.tv_sec<<"."<<tm.tv_nsec;
			add(&tm, aTime);
			VLOG(2)<<"Wait for to "<<tm.tv_sec<<"."<<tm.tv_nsec;
			_status = pthread_cond_timedwait(&FPImpl->FCondition,
					static_cast<pthread_mutex_t*>(aMutex->MGetPtr()), &tm);
		}
		else
			_status = pthread_cond_wait(&FPImpl->FCondition,
					static_cast<pthread_mutex_t*>(aMutex->MGetPtr()));
		VLOG_IF(1,_status<0) << "Condvar error " << strerror(errno)<<"("<<errno<<")";
		pthread_cleanup_pop(0);
	return !_status;
}
Example #5
0
int64_t WdtSocket::ioWithAbortCheck(F readOrWrite, T tbuf, int64_t numBytes,
                                    int timeoutMs, bool tryFull) {
  WDT_CHECK(threadCtx_.getAbortChecker() != nullptr)
      << "abort checker can not be null";
  bool checkAbort = (threadCtx_.getOptions().abort_check_interval_millis > 0);
  auto startTime = Clock::now();
  int64_t doneBytes = 0;
  int retries = 0;
  while (doneBytes < numBytes) {
    const int64_t ret =
        readOrWrite(fd_, tbuf + doneBytes, numBytes - doneBytes);
    if (ret < 0) {
      // error
      if (errno != EINTR && errno != EAGAIN) {
        PLOG(ERROR) << "non-retryable error encountered during socket io "
                    << fd_ << " " << doneBytes << " " << retries;
        return (doneBytes > 0 ? doneBytes : ret);
      }
    } else if (ret == 0) {
      // eof
      VLOG(1) << "EOF received during socket io. fd : " << fd_
              << ", finished bytes : " << doneBytes
              << ", retries : " << retries;
      return doneBytes;
    } else {
      // success
      doneBytes += ret;
      if (!tryFull) {
        // do not have to read/write entire data
        return doneBytes;
      }
    }
    if (checkAbort && threadCtx_.getAbortChecker()->shouldAbort()) {
      LOG(ERROR) << "transfer aborted during socket io " << fd_ << " "
                 << doneBytes << " " << retries;
      return (doneBytes > 0 ? doneBytes : -1);
    }
    if (timeoutMs > 0) {
      int duration = durationMillis(Clock::now() - startTime);
      if (duration >= timeoutMs) {
        LOG(INFO) << "socket io timed out after " << duration << " ms, retries "
                  << retries << " fd " << fd_ << " doneBytes " << doneBytes;
        return (doneBytes > 0 ? doneBytes : -1);
      }
    }
    retries++;
  }
  VLOG_IF(1, retries > 1) << "socket io for " << doneBytes << " bytes took "
                          << retries << " retries";
  return doneBytes;
}
Example #6
0
/// Get an idle worker Worker that can be paired with a Task.
///
/// Threads returned by this method are specifically running workerLoop().
Worker * TaskingScheduler::getWorker () {
  if (task_manager->available()) {
    // check the pool of unassigned coroutines
    Worker * result = unassignedQ.dequeue();
    if (result != NULL) return result;

    // possibly spawn more coroutines
    result = maybeSpawnCoroutines();
    VLOG_IF(3, result==NULL) << "run out of coroutines";
    return result;
  } else {
    return NULL;
  }
}
Example #7
0
INITIALIZE_EASYLOGGINGPP

int main(int argc, char** argv) {
    START_EASYLOGGINGPP(argc, argv);
    el::Loggers::addFlag(el::LoggingFlag::DisableApplicationAbortOnFatalLog);
    el::Loggers::addFlag(el::LoggingFlag::ColoredTerminalOutput);

    // You can uncomment following lines to take advantage of hierarchical logging
    // el::Loggers::addFlag(el::LoggingFlag::HierarchicalLogging);
    // el::Loggers::setLoggingLevel(el::Level::Global);

    LOG(INFO);
    LOG(DEBUG);
    LOG(WARNING);
    LOG(ERROR);
    LOG(TRACE);
    VLOG(1);
    LOG(FATAL);

    DLOG(INFO);
    DLOG(DEBUG);
    DLOG(WARNING);
    DLOG(ERROR);
    DLOG(TRACE);
    DVLOG(1);
    DLOG(FATAL);

    LOG(INFO) << "Turning off colored output";
    el::Loggers::removeFlag(el::LoggingFlag::ColoredTerminalOutput);
    
    LOG_IF(true, INFO);
    LOG_IF(true, DEBUG);
    LOG_IF(true, WARNING);
    LOG_IF(true, ERROR);
    LOG_IF(true, TRACE);
    VLOG_IF(true, 1);
    LOG_IF(true, FATAL);

    LOG_EVERY_N(1, INFO);
    LOG_EVERY_N(1, DEBUG);
    LOG_EVERY_N(1, WARNING);
    LOG_EVERY_N(1, ERROR);
    LOG_EVERY_N(1, TRACE);
    VLOG_EVERY_N(1, 1);
    LOG_EVERY_N(1, FATAL);

    CHECK(1 == 1);
    CCHECK(1 == 1, "default");
    return 0;
}
Example #8
0
_INITIALIZE_EASYLOGGINGPP

int main(int argc, char** argv) {
  _START_EASYLOGGINGPP(argc, argv);
  LOG(INFO) << "This is demo for verbose logs";
  VLOG(1) << "This will be printed when program is started using argument --v=1";
  VLOG(2) << "This will be printed when program is started using argument --v=2";
  VLOG(1) << "This will be printed when program is started using argument --v=1";
  VLOG_IF(true, 1) << "Always verbose for level 1";

  VLOG_EVERY_N(1, 3) << "Verbose every N";

  VLOG(4) << "Command line arguments provided " << *el::Helpers::commandLineArgs();
  return 0;
}
Example #9
0
int main(int argc, char* argv[]) 
{  
  paracel::main_env comm_main_env(argc, argv);
  paracel::Comm comm(MPI_COMM_WORLD);
  
  google::InitGoogleLogging(argv[0]);
  int num_cookies = 7;
  LOG(INFO) << "Found " << num_cookies << " cookies";
  LOG_IF(INFO, comm.get_rank() == 0) << "Got lots of cookies";
  
  DLOG(INFO) << "Found cookies";
  DLOG_IF(INFO, comm.get_rank() == 0) << "Got lots of cookies";

  VLOG_IF(1, comm.get_rank() == 0)
      << "I'm printed when size is more than 1024 and when you run the "
      "program with --v=1 or more";
  return 0;
}
Example #10
0
void *write(void* thrId){
  char* threadId = (char*)thrId;
  // Following line will be logged with every thread
  LOG(INFO) << "This standard log is written by [Thread #" << threadId << "]";
  // Following line will be logged with every thread only when --v=2 argument 
  // is provided, i.e, ./bin/multithread_test.cpp.bin --v=2
  VLOG(2) << "This is verbose level 2 logging from [Thread #" << threadId << "]";

  // Following line will be logged only once from second running thread (which every runs second into 
  // this line because of interval 2)
  LOG_EVERY_N(2, WARNING) << "This will be logged only once from thread who every reaches this line first. Currently running from [Thread #" << threadId << "]";

  for (int i = 1; i <= 10; ++i) {
     VLOG_IF(true, 2) << "Verbose condition [Thread #" << threadId << "]";
     VLOG_EVERY_N(2, 3) << "Verbose level 3 log every 4th time. This is at " << i << " from [Thread #" << threadId << "]";
  }

  // Following line will be logged once with every thread because of interval 1 
  LOG_EVERY_N(1, INFO) << "This interval log will be logged with every thread, this one is from [Thread #" << threadId << "]";

  LOG_IF(strcmp(threadId, "2") == 0, INFO) << "This log is only for thread 2 and is ran by [Thread #" << threadId << "]";
  
  // Register 5 vague loggers
  for (int i = 1; i <= 5; ++i) {
     std::stringstream ss;
     ss << "logger" << i;
     el::Logger* logger = el::Loggers::getLogger(ss.str());
     LOG(INFO) << "Registered logger [" << *logger << "] [Thread #" << threadId << "]";
     CLOG(INFO, "default", "network") << "Triggering network log from default and network";
  }
  CLOG(INFO, "logger1") << "Logging using new logger [Thread #" << threadId << "]";
  CLOG(INFO, "no-logger") << "THIS SHOULD SAY LOGGER NOT REGISTERED YET [Thread #" << threadId << "]"; // << -- NOTE THIS!
  CLOG(INFO, "default", "network") << "Triggering network log from default and network";

  el::Logger* logger = el::Loggers::getLogger("default");
  logger->info("Info log from [Thread #%v]", threadId);

  // Check for log counters positions
  for (int i = 1; i <= 50; ++i) {
     LOG_EVERY_N(2, INFO) << "Counter pos: " << ELPP_COUNTER_POS << " [Thread #" << threadId << "]";
  }
  LOG_EVERY_N(2, INFO) << "Counter pos: " << ELPP_COUNTER_POS << " [Thread #" << threadId << "]";
  return NULL;
}
Example #11
0
_INITIALIZE_EASYLOGGINGPP

int main(int argc, char** argv) {
    _START_EASYLOGGINGPP(argc, argv);
    el::Helpers::addFlag(el::LoggingFlag::DisableApplicationAbortOnFatalLog);
    
    LOG(INFO);
    LOG(DEBUG);
    LOG(WARNING);
    LOG(ERROR);
    LOG(TRACE);
    VLOG(1);
    LOG(FATAL);

    DLOG(INFO);
    DLOG(DEBUG);
    DLOG(WARNING);
    DLOG(ERROR);
    DLOG(TRACE);
    DVLOG(1);
    DLOG(FATAL);

    LOG_IF(true, INFO);
    LOG_IF(true, DEBUG);
    LOG_IF(true, WARNING);
    LOG_IF(true, ERROR);
    LOG_IF(true, TRACE);
    VLOG_IF(true, 1);
    LOG_IF(true, FATAL);

    LOG_EVERY_N(1, INFO);
    LOG_EVERY_N(1, DEBUG);
    LOG_EVERY_N(1, WARNING);
    LOG_EVERY_N(1, ERROR);
    LOG_EVERY_N(1, TRACE);
    VLOG_EVERY_N(1, 1);
    LOG_EVERY_N(1, FATAL);

    CHECK(1 == 1);
    CCHECK(1 == 1, "default");
    return 0;
}
bool IMPL_CLASS::MOpenServer(void* _p)
{
	VLOG(2) << "Open server addr=" << _p;
	CHECK_NOTNULL(_p);
	server_info_t* _info = reinterpret_cast<server_info_t*>(_p);

	bool _is = _info->MCheckCrc() && is_process_exist(_info->FInfo.FId.FPid);
	VLOG_IF(2, !_is) << "Invalid server info crc.";
	if (!_is)
	return false;

	_is = MInitSharedSem(_info);
	if (!_is)
	{
		LOG(ERROR)<<"Cannot initialize semaphore.";
		return false;
	}
	_is = MInitSignalEvent(FServerSignalEvent, &_info->FInfo);
	FServerInfo = _info;
	VLOG(2)<<"Opened";
	return true;
}
Example #13
0
	bool MInit(uint8_t* aBuf, size_t aSize, eOpenType aIsNew)
	{
		if(aSize<eReguredBufSize)
		{
			LOG(DFATAL)<<"Invalid size of buf "<<aSize<<" min size "<< eReguredBufSize;
			return false;
		}
		void *const _p=memchr(aBuf,'\0',aSize);
		bool const _is_empty=_p== NULL || _p==aBuf;

		if(aIsNew==E_UNDEF && _is_empty)
		{
			VLOG(2)<<"The buffer is empty. Create the mutex";
			aIsNew=E_HAS_TO_BE_NEW;
		}

		switch (aIsNew)
		{
			case E_HAS_TO_BE_NEW:
			{
				get_unique_name("cv",aBuf, aSize);
				break;
			}
			case E_UNDEF:
			{
				break;
			}
			case E_HAS_EXIST:
			{
				if(_is_empty)
				{
					LOG(ERROR)<<"The buffer is empty.";
					return false;
				}
				break;
			}
		}

		FSignalEvent = ::CreateEvent(NULL, FALSE, FALSE, (char*)aBuf);
		if (FSignalEvent == INVALID_HANDLE_VALUE)
			return false;

		DWORD const _last_error = GetLastError();
		LOG_IF(FATAL,FSignalEvent==INVALID_HANDLE_VALUE)
																	<< "CreateEvent failed. signalEvent:"
																	<< FSignalEvent
																	<< " error:"
																	<< _last_error;

		LOG_IF(DFATAL,_last_error == ERROR_ALREADY_EXISTS && aIsNew==E_HAS_TO_BE_NEW)
																								<< "The signalEvent  "
																								<< (char*)aBuf
																								<< " is exist.";
		VLOG_IF(2,_last_error != ERROR_ALREADY_EXISTS) << "The New signalEvent "
																<< (char*)aBuf
																<< " has been created.";
		VLOG_IF(2,_last_error == ERROR_ALREADY_EXISTS) << "The signalEvent "
																<< (char*)aBuf
																<< " is exist.";

		bool _is_new = _last_error != ERROR_ALREADY_EXISTS;
		switch (aIsNew)
		{
		case E_HAS_TO_BE_NEW:
		{
			if (!_is_new)
			{
				CloseHandle(FSignalEvent);
				return false;
			}
			return true;
			break;
		}
		case E_HAS_EXIST:
		{
			if (_is_new)
			{
				LOG(ERROR)<<"The mutex is not to be a new, but it's not true.";
				CloseHandle(FSignalEvent);
				return false;
			}
			return true;
		}
		case E_UNDEF:
		{
			if (_is_new)
				aIsNew = E_HAS_TO_BE_NEW;
			else
				aIsNew = E_HAS_EXIST;

			break;
		}
		}
		FType=aIsNew;
		FName=(char*)aBuf;
		return true;
	}
bool IMPL_CLASS::MConnect(double aTime)
{
	if (!MIsOpen())
	{
		LOG(ERROR)<<"The shared client is not opened.";
		return false;
	}
	CHECK_NOTNULL(FMyInfo);
	CHECK_NOTNULL(FEv.FEvents);
	CHECK_NOTNULL(FServerInfo);
	NSHARE::CRAII<CMutex> _block(FConnectMutex);
	if (MIsConnected())
	{
		LOG(ERROR)<<"connected already";
		return true;
	}
	VLOG(2) << "Connect "<<FMyInfo->FInfo.FId<<" Time=" << aTime;
	event_info_t _info;
	_info.FEventType = event_info_t::E_CONNECT;
	_info.FConnect.FClientOffset = static_cast<CSharedAllocator::offset_t>(FSharedMemory.MGetAllocator()->MOffset(FMyInfo));
	MInvokeEvent(&_info);

	_info.FEventType = event_info_t::E_NO;
	double  _time=NSHARE::get_time();
	bool _is_not_timeout=false;
	bool _is_try_again=false;
	do
	{
		for(;(NSHARE::get_time()-_time)<aTime && MIsOpen();)
		{
			_is_not_timeout=MWaitForEvent(FEv,&_info, aTime);
			VLOG_IF(2,_is_not_timeout)<<"Event recv="<<_info;
			if(!_is_not_timeout)
			{
				VLOG(2)<<"Event connecting is not received";
				continue;
			}
			if(_info.FEventType == event_info_t::E_CONNECTED)
				break;
			else
			{
				LOG(WARNING)<<"Receive unknown event "<<_info;
			}
		}

		if (_is_not_timeout)
		{
			if(_info.FEventType == event_info_t::E_CONNECTED)
			{
				LOG(INFO)<<"Connected";
				MEventConnected(_info.FIdFrom.MGetId(),_info.FConnect.FClientOffset);
				break;
			}
			else
				CHECK(false);
		}
		else if(MRemoveEvent(&_info,FEv))
		{
			LOG(ERROR)<<"Cannot connect";
			CHECK_EQ(FMyInfo->FInfo.FId.FUniqueID,0);
			break;
		}else
		{
			CHECK_NE(_info.FEventType, event_info_t::E_CONNECTED);
			if(_is_try_again)
			{
				DCHECK_EQ(FMyInfo->FInfo.FId.FUniqueID,0);
				if(FMyInfo->FInfo.FId.FUniqueID==0)
					return false;
				break;
			}
			_is_try_again=true;
			LOG(ERROR)<<"The server is handling the connection";
			 _time=NSHARE::get_time();
			 continue;
		}
	}while(!FIsConnected && MIsOpen());

	return FIsConnected;
}