Log filter writes by default to stdout again.
[metaproxy-moved-to-github.git] / src / filter_log.cpp
index fc457bb..2e60d6d 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: filter_log.cpp,v 1.19 2006-06-19 13:08:00 adam Exp $
+/* $Id: filter_log.cpp,v 1.21 2006-06-21 12:50:35 adam Exp $
    Copyright (c) 2005-2006, Index Data.
 
    See the LICENSE file for details
@@ -28,8 +28,10 @@ namespace metaproxy_1 {
         public:
             boost::mutex m_mutex;
             std::string m_fname;
-            std::ofstream out;
+            std::ofstream fout;
+            std::ostream &out;
             LFile(std::string fname);
+            LFile(std::string fname, std::ostream &use_this);
         };
         typedef boost::shared_ptr<Log::LFile> LFilePtr;
         class Log::Rep {
@@ -42,6 +44,7 @@ namespace metaproxy_1 {
             bool m_res_apdu;
             bool m_req_session;
             bool m_res_session;
+            bool m_init_options;
         };
         // Only used during configure stage (no threading)
         static std::list<LFilePtr> filter_log_files;
@@ -54,6 +57,8 @@ yf::Log::Rep::Rep()
     m_res_apdu = true;
     m_req_session = true;
     m_res_session = true;
+    m_init_options = false;
+    openfile("");
 }
 
 yf::Log::Log(const std::string &x) : m_p(new Rep)
@@ -73,6 +78,12 @@ void stream_write(ODR o, void *handle, int type, const char *buf, int len)
     lfile->out.write(buf, len);
 }
 
+void option_write(const char *name, void *handle)
+{
+    yf::Log::LFile *lfile = (yf::Log::LFile*) handle;
+    lfile->out << " " << name;
+}
+
 void yf::Log::process(mp::Package &package) const
 {
     Z_GDU *gdu;
@@ -93,6 +104,20 @@ void yf::Log::process(mp::Package &package) const
                              << (package.session().is_closed() ? "yes" : "no")
                              << "\n";
         }
+        if (m_p->m_init_options)
+        {
+            gdu = package.request().get();
+            if (gdu && gdu->which == Z_GDU_Z3950 &&
+                gdu->u.z3950->which == Z_APDU_initRequest)
+            {
+                m_p->m_file->out << receive_time << " " << m_p->m_msg;
+                m_p->m_file->out << " init options:";
+                yaz_init_opt_decode(gdu->u.z3950->u.initRequest->options,
+                                    option_write, m_p->m_file.get());
+                m_p->m_file->out << "\n";
+            }
+        }
+
         if (m_p->m_req_apdu)
         {
             gdu = package.request().get();
@@ -127,6 +152,19 @@ void yf::Log::process(mp::Package &package) const
                              << "duration=" << duration      
                              << "\n";
         }
+        if (m_p->m_init_options)
+        {
+            gdu = package.response().get();
+            if (gdu && gdu->which == Z_GDU_Z3950 &&
+                gdu->u.z3950->which == Z_APDU_initResponse)
+            {
+                m_p->m_file->out << receive_time << " " << m_p->m_msg;
+                m_p->m_file->out << " init options:";
+                yaz_init_opt_decode(gdu->u.z3950->u.initResponse->options,
+                                    option_write, m_p->m_file.get());
+                m_p->m_file->out << "\n";
+            }
+        }
         if (m_p->m_res_apdu)
         {
             gdu = package.response().get();
@@ -141,9 +179,14 @@ void yf::Log::process(mp::Package &package) const
     }
 }
 
-yf::Log::LFile::LFile(std::string fname) : m_fname(fname)
+yf::Log::LFile::LFile(std::string fname) : 
+    m_fname(fname), fout(fname.c_str()), out(fout)
+{
+}
+
+yf::Log::LFile::LFile(std::string fname, std::ostream &use_this) : 
+    m_fname(fname), out(use_this)
 {
-    out.open(fname.c_str());
 }
 
 void yf::Log::Rep::openfile(const std::string &fname)
@@ -157,7 +200,10 @@ void yf::Log::Rep::openfile(const std::string &fname)
             return;
         }
     }
-    LFilePtr newfile(new LFile(fname));
+    // open stdout for empty file
+    LFilePtr newfile(fname.length() == 0 
+                     ? new LFile(fname, std::cout) 
+                     : new LFile(fname));
     filter_log_files.push_back(newfile);
     m_file = newfile;
 }
@@ -184,6 +230,11 @@ void yf::Log::configure(const xmlNode *ptr)
                     m_p->m_req_apdu = mp::xml::get_bool(attr->children, true);
                 else if (!strcmp((const char *) attr->name, "response-apdu"))
                     m_p->m_res_apdu = mp::xml::get_bool(attr->children, true);
+                else if (!strcmp((const char *) attr->name, "apdu"))
+                {
+                    m_p->m_req_apdu = mp::xml::get_bool(attr->children, true);
+                    m_p->m_res_apdu = m_p->m_req_apdu;
+                }
                 else if (!strcmp((const char *) attr->name,
                                  "request-session"))
                     m_p->m_req_session = 
@@ -192,6 +243,17 @@ void yf::Log::configure(const xmlNode *ptr)
                                  "response-session"))
                     m_p->m_res_session = 
                         mp::xml::get_bool(attr->children, true);
+                else if (!strcmp((const char *) attr->name,
+                                 "session"))
+                {
+                    m_p->m_req_session = 
+                        mp::xml::get_bool(attr->children, true);
+                    m_p->m_res_session = m_p->m_req_session;
+                }
+                else if (!strcmp((const char *) attr->name, 
+                                 "init-options"))
+                    m_p->m_init_options = 
+                        mp::xml::get_bool(attr->children, true);
                 else
                     throw mp::filter::FilterException(
                         "Bad attribute " + std::string((const char *)
@@ -205,8 +267,6 @@ void yf::Log::configure(const xmlNode *ptr)
                                                              ptr->name));
         }
     }
-    if (!m_p->m_file)
-        m_p->openfile("metaproxy.log");
 }
 
 static mp::filter::Base* filter_creator()