From 3e584362b6933174b28cf98ff3138a589ed8326d Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Mon, 24 Oct 2011 14:28:11 +0200 Subject: [PATCH] New log method for package class This is to facilitate some kind of consistency for log messages. The zoom filter uses it. For example x-session-id stuff is included. --- include/metaproxy/origin.hpp | 4 ++-- include/metaproxy/package.hpp | 5 +++-- src/filter_zoom.cpp | 21 ++++++++++++--------- src/origin.cpp | 2 +- src/package.cpp | 19 ++++++++++++++++++- src/util.cpp | 1 - 6 files changed, 36 insertions(+), 16 deletions(-) diff --git a/include/metaproxy/origin.hpp b/include/metaproxy/origin.hpp index 955476f..44eefcf 100644 --- a/include/metaproxy/origin.hpp +++ b/include/metaproxy/origin.hpp @@ -27,7 +27,7 @@ namespace metaproxy_1 { } namespace std { - std::ostream& operator<<(std::ostream& os, metaproxy_1::Origin& o); + std::ostream& operator<<(std::ostream& os, const metaproxy_1::Origin& o); } namespace metaproxy_1 { @@ -63,7 +63,7 @@ namespace metaproxy_1 { void set_custom_session(const std::string &s); private: friend std::ostream& - std::operator<<(std::ostream& os, metaproxy_1::Origin& o); + std::operator<<(std::ostream& os, const metaproxy_1::Origin& o); enum origin_t { API, diff --git a/include/metaproxy/package.hpp b/include/metaproxy/package.hpp index 8800fa3..eaa9fd8 100644 --- a/include/metaproxy/package.hpp +++ b/include/metaproxy/package.hpp @@ -35,7 +35,7 @@ namespace metaproxy_1 { namespace std { - std::ostream& operator<<(std::ostream& os, metaproxy_1::Package& p); + std::ostream& operator<<(std::ostream& os, const metaproxy_1::Package& p); } namespace metaproxy_1 { @@ -79,7 +79,8 @@ namespace metaproxy_1 { /// get function - right val in assignment Session session() const; - + + void log(const char *module, int level, const char *fmt, ...) const; private: Session m_session; Origin m_origin; diff --git a/src/filter_zoom.cpp b/src/filter_zoom.cpp index b735881..fecb7bd 100644 --- a/src/filter_zoom.cpp +++ b/src/filter_zoom.cpp @@ -113,7 +113,8 @@ namespace metaproxy_1 { void handle_package(mp::Package &package); void handle_search(mp::Package &package); void handle_present(mp::Package &package); - BackendPtr get_backend_from_databases(std::string &database, + BackendPtr get_backend_from_databases(const mp::Package &package, + std::string &database, int *error, char **addinfo, ODR odr); @@ -635,6 +636,7 @@ void yf::Zoom::Impl::configure(const xmlNode *ptr, bool test_only, } yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( + const mp::Package &package, std::string &database, int *error, char **addinfo, ODR odr) { std::list::const_iterator map_it; @@ -904,7 +906,7 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( FILE *file = fopen(fname, "w"); if (!file) { - yaz_log(YLOG_WARN|YLOG_ERRNO, "create %s", fname); + package.log("zoom", YLOG_WARN|YLOG_ERRNO, "create %s", fname); *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR; *addinfo = (char *) odr_malloc(odr, 40 + strlen(fname)); sprintf(*addinfo, "Could not create %s", fname); @@ -925,7 +927,7 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( fwrite(wrbuf_buf(w), 1, wrbuf_len(w), file); fclose(file); - yaz_log(YLOG_LOG, "file %s created\n", fname); + package.log("zoom", YLOG_LOG, "file %s created\n", fname); xfree(fname); } @@ -941,7 +943,7 @@ yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases( } if (db_args.length()) url += "," + db_args; - yaz_log(YLOG_LOG, "url=%s", url.c_str()); + package.log("zoom", YLOG_LOG, "url=%s", url.c_str()); b->connect(url, error, addinfo, odr); if (*error == 0) { @@ -1278,7 +1280,8 @@ void yf::Zoom::Frontend::handle_search(mp::Package &package) int error = 0; char *addinfo = 0; std::string db(sr->databaseNames[0]); - BackendPtr b = get_backend_from_databases(db, &error, &addinfo, odr); + BackendPtr b = get_backend_from_databases(package, db, &error, + &addinfo, odr); if (error) { apdu_res = @@ -1424,7 +1427,7 @@ void yf::Zoom::Frontend::handle_search(mp::Package &package) assert(pqf_wrbuf == 0); int cerror, cpos; struct ccl_rpn_node *cn; - yaz_log(YLOG_LOG, "CCL: %s", wrbuf_cstr(ccl_wrbuf)); + package.log("zoom", YLOG_LOG, "CCL: %s", wrbuf_cstr(ccl_wrbuf)); cn = ccl_find_str(b->sptr->ccl_bibset, wrbuf_cstr(ccl_wrbuf), &cerror, &cpos); wrbuf_destroy(ccl_wrbuf); @@ -1451,7 +1454,7 @@ void yf::Zoom::Frontend::handle_search(mp::Package &package) } pqf_wrbuf = wrbuf_alloc(); ccl_pquery(pqf_wrbuf, cn); - yaz_log(YLOG_LOG, "RPN: %s", wrbuf_cstr(pqf_wrbuf)); + package.log("zoom", YLOG_LOG, "RPN: %s", wrbuf_cstr(pqf_wrbuf)); ccl_rpn_delete(cn); } @@ -1486,7 +1489,7 @@ void yf::Zoom::Frontend::handle_search(mp::Package &package) if (status == 0) { ZOOM_query_cql(q, wrbuf_cstr(wrb)); - yaz_log(YLOG_LOG, "CQL: %s", wrbuf_cstr(wrb)); + package.log("zoom", YLOG_LOG, "CQL: %s", wrbuf_cstr(wrb)); b->search(q, &hits, &error, &addinfo, odr); } ZOOM_query_destroy(q); @@ -1505,7 +1508,7 @@ void yf::Zoom::Frontend::handle_search(mp::Package &package) else { ZOOM_query_prefix(q, wrbuf_cstr(pqf_wrbuf)); - yaz_log(YLOG_LOG, "search PQF: %s", wrbuf_cstr(pqf_wrbuf)); + package.log("zoom", YLOG_LOG, "search PQF: %s", wrbuf_cstr(pqf_wrbuf)); b->search(q, &hits, &error, &addinfo, odr); ZOOM_query_destroy(q); wrbuf_destroy(pqf_wrbuf); diff --git a/src/origin.cpp b/src/origin.cpp index 6901a92..f76a4d6 100644 --- a/src/origin.cpp +++ b/src/origin.cpp @@ -77,7 +77,7 @@ std::string mp::Origin::get_address() return m_address; } -std::ostream& std::operator<<(std::ostream& os, mp::Origin& o) +std::ostream& std::operator<<(std::ostream& os, const mp::Origin& o) { if (o.m_address.length()) os << o.m_address; diff --git a/src/package.cpp b/src/package.cpp index df45faf..b3021a9 100644 --- a/src/package.cpp +++ b/src/package.cpp @@ -18,6 +18,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include "config.hpp" #include +#include +#include #include @@ -113,13 +115,28 @@ mp::Session mp::Package::session() const return m_session; } -std::ostream& std::operator<<(std::ostream& os, mp::Package& p) +std::ostream& std::operator<<(std::ostream& os, const mp::Package& p) { os << p.origin() << " "; os << p.session().id(); return os; } +void mp::Package::log(const char *module, int level, const char *fmt, ...) const +{ + char buf[4096]; + va_list ap; + va_start(ap, fmt); + + yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap); + + std::ostringstream os; + + os << module << " " << *this << " " << buf; + + va_end(ap); + yaz_log(level, "%s", os.str().c_str()); +} /* * Local variables: diff --git a/src/util.cpp b/src/util.cpp index f792e02..1f335a1 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -693,7 +693,6 @@ std::string mp_util::uri_encode(std::string s) return result; } - /* * Local variables: * c-basic-offset: 4 -- 1.7.10.4