From 9ccf159f811534553d7b35c897af23e22ae367f0 Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 8 Feb 2013 11:43:51 +0000 Subject: [PATCH] Refactor FG_HOME init, so we can log sooner. Logging to file now happens earlier, so some useful early output is captured. --- src/Main/bootstrap.cxx | 11 +++-------- src/Main/fg_init.cxx | 21 ++++++++++++--------- src/Main/fg_init.hxx | 1 + src/Main/main.cxx | 38 ++++++++++++++++++++++++-------------- src/Main/main.hxx | 1 - src/Main/options.cxx | 2 ++ 6 files changed, 42 insertions(+), 32 deletions(-) diff --git a/src/Main/bootstrap.cxx b/src/Main/bootstrap.cxx index 4eff67b5b..a4425c76a 100644 --- a/src/Main/bootstrap.cxx +++ b/src/Main/bootstrap.cxx @@ -163,26 +163,21 @@ static void fg_terminate() { int _bootstrap_OSInit; // Main entry point; catch any exceptions that have made it this far. -int main ( int argc, char **argv ) { +int main ( int argc, char **argv ) +{ #if _MSC_VER // Don't show blocking "no disk in drive" error messages on Windows 7, // silently return errors to application instead. // See Microsoft MSDN #ms680621: "GUI apps should specify SEM_NOOPENFILEERRORBOX" SetErrorMode(SEM_NOOPENFILEERRORBOX); - // Windows has no $HOME aka %HOME%, so we have to construct the full path. - homedir = ::getenv("APPDATA"); - homedir.append("\\flightgear.org"); - hostname = ::getenv( "COMPUTERNAME" ); #else // Unix(alike) systems char _hostname[256]; gethostname(_hostname, 256); hostname = _hostname; - - homedir = ::getenv( "HOME" ); - + signal(SIGPIPE, SIG_IGN); #endif diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx index 755484f34..5e91d5ec9 100644 --- a/src/Main/fg_init.cxx +++ b/src/Main/fg_init.cxx @@ -390,23 +390,26 @@ static SGPath platformDefaultDataPath() #else static SGPath platformDefaultDataPath() { - SGPath config( homedir ); + SGPath config( getenv("HOME") ); config.append( ".fgfs" ); return config; } #endif +void fgInitHome() +{ + SGPath dataPath = platformDefaultDataPath(); + const char *fg_home = getenv("FG_HOME"); + if (fg_home) + dataPath = fg_home; + + globals->set_fg_home(dataPath.c_str()); +} + // Read in configuration (file and command line) bool fgInitConfig ( int argc, char **argv ) { - SGPath dataPath = platformDefaultDataPath(); - - const char *fg_home = getenv("FG_HOME"); - if (fg_home) - dataPath = fg_home; - - globals->set_fg_home(dataPath.c_str()); - + SGPath dataPath = globals->get_fg_home(); simgear::Dir exportDir(simgear::Dir(dataPath).file("Export")); if (!exportDir.exists()) { exportDir.create(0777); diff --git a/src/Main/fg_init.hxx b/src/Main/fg_init.hxx index f5215bb72..f79a6f531 100644 --- a/src/Main/fg_init.hxx +++ b/src/Main/fg_init.hxx @@ -34,6 +34,7 @@ class SGPath; // Return the current base package version std::string fgBasePackageVersion(); +void fgInitHome(); // Read in configuration (file and command line) bool fgInitConfig ( int argc, char **argv ); diff --git a/src/Main/main.cxx b/src/Main/main.cxx index e1b98d3c3..1f885e7c2 100644 --- a/src/Main/main.cxx +++ b/src/Main/main.cxx @@ -273,12 +273,34 @@ static void ATIScreenSizeHack() globals->get_renderer()->addCamera(hackCam, false); } + +static void logToFile() +{ + SGPath logPath = globals->get_fg_home(); + logPath.append("fgfs.log"); + if (logPath.exists()) { + SGPath prevLogPath = globals->get_fg_home(); + prevLogPath.append("fgfs_0.log"); + logPath.rename(prevLogPath); + // bit strange, we need to restore the correct value of logPath now + logPath = globals->get_fg_home(); + logPath.append("fgfs.log"); + } + sglog().logToFile(logPath, SG_ALL, SG_INFO); +} + // Main top level initialization int fgMainInit( int argc, char **argv ) { // set default log levels sglog().setLogLevels( SG_ALL, SG_ALERT ); + globals = new FGGlobals; + fgInitHome(); + + // now home is initialised, we can log to a file inside it + logToFile(); + string version; #ifdef FLIGHTGEAR_VERSION version = FLIGHTGEAR_VERSION; @@ -292,8 +314,8 @@ int fgMainInit( int argc, char **argv ) { // Allocate global data structures. This needs to happen before // we parse command line options - globals = new FGGlobals; - + + // seed the random number generator sg_srandom_time(); @@ -313,18 +335,6 @@ int fgMainInit( int argc, char **argv ) { SG_LOG( SG_GENERAL, SG_ALERT, "Config option parsing failed ..." ); exit(-1); } - - SGPath logPath = globals->get_fg_home(); - logPath.append("fgfs.log"); - if (logPath.exists()) { - SGPath prevLogPath = globals->get_fg_home(); - prevLogPath.append("fgfs_0.log"); - logPath.rename(prevLogPath); - // bit strange, we need to restore the correct value of logPath now - logPath = globals->get_fg_home(); - logPath.append("fgfs.log"); - } - sglog().logToFile(logPath, SG_ALL, SG_INFO); // Initialize the Window/Graphics environment. fgOSInit(&argc, argv); diff --git a/src/Main/main.hxx b/src/Main/main.hxx index 145feacf1..7cea8baaf 100644 --- a/src/Main/main.hxx +++ b/src/Main/main.hxx @@ -23,7 +23,6 @@ int fgMainInit( int argc, char **argv ); -extern std::string homedir; extern std::string hostname; #endif diff --git a/src/Main/options.cxx b/src/Main/options.cxx index 718743144..365eb4158 100644 --- a/src/Main/options.cxx +++ b/src/Main/options.cxx @@ -1745,6 +1745,8 @@ void Options::init(int argc, char **argv, const SGPath& appDataPath) // then config files SGPath config; + std::string homedir(getenv("HOME")); + if( homedir.size() && hostname.size() ) { // Check for ~/.fgfsrc.hostname config.set(homedir);