diff options
author | randomdan <randomdan@localhost> | 2011-02-10 10:33:36 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2011-02-10 10:33:36 +0000 |
commit | f98bc01af0208321eeb988dd7c708526908f1f36 (patch) | |
tree | 705a8548c3df1048e6bcef70f703a01d3867a758 /project2/cgi | |
parent | Make different DB connectors optional (diff) | |
download | project2-f98bc01af0208321eeb988dd7c708526908f1f36.tar.bz2 project2-f98bc01af0208321eeb988dd7c708526908f1f36.tar.xz project2-f98bc01af0208321eeb988dd7c708526908f1f36.zip |
Fix silly bug loading readonly RDBMS config entries
Add centralised logging system
Add code for runtime configuration
Add support for configuring logging
Move all existing logging to new logging system
Diffstat (limited to 'project2/cgi')
-rw-r--r-- | project2/cgi/cgiAppEngine.cpp | 7 | ||||
-rw-r--r-- | project2/cgi/cgiAppEngine.h | 12 | ||||
-rw-r--r-- | project2/cgi/cgiCommon.cpp | 59 | ||||
-rw-r--r-- | project2/cgi/cgiEnvironment.cpp | 14 | ||||
-rw-r--r-- | project2/cgi/cgiEnvironment.h | 4 | ||||
-rw-r--r-- | project2/cgi/p2webFCgi.cpp | 9 |
6 files changed, 73 insertions, 32 deletions
diff --git a/project2/cgi/cgiAppEngine.cpp b/project2/cgi/cgiAppEngine.cpp index 3a9e362..364d393 100644 --- a/project2/cgi/cgiAppEngine.cpp +++ b/project2/cgi/cgiAppEngine.cpp @@ -1,5 +1,4 @@ #include "cgiAppEngine.h" -#include <syslog.h> #include <cgicc/Cgicc.h> #include <cgicc/HTTPContentHeader.h> #include "cgiEnvironment.h" @@ -48,6 +47,12 @@ CgiApplicationEngine::env() const } void +CgiApplicationEngine::write(const XmlWriter & w) const +{ + w(doc->cobj()); +} + +void CgiApplicationEngine::process() const { while (!doc && currentStage) { diff --git a/project2/cgi/cgiAppEngine.h b/project2/cgi/cgiAppEngine.h index 5a21e98..25c6537 100644 --- a/project2/cgi/cgiAppEngine.h +++ b/project2/cgi/cgiAppEngine.h @@ -8,6 +8,7 @@ #include "../commonObjects.h" #include "../uuid.h" #include <boost/intrusive_ptr.hpp> +#include <boost/function.hpp> #include <libxml++/document.h> #include <libxml++/parsers/domparser.h> @@ -19,16 +20,15 @@ namespace cgicc { class CgiApplicationEngine : public ApplicationEngine { public: + typedef boost::shared_ptr<xmlpp::Document> XmlDocPtr; + typedef boost::function1<void, xmlDocPtr> XmlWriter; + CgiApplicationEngine(const CgiEnvironment *); virtual ~CgiApplicationEngine(); void process() const; const cgicc::HTTPContentHeader * getHeader() const { return header; } - template <class Writer> - void write(const Writer & w) const - { - w(doc->cobj()); - } + void write(const XmlWriter & w) const; const Environment * env() const; SessionPtr session() const; virtual Glib::ustring resolveCurrentConfig() const; @@ -45,7 +45,7 @@ class CgiApplicationEngine : public ApplicationEngine { bool checkDomain(const DomainPlatforms::value_type & i) const; void loadEngineSection(const xmlpp::Element *) const; - mutable boost::shared_ptr<xmlpp::Document> doc; + mutable XmlDocPtr doc; class Stage; typedef boost::intrusive_ptr<Stage> StagePtr; diff --git a/project2/cgi/cgiCommon.cpp b/project2/cgi/cgiCommon.cpp index bfbec36..322bdf7 100644 --- a/project2/cgi/cgiCommon.cpp +++ b/project2/cgi/cgiCommon.cpp @@ -1,10 +1,10 @@ #include "cgiCommon.h" +#include "../logger.h" #include <libxml/tree.h> #include <glibmm/exception.h> #include <cgicc/CgiEnvironment.h> #include <cgicc/HTTPContentHeader.h> #include <cgicc/HTTPStatusHeader.h> -#include <fcgi_stdio.h> #include "cgiEnvironment.h" #include "cgiAppEngine.h" #include <boost/bind.hpp> @@ -19,41 +19,62 @@ xmlWrite(void * _out, const char * buf, int len) return len; } +// These are templates because some people don't inherit their +// exceptions from std::exception like normal people (Glib) +const char * +what(const Glib::Exception & e) +{ + return e.what().c_str(); +} +const char * +what(const std::exception & e) +{ + return e.what(); +} +template <typename E> +void +doExceptionReporting(const E & e, std::ostream & IO) +{ + char * buf = __cxxabiv1::__cxa_demangle(typeid(e).name(), NULL, NULL, NULL); + Logger()->messagef(LOG_ERR, "%s: Request errored: %s: %s", __FUNCTION__, buf, what(e)); + cgicc::HTTPStatusHeader header(500, e.what()); + header.render(IO); + IO << "Kaboom!" << std::endl + << std::endl + << buf << std::endl + << e.what() << std::endl; + free(buf); +} + void cgiServe(cgicc::CgiInput * i, std::ostream & IO) { + cgicc::Cgicc cgi(i); + CgiEnvironment env(&cgi); + env.init(); try { - cgicc::Cgicc cgi(i); - CgiEnvironment env(&cgi); CgiApplicationEngine app(&env); + + Logger()->messagef(LOG_DEBUG, "%s: Processing request", __FUNCTION__); app.process(); + + Logger()->messagef(LOG_DEBUG, "%s: Sending request result", __FUNCTION__); IO << "Cache-control: no-cache" << std::endl; app.getHeader()->render(IO); xmlOutputBufferPtr out = xmlOutputBufferCreateIO( xmlWrite, NULL, &IO, xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8)); app.write(boost::bind(xmlSaveFileTo, out, _1, "utf-8")); + + Logger()->messagef(LOG_DEBUG, "%s: Completed request", __FUNCTION__); } catch (const std::exception & e) { - cgicc::HTTPStatusHeader header(500, e.what()); - header.render(IO); - char * buf = __cxxabiv1::__cxa_demangle(typeid(e).name(), NULL, NULL, NULL); - IO << "Kaboom!" << std::endl - << std::endl - << buf << std::endl - << e.what() << std::endl; - free(buf); + doExceptionReporting(e, IO); } catch (const Glib::Exception & e) { - cgicc::HTTPStatusHeader header(500, e.what()); - header.render(IO); - char * buf = __cxxabiv1::__cxa_demangle(typeid(e).name(), NULL, NULL, NULL); - IO << "Kaboom!" << std::endl - << std::endl - << buf << std::endl - << e.what() << std::endl; - free(buf); + doExceptionReporting(e, IO); } catch (...) { + Logger()->messagef(LOG_ERR, "%s: Request errored: Unknown exception", __FUNCTION__); cgicc::HTTPStatusHeader header(500, "Unknown exception"); header.render(IO); IO << "Kaboom!" << std::endl diff --git a/project2/cgi/cgiEnvironment.cpp b/project2/cgi/cgiEnvironment.cpp index e541f7f..ae65183 100644 --- a/project2/cgi/cgiEnvironment.cpp +++ b/project2/cgi/cgiEnvironment.cpp @@ -2,7 +2,6 @@ #include "../appEngine.h" #include "../exceptions.h" #include <map> -#include <syslog.h> #include <cgicc/Cgicc.h> #include <boost/tokenizer.hpp> @@ -13,6 +12,7 @@ std::vector<X>makeVector(const Y & y) } CgiEnvironment::CgiEnvironment(cgicc::Cgicc * c) : + Environment(0, NULL), cgicc::CgiEnvironment(c->getEnvironment()), elems(makeVector<std::string>(boost::tokenizer<boost::char_separator<char> >(getRedirectURL(), boost::char_separator<char>("/")))), cgi(c) @@ -23,6 +23,18 @@ CgiEnvironment::~CgiEnvironment() { } +boost::program_options::options_description +CgiEnvironment::addOptions(boost::program_options::positional_options_description &) +{ + boost::program_options::options_description cgi("Project2 CGI options"); + return cgi; +} + +void +CgiEnvironment::postinit(const boost::program_options::options_description &, const boost::program_options::variables_map &) +{ +} + Glib::ustring CgiEnvironment::getParamUri(unsigned int p) const { diff --git a/project2/cgi/cgiEnvironment.h b/project2/cgi/cgiEnvironment.h index 5c0455c..005a49b 100644 --- a/project2/cgi/cgiEnvironment.h +++ b/project2/cgi/cgiEnvironment.h @@ -22,6 +22,10 @@ class CgiEnvironment : public Environment, public cgicc::CgiEnvironment { std::vector<std::string> elems; const cgicc::Cgicc * const cgi; + + private: + boost::program_options::options_description addOptions(boost::program_options::positional_options_description &); + void postinit(const boost::program_options::options_description &, const boost::program_options::variables_map &); }; #endif diff --git a/project2/cgi/p2webFCgi.cpp b/project2/cgi/p2webFCgi.cpp index 620c441..d501787 100644 --- a/project2/cgi/p2webFCgi.cpp +++ b/project2/cgi/p2webFCgi.cpp @@ -1,7 +1,7 @@ -#include <syslog.h> #include "cgiCommon.h" #include "FCgiIO.h" #include "../xmlObjectLoader.h" +#include "../logger.h" time_t lastPeriodic = 0; time_t periodicDelay = 600; @@ -28,7 +28,6 @@ int main(void) { if (!FCGX_IsCGI()) { - syslog(LOG_NOTICE, "FCGID Startup ($Id$)"); FCGX_Request request; FCGX_Init(); @@ -38,7 +37,7 @@ main(void) onAlarm.sa_handler = &p2webGoingIdle; onAlarm.sa_flags = 0; if (sigaction(SIGALRM, &onAlarm, NULL)) { - syslog(LOG_WARNING, "Failed to set signal handler"); + fprintf(stderr, "Failed to set signal handler\n"); } alarm(60); while (FCGX_Accept_r(&request) == 0) { @@ -51,9 +50,9 @@ main(void) p2webPeriodic(); } } + return 0; } else { - syslog(LOG_ERR, "FCGID not running as a FastCGI program"); + return 1; } - return 0; } |