summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--project2/cgi/cgiCommon.cpp3
-rw-r--r--project2/cgi/cgiEnvironment.cpp6
-rw-r--r--project2/cgi/cgiEnvironment.h1
-rw-r--r--project2/cgi/cgiSimpleRouter.cpp1
-rw-r--r--project2/cgi/testCgi.cpp2
-rw-r--r--project2/common/consoleLog.cpp15
-rw-r--r--project2/common/environment.cpp55
-rw-r--r--project2/common/environment.h10
-rw-r--r--project2/common/genLoader.h4
-rw-r--r--project2/common/logger.cpp30
-rw-r--r--project2/common/logger.h41
-rw-r--r--project2/common/optionsSource.cpp37
-rw-r--r--project2/common/optionsSource.h11
-rw-r--r--project2/common/syslogLog.cpp21
-rw-r--r--project2/console/claOptions.cpp125
-rw-r--r--project2/console/claOptions.h19
-rw-r--r--project2/console/consoleEnvironment.cpp15
-rw-r--r--project2/console/consoleEnvironment.h7
-rw-r--r--project2/console/p2consoleMain.cpp6
-rw-r--r--project2/files/fileLog.cpp19
-rw-r--r--project2/files/optionsSource.cpp2
21 files changed, 233 insertions, 197 deletions
diff --git a/project2/cgi/cgiCommon.cpp b/project2/cgi/cgiCommon.cpp
index 2edbdef..a4ed8c4 100644
--- a/project2/cgi/cgiCommon.cpp
+++ b/project2/cgi/cgiCommon.cpp
@@ -1,5 +1,6 @@
#include <pch.hpp>
#include "cgiCommon.h"
+#include "optionsSource.h"
#include "logger.h"
#include <glibmm/exception.h>
#include <cgicc/CgiEnvironment.h>
@@ -42,7 +43,7 @@ cgiServe(cgicc::CgiInput * i, CgiEnvironment * env, std::ostream & IO, const Cgi
try {
cgicc::Cgicc cgi(i);
env->setCGICC(&cgi, e);
- env->init();
+ OptionsSource::loadSources();
CgiApplicationEngine app(env, IO);
Plugable::onAllComponents(boost::bind(&ComponentLoader::onBefore, _1));
diff --git a/project2/cgi/cgiEnvironment.cpp b/project2/cgi/cgiEnvironment.cpp
index 0276d57..d2ecf77 100644
--- a/project2/cgi/cgiEnvironment.cpp
+++ b/project2/cgi/cgiEnvironment.cpp
@@ -101,12 +101,6 @@ CgiEnvironment::getRedirectURL() const
}
std::string
-CgiEnvironment::getScriptName() const
-{
- return cgi->getEnvironment().getScriptName();
-}
-
-std::string
CgiEnvironment::getServerName() const
{
return cgi->getEnvironment().getServerName();
diff --git a/project2/cgi/cgiEnvironment.h b/project2/cgi/cgiEnvironment.h
index d1d51c0..e9de59d 100644
--- a/project2/cgi/cgiEnvironment.h
+++ b/project2/cgi/cgiEnvironment.h
@@ -40,7 +40,6 @@ class CgiEnvironment : public Environment {
Glib::ustring getParamQuery(const std::string & idx) const;
void applyAllParameters(const boost::function<void (const std::string &, const std::string &)> &) const;
std::string getServerName() const;
- std::string getScriptName() const;
std::string getRedirectURL() const;
std::string getRequestMethod() const;
std::string getAccept() const;
diff --git a/project2/cgi/cgiSimpleRouter.cpp b/project2/cgi/cgiSimpleRouter.cpp
index 2359c4e..cd14c53 100644
--- a/project2/cgi/cgiSimpleRouter.cpp
+++ b/project2/cgi/cgiSimpleRouter.cpp
@@ -1,4 +1,5 @@
#include "cgiEnvironment.h"
+#include <boost/filesystem/path.hpp>
#include "scriptLoader.h"
#include "presenter.h"
diff --git a/project2/cgi/testCgi.cpp b/project2/cgi/testCgi.cpp
index cd42873..9ef0079 100644
--- a/project2/cgi/testCgi.cpp
+++ b/project2/cgi/testCgi.cpp
@@ -24,7 +24,7 @@ class TestInput : public cgicc::CgiInput, public CgiEnvInput {
typedef std::map<std::string, StrPtr> OptStore;
TestInput(int argc, char ** argv)
{
- env.init();
+ OptionsSource::loadSources();
FileOptions fo(".testCgi.settings");
fo.loadInto(TestConfigConsumer());
if (argc > 1) {
diff --git a/project2/common/consoleLog.cpp b/project2/common/consoleLog.cpp
index 9c5c2d8..de45245 100644
--- a/project2/common/consoleLog.cpp
+++ b/project2/common/consoleLog.cpp
@@ -1,10 +1,10 @@
#include "logger.h"
+#include "options.h"
/// Logger that writes to the console
class ConsoleLogDriver : public LogDriverBase {
public:
- ConsoleLogDriver(int level, const std::string &) :
- LogDriverBase(level)
+ ConsoleLogDriver()
{
}
void message(int priority, const char * msg) const
@@ -14,7 +14,16 @@ class ConsoleLogDriver : public LogDriverBase {
fflush(stderr);
}
}
+ INITOPTIONS;
+ static int level;
};
-DECLARE_GENERIC_LOADER("console", LogDriverLoader, ConsoleLogDriver);
+DECLARE_OPTIONS(ConsoleLogDriver, "Console log options")
+("common.consolelogLevel", Options::value(&level, LOG_WARNING),
+ "Log to console with level <arg> (default WARNING)")("c")
+END_OPTIONS(ConsoleLogDriver);
+
+int ConsoleLogDriver::level;
+
+DECLARE_LOGGER_LOADER("console", ConsoleLogDriver);
diff --git a/project2/common/environment.cpp b/project2/common/environment.cpp
index 7765a06..0b9a1fe 100644
--- a/project2/common/environment.cpp
+++ b/project2/common/environment.cpp
@@ -11,14 +11,8 @@
#include <boost/bind.hpp>
const Environment * Environment::currentEnv(NULL);
-#include <boost/preprocessor/control/if.hpp>
-#include <boost/preprocessor/facilities/expand.hpp>
DECLARE_OPTIONS(Environment, "Project2 Common options")
-("common.syslogLevel", Options::value(&slLevel, -1),
- "Log to syslog with level <arg> (default OFF)")("s")
-("common.consolelogLevel", Options::value(&clLevel, LOG_WARNING),
- "Log to console with level <arg> (default WARNING)")("c")
("common.datasourceRoot", Options::value(&datasourceRoot, "datasources"),
"The folder in which to find datasource definitions")
("common.namespace", Options::value(&scriptNamespace, "http://project2.randomdan.homeip.net"),
@@ -33,59 +27,10 @@ time_t Environment::sessionTimeOut;
std::string Environment::scriptNamespacePrefix;
std::string Environment::scriptNamespace;
std::string Environment::datasourceRoot;
-int Environment::clLevel;
-int Environment::slLevel;
Environment::Environment()
{
currentEnv = this;
- typedef std::map<std::string, boost::shared_ptr<OptionsSourceLoader> > ConfigParsersMap;
- BOOST_FOREACH(const ConfigParsersMap::value_type & cp, *Plugable::objLoaders<OptionsSourceLoader>()) {
- configs.push_back(cp.second->create());
- }
-}
-
-typedef std::vector<const Options *> AllOptions;
-
-class DefaultConfigConsumer : public ConfigConsumer {
- public:
- void operator()(const Glib::ustring & n, const Glib::ustring & p, const Glib::ustring & v) const {
- Plugable::onAll<Options>(boost::bind(&Options::consume, _1, n, p, v));
- }
- const Options::Option * get(const Glib::ustring & n) const {
- const Options::Option * rtn = NULL;
- Plugable::onAll<Options>([n,&rtn](const Options * os) {
- const Options::Option * o = os->find(n);
- if (o) {
- rtn = o;
- }
- });
- return rtn;
- }
-};
-
-void
-Environment::init()
-{
- if (std::find_if(configs.begin(), configs.end(), boost::bind(&OptionsSource::needReload, _1)) != configs.end()) {
- DefaultConfigConsumer dcc;
-
- Plugable::onAll<Options>(boost::bind(&Options::reset, _1));
-
- BOOST_FOREACH(const ConfigsMap::value_type & c, configs) {
- c->loadInto(dcc);
- }
- Plugable::onAllComponents(boost::bind(&ComponentLoader::onConfigLoad, _1));
-
- Logger()->clear();
- if (clLevel >= 0) {
- Logger()->addLogger(LogDriverLoader::createNew("console", clLevel, std::string()));
- }
- if (slLevel >= 0) {
- Logger()->addLogger(LogDriverLoader::createNew("syslog", slLevel, getScriptName()));
- }
- Logger()->message(LOG_DEBUG, "Loaded configuration");
- }
}
Environment::~Environment()
diff --git a/project2/common/environment.h b/project2/common/environment.h
index a7f8f4b..7b5e976 100644
--- a/project2/common/environment.h
+++ b/project2/common/environment.h
@@ -3,10 +3,7 @@
#include <string>
#include <glibmm/ustring.h>
-#include <boost/function.hpp>
#include <boost/tuple/tuple.hpp>
-#include <boost/filesystem/path.hpp>
-#include "optionsSource.h"
#include "options.h"
#include "exceptions.h"
#include "scripts.h"
@@ -18,14 +15,11 @@ class Environment {
Environment();
virtual ~Environment() = 0;
- void init();
-
static const Environment * getCurrent();
virtual Glib::ustring getParamQuery(const std::string & idx) const = 0;
virtual std::string getServerName() const = 0;
- virtual std::string getScriptName() const = 0;
virtual const Glib::ustring & platform() const = 0;
ScriptReaderPtr resolveScript(const std::string & group, const std::string & name, bool ii) const;
@@ -34,15 +28,11 @@ class Environment {
private:
static const Environment * currentEnv;
- typedef std::vector<OptionsSourcePtr> ConfigsMap;
- ConfigsMap configs;
typedef boost::tuple<const std::string, const std::string> ScriptKey;
typedef std::map<ScriptKey, ScriptReaderPtr> ScriptCache;
mutable ScriptCache scriptCache;
public:
- static int clLevel;
- static int slLevel;
static std::string datasourceRoot;
static std::string scriptNamespace;
static std::string scriptNamespacePrefix;
diff --git a/project2/common/genLoader.h b/project2/common/genLoader.h
index e8fb683..4208f40 100644
--- a/project2/common/genLoader.h
+++ b/project2/common/genLoader.h
@@ -7,8 +7,8 @@
template <class Impl, typename... Params>
class GenLoader : public ComponentLoader {
public:
- template <class T>
- class For : public GenLoader<Impl, Params...> {
+ template <class T, class BaseLoader = GenLoader<Impl, Params...>>
+ class For : public BaseLoader {
public:
inline Impl * create(const Params & ... p) const
{
diff --git a/project2/common/logger.cpp b/project2/common/logger.cpp
index 8140155..5ce82f9 100644
--- a/project2/common/logger.cpp
+++ b/project2/common/logger.cpp
@@ -15,23 +15,23 @@ Log::Log() :
Log::~Log()
{
- logs.clear();
-}
-
-int
-Log::addLogger(LogDriverBasePtr log)
-{
- logs[++nextId] = log;
- if (log->level > lowestLevel) {
- lowestLevel = log->level;
- }
- return nextId;
}
void
-Log::clear()
+Log::setLoggerAt(LogDriverLoader * ldr, int level)
{
- logs.clear();
+ if (level == -1) {
+ logs.erase(ldr);
+ }
+ else {
+ logs[ldr] = ldr->create();
+ }
+ lowestLevel = -1;
+ BOOST_FOREACH(const auto & log, *Plugable::objLoaders<LogDriverLoader>()) {
+ if (log.second->loggerLevel() > lowestLevel) {
+ lowestLevel = log.second->loggerLevel();
+ }
+ }
}
const char *
@@ -46,7 +46,6 @@ Log::priorityName(int priority)
return name;
}
-
void
Log::message(int priority, const char * msg) const
{
@@ -90,8 +89,7 @@ Logger::operator->() const
return &log;
}
-LogDriverBase::LogDriverBase(int l) :
- level(l)
+LogDriverBase::LogDriverBase()
{
}
diff --git a/project2/common/logger.h b/project2/common/logger.h
index 5f2ff2a..c49d40e 100644
--- a/project2/common/logger.h
+++ b/project2/common/logger.h
@@ -13,24 +13,26 @@
/// Base class for classes providing a logging facility
class LogDriverBase : public virtual IntrusivePtrBase {
public:
- LogDriverBase(int level);
+ LogDriverBase();
virtual ~LogDriverBase();
virtual void message(int priority, const char * msg) const = 0;
- const int level;
};
+class LogDriverLoader : public GenLoader<LogDriverBase> {
+ public:
+ virtual int loggerLevel() const = 0;
+};
class Log {
public:
typedef boost::intrusive_ptr<LogDriverBase> LogDriverBasePtr;
- typedef std::map<int, LogDriverBasePtr> LogDrivers;
+ typedef std::map<LogDriverLoader *, LogDriverBasePtr> LogDrivers;
Log();
~Log();
- int addLogger(LogDriverBasePtr);
- void clear();
+ void setLoggerAt(LogDriverLoader *, int level);
void message(int priority, const char * msg) const;
void messagef(int priority, const char * msgfmt, ...) const __attribute__ ((format (printf, 3, 4)));
@@ -63,7 +65,34 @@ class Logger {
static Log log;
};
-typedef GenLoader<LogDriverBase, int, std::string> LogDriverLoader;
+template<typename LoggerType>
+class LogDriverLoaderImpl : public LogDriverLoader::For<LoggerType, LogDriverLoader> {
+ public:
+ LogDriverLoaderImpl() :
+ level(LoggerType::level)
+ {
+ }
+ virtual void onConfigLoad() override {
+ Logger()->setLoggerAt(this, level);
+ if (level == -1) {
+ instance.reset();
+ }
+ }
+ inline LoggerType * create() const override {
+ if (!instance) {
+ instance = new LoggerType();
+ }
+ return instance.get();
+ }
+ int loggerLevel() const override {
+ return level;
+ }
+ const int & level;
+ private:
+ mutable boost::intrusive_ptr<LoggerType> instance;
+};
+#define DECLARE_LOGGER_LOADER(N, T) \
+ DECLARE_CUSTOM_COMPONENT_LOADER(N, T, LogDriverLoaderImpl<T>, LogDriverLoader);
#endif
diff --git a/project2/common/optionsSource.cpp b/project2/common/optionsSource.cpp
new file mode 100644
index 0000000..ed40df9
--- /dev/null
+++ b/project2/common/optionsSource.cpp
@@ -0,0 +1,37 @@
+#include "optionsSource.h"
+#include "logger.h"
+
+class DefaultConfigConsumer : public ConfigConsumer {
+ public:
+ void operator()(const Glib::ustring & n, const Glib::ustring & p, const Glib::ustring & v) const {
+ Plugable::onAll<Options>(boost::bind(&Options::consume, _1, n, p, v));
+ }
+ const Options::Option * get(const Glib::ustring & n) const {
+ const Options::Option * rtn = NULL;
+ Plugable::onAll<Options>([n,&rtn](const Options * os) {
+ const Options::Option * o = os->find(n);
+ if (o) {
+ rtn = o;
+ }
+ });
+ return rtn;
+ }
+};
+
+void
+OptionsSource::loadSources()
+{
+ const auto & configs = Plugable::ComponentType<OptionsSource>::components();
+ if (std::find_if(configs->begin(), configs->end(), boost::bind(&OptionsSource::needReload, _1)) != configs->end()) {
+ Plugable::onAll<Options>(boost::bind(&Options::reset, _1));
+
+ DefaultConfigConsumer dcc;
+ BOOST_FOREACH(const auto & c, *configs) {
+ c->loadInto(dcc);
+ }
+ Plugable::onAllComponents(boost::bind(&ComponentLoader::onConfigLoad, _1));
+
+ Logger()->message(LOG_DEBUG, "Loaded configuration");
+ }
+}
+
diff --git a/project2/common/optionsSource.h b/project2/common/optionsSource.h
index 208e5ff..49bb876 100644
--- a/project2/common/optionsSource.h
+++ b/project2/common/optionsSource.h
@@ -14,15 +14,20 @@ class ConfigConsumer {
};
/// Base class of things that load options
-class OptionsSource : public IntrusivePtrBase {
+class OptionsSource {
public:
virtual void loadInto(const ConfigConsumer &) const = 0;
virtual bool needReload() const = 0;
+
+ static void loadSources();
};
typedef boost::intrusive_ptr<OptionsSource> OptionsSourcePtr;
-/// Base class to implement presenter modules
-typedef GenLoader<OptionsSource> OptionsSourceLoader;
+#define DECLARE_OPTIONSSOURCE(Id, Inst) \
+ static void init_optionsSource_##Type() __attribute__ ((constructor(200))); \
+ static void init_optionsSource_##Type() { Plugable::newLoader<OptionsSource, OptionsSource>(Id, Inst); } \
+ static void kill_optionsSource_##Type() __attribute__ ((destructor(200))); \
+ static void kill_optionsSource_##Type() { Plugable::removeLoader<OptionsSource, OptionsSource>(Id); }
#endif
diff --git a/project2/common/syslogLog.cpp b/project2/common/syslogLog.cpp
index 3c8bbb0..8e58326 100644
--- a/project2/common/syslogLog.cpp
+++ b/project2/common/syslogLog.cpp
@@ -1,10 +1,10 @@
#include "logger.h"
+#include "options.h"
/// Logger that writes to syslog
class SyslogLogDriver : public LogDriverBase {
public:
- SyslogLogDriver(int level, const std::string & ident) :
- LogDriverBase(level)
+ SyslogLogDriver()
{
openlog(ident.c_str(), 0, LOG_USER);
}
@@ -18,7 +18,22 @@ class SyslogLogDriver : public LogDriverBase {
syslog(priority, "%s", msg);
}
}
+
+ INITOPTIONS;
+ static int level;
+ private:
+ static std::string ident;
};
-DECLARE_GENERIC_LOADER("syslog", LogDriverLoader, SyslogLogDriver);
+DECLARE_OPTIONS(SyslogLogDriver, "Syslog options")
+("common.syslog.level", Options::value(&level, -1),
+ "Log to syslog with level <arg> (default OFF)")("s")
+("common.syslog.ident", Options::value(&ident, "project2"),
+ "Log to syslog with ident <arg> (default 'project2')")
+END_OPTIONS(SyslogLogDriver);
+
+int SyslogLogDriver::level;
+std::string SyslogLogDriver::ident;
+
+DECLARE_LOGGER_LOADER("syslog", SyslogLogDriver);
diff --git a/project2/console/claOptions.cpp b/project2/console/claOptions.cpp
index 4660134..79be7dc 100644
--- a/project2/console/claOptions.cpp
+++ b/project2/console/claOptions.cpp
@@ -3,33 +3,54 @@
#include <boost/foreach.hpp>
#include "../common/optionsSource.h"
#include "../common/exceptions.h"
-#include "consoleEnvironment.h"
+#include "claOptions.h"
StaticMessageException(InvalidScriptName, "Script name should be group/name");
SimpleMessageException(ArgumentRequired);
SimpleMessageException(UnknownOption);
-class CommandLineArguments : public OptionsSource {
- public:
- CommandLineArguments() :
- loadedAt(0) {
- }
- void loadInto(const ConfigConsumer & consume) const {
- int argc = ConsoleEnvironment::argc;
- char ** argv = ConsoleEnvironment::argv;
- bool moreopts = true;
+CommandLineArguments::CommandLineArguments(int c, const char * const * v) :
+ argc(c),
+ argv(v),
+ loadedAt(0)
+{
+}
+
+void
+CommandLineArguments::loadInto(const ConfigConsumer & consume) const {
+ bool moreopts = true;
- for (int x = 1; x < argc; x += 1) {
- if (moreopts && strcmp(argv[x], "--") == 0) {
- moreopts = false;
+ for (int x = 1; x < argc; x += 1) {
+ if (moreopts && strcmp(argv[x], "--") == 0) {
+ moreopts = false;
+ }
+ else if (moreopts && strncmp(argv[x], "--", 2) == 0) {
+ Glib::ustring name(argv[x] + 2);
+ const Options::Option * o = consume.get(name);
+ if (!o) {
+ throw UnknownOption(name);
+ }
+ if (o->paramRequired()) {
+ x += 1;
+ if (x >= argc) {
+ throw ArgumentRequired(name);
}
- else if (moreopts && strncmp(argv[x], "--", 2) == 0) {
- Glib::ustring name(argv[x] + 2);
- const Options::Option * o = consume.get(name);
- if (!o) {
- throw UnknownOption(name);
- }
- if (o->paramRequired()) {
+ consume(name, Environment::getCurrent()->platform(), argv[x]);
+ }
+ else {
+ consume(name, Environment::getCurrent()->platform(), name);
+ }
+ }
+ else if (moreopts && *(argv[x]) == '-') {
+ const char * n = argv[x] + 1;
+ while (*n) {
+ Glib::ustring name(1, *n++);
+ const Options::Option * o = consume.get(name);
+ if (!o) {
+ throw UnknownOption(name);
+ }
+ if (o->paramRequired()) {
+ if (!*n) {
x += 1;
if (x >= argc) {
throw ArgumentRequired(name);
@@ -37,56 +58,32 @@ class CommandLineArguments : public OptionsSource {
consume(name, Environment::getCurrent()->platform(), argv[x]);
}
else {
- consume(name, Environment::getCurrent()->platform(), name);
- }
- }
- else if (moreopts && *(argv[x]) == '-') {
- const char * n = argv[x] + 1;
- while (*n) {
- Glib::ustring name(1, *n++);
- const Options::Option * o = consume.get(name);
- if (!o) {
- throw UnknownOption(name);
- }
- if (o->paramRequired()) {
- if (!*n) {
- x += 1;
- if (x >= argc) {
- throw ArgumentRequired(name);
- }
- consume(name, Environment::getCurrent()->platform(), argv[x]);
- }
- else {
- consume(name, Environment::getCurrent()->platform(), n);
- n += 1;
- }
- }
- else {
- consume(name, Environment::getCurrent()->platform(), name);
- }
+ consume(name, Environment::getCurrent()->platform(), n);
+ n += 1;
}
}
else {
- char * sl = strchr(argv[x], '/');
- if (sl) {
- *sl++ = '\0';
- ConsoleEnvironment::todolist.push_back(ConsoleEnvironment::ToDo(argv[x], sl));
- }
- else {
- throw InvalidScriptName();
- }
+ consume(name, Environment::getCurrent()->platform(), name);
}
}
- time(&loadedAt);
}
- bool needReload() const
- {
- return !loadedAt;
+ else {
+ const char * sl = strchr(argv[x], '/');
+ if (sl) {
+ ConsoleEnvironment::todolist.push_back(ConsoleEnvironment::ToDo(std::string(argv[x], sl), sl + 1));
+ sl++;
+ }
+ else {
+ throw InvalidScriptName();
+ }
}
- private:
- mutable time_t loadedAt;
-};
+ }
+ time(&loadedAt);
+}
-// A... process first :)
-DECLARE_COMPONENT_LOADER("A_commandLineArguments", CommandLineArguments, OptionsSourceLoader)
+bool
+CommandLineArguments::needReload() const
+{
+ return !loadedAt;
+}
diff --git a/project2/console/claOptions.h b/project2/console/claOptions.h
new file mode 100644
index 0000000..e1fee8d
--- /dev/null
+++ b/project2/console/claOptions.h
@@ -0,0 +1,19 @@
+#ifndef CLAOPTIONS_H
+#define CLAOPTIONS_H
+
+#include "../common/optionsSource.h"
+
+class CommandLineArguments : public OptionsSource {
+ public:
+ CommandLineArguments(int c, const char * const * v);
+ void loadInto(const ConfigConsumer & consume) const;
+ bool needReload() const;
+
+ private:
+ const int argc;
+ const char * const * argv;
+ mutable time_t loadedAt;
+};
+
+#endif
+
diff --git a/project2/console/consoleEnvironment.cpp b/project2/console/consoleEnvironment.cpp
index 414bbed..d9abde2 100644
--- a/project2/console/consoleEnvironment.cpp
+++ b/project2/console/consoleEnvironment.cpp
@@ -13,8 +13,6 @@
#include "options.h"
#include <boost/bind.hpp>
-int ConsoleEnvironment::argc;
-char ** ConsoleEnvironment::argv;
ConsoleEnvironment::ToDoList ConsoleEnvironment::todolist;
class ShowHelpTrigger : public Options::Target {
@@ -40,8 +38,6 @@ class ShowHelpTrigger : public Options::Target {
DECLARE_OPTIONS(ConsoleEnvironment, "Console options")
("help", new ShowHelpTrigger(),
"Print usage and exit")("h")
-("console.syslogIdent", Options::value(&scriptname, "p2console"),
- "Log to syslog with ident <arg>")
("console.platform", Options::value(&reqPlatform),
"Platform")("p")
("console.queryParam", Options::functions(
@@ -53,14 +49,11 @@ DECLARE_OPTIONS(ConsoleEnvironment, "Console options")
"Query parameter")("q")
END_OPTIONS(ConsoleEnvironment);
-std::string ConsoleEnvironment::scriptname;
Glib::ustring ConsoleEnvironment::reqPlatform;
ConsoleEnvironment::QueryParams ConsoleEnvironment::queryParams;
-ConsoleEnvironment::ConsoleEnvironment(int c, char ** v)
+ConsoleEnvironment::ConsoleEnvironment()
{
- argc = c;
- argv = v;
}
ConsoleEnvironment::~ConsoleEnvironment()
@@ -99,12 +92,6 @@ ConsoleEnvironment::getServerName() const
}
}
-std::string
-ConsoleEnvironment::getScriptName() const
-{
- return scriptname;
-}
-
const ConsoleEnvironment::ToDoList &
ConsoleEnvironment::todoList() const
{
diff --git a/project2/console/consoleEnvironment.h b/project2/console/consoleEnvironment.h
index 1379584..48ab439 100644
--- a/project2/console/consoleEnvironment.h
+++ b/project2/console/consoleEnvironment.h
@@ -12,13 +12,12 @@ class ConsoleEnvironment : public Environment {
typedef std::vector<ToDo> ToDoList;
typedef std::vector<std::pair<std::string, std::string> > QueryParams;
- ConsoleEnvironment(int argc, char ** argv);
+ ConsoleEnvironment();
virtual ~ConsoleEnvironment();
Glib::ustring getParamQuery(const std::string & idx) const;
std::string getServerName() const;
- std::string getScriptName() const;
const ToDoList & todoList() const;
INITOPTIONS;
@@ -26,10 +25,6 @@ class ConsoleEnvironment : public Environment {
const Glib::ustring & platform() const;
friend class CommandLineArguments;
- static int argc;
- static char ** argv;
- static bool help;
- static std::string scriptname;
static Glib::ustring reqPlatform;
static QueryParams queryParams;
static ToDoList todolist;
diff --git a/project2/console/p2consoleMain.cpp b/project2/console/p2consoleMain.cpp
index 1643eda..d443ab0 100644
--- a/project2/console/p2consoleMain.cpp
+++ b/project2/console/p2consoleMain.cpp
@@ -5,13 +5,15 @@
#include "logger.h"
#include <boost/foreach.hpp>
#include <boost/bind.hpp>
+#include "claOptions.h"
int
main(int argc, char ** argv)
{
- ConsoleEnvironment env(argc, argv);
+ Plugable::newLoader<OptionsSource, OptionsSource>("", new CommandLineArguments(argc, argv));
+ ConsoleEnvironment env;
Plugable::onAllComponents(boost::bind(&ComponentLoader::onBegin, _1));
- env.init();
+ OptionsSource::loadSources();
BOOST_FOREACH(const ConsoleEnvironment::ToDo & todo, env.todoList()) {
Plugable::onAllComponents(boost::bind(&ComponentLoader::onBefore, _1));
Logger()->messagebf(LOG_DEBUG, "%s: Beginning script '%s/%s'", __FUNCTION__, todo.get<0>(), todo.get<1>());
diff --git a/project2/files/fileLog.cpp b/project2/files/fileLog.cpp
index a860080..1f7e6e9 100644
--- a/project2/files/fileLog.cpp
+++ b/project2/files/fileLog.cpp
@@ -1,10 +1,10 @@
#include "logger.h"
+#include "options.h"
/// Logger that writes to a file
class FileLogDriver : public LogDriverBase {
public:
- FileLogDriver(int level, const std::string & path) :
- LogDriverBase(level),
+ FileLogDriver() :
file(fopen(path.c_str(), "a"))
{
}
@@ -26,9 +26,22 @@ class FileLogDriver : public LogDriverBase {
}
}
+ INITOPTIONS;
+ static int level;
private:
+ static std::string path;
FILE * file;
};
-DECLARE_GENERIC_LOADER("file", LogDriverLoader, FileLogDriver);
+DECLARE_OPTIONS(FileLogDriver, "File log options")
+("common.filelog.level", Options::value(&level, -1),
+ "Log to file with level <arg> (default OFF)")
+("common.filelog.path", Options::value(&path, "/var/log/project2.log"),
+ "Log to file with path <arg> (default '/var/log/project2.log')")
+END_OPTIONS(FileLogDriver);
+
+int FileLogDriver::level;
+std::string FileLogDriver::path;
+
+DECLARE_LOGGER_LOADER("file", FileLogDriver);
diff --git a/project2/files/optionsSource.cpp b/project2/files/optionsSource.cpp
index db79b98..808d481 100644
--- a/project2/files/optionsSource.cpp
+++ b/project2/files/optionsSource.cpp
@@ -76,5 +76,5 @@ FileOptions::needReload() const
}
// Z... process last :)
-DECLARE_COMPONENT_LOADER("Z_configfile", FileOptions, OptionsSourceLoader)
+DECLARE_OPTIONSSOURCE("Z_configfile", new FileOptions())