All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines
Public Member Functions | Static Public Member Functions | Protected Member Functions
GpCoreTools::CoreApplication Class Reference

Brief description of class still missing. More...

#include <CoreApplication.h>

Inheritance diagram for GpCoreTools::CoreApplication:
GpCoreTools::DaemonApplication CubeDaemonApplication GpsDaemonApplication

List of all members.

Public Member Functions

const char * applicationName () const
virtual void close ()
 CoreApplication (int &argc, char **argv, ApplicationHelp *(*help)(), bool reportBugs=true)
void initInternalDebugger ()
void showHelp (int &i, int argc, char **argv, ApplicationHelp *(*help)())
int terminalCols () const
int terminalRows () const
virtual ~CoreApplication ()

Static Public Member Functions

static std::string authors ()
static std::string backTrace ()
static std::string backTraceBug ()
static bool checkOptionArg (int &i, int argc, char **argv, bool mandatory=true)
static void exit (int exitCode)
static std::string getStdin ()
static CoreApplicationinstance ()
static void osSignal (int sigNum)
static std::string version ()
static std::string version (const char *item)

Protected Member Functions

void setHelp (ApplicationHelp *h)

Detailed Description

Brief description of class still missing.

Full description of class still missing


Constructor & Destructor Documentation

GpCoreTools::CoreApplication::CoreApplication ( int &  argc,
char **  argv,
ApplicationHelp *(*)()  help,
bool  reportBugs = true 
)

Description of constructor still missing

References checkOptionArg(), exit(), help(), GpCoreTools::PThread::init(), initInternalDebugger(), GpCoreTools::Log::setVerbosity(), showHelp(), version(), and GpCoreTools::Log::write().

{
  PThread::init();
  _applicationName=argv[0];
  int index=_applicationName.find_last_of("/");
  if(index>-1) {
    _applicationName=_applicationName.substr(index+1, _applicationName.length()-index);
  }
  srand(time(NULL)); // init random generator

  if(!_self) {
    _self=this;
  } else {
    Log::write(0, "error: only one instance of ApplicationCore is allowed.");
    abort();
  }
  // Size of terminal
  _terminalCols=0;
  _terminalRows=0;

  // Check arguments
  int i, j=1;
  for(i=1; i<argc; i++) {
    const char * arg=argv[i];
    if(help && (strcmp(arg, "--help")==0 || strcmp(arg, "-help")==0 || strcmp(arg, "-h")==0)) {
      showHelp(i, argc, argv, help);
      ::exit(0);
    } else if(strcmp(arg, "--verbosity")==0 || strcmp(arg, "-verbosity")==0 || strcmp(arg, "-v")==0) {
      checkOptionArg(i, argc, argv, true);
      Log::setVerbosity(atoi(argv[i]));
    } else if(strcmp(arg, "--nobugreport")==0 || strcmp(arg, "-nobugreport")==0) {
      reportBugs=false;
    } else if(strcmp(arg, "--version")==0 || strcmp(arg, "-version")==0) {
      printf("Versions:\n%s", version().data());
      ::exit(0);
    } else if(strcmp(arg, "--app-version")==0 || strcmp(arg, "-app-version")==0) {
      printf("%s %s\n", _applicationName.data(), version(_applicationName.data()).data());
      ::exit(0);
    } else {
      argv[j++]=argv[i];
    }
  }
  if(j<argc) {
    argv[j]=0;
    argc=j;
  }

  _reportBugs=reportBugs;
  initInternalDebugger();
}

Description of destructor still missing

{
}

Member Function Documentation

const char* GpCoreTools::CoreApplication::applicationName ( ) const [inline]
std::string GpCoreTools::CoreApplication::authors ( ) [static]

Returns the copyright message

References applicationName(), instance(), and GpCoreTools::PackageInfo::list().

Referenced by GpCoreTools::ApplicationHelp::exec().

{
  for(std::list<PackageInfo>::iterator it=PackageInfo::list()->begin();it!=PackageInfo::list()->end(); it++ ) {
    if(it->package()==CoreApplication::instance()->applicationName()) {
      return it->authors();
    }
  }
  return "";
}
static std::string GpCoreTools::CoreApplication::backTrace ( ) [static]
static std::string GpCoreTools::CoreApplication::backTraceBug ( ) [static]
bool GpCoreTools::CoreApplication::checkOptionArg ( int &  i,
int  argc,
char **  argv,
bool  mandatory = true 
) [static]

Check if option argument at i is available. If not and if mandatory is true, the application exits with error code 2. Else, it returns true if an argument is available.

This function can be used even without a CoreApplication running.

References exit(), UNUSED, and GpCoreTools::Log::write().

Referenced by CoreApplication(), GpCoreTools::DaemonApplication::DaemonApplication(), and showHelp().

{
  i++;
  if(i<argc) {
    if(argv[i][0]=='-' ) {
      char * ptr=0;
      float f=strtod(argv[i], &ptr);
      UNUSED(f);
      if(ptr>argv[i]) return true;
    } else return true;
  }
  if(mandatory) {
    Log::write(0, "error: option %s requires an argument, see -help for details.\n", argv[i-1]);
    CoreApplication::exit(2);
  }
  return false;
}
virtual void GpCoreTools::CoreApplication::close ( ) [inline, virtual]
void GpCoreTools::CoreApplication::exit ( int  exitCode) [static]
std::string GpCoreTools::CoreApplication::getStdin ( ) [static]

References GpCoreTools::File::readLine(), and str.

{
  std::string str;
  int n=256;
  char * buf=new char[n];
  while(true) {
    if(File::readLine(buf, n, stdin)) {
      if(strcmp(buf,"END\n")==0) break;
      str+=buf;
    }
  }
  delete [] buf;
  return str;
}

In case of fork, recall this function in the child.

References osSignal().

Referenced by CoreApplication(), and GpCoreTools::DaemonApplication::fork().

{
  if(_reportBugs) {
    signal (SIGABRT, osSignal);
    signal (SIGSEGV, osSignal);
    signal (SIGPIPE, osSignal);
    signal (SIGILL, osSignal);
  }
}
void GpCoreTools::CoreApplication::osSignal ( int  sig) [static]

In case an exception occurs during normal execution

References GpCoreTools::Trace::backTrace(), exit(), GpCoreTools::PThread::lockThreads(), GpCoreTools::PThread::stacks(), GpCoreTools::PThread::unlockThreads(), GpCoreTools::Log::write(), and GpCoreTools::Log::writeNoStamp().

Referenced by initInternalDebugger().

{
  static pthread_mutex_t lock;
  if(pthread_mutex_trylock(&lock)!=0) {
    signal(sig, SIG_DFL);
    sleep(10000);
    fprintf(stderr, "EXCEPTION occured while processing exceptions.\n");
    fprintf(stderr, "No backtrace information can be provided\n");
    fprintf(stderr, "Eventually kill remaining instances:\n");
    fprintf(stderr, "  ps\n" );
    fprintf(stderr, "  pkill <application name>\n" );
    exit(2);
  }

  Log::write(0, "received SEGV or ABRT signal\n");
#ifdef TRACE_ENABLED
  PThread::lockThreads();
  std::list< std::vector<const TraceStamp *> * > sList=PThread::stacks();
  std::list< std::vector<const TraceStamp *> * >::iterator it;
  for(it=sList.begin(); it!=sList.end(); it++) {
    Log::writeNoStamp(0, "---- thread\n");
    Log::writeNoStamp(0, Trace::backTrace(*it).data());
  }
  PThread::unlockThreads();
#endif
  signal (SIGABRT, 0);
  abort();
}

References GpCoreTools::ApplicationHelp::addGroup(), GpCoreTools::ApplicationHelp::addOption(), GpCoreTools::compareStrings(), GpCoreTools::ApplicationHelp::sections(), and str.

Referenced by showHelp().

{
  h->addGroup("Generic", "generic");
  std::list<const char *> sections=h->sections();
  sections.sort(compareStrings);
  std::string str="Show help about options. SECTION may be empty or: all, html";
  for(std::list<const char *>::iterator it=sections.begin();it!=sections.end();it++) {
    str+= ", ";
    str+=*it;
  }
  h->addOption("-help <SECTION>", str.data());
  h->addOption("-verbosity <LEVEL>", "Select level of output message (default=0).");
  h->addOption("-version", "Show version information");
  h->addOption("-app-version", "Show short version information");
  h->addOption("-nobugreport", "Do not generate bug report in case of error");
}
void GpCoreTools::CoreApplication::showHelp ( int &  i,
int  argc,
char **  argv,
ApplicationHelp *(*)()  help 
)

References checkOptionArg(), GpCoreTools::ApplicationHelp::exec(), help(), and setHelp().

Referenced by CoreApplication().

{
  ApplicationHelp * h=help();
  setHelp(h);
  if(checkOptionArg(i, argc, argv, false)) {
    h->exec(argv[i]);
  } else {
    h->exec();
  }
  delete h;
}
{
  if(_terminalCols==0) {
    terminalSize();
  }
  return _terminalCols;
}
{
  if(_terminalRows==0) {
    terminalSize();
  }
  return _terminalRows;
}
std::string GpCoreTools::CoreApplication::version ( ) [static]

Returns a complete description of all versions

References GpCoreTools::PackageInfo::list(), and str.

Referenced by CoreApplication().

{
  char buf[256];
  std::string str;
  for(std::list<PackageInfo>::iterator it=PackageInfo::list()->begin();it!=PackageInfo::list()->end(); it++ ) {
    sprintf(buf, "  %-20s%s", +it->package().data(), it->version().data());
    str+=buf;
    if(it->distribution().empty()) {
      str+="\n";
    } else {
      str+=" from ";
      str+=it->distribution();
      str+="\n";
    }
    str+="      (built on ";
    str+=it->versionTime();
    str+=")\n";
  }
  return str;
}
std::string GpCoreTools::CoreApplication::version ( const char *  item) [static]

Returns the version of item

References GpCoreTools::PackageInfo::list().

{
  for(std::list<PackageInfo>::iterator it=PackageInfo::list()->begin();it!=PackageInfo::list()->end(); it++ ) {
    if(it->package()==item) {
      return it->version();
    }
  }
  return "";
}

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines