Allow --logfile option

This commit is contained in:
Sebastian Meßmer 2015-10-17 18:31:17 +02:00
parent 7d1af188fd
commit 7b3dcb8260
4 changed files with 35 additions and 7 deletions

View File

@ -26,6 +26,7 @@ using cpputils::make_unique_ref;
using std::cout;
using std::endl;
using std::vector;
using boost::none;
//TODO Support files > 4GB
//TODO Improve parallelity.
@ -54,9 +55,12 @@ void runFilesystem(const ProgramOptions &options) {
//TODO This daemonize causes error messages when initializing CryDevice to get lost.
// However, initializing CryDevice might (?) already spawn threads and we have to do daemonization before that
// because it doesn't fork threads. What to do?
//TODO Setup stdout/stderr as log files so we see the program output when detached
if (!options.foreground()) {
cpputils::daemonize("cryfs");
cpputils::daemonize();
if (options.logFile() == none) {
// Setup logging to syslog.
cpputils::logging::setLogger(spdlog::syslog_logger("cryfs", "cryfs", LOG_PID));
}
}
auto blockStore = make_unique_ref<OnDiskBlockStore>(bf::path(options.baseDir()));
CryDevice device(std::move(config), std::move(blockStore));
@ -72,6 +76,10 @@ int main(int argc, char *argv[]) {
showVersion();
ProgramOptions options = program_options::Parser(argc, argv).parse();
//TODO Test that --logfile parameter works. Should be: file if specified, otherwise stderr if foreground, else syslog.
if (options.logFile() != none) {
cpputils::logging::setLogger(spdlog::create<spdlog::sinks::simple_file_sink<std::mutex>>("cryfs", *options.logFile()));
}
runFilesystem(options);
return 0;
}

View File

@ -1,6 +1,7 @@
#include "Parser.h"
#include "utils.h"
#include <iostream>
#include <boost/optional.hpp>
namespace po = boost::program_options;
using namespace cryfs::program_options;
@ -9,6 +10,8 @@ using std::vector;
using std::cerr;
using std::endl;
using std::string;
using boost::optional;
using boost::none;
Parser::Parser(int argc, char *argv[]) :_options(_argsToVector(argc, argv)) {}
@ -28,8 +31,12 @@ ProgramOptions Parser::parse() const {
string mountDir = vm["mount-dir"].as<string>();
string configFile = vm["config"].as<string>();
bool foreground = vm.count("foreground");
optional<string> logfile = none;
if (vm.count("logfile")) {
logfile = vm["logfile"].as<string>();
}
return ProgramOptions(baseDir, mountDir, configFile, foreground, options.second);
return ProgramOptions(baseDir, mountDir, configFile, foreground, logfile, options.second);
}
po::variables_map Parser::_parseOptionsOrShowHelp(const vector<char*> options) {
@ -61,8 +68,9 @@ void Parser::_addAllowedOptions(po::options_description *desc) {
po::options_description options("Allowed options");
options.add_options()
("help,h", "show help message")
("config,c", po::value<string>()->required(), "Config file")
("config,c", po::value<string>()->required(), "Configuration file")
("foreground,f", "Run CryFS in foreground.")
("logfile", po::value<string>(), "Specify the file to write log messages to. If this is not specified, log messages will go to stdout, or syslog if CryFS is running in the background.")
;
desc->add(options);
}

View File

@ -5,9 +5,12 @@
using namespace cryfs::program_options;
using std::string;
using std::vector;
using boost::optional;
ProgramOptions::ProgramOptions(const string &baseDir, const string &mountDir, const std::string &configFile, bool foreground, const vector<char*> &fuseOptions)
:_baseDir(baseDir), _mountDir(new char[mountDir.size()+1]), _configFile(configFile), _foreground(foreground), _fuseOptions(fuseOptions) {
ProgramOptions::ProgramOptions(const string &baseDir, const string &mountDir, const string &configFile,
bool foreground, const optional<string> &logFile, const vector<char*> &fuseOptions)
:_baseDir(baseDir), _mountDir(new char[mountDir.size()+1]), _configFile(configFile), _foreground(foreground),
_logFile(logFile), _fuseOptions(fuseOptions) {
std::memcpy(_mountDir, mountDir.c_str(), mountDir.size()+1);
// Fuse needs the mountDir passed as first option (first option = position 1, since 0 is the executable name)
ASSERT(_fuseOptions.size() >= 1, "There has to be one parameter at least for the executable name");
@ -34,6 +37,10 @@ bool ProgramOptions::foreground() const {
return _foreground;
}
const optional<string> ProgramOptions::logFile() const {
return _logFile;
}
const vector<char *> &ProgramOptions::fuseOptions() const {
return _fuseOptions;
}

View File

@ -4,18 +4,22 @@
#include <vector>
#include <string>
#include <boost/optional.hpp>
namespace cryfs {
namespace program_options {
class ProgramOptions final {
public:
ProgramOptions(const std::string &baseDir, const std::string &mountDir, const std::string &configFile, bool foreground, const std::vector<char *> &fuseOptions);
ProgramOptions(const std::string &baseDir, const std::string &mountDir, const std::string &configFile,
bool foreground, const boost::optional<std::string> &logFile,
const std::vector<char *> &fuseOptions);
~ProgramOptions();
const std::string &baseDir() const;
std::string mountDir() const;
const std::string &configFile() const;
bool foreground() const;
const boost::optional<std::string> logFile() const;
const std::vector<char *> &fuseOptions() const;
private:
@ -23,6 +27,7 @@ namespace cryfs {
char *_mountDir;
std::string _configFile;
bool _foreground;
boost::optional<std::string> _logFile;
std::vector<char *> _fuseOptions;
};
}