From 04c1324c57692dfd22fab211a7443aaf484f07ce Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 27 Apr 2015 13:52:25 +0200 Subject: Implement module configuration, cleanup the code --- brep/module.cxx | 153 ++++++++++++++++++++++++++------------------------------ 1 file changed, 70 insertions(+), 83 deletions(-) (limited to 'brep/module.cxx') diff --git a/brep/module.cxx b/brep/module.cxx index 5937028..4ce635a 100644 --- a/brep/module.cxx +++ b/brep/module.cxx @@ -4,15 +4,15 @@ #include -#include +#include +#include + #include -#include // bind() #include // strncmp() - -#include +#include +#include // bind() #include - #include using namespace std; @@ -24,57 +24,56 @@ namespace brep handle (request& rq, response& rs, log& l) { log_ = &l; - const basic_mark error (severity::error, log_writer_, __PRETTY_FUNCTION__); try { handle (rq, rs); } - catch (const invalid_request& e) + catch (const server_error& e) { - if (e.description.empty ()) - { - rs.status (e.status); - } - else + log_write (e.data); + + try { - try - { - rs.content (e.status, "text/html;charset=utf-8") << e.description; - } - catch (const sequence_error& se) + static const char* s[] = {"err", "warn", "info", "trace"}; + std::ostream& o = rs.content (500, "text/plain;charset=utf-8"); + + for (const auto& d: e.data) { - error << se.what (); - rs.status (e.status); + string name; + + try + { + name = func_name (d.name); + } + catch (const invalid_argument&) + { + // Log "pretty" function description, see in log file & fix. + name = d.name; + } + + o << "[" << s[static_cast (d.sev)] << "] [" + << name << "] " << d.msg << std::endl; } } - } - catch (server_error& e) // Non-const because of move() below. - { - log_write (move (e.data)); - rs.status (HTTP_INTERNAL_SERVER_ERROR); - } - catch (const exception& e) - { - error << e.what (); - rs.status (HTTP_INTERNAL_SERVER_ERROR); - } - catch (...) - { - error << "unknown error"; - rs.status (HTTP_INTERNAL_SERVER_ERROR); + catch (const sequence_error&) + { + } } } + void module:: + init (const char* path) + { + } + module:: module (): log_writer_ (bind (&module::log_write, this, _1)) {} // Custom copy constructor is required to initialize log_writer_ properly. // - // @@ Won't log_writer_ be left empty by this implementation? - // module:: - module (const module& m): module (), verb_ (m.verb_) {} + module (const module& m): module () {verb_ = m.verb_;} // For function func declared like this: // using B = std::string (*)(int); @@ -85,65 +84,53 @@ namespace brep // ,std::string (* (*)(wchar_t))(int)) const)(int, int))(int) // string module:: - func_name (const string& pretty_name) + func_name (const char* pretty_name) { - string::size_type b (0); - string::size_type e (pretty_name.find (' ')); + const char* e = strchr (pretty_name, ')'); - // Position b at beginning of supposed function name, - // - if (e != string::npos && !strncmp (pretty_name.c_str (), "virtual ", 8)) + if (e && e > pretty_name) { - // Skip keyword virtual. + // Position e at last matching '(' which is the beginning of the + // argument list.. // - b = pretty_name.find_first_not_of (' ', e); - e = pretty_name.find (' ', b); - } + size_t d (1); - if (pretty_name.find ('(', b) > e) - { - // Not a constructor nor destructor. Skip type or *. - // - b = pretty_name.find_first_not_of (' ', e); - } - - if (b != string::npos) - { - // Position e at the last character of supposed function name. - // - e = pretty_name.find_last_of (')'); - - if (e != string::npos && e > b) + do { - size_t d (1); - - while (--e > b && d) + switch (*--e) { - switch (pretty_name[e]) - { - case ')': ++d; break; - case '(': --d; break; - } + case ')': ++d; break; + case '(': --d; break; } + } + while (d && e > pretty_name); - if (!d) + if (!d && e > pretty_name) + { + // Position e at the character following the function name. + // + while (e > pretty_name && + (*e != '(' || *(e - 1) == ' ' || *(e - 1) == ')')) + --e; + + if (e > pretty_name) { - return pretty_name[b] == '(' && pretty_name[e] == ')' ? - // Not a name yet, go deeper. - // - func_name (string(pretty_name, b + 1, e - b - 1)) : - // Got the name. - // - string (pretty_name, b, e - b + 1); + // Position b at the beginning of the qualified function name. + // + const char* b (e); + while (--b > pretty_name && *b != ' '); + if (*b == ' ') ++b; + + return string (b, e - b); } } } - throw invalid_argument (""); + throw invalid_argument ("::brep::module::func_name"); } void module:: - log_write (diag_data&& d) const + log_write (const diag_data& d) const { if (log_ == nullptr) return; // No backend yet. @@ -157,7 +144,7 @@ namespace brep // static int s[] = {APLOG_ERR, APLOG_WARNING, APLOG_INFO, APLOG_TRACE1}; - for (const auto& e : d) + for (const auto& e: d) { string name; @@ -171,11 +158,11 @@ namespace brep name = e.name; } - al->write (e.loc.file.c_str(), + al->write (e.loc.file.c_str (), e.loc.line, - name.c_str(), + name.c_str (), s[static_cast (e.sev)], - e.msg.c_str()); + e.msg.c_str ()); } } -- cgit v1.1