Fix PR10: Gcm_input may loose its input message
[gnucomo.git] / src / gcm_input / message.cpp
index 9e761b5..9393a16 100644 (file)
 
 *****************************/
 
-static const char *RCSID = "$Id: message.cpp,v 1.19 2011-03-24 10:20:37 arjen Exp $";
+#include <unistd.h>
 
+#include <fstream>
 #include <algorithm>
 #include <libxml/xpath.h>
 #include <libxml/debugXML.h>
+
 #include "message.h"
 
 //#define DEBUG
@@ -508,6 +510,8 @@ void client_message::enterXML()
       }
       if (strcmp((char *)node->name, "log") == 0)
       {
+         int log_entry_counter = 0;
+
          //  Each child contains a log entry, raw or cooked.
 
          node = node->children;
@@ -515,12 +519,18 @@ void client_message::enterXML()
          {
             if (node->type == XML_ELEMENT_NODE)
             {
+               log_entry_counter++;
+
                xmlNodePtr  item;
                String      log_hostname;
                UTC         log_date;
                String      raw("");;
                String      log_service;
 
+               if (verbose)
+               {
+                  *Log << "Log entry " << log_entry_counter << " is " << node->name << "\n";
+               }
                if (strcmp((char *)node->name, "raw") == 0 && node->children != NULL)
                {
                   item = node->children;
@@ -598,7 +608,7 @@ void client_message::enterXML()
                   }
                   else
                   {
-                     *Log << "<timestamp> missing from cooked log element.\n";
+                     *Log << "<timestamp> missing from cooked log element nr " << log_entry_counter << ".\n";
                   }
 
                   res = xmlXPathEval((const xmlChar *)"raw/text()", pathcontext);
@@ -609,7 +619,7 @@ void client_message::enterXML()
                   }
                   else
                   {
-                     *Log << "<raw> missing from cooked log element.\n";
+                     *Log << "<raw> missing from cooked log element nr " << log_entry_counter << ".\n";
                   }
 
                }
@@ -1102,11 +1112,23 @@ void client_message::enterXML()
    }
 }
 
+void client_message::saveXML(String filename)
+{
+   std::ofstream    xmlfile;
+
+   xmlfile.open(filename);
+   xmlfile << xmlBuffer.str();
+}
+
 /*=========================================================================
 **  NAME           : enter
 **  SYNOPSIS       : int enter()
 **  PARAMETERS     : 
 **  RETURN VALUE   : The number of lines successfully parsed from the input
+**                   or a negative number if an error is encountered.
+**                   The error return values are:
+**                    -1  No database connection.
+**                    -2  XML parser error.
 **
 **  DESCRIPTION    : 
 **
@@ -1114,12 +1136,14 @@ void client_message::enterXML()
 **  VARS CHANGED   :
 **  FUNCTIONS USED :
 **  SEE ALSO       :
-**  LAST MODIFIED  : Nov 26, 2003
+**  LAST MODIFIED  : Sep 22, 2020
 **=========================================================================
 */
 
 int client_message::enter()
 {
+   int nr_lines = 0;
+
    pan.mf->set_message_type(pan.lc->message_type());
 
    pan.mf->construct_XML(input, xmlBuffer);
@@ -1132,18 +1156,27 @@ int client_message::enter()
 
    xmlDom = xmlParseMemory(xmlBuffer.str(), xmlBuffer.pcount());
 
-   if (xmlDom)
+   if (database.is_connected())
    {
-      if (extractHeader())
+      if (xmlDom)
+      {
+         if (extractHeader())
+         {
+            enterXML();
+         }
+      }
+      else
       {
-         enterXML();
+         *Log << "XML parser FAILED.\n";
+         nr_lines = -2;   //  XML parse error
       }
    }
    else
    {
-      *Log << "XML parser FAILED.\n";
+      *Log << "Database connection FAILED.\n";
+      nr_lines = -1;  // No database connection
    }
 
-   return 0;
+   return nr_lines;
 
 }