1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "filter_log.hpp"
21 #include <metaproxy/package.hpp>
26 #include <boost/thread/mutex.hpp>
28 #include "gduutil.hpp"
29 #include <metaproxy/util.hpp>
30 #include <metaproxy/xmlutil.hpp>
32 #include <yaz/oid_db.h>
34 #include <yaz/wrbuf.h>
36 #include <yaz/querytowrbuf.h>
37 #include <yaz/timing.h>
41 namespace mp = metaproxy_1;
42 namespace yf = metaproxy_1::filter;
44 namespace metaproxy_1 {
49 typedef boost::shared_ptr<Log::Impl::LFile> LFilePtr;
52 Impl(const std::string &x = "-");
54 void process(metaproxy_1::Package & package);
55 void configure(const xmlNode * ptr);
57 void openfile(const std::string &fname);
58 // needs to be static to be called by C pointer-to-function-syntax
59 static void stream_write(ODR o, void *handle, int type,
60 const char *buf, int len);
61 // needs to be static to be called by C pointer-to-function-syntax
62 static void option_write(const char *name, void *handle);
64 std::string m_msg_config;
74 std::string m_time_format;
75 // Only used during confiqgure stage (no threading),
76 // for performance avoid opening files which other log filter
77 // instances already have opened
78 static std::list<LFilePtr> filter_log_files;
80 boost::mutex m_session_mutex;
81 std::map<mp::Session, std::string> m_sessions;
84 class Log::Impl::LFile {
90 LFile(std::string fname);
91 LFile(std::string fname, FILE *outf);
92 void log(const std::string &date_format,
93 std::ostringstream &os);
100 // define Pimpl wrapper forwarding to Impl
102 yf::Log::Log() : m_p(new Impl)
106 yf::Log::Log(const std::string &x) : m_p(new Impl(x))
111 { // must have a destructor because of boost::scoped_ptr
114 void yf::Log::configure(const xmlNode *xmlnode, bool test_only,
117 m_p->configure(xmlnode);
120 void yf::Log::process(mp::Package &package) const
122 m_p->process(package);
126 // static initialization
127 std::list<yf::Log::Impl::LFilePtr> yf::Log::Impl::filter_log_files;
130 yf::Log::Impl::Impl(const std::string &x)
134 m_user_access(false),
137 m_req_session(false),
138 m_res_session(false),
139 m_init_options(false),
140 m_time_format("%H:%M:%S-%d/%m")
146 yf::Log::Impl::~Impl()
150 static void log_DefaultDiagFormat(WRBUF w, Z_DefaultDiagFormat *e)
153 wrbuf_printf(w, ODR_INT_PRINTF " ", *e->condition);
155 wrbuf_puts(w, "?? ");
156 if (e->which == Z_DefaultDiagFormat_v2Addinfo && e->u.v2Addinfo)
159 wrbuf_puts(w, e->u.v2Addinfo);
162 else if (e->which == Z_DefaultDiagFormat_v3Addinfo && e->u.v3Addinfo)
165 wrbuf_puts(w, e->u.v3Addinfo);
172 static void log_DiagRecs(WRBUF w, int num_diagRecs, Z_DiagRec **diags)
174 if (diags[0]->which != Z_DiagRec_defaultFormat)
175 wrbuf_puts(w ,"(diag not in default format?)");
178 Z_DefaultDiagFormat *e = diags[0]->u.defaultFormat;
179 log_DefaultDiagFormat(w, e);
183 static void log_syntax(WRBUF w, const Odr_oid *syntax)
187 char oid_name[OID_STR_MAX+1];
188 wrbuf_puts(w, yaz_oid_to_string_buf(syntax, 0, oid_name));
194 static void log_1line_Z_APDU(Z_APDU *z_req, Z_APDU *z_res, WRBUF w)
196 switch (z_req->which)
198 case Z_APDU_initRequest:
199 wrbuf_puts(w, "Init ");
202 else if (z_res->which != Z_APDU_initResponse)
203 wrbuf_printf(w, "? response=%d", z_res->which);
206 Z_InitRequest *req = z_req->u.initRequest;
207 Z_InitResponse *res = z_res->u.initResponse;
208 if (res->result && *res->result)
209 wrbuf_printf(w, "OK -");
212 Z_External *uif = res->userInformationField;
213 bool got_code = false;
214 wrbuf_printf(w, "ERROR ");
215 if (uif && uif->which == Z_External_userInfo1)
217 Z_OtherInformation *ui = uif->u.userInfo1;
218 if (ui->num_elements >= 1)
220 Z_OtherInformationUnit *unit = ui->list[0];
221 if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
222 unit->information.externallyDefinedInfo &&
223 unit->information.externallyDefinedInfo->which ==
226 Z_DiagnosticFormat *diag =
227 unit->information.externallyDefinedInfo->
231 Z_DiagnosticFormat_s *ds = diag->elements[0];
233 Z_DiagnosticFormat_s_defaultDiagRec)
235 log_DefaultDiagFormat(w,
236 ds->u.defaultDiagRec);
247 wrbuf_printf(w, " ID:%s Name:%s Version:%s",
248 req->implementationId ? req->implementationId :"-",
249 req->implementationName ?req->implementationName : "-",
250 req->implementationVersion ?
251 req->implementationVersion : "-");
254 case Z_APDU_searchRequest:
255 wrbuf_puts(w, "Search ");
258 else if (z_res->which != Z_APDU_searchResponse)
259 wrbuf_printf(w, "? response=%d", z_res->which);
262 Z_SearchRequest *req = z_req->u.searchRequest;
263 Z_SearchResponse *res = z_res->u.searchResponse;
265 for (i = 0 ; i < req->num_databaseNames; i++)
268 wrbuf_printf(w, "+");
269 wrbuf_puts(w, req->databaseNames[i]);
271 wrbuf_printf(w, " ");
274 wrbuf_printf(w, "OK " ODR_INT_PRINTF " %s", *res->resultCount,
277 else if (res->records->which == Z_Records_DBOSD)
279 wrbuf_printf(w, "OK " ODR_INT_PRINTF " %s", *res->resultCount,
282 else if (res->records->which == Z_Records_NSD)
284 wrbuf_puts(w, "ERROR ");
285 log_DefaultDiagFormat(w,
286 res->records->u.nonSurrogateDiagnostic);
288 else if (res->records->which == Z_Records_multipleNSD)
290 wrbuf_puts(w, "ERROR ");
293 res->records->u.multipleNonSurDiagnostics->num_diagRecs,
294 res->records->u.multipleNonSurDiagnostics->diagRecs);
297 log_syntax(w, req->preferredRecordSyntax);
298 wrbuf_printf(w, " 1+" ODR_INT_PRINTF " ",
299 res->numberOfRecordsReturned
300 ? *res->numberOfRecordsReturned : 0);
301 yaz_query_to_wrbuf(w, req->query);
304 case Z_APDU_presentRequest:
305 wrbuf_puts(w, "Present ");
308 else if (z_res->which != Z_APDU_presentResponse)
309 wrbuf_printf(w, "? response=%d", z_res->which);
312 Z_PresentRequest *req = z_req->u.presentRequest;
313 Z_PresentResponse *res = z_res->u.presentResponse;
317 wrbuf_printf(w, "OK");
319 else if (res->records->which == Z_Records_DBOSD)
321 wrbuf_printf(w, "OK");
323 else if (res->records->which == Z_Records_NSD)
325 wrbuf_puts(w, "ERROR ");
326 log_DefaultDiagFormat(w,
327 res->records->u.nonSurrogateDiagnostic);
329 else if (res->records->which == Z_Records_multipleNSD)
331 wrbuf_puts(w, "ERROR ");
334 res->records->u.multipleNonSurDiagnostics->num_diagRecs,
335 res->records->u.multipleNonSurDiagnostics->diagRecs);
338 assert(req->preferredRecordSyntax);
339 log_syntax(w, req->preferredRecordSyntax);
341 wrbuf_printf(w, " %s " ODR_INT_PRINTF "+" ODR_INT_PRINTF " ",
342 req->resultSetId, *req->resultSetStartPoint,
343 *req->numberOfRecordsRequested);
346 case Z_APDU_deleteResultSetRequest:
347 wrbuf_puts(w, "deleteResultSet ");
349 case Z_APDU_accessControlRequest:
350 wrbuf_puts(w, "accessControl ");
352 case Z_APDU_resourceControlRequest:
353 wrbuf_puts(w, "resourceControl ");
355 case Z_APDU_triggerResourceControlRequest:
356 wrbuf_puts(w, "triggerResourceControlRequest");
358 case Z_APDU_resourceReportRequest:
359 wrbuf_puts(w, "resourceReport ");
361 case Z_APDU_scanRequest:
362 wrbuf_puts(w, "Scan ");
365 else if (z_res->which != Z_APDU_scanResponse)
366 wrbuf_printf(w, "? response=%d", z_res->which);
369 Z_ScanRequest *req = z_req->u.scanRequest;
370 Z_ScanResponse *res = z_res->u.scanResponse;
372 for (i = 0 ; i < req->num_databaseNames; i++)
375 wrbuf_printf(w, "+");
376 wrbuf_puts(w, req->databaseNames[i]);
379 if (!res->scanStatus || *res->scanStatus == 0)
381 else if (*res->scanStatus == 6)
382 wrbuf_puts(w, "ERROR");
384 wrbuf_printf(w, "PARTIAL" ODR_INT_PRINTF, *res->scanStatus);
386 wrbuf_printf(w, " " ODR_INT_PRINTF " " ODR_INT_PRINTF "+"
387 ODR_INT_PRINTF "+" ODR_INT_PRINTF " ",
388 res->numberOfEntriesReturned ?
389 *res->numberOfEntriesReturned : 0,
390 req->preferredPositionInResponse ?
391 *req->preferredPositionInResponse : 1,
392 *req->numberOfTermsRequested,
393 res->stepSize ? *res->stepSize : 1);
395 yaz_scan_to_wrbuf(w, req->termListAndStartPoint,
399 case Z_APDU_sortRequest:
400 wrbuf_puts(w, "sort ");
403 else if (z_res->which != Z_APDU_sortResponse)
404 wrbuf_printf(w, "? response=%d", z_res->which);
407 Z_SortResponse *res = z_res->u.sortResponse;
408 Z_SortRequest *req = z_res->u.sortRequest;
411 if (*res->sortStatus == Z_SortResponse_success)
413 else if (*res->sortStatus == Z_SortResponse_partial_1)
414 wrbuf_puts(w, "PARTIAL");
415 else if (*res->sortStatus == Z_SortResponse_failure)
416 wrbuf_puts(w, "ERROR");
419 if (res->diagnostics && res->num_diagnostics >= 1)
420 log_DiagRecs(w, res->num_diagnostics,res->diagnostics);
425 for (i = 0; i < req->num_inputResultSetNames; i++)
429 wrbuf_puts(w, req->inputResultSetNames[i]);
431 wrbuf_printf(w, ")->%s ",req->sortedResultSetName);
435 case Z_APDU_segmentRequest:
436 wrbuf_puts(w, "segmentRequest");
438 case Z_APDU_extendedServicesRequest:
439 wrbuf_puts(w, "extendedServices ");
442 else if (z_res->which != Z_APDU_extendedServicesResponse)
443 wrbuf_printf(w, "? response=%d", z_res->which);
446 Z_ExtendedServicesResponse *res =
447 z_res->u.extendedServicesResponse;
448 switch (*res->operationStatus)
450 case Z_ExtendedServicesResponse_done:
451 wrbuf_puts(w, "DONE"); break;
452 case Z_ExtendedServicesResponse_accepted:
453 wrbuf_puts(w, "ACCEPTED"); break;
454 case Z_ExtendedServicesResponse_failure:
455 wrbuf_puts(w, "ERROR"); break;
457 wrbuf_printf(w, ODR_INT_PRINTF, *res->operationStatus);
460 if (res->diagnostics && res->num_diagnostics >= 1)
461 log_DiagRecs(w, res->num_diagnostics,res->diagnostics);
467 wrbuf_puts(w, "close");
469 case Z_APDU_duplicateDetectionRequest:
470 wrbuf_puts(w, "duplicateDetention ");
473 else if (z_res->which != Z_APDU_duplicateDetectionResponse)
474 wrbuf_printf(w, "? response=%d", z_res->which);
477 Z_DuplicateDetectionResponse *res =
478 z_res->u.duplicateDetectionResponse;
482 wrbuf_puts(w, "ERROR");
485 if (res->diagnostics && res->num_diagnostics >= 1)
486 log_DiagRecs(w, res->num_diagnostics, res->diagnostics);
492 wrbuf_printf(w, "REQ=%d RES=%d", z_req->which, z_res->which);
497 static void log_1line_Z_HTTP(Z_HTTP_Request *req, Z_HTTP_Response *res, WRBUF w)
499 wrbuf_printf(w, "%s %s HTTP/%s", req->method, req->path, req->version);
501 wrbuf_printf(w, " %d %d", res->code, res->content_len);
503 wrbuf_printf(w, " ?");
506 static void log_1line_Z_GDU(Z_GDU *gdu_req, Z_GDU *gdu_res, WRBUF w)
508 if (gdu_req && gdu_req->which == Z_GDU_Z3950)
510 log_1line_Z_APDU(gdu_req->u.z3950,
511 (gdu_res && gdu_res->which == Z_GDU_Z3950) ?
512 gdu_res->u.z3950 : 0, w);
514 else if (gdu_req && gdu_req->which == Z_GDU_HTTP_Request)
516 log_1line_Z_HTTP(gdu_req->u.HTTP_Request,
517 (gdu_res && gdu_res->which == Z_GDU_HTTP_Response) ?
518 gdu_res->u.HTTP_Response : 0, w);
522 void yf::Log::Impl::configure(const xmlNode *ptr)
524 for (ptr = ptr->children; ptr; ptr = ptr->next)
526 if (ptr->type != XML_ELEMENT_NODE)
528 if (!strcmp((const char *) ptr->name, "message"))
529 m_msg_config = mp::xml::get_text(ptr);
530 else if (!strcmp((const char *) ptr->name, "filename"))
532 std::string fname = mp::xml::get_text(ptr);
535 else if (!strcmp((const char *) ptr->name, "time-format"))
537 m_time_format = mp::xml::get_text(ptr);
539 else if (!strcmp((const char *) ptr->name, "category"))
541 const struct _xmlAttr *attr;
542 for (attr = ptr->properties; attr; attr = attr->next)
544 if (!strcmp((const char *) attr->name, "line"))
545 m_1line = mp::xml::get_bool(attr->children, true);
546 else if (!strcmp((const char *) attr->name, "access"))
547 m_access = mp::xml::get_bool(attr->children, true);
548 else if (!strcmp((const char *) attr->name, "user-access"))
549 m_user_access = mp::xml::get_bool(attr->children, true);
550 else if (!strcmp((const char *) attr->name, "request-apdu"))
551 m_req_apdu = mp::xml::get_bool(attr->children, true);
552 else if (!strcmp((const char *) attr->name, "response-apdu"))
553 m_res_apdu = mp::xml::get_bool(attr->children, true);
554 else if (!strcmp((const char *) attr->name, "apdu"))
556 m_req_apdu = mp::xml::get_bool(attr->children, true);
557 m_res_apdu = m_req_apdu;
559 else if (!strcmp((const char *) attr->name,
562 mp::xml::get_bool(attr->children, true);
563 else if (!strcmp((const char *) attr->name,
566 mp::xml::get_bool(attr->children, true);
567 else if (!strcmp((const char *) attr->name,
571 mp::xml::get_bool(attr->children, true);
572 m_res_session = m_req_session;
574 else if (!strcmp((const char *) attr->name,
577 mp::xml::get_bool(attr->children, true);
578 else if (!strcmp((const char *) attr->name,
581 mp::xml::get_bool(attr->children, true);
583 throw mp::filter::FilterException(
584 "Bad attribute " + std::string((const char *)
590 throw mp::filter::FilterException("Bad element "
591 + std::string((const char *)
597 void yf::Log::Impl::process(mp::Package &package)
599 Z_GDU *gdu_req = package.request().get();
600 std::string user("-");
602 yaz_timing_t timer = yaz_timing_create();
604 // scope for session lock
606 boost::mutex::scoped_lock scoped_lock(m_session_mutex);
608 if (gdu_req && gdu_req->which == Z_GDU_Z3950)
610 Z_APDU *apdu_req = gdu_req->u.z3950;
611 if (apdu_req->which == Z_APDU_initRequest)
613 Z_InitRequest *req = apdu_req->u.initRequest;
614 Z_IdAuthentication *a = req->idAuthentication;
617 if (a->which == Z_IdAuthentication_idPass)
618 user = a->u.idPass->userId;
619 else if (a->which == Z_IdAuthentication_open)
622 m_sessions[package.session()] = user;
626 std::map<mp::Session,std::string>::iterator it =
627 m_sessions.find(package.session());
628 if (it != m_sessions.end())
631 if (package.session().is_closed())
632 m_sessions.erase(package.session());
634 // scope for locking Ostream
636 boost::mutex::scoped_lock scoped_lock(m_file->m_mutex);
642 std::ostringstream os;
643 os << m_msg_config << " "
647 m_file->log(m_time_format, os);
655 std::ostringstream os;
656 os << m_msg_config << " " << user << " "
660 m_file->log(m_time_format, os);
666 std::ostringstream os;
668 os << " request id=" << package.session().id();
670 << (package.session().is_closed() ? "yes" : "no");
671 m_file->log(m_time_format, os);
676 if (gdu_req && gdu_req->which == Z_GDU_Z3950 &&
677 gdu_req->u.z3950->which == Z_APDU_initRequest)
679 std::ostringstream os;
680 os << m_msg_config << " init options:";
681 yaz_init_opt_decode(gdu_req->u.z3950->u.initRequest->options,
683 m_file->log(m_time_format, os);
691 mp::odr odr(ODR_PRINT);
692 odr_set_stream(odr, m_file->fhandle, stream_write, 0);
693 z_GDU(odr, &gdu_req, 0, 0);
698 // unlocked during move
701 Z_GDU *gdu_res = package.response().get();
703 yaz_timing_stop(timer);
704 double duration = yaz_timing_get_real(timer);
706 // scope for locking Ostream
708 boost::mutex::scoped_lock scoped_lock(m_file->m_mutex);
714 log_1line_Z_GDU(gdu_req, gdu_res, w);
717 const char *message = wrbuf_cstr(w);
718 std::ostringstream os;
719 os << m_msg_config << " "
721 << std::fixed << std::setprecision (6) << duration
724 m_file->log(m_time_format, os);
732 std::ostringstream os;
733 os << m_msg_config << " "
735 << std::fixed << std::setprecision (6) << duration
738 m_file->log(m_time_format, os);
745 std::ostringstream os;
746 os << m_msg_config << " " << user << " "
748 << std::fixed << std::setprecision (6) << duration << " "
750 m_file->log(m_time_format, os);
756 std::ostringstream os;
758 os << " response id=" << package.session().id();
760 << (package.session().is_closed() ? "yes " : "no ")
762 << std::fixed << std::setprecision (6) << duration;
763 m_file->log(m_time_format, os);
768 if (gdu_res && gdu_res->which == Z_GDU_Z3950 &&
769 gdu_res->u.z3950->which == Z_APDU_initResponse)
771 std::ostringstream os;
773 os << " init options:";
774 yaz_init_opt_decode(gdu_res->u.z3950->u.initResponse->options,
776 m_file->log(m_time_format, os);
784 mp::odr odr(ODR_PRINT);
785 odr_set_stream(odr, m_file->fhandle, stream_write, 0);
786 z_GDU(odr, &gdu_res, 0, 0);
791 yaz_timing_destroy(&timer);
795 void yf::Log::Impl::openfile(const std::string &fname)
797 std::list<LFilePtr>::const_iterator it
798 = filter_log_files.begin();
799 for (; it != filter_log_files.end(); it++)
801 if ((*it)->m_fname == fname)
807 LFilePtr newfile(new LFile(fname));
808 filter_log_files.push_back(newfile);
813 void yf::Log::Impl::stream_write(ODR o, void *handle, int type, const char *buf, int len)
815 FILE *f = (FILE*) handle;
816 fwrite(buf, len, 1, f ? f : yaz_log_file());
819 void yf::Log::Impl::option_write(const char *name, void *handle)
821 std::ostringstream *os = (std::ostringstream *) handle;
826 yf::Log::Impl::LFile::LFile(std::string fname) :
831 fhandle = fopen(fname.c_str(), "a");
836 yf::Log::Impl::LFile::~LFile()
840 void yf::Log::Impl::LFile::log(const std::string &date_format,
841 std::ostringstream &os)
848 struct tm tm0, *tm = &tm0;
849 localtime_r(&ti, tm);
851 struct tm *tm = localtime(&ti);
853 if (strftime(datestr, sizeof(datestr)-1, date_format.c_str(), tm))
855 fputs(datestr, fhandle);
858 fputs(os.str().c_str(), fhandle);
859 fputc('\n', fhandle);
862 yaz_log(YLOG_LOG, "%s", os.str().c_str());
865 void yf::Log::Impl::LFile::flush()
871 static mp::filter::Base* filter_creator()
873 return new mp::filter::Log;
877 struct metaproxy_1_filter_struct metaproxy_1_filter_log = {
887 * c-file-style: "Stroustrup"
888 * indent-tabs-mode: nil
890 * vim: shiftwidth=4 tabstop=8 expandtab