21 #include <metaproxy/package.hpp>
26 #include <boost/thread/mutex.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>
49 typedef boost::shared_ptr<Log::Impl::LFile>
LFilePtr;
52 Impl(
const std::string &x =
"-");
54 void process(metaproxy_1::Package & package);
57 void openfile(
const std::string &fname);
60 const char *buf,
int len);
62 static void option_write(
const char *name,
void *handle);
85 LFile(std::string fname);
86 LFile(std::string fname, FILE *outf);
87 void log(
const std::string &date_format,
88 std::ostringstream &os);
97 yf::Log::Log() : m_p(new
Impl)
101 yf::Log::Log(
const std::string &x) : m_p(new
Impl(x))
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()
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);
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;
178 char oid_name[OID_STR_MAX+1];
179 wrbuf_puts(w, yaz_oid_to_string_buf(syntax, 0, oid_name));
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)
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 ");
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);
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 ");
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);
331 wrbuf_printf(w,
" %s " ODR_INT_PRINTF
"+" ODR_INT_PRINTF
" ",
332 req->resultSetId, *req->resultSetStartPoint,
333 *req->numberOfRecordsRequested);
336 case Z_APDU_deleteResultSetRequest:
337 wrbuf_puts(w,
"deleteResultSet ");
339 case Z_APDU_accessControlRequest:
340 wrbuf_puts(w,
"accessControl ");
342 case Z_APDU_resourceControlRequest:
343 wrbuf_puts(w,
"resourceControl ");
345 case Z_APDU_triggerResourceControlRequest:
346 wrbuf_puts(w,
"triggerResourceControlRequest");
348 case Z_APDU_resourceReportRequest:
349 wrbuf_puts(w,
"resourceReport ");
351 case Z_APDU_scanRequest:
352 wrbuf_puts(w,
"Scan ");
355 else if (z_res->which != Z_APDU_scanResponse)
356 wrbuf_printf(w,
"? response=%d", z_res->which);
359 Z_ScanRequest *req = z_req->u.scanRequest;
360 Z_ScanResponse *res = z_res->u.scanResponse;
362 for (i = 0 ; i < req->num_databaseNames; i++)
365 wrbuf_printf(w,
"+");
366 wrbuf_puts(w, req->databaseNames[i]);
369 if (!res->scanStatus || *res->scanStatus == 0)
371 else if (*res->scanStatus == 6)
372 wrbuf_puts(w,
"ERROR");
374 wrbuf_printf(w,
"PARTIAL" ODR_INT_PRINTF, *res->scanStatus);
376 wrbuf_printf(w,
" " ODR_INT_PRINTF
" " ODR_INT_PRINTF
"+"
377 ODR_INT_PRINTF
"+" ODR_INT_PRINTF
" ",
378 res->numberOfEntriesReturned ?
379 *res->numberOfEntriesReturned : 0,
380 req->preferredPositionInResponse ?
381 *req->preferredPositionInResponse : 1,
382 *req->numberOfTermsRequested,
383 res->stepSize ? *res->stepSize : 1);
385 yaz_scan_to_wrbuf(w, req->termListAndStartPoint,
389 case Z_APDU_sortRequest:
390 wrbuf_puts(w,
"sort ");
393 else if (z_res->which != Z_APDU_sortResponse)
394 wrbuf_printf(w,
"? response=%d", z_res->which);
397 Z_SortResponse *res = z_res->u.sortResponse;
398 Z_SortRequest *req = z_res->u.sortRequest;
401 if (*res->sortStatus == Z_SortResponse_success)
403 else if (*res->sortStatus == Z_SortResponse_partial_1)
404 wrbuf_puts(w,
"PARTIAL");
405 else if (*res->sortStatus == Z_SortResponse_failure)
406 wrbuf_puts(w,
"ERROR");
409 if (res->diagnostics && res->num_diagnostics >= 1)
415 for (i = 0; i < req->num_inputResultSetNames; i++)
419 wrbuf_puts(w, req->inputResultSetNames[i]);
421 wrbuf_printf(w,
")->%s ",req->sortedResultSetName);
425 case Z_APDU_segmentRequest:
426 wrbuf_puts(w,
"segmentRequest");
428 case Z_APDU_extendedServicesRequest:
429 wrbuf_puts(w,
"extendedServices ");
432 else if (z_res->which != Z_APDU_extendedServicesResponse)
433 wrbuf_printf(w,
"? response=%d", z_res->which);
436 Z_ExtendedServicesResponse *res =
437 z_res->u.extendedServicesResponse;
438 switch (*res->operationStatus)
440 case Z_ExtendedServicesResponse_done:
441 wrbuf_puts(w,
"DONE");
break;
442 case Z_ExtendedServicesResponse_accepted:
443 wrbuf_puts(w,
"ACCEPTED");
break;
444 case Z_ExtendedServicesResponse_failure:
445 wrbuf_puts(w,
"ERROR");
break;
447 wrbuf_printf(w, ODR_INT_PRINTF, *res->operationStatus);
450 if (res->diagnostics && res->num_diagnostics >= 1)
457 wrbuf_puts(w,
"close");
459 case Z_APDU_duplicateDetectionRequest:
460 wrbuf_puts(w,
"duplicateDetention ");
463 else if (z_res->which != Z_APDU_duplicateDetectionResponse)
464 wrbuf_printf(w,
"? response=%d", z_res->which);
467 Z_DuplicateDetectionResponse *res =
468 z_res->u.duplicateDetectionResponse;
472 wrbuf_puts(w,
"ERROR");
475 if (res->diagnostics && res->num_diagnostics >= 1)
476 log_DiagRecs(w, res->num_diagnostics, res->diagnostics);
482 wrbuf_printf(w,
"REQ=%d RES=%d", z_req->which, z_res->which);
489 wrbuf_printf(w,
"%s %s HTTP/%s", req->method, req->path, req->version);
491 wrbuf_printf(w,
" %d %d", res->code, res->content_len);
493 wrbuf_printf(w,
" ?");
498 if (gdu_req && gdu_req->which == Z_GDU_Z3950)
501 (gdu_res && gdu_res->which == Z_GDU_Z3950) ?
502 gdu_res->u.z3950 : 0, w);
504 else if (gdu_req && gdu_req->which == Z_GDU_HTTP_Request)
507 (gdu_res && gdu_res->which == Z_GDU_HTTP_Response) ?
508 gdu_res->u.HTTP_Response : 0, w);
512 void yf::Log::Impl::configure(
const xmlNode *ptr)
514 for (ptr = ptr->children; ptr; ptr = ptr->next)
516 if (ptr->type != XML_ELEMENT_NODE)
518 if (!strcmp((
const char *) ptr->name,
"message"))
519 m_msg_config = mp::xml::get_text(ptr);
520 else if (!strcmp((
const char *) ptr->name,
"filename"))
522 std::string fname = mp::xml::get_text(ptr);
525 else if (!strcmp((
const char *) ptr->name,
"time-format"))
527 m_time_format = mp::xml::get_text(ptr);
529 else if (!strcmp((
const char *) ptr->name,
"category"))
531 const struct _xmlAttr *attr;
532 for (attr = ptr->properties; attr; attr = attr->next)
534 if (!strcmp((
const char *) attr->name,
"line"))
535 m_1line = mp::xml::get_bool(attr->children,
true);
536 else if (!strcmp((
const char *) attr->name,
"access"))
537 m_access = mp::xml::get_bool(attr->children,
true);
538 else if (!strcmp((
const char *) attr->name,
"user-access"))
539 m_user_access = mp::xml::get_bool(attr->children,
true);
540 else if (!strcmp((
const char *) attr->name,
"request-apdu"))
541 m_req_apdu = mp::xml::get_bool(attr->children,
true);
542 else if (!strcmp((
const char *) attr->name,
"response-apdu"))
543 m_res_apdu = mp::xml::get_bool(attr->children,
true);
544 else if (!strcmp((
const char *) attr->name,
"apdu"))
546 m_req_apdu = mp::xml::get_bool(attr->children,
true);
547 m_res_apdu = m_req_apdu;
549 else if (!strcmp((
const char *) attr->name,
552 mp::xml::get_bool(attr->children,
true);
553 else if (!strcmp((
const char *) attr->name,
556 mp::xml::get_bool(attr->children,
true);
557 else if (!strcmp((
const char *) attr->name,
561 mp::xml::get_bool(attr->children,
true);
562 m_res_session = m_req_session;
564 else if (!strcmp((
const char *) attr->name,
567 mp::xml::get_bool(attr->children,
true);
568 else if (!strcmp((
const char *) attr->name,
571 mp::xml::get_bool(attr->children,
true);
573 throw mp::filter::FilterException(
574 "Bad attribute " + std::string((
const char *)
580 throw mp::filter::FilterException(
"Bad element "
581 + std::string((
const char *)
587 void yf::Log::Impl::process(mp::Package &package)
589 Z_GDU *gdu_req = package.request().get();
590 std::string user(
"-");
592 yaz_timing_t timer = yaz_timing_create();
596 boost::mutex::scoped_lock scoped_lock(m_session_mutex);
598 if (gdu_req && gdu_req->which == Z_GDU_Z3950)
600 Z_APDU *apdu_req = gdu_req->u.z3950;
601 if (apdu_req->which == Z_APDU_initRequest)
603 Z_InitRequest *req = apdu_req->u.initRequest;
604 Z_IdAuthentication *a = req->idAuthentication;
607 if (a->which == Z_IdAuthentication_idPass
608 && a->u.idPass->userId)
609 user = a->u.idPass->userId;
610 else if (a->which == Z_IdAuthentication_open)
613 m_sessions[package.session()] = user;
617 std::map<mp::Session,std::string>::iterator it =
618 m_sessions.find(package.session());
619 if (it != m_sessions.end())
622 if (package.session().is_closed())
623 m_sessions.erase(package.session());
627 boost::mutex::scoped_lock scoped_lock(m_file->m_mutex);
633 std::ostringstream os;
634 os << m_msg_config <<
" "
638 m_file->log(m_time_format, os);
646 std::ostringstream os;
647 os << m_msg_config <<
" " << user <<
" "
651 m_file->log(m_time_format, os);
657 std::ostringstream os;
659 os <<
" request id=" << package.session().id();
661 << (package.session().is_closed() ?
"yes" :
"no");
662 m_file->log(m_time_format, os);
667 if (gdu_req && gdu_req->which == Z_GDU_Z3950 &&
668 gdu_req->u.z3950->which == Z_APDU_initRequest)
670 std::ostringstream os;
671 os << m_msg_config <<
" init options:";
672 yaz_init_opt_decode(gdu_req->u.z3950->u.initRequest->options,
674 m_file->log(m_time_format, os);
682 mp::odr odr(ODR_PRINT);
683 odr_set_stream(odr, m_file->fhandle, stream_write, 0);
684 if (!m_file->fhandle)
686 z_GDU(odr, &gdu_req, 0, 0);
687 if (!m_file->fhandle)
696 Z_GDU *gdu_res = package.response().get();
698 gdu_req = package.request().get();
700 yaz_timing_stop(timer);
701 double duration = yaz_timing_get_real(timer);
705 boost::mutex::scoped_lock scoped_lock(m_file->m_mutex);
714 const char *message = wrbuf_cstr(w);
715 std::ostringstream os;
716 os << m_msg_config <<
" "
718 << std::fixed << std::setprecision (6) << duration
721 m_file->log(m_time_format, os);
729 std::ostringstream os;
730 os << m_msg_config <<
" "
732 << std::fixed << std::setprecision (6) << duration
735 m_file->log(m_time_format, os);
742 std::ostringstream os;
743 os << m_msg_config <<
" " << user <<
" "
745 << std::fixed << std::setprecision (6) << duration << " "
747 m_file->log(m_time_format, os);
753 std::ostringstream os;
755 os <<
" response id=" << package.session().id();
757 << (package.session().is_closed() ?
"yes " :
"no ")
759 << std::fixed << std::setprecision (6) << duration;
760 m_file->log(m_time_format, os);
765 if (gdu_res && gdu_res->which == Z_GDU_Z3950 &&
766 gdu_res->u.z3950->which == Z_APDU_initResponse)
768 std::ostringstream os;
770 os <<
" init options:";
771 yaz_init_opt_decode(gdu_res->u.z3950->u.initResponse->options,
773 m_file->log(m_time_format, os);
781 mp::odr odr(ODR_PRINT);
782 odr_set_stream(odr, m_file->fhandle, stream_write, 0);
783 if (!m_file->fhandle)
785 z_GDU(odr, &gdu_res, 0, 0);
786 if (!m_file->fhandle)
792 yaz_timing_destroy(&timer);
796 void yf::Log::Impl::openfile(
const std::string &fname)
798 std::list<LFilePtr>::const_iterator it
802 if ((*it)->m_fname == fname)
814 void yf::Log::Impl::stream_write(ODR o,
void *handle,
int type,
const char *buf,
int len)
816 FILE *f = (FILE*) handle;
817 fwrite(buf, len, 1, f ? f : yaz_log_file());
820 void yf::Log::Impl::option_write(
const char *name,
void *handle)
822 std::ostringstream *os = (std::ostringstream *) handle;
827 yf::Log::Impl::LFile::LFile(std::string fname) :
832 fhandle = fopen(fname.c_str(),
"a");
837 yf::Log::Impl::LFile::~LFile()
841 void yf::Log::Impl::LFile::log(
const std::string &date_format,
842 std::ostringstream &os)
849 struct tm tm0, *tm = &tm0;
850 localtime_r(&ti, tm);
852 struct tm *tm = localtime(&ti);
854 if (strftime(datestr,
sizeof(datestr)-1, date_format.c_str(), tm))
856 fputs(datestr, fhandle);
859 fputs(os.str().c_str(), fhandle);
860 fputc(
'\n', fhandle);
863 yaz_log(YLOG_LOG,
"%s", os.str().c_str());
866 void yf::Log::Impl::LFile::flush()
874 return new mp::filter::Log;
static void log_1line_Z_APDU(Z_APDU *z_req, Z_APDU *z_res, WRBUF w)
static void log_syntax(WRBUF w, const Odr_oid *syntax)
static void log_DefaultDiagFormat(WRBUF w, Z_DefaultDiagFormat *e)
static void log_DiagRecs(WRBUF w, int num_diagRecs, Z_DiagRec **diags)
static void log_1line_Z_GDU(Z_GDU *gdu_req, Z_GDU *gdu_res, WRBUF w)
struct metaproxy_1_filter_struct metaproxy_1_filter_log
static mp::filter::Base * filter_creator()
static void log_1line_Z_HTTP(Z_HTTP_Request *req, Z_HTTP_Response *res, WRBUF w)