1 /* This file is part of Metaproxy.
2 Copyright (C) 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 boost::mutex m_session_mutex;
76 std::map<mp::Session, std::string> m_sessions;
79 class Log::Impl::LFile {
85 LFile(std::string fname);
86 LFile(std::string fname, FILE *outf);
87 void log(const std::string &date_format,
88 std::ostringstream &os);
91 static std::list<yf::Log::Impl::LFilePtr> filter_log_files;
95 // define Pimpl wrapper forwarding to Impl
97 yf::Log::Log() : m_p(new Impl)
101 yf::Log::Log(const std::string &x) : m_p(new Impl(x))
106 { // must have a destructor because of boost::scoped_ptr
109 void yf::Log::configure(const xmlNode *xmlnode, bool test_only,
112 m_p->configure(xmlnode);
115 void yf::Log::process(mp::Package &package) const
117 m_p->process(package);
121 yf::Log::Impl::Impl(const std::string &x)
125 m_user_access(false),
128 m_req_session(false),
129 m_res_session(false),
130 m_init_options(false),
131 m_time_format("%H:%M:%S-%d/%m")
137 yf::Log::Impl::~Impl()
141 static void log_DefaultDiagFormat(WRBUF w, Z_DefaultDiagFormat *e)
144 wrbuf_printf(w, ODR_INT_PRINTF " ", *e->condition);
146 wrbuf_puts(w, "?? ");
147 if (e->which == Z_DefaultDiagFormat_v2Addinfo && e->u.v2Addinfo)
150 wrbuf_puts(w, e->u.v2Addinfo);
153 else if (e->which == Z_DefaultDiagFormat_v3Addinfo && e->u.v3Addinfo)
156 wrbuf_puts(w, e->u.v3Addinfo);
163 static void log_DiagRecs(WRBUF w, int num_diagRecs, Z_DiagRec **diags)
165 if (diags[0]->which != Z_DiagRec_defaultFormat)
166 wrbuf_puts(w ,"(diag not in default format?)");
169 Z_DefaultDiagFormat *e = diags[0]->u.defaultFormat;
170 log_DefaultDiagFormat(w, e);
174 static void log_syntax(WRBUF w, const Odr_oid *syntax)
178 char oid_name[OID_STR_MAX+1];
179 wrbuf_puts(w, yaz_oid_to_string_buf(syntax, 0, oid_name));
185 static void log_1line_Z_APDU(Z_APDU *z_req, Z_APDU *z_res, WRBUF w)
187 switch (z_req->which)
189 case Z_APDU_initRequest:
190 wrbuf_puts(w, "Init ");
193 else if (z_res->which != Z_APDU_initResponse)
194 wrbuf_printf(w, "? response=%d", z_res->which);
197 Z_InitRequest *req = z_req->u.initRequest;
198 Z_InitResponse *res = z_res->u.initResponse;
199 if (res->result && *res->result)
200 wrbuf_printf(w, "OK -");
203 Z_External *uif = res->userInformationField;
204 bool got_code = false;
205 wrbuf_printf(w, "ERROR ");
206 if (uif && uif->which == Z_External_userInfo1)
208 Z_OtherInformation *ui = uif->u.userInfo1;
209 if (ui->num_elements >= 1)
211 Z_OtherInformationUnit *unit = ui->list[0];
212 if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
213 unit->information.externallyDefinedInfo &&
214 unit->information.externallyDefinedInfo->which ==
217 Z_DiagnosticFormat *diag =
218 unit->information.externallyDefinedInfo->
222 Z_DiagnosticFormat_s *ds = diag->elements[0];
224 Z_DiagnosticFormat_s_defaultDiagRec)
226 log_DefaultDiagFormat(w,
227 ds->u.defaultDiagRec);
238 wrbuf_printf(w, " ID:%s Name:%s Version:%s",
239 req->implementationId ? req->implementationId :"-",
240 req->implementationName ?req->implementationName : "-",
241 req->implementationVersion ?
242 req->implementationVersion : "-");
245 case Z_APDU_searchRequest:
246 wrbuf_puts(w, "Search ");
249 else if (z_res->which != Z_APDU_searchResponse)
250 wrbuf_printf(w, "? response=%d", z_res->which);
253 Z_SearchRequest *req = z_req->u.searchRequest;
254 Z_SearchResponse *res = z_res->u.searchResponse;
256 for (i = 0 ; i < req->num_databaseNames; i++)
259 wrbuf_printf(w, "+");
260 wrbuf_puts(w, req->databaseNames[i]);
262 wrbuf_printf(w, " ");
265 wrbuf_printf(w, "OK " ODR_INT_PRINTF " %s", *res->resultCount,
268 else if (res->records->which == Z_Records_DBOSD)
270 wrbuf_printf(w, "OK " ODR_INT_PRINTF " %s", *res->resultCount,
273 else if (res->records->which == Z_Records_NSD)
275 wrbuf_puts(w, "ERROR ");
276 log_DefaultDiagFormat(w,
277 res->records->u.nonSurrogateDiagnostic);
279 else if (res->records->which == Z_Records_multipleNSD)
281 wrbuf_puts(w, "ERROR ");
284 res->records->u.multipleNonSurDiagnostics->num_diagRecs,
285 res->records->u.multipleNonSurDiagnostics->diagRecs);
288 log_syntax(w, req->preferredRecordSyntax);
289 wrbuf_printf(w, " 1+" ODR_INT_PRINTF " ",
290 res->numberOfRecordsReturned
291 ? *res->numberOfRecordsReturned : 0);
292 yaz_query_to_wrbuf(w, req->query);
295 case Z_APDU_presentRequest:
296 wrbuf_puts(w, "Present ");
299 else if (z_res->which != Z_APDU_presentResponse)
300 wrbuf_printf(w, "? response=%d", z_res->which);
303 Z_PresentRequest *req = z_req->u.presentRequest;
304 Z_PresentResponse *res = z_res->u.presentResponse;
308 wrbuf_printf(w, "OK");
310 else if (res->records->which == Z_Records_DBOSD)
312 wrbuf_printf(w, "OK");
314 else if (res->records->which == Z_Records_NSD)
316 wrbuf_puts(w, "ERROR ");
317 log_DefaultDiagFormat(w,
318 res->records->u.nonSurrogateDiagnostic);
320 else if (res->records->which == Z_Records_multipleNSD)
322 wrbuf_puts(w, "ERROR ");
325 res->records->u.multipleNonSurDiagnostics->num_diagRecs,
326 res->records->u.multipleNonSurDiagnostics->diagRecs);
329 assert(req->preferredRecordSyntax);
330 log_syntax(w, req->preferredRecordSyntax);
332 wrbuf_printf(w, " %s " ODR_INT_PRINTF "+" ODR_INT_PRINTF " ",
333 req->resultSetId, *req->resultSetStartPoint,
334 *req->numberOfRecordsRequested);
337 case Z_APDU_deleteResultSetRequest:
338 wrbuf_puts(w, "deleteResultSet ");
340 case Z_APDU_accessControlRequest:
341 wrbuf_puts(w, "accessControl ");
343 case Z_APDU_resourceControlRequest:
344 wrbuf_puts(w, "resourceControl ");
346 case Z_APDU_triggerResourceControlRequest:
347 wrbuf_puts(w, "triggerResourceControlRequest");
349 case Z_APDU_resourceReportRequest:
350 wrbuf_puts(w, "resourceReport ");
352 case Z_APDU_scanRequest:
353 wrbuf_puts(w, "Scan ");
356 else if (z_res->which != Z_APDU_scanResponse)
357 wrbuf_printf(w, "? response=%d", z_res->which);
360 Z_ScanRequest *req = z_req->u.scanRequest;
361 Z_ScanResponse *res = z_res->u.scanResponse;
363 for (i = 0 ; i < req->num_databaseNames; i++)
366 wrbuf_printf(w, "+");
367 wrbuf_puts(w, req->databaseNames[i]);
370 if (!res->scanStatus || *res->scanStatus == 0)
372 else if (*res->scanStatus == 6)
373 wrbuf_puts(w, "ERROR");
375 wrbuf_printf(w, "PARTIAL" ODR_INT_PRINTF, *res->scanStatus);
377 wrbuf_printf(w, " " ODR_INT_PRINTF " " ODR_INT_PRINTF "+"
378 ODR_INT_PRINTF "+" ODR_INT_PRINTF " ",
379 res->numberOfEntriesReturned ?
380 *res->numberOfEntriesReturned : 0,
381 req->preferredPositionInResponse ?
382 *req->preferredPositionInResponse : 1,
383 *req->numberOfTermsRequested,
384 res->stepSize ? *res->stepSize : 1);
386 yaz_scan_to_wrbuf(w, req->termListAndStartPoint,
390 case Z_APDU_sortRequest:
391 wrbuf_puts(w, "sort ");
394 else if (z_res->which != Z_APDU_sortResponse)
395 wrbuf_printf(w, "? response=%d", z_res->which);
398 Z_SortResponse *res = z_res->u.sortResponse;
399 Z_SortRequest *req = z_res->u.sortRequest;
402 if (*res->sortStatus == Z_SortResponse_success)
404 else if (*res->sortStatus == Z_SortResponse_partial_1)
405 wrbuf_puts(w, "PARTIAL");
406 else if (*res->sortStatus == Z_SortResponse_failure)
407 wrbuf_puts(w, "ERROR");
410 if (res->diagnostics && res->num_diagnostics >= 1)
411 log_DiagRecs(w, res->num_diagnostics,res->diagnostics);
416 for (i = 0; i < req->num_inputResultSetNames; i++)
420 wrbuf_puts(w, req->inputResultSetNames[i]);
422 wrbuf_printf(w, ")->%s ",req->sortedResultSetName);
426 case Z_APDU_segmentRequest:
427 wrbuf_puts(w, "segmentRequest");
429 case Z_APDU_extendedServicesRequest:
430 wrbuf_puts(w, "extendedServices ");
433 else if (z_res->which != Z_APDU_extendedServicesResponse)
434 wrbuf_printf(w, "? response=%d", z_res->which);
437 Z_ExtendedServicesResponse *res =
438 z_res->u.extendedServicesResponse;
439 switch (*res->operationStatus)
441 case Z_ExtendedServicesResponse_done:
442 wrbuf_puts(w, "DONE"); break;
443 case Z_ExtendedServicesResponse_accepted:
444 wrbuf_puts(w, "ACCEPTED"); break;
445 case Z_ExtendedServicesResponse_failure:
446 wrbuf_puts(w, "ERROR"); break;
448 wrbuf_printf(w, ODR_INT_PRINTF, *res->operationStatus);
451 if (res->diagnostics && res->num_diagnostics >= 1)
452 log_DiagRecs(w, res->num_diagnostics,res->diagnostics);
458 wrbuf_puts(w, "close");
460 case Z_APDU_duplicateDetectionRequest:
461 wrbuf_puts(w, "duplicateDetention ");
464 else if (z_res->which != Z_APDU_duplicateDetectionResponse)
465 wrbuf_printf(w, "? response=%d", z_res->which);
468 Z_DuplicateDetectionResponse *res =
469 z_res->u.duplicateDetectionResponse;
473 wrbuf_puts(w, "ERROR");
476 if (res->diagnostics && res->num_diagnostics >= 1)
477 log_DiagRecs(w, res->num_diagnostics, res->diagnostics);
483 wrbuf_printf(w, "REQ=%d RES=%d", z_req->which, z_res->which);
488 static void log_1line_Z_HTTP(Z_HTTP_Request *req, Z_HTTP_Response *res, WRBUF w)
490 wrbuf_printf(w, "%s %s HTTP/%s", req->method, req->path, req->version);
492 wrbuf_printf(w, " %d %d", res->code, res->content_len);
494 wrbuf_printf(w, " ?");
497 static void log_1line_Z_GDU(Z_GDU *gdu_req, Z_GDU *gdu_res, WRBUF w)
499 if (gdu_req && gdu_req->which == Z_GDU_Z3950)
501 log_1line_Z_APDU(gdu_req->u.z3950,
502 (gdu_res && gdu_res->which == Z_GDU_Z3950) ?
503 gdu_res->u.z3950 : 0, w);
505 else if (gdu_req && gdu_req->which == Z_GDU_HTTP_Request)
507 log_1line_Z_HTTP(gdu_req->u.HTTP_Request,
508 (gdu_res && gdu_res->which == Z_GDU_HTTP_Response) ?
509 gdu_res->u.HTTP_Response : 0, w);
513 void yf::Log::Impl::configure(const xmlNode *ptr)
515 for (ptr = ptr->children; ptr; ptr = ptr->next)
517 if (ptr->type != XML_ELEMENT_NODE)
519 if (!strcmp((const char *) ptr->name, "message"))
520 m_msg_config = mp::xml::get_text(ptr);
521 else if (!strcmp((const char *) ptr->name, "filename"))
523 std::string fname = mp::xml::get_text(ptr);
526 else if (!strcmp((const char *) ptr->name, "time-format"))
528 m_time_format = mp::xml::get_text(ptr);
530 else if (!strcmp((const char *) ptr->name, "category"))
532 const struct _xmlAttr *attr;
533 for (attr = ptr->properties; attr; attr = attr->next)
535 if (!strcmp((const char *) attr->name, "line"))
536 m_1line = mp::xml::get_bool(attr->children, true);
537 else if (!strcmp((const char *) attr->name, "access"))
538 m_access = mp::xml::get_bool(attr->children, true);
539 else if (!strcmp((const char *) attr->name, "user-access"))
540 m_user_access = mp::xml::get_bool(attr->children, true);
541 else if (!strcmp((const char *) attr->name, "request-apdu"))
542 m_req_apdu = mp::xml::get_bool(attr->children, true);
543 else if (!strcmp((const char *) attr->name, "response-apdu"))
544 m_res_apdu = mp::xml::get_bool(attr->children, true);
545 else if (!strcmp((const char *) attr->name, "apdu"))
547 m_req_apdu = mp::xml::get_bool(attr->children, true);
548 m_res_apdu = m_req_apdu;
550 else if (!strcmp((const char *) attr->name,
553 mp::xml::get_bool(attr->children, true);
554 else if (!strcmp((const char *) attr->name,
557 mp::xml::get_bool(attr->children, true);
558 else if (!strcmp((const char *) attr->name,
562 mp::xml::get_bool(attr->children, true);
563 m_res_session = m_req_session;
565 else if (!strcmp((const char *) attr->name,
568 mp::xml::get_bool(attr->children, true);
569 else if (!strcmp((const char *) attr->name,
572 mp::xml::get_bool(attr->children, true);
574 throw mp::filter::FilterException(
575 "Bad attribute " + std::string((const char *)
581 throw mp::filter::FilterException("Bad element "
582 + std::string((const char *)
588 void yf::Log::Impl::process(mp::Package &package)
590 Z_GDU *gdu_req = package.request().get();
591 std::string user("-");
593 yaz_timing_t timer = yaz_timing_create();
595 // scope for session lock
597 boost::mutex::scoped_lock scoped_lock(m_session_mutex);
599 if (gdu_req && gdu_req->which == Z_GDU_Z3950)
601 Z_APDU *apdu_req = gdu_req->u.z3950;
602 if (apdu_req->which == Z_APDU_initRequest)
604 Z_InitRequest *req = apdu_req->u.initRequest;
605 Z_IdAuthentication *a = req->idAuthentication;
608 if (a->which == Z_IdAuthentication_idPass
609 && a->u.idPass->userId)
610 user = a->u.idPass->userId;
611 else if (a->which == Z_IdAuthentication_open)
614 m_sessions[package.session()] = user;
618 std::map<mp::Session,std::string>::iterator it =
619 m_sessions.find(package.session());
620 if (it != m_sessions.end())
623 if (package.session().is_closed())
624 m_sessions.erase(package.session());
626 // scope for locking Ostream
628 boost::mutex::scoped_lock scoped_lock(m_file->m_mutex);
634 std::ostringstream os;
635 os << m_msg_config << " "
639 m_file->log(m_time_format, os);
647 std::ostringstream os;
648 os << m_msg_config << " " << user << " "
652 m_file->log(m_time_format, os);
658 std::ostringstream os;
660 os << " request id=" << package.session().id();
662 << (package.session().is_closed() ? "yes" : "no");
663 m_file->log(m_time_format, os);
668 if (gdu_req && gdu_req->which == Z_GDU_Z3950 &&
669 gdu_req->u.z3950->which == Z_APDU_initRequest)
671 std::ostringstream os;
672 os << m_msg_config << " init options:";
673 yaz_init_opt_decode(gdu_req->u.z3950->u.initRequest->options,
675 m_file->log(m_time_format, os);
683 mp::odr odr(ODR_PRINT);
684 odr_set_stream(odr, m_file->fhandle, stream_write, 0);
685 z_GDU(odr, &gdu_req, 0, 0);
690 // unlocked during move
693 Z_GDU *gdu_res = package.response().get();
695 gdu_req = package.request().get();
697 yaz_timing_stop(timer);
698 double duration = yaz_timing_get_real(timer);
700 // scope for locking Ostream
702 boost::mutex::scoped_lock scoped_lock(m_file->m_mutex);
708 log_1line_Z_GDU(gdu_req, gdu_res, w);
711 const char *message = wrbuf_cstr(w);
712 std::ostringstream os;
713 os << m_msg_config << " "
715 << std::fixed << std::setprecision (6) << duration
718 m_file->log(m_time_format, os);
726 std::ostringstream os;
727 os << m_msg_config << " "
729 << std::fixed << std::setprecision (6) << duration
732 m_file->log(m_time_format, os);
739 std::ostringstream os;
740 os << m_msg_config << " " << user << " "
742 << std::fixed << std::setprecision (6) << duration << " "
744 m_file->log(m_time_format, os);
750 std::ostringstream os;
752 os << " response id=" << package.session().id();
754 << (package.session().is_closed() ? "yes " : "no ")
756 << std::fixed << std::setprecision (6) << duration;
757 m_file->log(m_time_format, os);
762 if (gdu_res && gdu_res->which == Z_GDU_Z3950 &&
763 gdu_res->u.z3950->which == Z_APDU_initResponse)
765 std::ostringstream os;
767 os << " init options:";
768 yaz_init_opt_decode(gdu_res->u.z3950->u.initResponse->options,
770 m_file->log(m_time_format, os);
778 mp::odr odr(ODR_PRINT);
779 odr_set_stream(odr, m_file->fhandle, stream_write, 0);
780 z_GDU(odr, &gdu_res, 0, 0);
785 yaz_timing_destroy(&timer);
789 void yf::Log::Impl::openfile(const std::string &fname)
791 std::list<LFilePtr>::const_iterator it
792 = filter_log_files.begin();
793 for (; it != filter_log_files.end(); it++)
795 if ((*it)->m_fname == fname)
801 LFilePtr newfile(new LFile(fname));
802 filter_log_files.push_back(newfile);
807 void yf::Log::Impl::stream_write(ODR o, void *handle, int type, const char *buf, int len)
809 FILE *f = (FILE*) handle;
810 fwrite(buf, len, 1, f ? f : yaz_log_file());
813 void yf::Log::Impl::option_write(const char *name, void *handle)
815 std::ostringstream *os = (std::ostringstream *) handle;
820 yf::Log::Impl::LFile::LFile(std::string fname) :
825 fhandle = fopen(fname.c_str(), "a");
830 yf::Log::Impl::LFile::~LFile()
834 void yf::Log::Impl::LFile::log(const std::string &date_format,
835 std::ostringstream &os)
842 struct tm tm0, *tm = &tm0;
843 localtime_r(&ti, tm);
845 struct tm *tm = localtime(&ti);
847 if (strftime(datestr, sizeof(datestr)-1, date_format.c_str(), tm))
849 fputs(datestr, fhandle);
852 fputs(os.str().c_str(), fhandle);
853 fputc('\n', fhandle);
856 yaz_log(YLOG_LOG, "%s", os.str().c_str());
859 void yf::Log::Impl::LFile::flush()
865 static mp::filter::Base* filter_creator()
867 return new mp::filter::Log;
871 struct metaproxy_1_filter_struct metaproxy_1_filter_log = {
881 * c-file-style: "Stroustrup"
882 * indent-tabs-mode: nil
884 * vim: shiftwidth=4 tabstop=8 expandtab