Changeset 17348 for trunk


Ignore:
Timestamp:
11/23/13 13:01:26 (11 years ago)
Author:
tbretz
Message:
A more efficient handling and more options for the path to the log files.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/Main.h

    r16759 r17348  
    2121{
    2222    using namespace std;
     23    namespace fs = boost::filesystem;
    2324
    2425    void SetupConfiguration(Configuration &conf)
     
    3132            ("host",       var<string>(),                  "Address with which the Dim nameserver can connect to this host (overwites DIM_HOST_NODE environment variable)")
    3233            ("log,l",      var<string>(n), "Name of local log-file")
     34            ("logpath",    var<string>(),  "Absolute path to log-files (default: excutable's directory)")
    3335            ("no-log",     po_switch(),    "Supress log-file")
    3436            ("append-log", po_bool(),      "Append log information to local log-file")
     
    3840            ("exec,e",     vars<string>(), "Execute one or more scrips at startup ('file:N' - start at label N)")
    3941            ("arg:*",      var<string>(),  "Arguments for script execution with --exc, e.g. --arg:ra='12.5436'")
     42            ("home",       var<string>(),  "Path to home directory (used as default for logpath if standard log files not writable)")
    4043            ("quit",       po_switch(),    "Quit after startup");
    4144        ;
     
    4346        conf.AddEnv("dns",  "DIM_DNS_NODE");
    4447        conf.AddEnv("host", "DIM_HOST_NODE");
     48        conf.AddEnv("home", "HOME");
    4549
    4650        conf.AddOptions(config);
     
    8791
    8892        // -----------------------------------------------------------------
    89 
    90         static T shell(conf.GetName().c_str(),
     93        const fs::path program(conf.GetName());
     94
     95        // Split path to program into path and filename
     96        const string prgpath = program.parent_path().string();
     97
     98#if BOOST_VERSION < 104600
     99        const string prgname = program.filename();
     100#else
     101        const string prgname = program.filename().string();
     102#endif
     103
     104        fs::path path = conf.Has("logpath") ? conf.Get<string>("logpath") : "";
     105
     106        // No explicit path given
     107        if (path.empty())
     108        {
     109            path = prgpath;
     110
     111            // default path not accessible
     112            if (access(prgpath.empty() ? "." : prgpath.c_str(), W_OK))
     113            {
     114                path  = conf.Get<string>("home");
     115                path /= ".fact++";
     116            }
     117        }
     118
     119        // Create directories if necessary
     120        fs::create_directories(path);
     121
     122        // -----------------------------------------------------------------
     123
     124        static T shell((path/prgname).string().c_str(),
    91125                       conf.Has("console") ? conf.Get<int>("console")!=1 : conf.Get<bool>("null"));
    92126
     
    108142
    109143        if (conf.Has("log") && !conf.Get<bool>("no-log"))
    110             if (!wout.OpenLogFile(conf.Get<string>("log"), conf.Get<bool>("append-log")))
    111                 win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
     144        {
     145#if BOOST_VERSION < 104600
     146            const fs::path file = fs::path(conf.Get<string>("log")).filename();
     147#else
     148            const fs::path file = fs::path(conf.Get<string>("log")).filename();
     149#endif
     150            if (!wout.OpenLogFile((path/file).string(), conf.Get<bool>("append-log")))
     151                win << kYellow << "WARNING - Couldn't open log-file " << path.string() << ": " << strerror(errno) << endl;
     152        }
    112153
    113154        S io_service(wout);
    114155
    115         const boost::filesystem::path path(conf.GetName());
    116 
    117         const string pname = path.parent_path().string();
    118 #if BOOST_VERSION < 104600
    119         const string fname = path.filename();
    120 #else
    121         const string fname = path.filename().string();
    122 #endif
    123156        const Time now;
    124157        io_service.Write(now, "/----------------------- Program ------------------------");
    125         io_service.Write(now, "| Program:  "PACKAGE_STRING" ("+fname+":"+to_string(getpid())+")");
    126         io_service.Write(now, "| CallPath: "+pname);
     158        io_service.Write(now, "| Program:  "PACKAGE_STRING" ("+prgname+":"+to_string(getpid())+")");
     159        io_service.Write(now, "| CallPath: "+prgpath);
    127160        io_service.Write(now, "| Compiled: "__DATE__" "__TIME__);
    128161        io_service.Write(now, "| Revision: "REVISION);
Note: See TracChangeset for help on using the changeset viewer.