[mythtv] Patch: Event log in database

Matt White whitem at arts.usask.ca
Sat Feb 21 23:17:55 EST 2004


Attached is a patch I've been working on.  This patch adds a logging
function that logs to a table in the database.  With the patch applied,
a new method is added to gContext:

gContext->LogEntry(const QString &module, int priority,
                    const QString &message, const QString &details);

where
   module is the name of the Myth module logging the event
          (ie. mythbackend, mythfilldatabase, etc).

   priority is the level of importance of the event, from the following
          choices:

enum LogPriorities {
     LP_DEBUG     = 1,
     LP_INFO      = 2,
     LP_NOTICE    = 3,
     LP_WARNING   = 4,
     LP_ERROR     = 5,
     LP_CRITICAL  = 6,
     LP_EMERG     = 7
};

   message is a short summary message for the event

   details is an optional more detailed message

Also attached is a little perl script that can be run periodically to
email log events.

The reason I've added this function is so that we have some way of
displaying messages to the user without having to ssh into your Myth
box to check logs.  I've got this patch installed on my parents' Myth
machine.  That way, it emails me every couple of days, and I can keep
track of any problems they're having more easily.

I've also created a MythLog module, which will show events via the
Myth interface.  I'll likely create a patch for MythWeb to display
the log as well if this patch is accepted.

The patch is against CVS as of Feb. 21, 20:08 CST.  I've attached
the perl script (mythmaillog.pl) as a separate file, since cvs diff
won't include new files.

My patch adds a few log calls (in mythbackend and mythfilldatabase) -
I've been going through the code and adding log calls in places where
I thought they were appropriate.  If this patch gets applied,
Isaac and the other module writers will probably have better ideas
about what needs logging.

My patch also adds the code to dbcheck.cpp to create the mythlog table,
upgrading the dbschemaversion to 1034.

This patch does apply against 0.14, with the exception of a couple of
log calls in mythbackend (starting mythbackend).

The MythLog module can be found at:

http://borris.usask.ca/mythtv/mythlog.tar.gz

A screenshot is also available:

http://borris.usask.ca/mythtv/mythevent.jpg

I've put the patch and perl script up there, too, just in case they
don't come through with this message:

http://borris.usask.ca/mythtv/logpatch.diff
http://borris.usask.ca/mythtv/mythmaillog.pl

The patch should be applied with 'patch -p1 < logpatch.diff' at the
top of the MythTV tree.

The MythLog module unfortunately shows off my complete uselessness
with designing pretty user interfaces :-)

Any patches or suggestions are welcome.

-- 
Matt White                          whitem at arts.usask.ca
Arts and Science Computer Labs      University of Saskatchewan

It sure is Monday... Ain't it a sin
I've gotta work my way thru the week again.
	- Mark Chesnutt..."Sure Is Monday"
-------------- next part --------------
Index: libs/libmyth/mythcontext.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmyth/mythcontext.cpp,v
retrieving revision 1.110
diff -u -w -r1.110 mythcontext.cpp
--- libs/libmyth/mythcontext.cpp	5 Feb 2004 22:50:36 -0000	1.110
+++ libs/libmyth/mythcontext.cpp	22 Feb 2004 03:55:27 -0000
@@ -845,6 +845,33 @@
     return retval.toInt();
 }
 
+void MythContext::LogEntry(const QString &module, int priority,
+                     const QString &message, const QString &details)
+{
+    if (gContext->GetNumSetting("LogEnabled", 0) == 1)
+    {
+        d->dbLock.lock();
+    
+        if (d->m_db->isOpen())
+        {
+            KickDatabase(d->m_db);
+    
+            QString querystr = QString("INSERT INTO mythlog ( module, priority, "
+                                       "logdate, host, message, details) "
+                                       "values ( '%1', %2, now(), '%3', "
+                                       "'%4','%5' );") . arg(module) .
+                                       arg(priority) . arg(d->m_localhostname) .
+                                       arg(message) . arg(details);
+    
+            QSqlQuery result = d->m_db->exec(querystr);
+            if (!result.isActive())
+                MythContext::DBError("LogEntry", querystr);
+        }
+    
+        d->dbLock.unlock();
+    }
+}
+
 QString MythContext::GetSettingOnHost(const QString &key, const QString &host,
                                       const QString &defaultval)
 {
Index: libs/libmyth/mythcontext.h
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmyth/mythcontext.h,v
retrieving revision 1.128
diff -u -w -r1.128 mythcontext.h
--- libs/libmyth/mythcontext.h	17 Feb 2004 01:13:19 -0000	1.128
+++ libs/libmyth/mythcontext.h	22 Feb 2004 03:55:27 -0000
@@ -42,6 +42,16 @@
     VB_ALL       = 0xffff
 };
 
+enum LogPriorities {
+    LP_DEBUG     = 1,
+    LP_INFO      = 2,
+    LP_NOTICE    = 3,
+    LP_WARNING   = 4,
+    LP_ERROR     = 5,
+    LP_CRITICAL  = 6,
+    LP_EMERG     = 7
+};
+
 #define VERBOSE(mask,args...) \
 do { \
 if ((print_verbose_messages & mask) != 0) \
@@ -129,6 +139,9 @@
     QString GetSetting(const QString &key, const QString &defaultval = "");
     int GetNumSetting(const QString &key, int defaultval = 0);
 
+    void LogEntry(const QString &module, int priority,
+                  const QString &message, const QString &details);
+
     QString GetSettingOnHost(const QString &key, const QString &host,
                              const QString &defaultval = "");
     int GetNumSettingOnHost(const QString &key, const QString &host,
Index: libs/libmythtv/dbcheck.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmythtv/dbcheck.cpp,v
retrieving revision 1.35
diff -u -w -r1.35 dbcheck.cpp
--- libs/libmythtv/dbcheck.cpp	12 Feb 2004 05:58:02 -0000	1.35
+++ libs/libmythtv/dbcheck.cpp	22 Feb 2004 03:55:30 -0000
@@ -8,7 +8,7 @@
 
 #include "mythcontext.h"
 
-const QString currentDatabaseVersion = "1033";
+const QString currentDatabaseVersion = "1034";
 
 void UpdateDBVersionNumber(const QString &newnumber)
 {
@@ -638,7 +638,25 @@
 };
         performActualUpdate(updates, "1033", dbver);
     }
+    if (dbver == "1033")
+    {
+        const QString updates[] = {
+"CREATE TABLE mythlog ("
+"  logid int(10) unsigned PRIMARY KEY NOT NULL auto_increment,"
+"  module char(32) NOT NULL,"
+"  priority int(11) NOT NULL,"
+"  acknowledged bool default 0,"
+"  logdate datetime,"
+"  host varchar(128),"
+"  message varchar(255) NOT NULL,"
+"  details text"
+");",
+""
 };
+        performActualUpdate(updates, "1034", dbver);
+    }
+
+}
 
 void InitializeDatabase(void)
 {
Index: programs/mythbackend/main.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/programs/mythbackend/main.cpp,v
retrieving revision 1.58
diff -u -w -r1.58 main.cpp
--- programs/mythbackend/main.cpp	10 Feb 2004 03:34:25 -0000	1.58
+++ programs/mythbackend/main.cpp	22 Feb 2004 03:55:32 -0000
@@ -51,6 +51,11 @@
                 cerr << "One of your capturecard entries does not have a "
                      << "hostname.\n  Please run setup and confirm all of the "
                      << "capture cards.\n";
+                gContext->LogEntry("mythbackend",LP_CRITICAL,
+                      "Problem with capture cards",
+                      "One of your capturecard entries does not have a "
+                      "hostname.\n  Please run setup and confirm all of the "
+                      "capture cards.\n");
                 exit(-1);
             }
 
@@ -85,6 +90,8 @@
     {
         cerr << "ERROR: no capture cards are defined in the database.\n";
         cerr << "Perhaps you should read the installation instructions?\n";
+        gContext->LogEntry("mythbackend",LP_CRITICAL,
+                "No capture cards are defined","Please run the setup program.");
         return false;
     }
 
@@ -436,6 +443,9 @@
     if (masterip == myip)
     {
         cerr << "Starting up as the master server.\n";
+        gContext->LogEntry("mythbackend",LP_INFO,
+                           "MythBackend started as master server","");
+
         ismaster = true;
 
         if (nosched)
@@ -445,6 +455,8 @@
     else
     {
         cerr << "Running as a slave backend.\n";
+        gContext->LogEntry("mythbackend",LP_INFO,
+                           "MythBackend started as a slave backend","");
     }
  
     bool runsched = setupTVs(ismaster);
@@ -503,6 +515,8 @@
     a.exec();
 
     // delete trans;
+    gContext->LogEntry("mythbackend",LP_INFO,
+                       "MythBackend exiting","");
 
     cleanup();
 
Index: programs/mythfilldatabase/filldata.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/programs/mythfilldatabase/filldata.cpp,v
retrieving revision 1.90
diff -u -w -r1.90 filldata.cpp
--- programs/mythfilldatabase/filldata.cpp	13 Feb 2004 05:59:34 -0000	1.90
+++ programs/mythfilldatabase/filldata.cpp	22 Feb 2004 03:55:37 -0000
@@ -2051,6 +2051,7 @@
         return -1;
     }
 
+    gContext->LogEntry("mythfilldatabase",LP_INFO,"Listings Download Started","");
     if (from_xawfile)
     {
         readXawtvChannels(fromxawfile_id, fromxawfile_name);
@@ -2088,6 +2089,7 @@
              {
                   cerr << "There are no channel sources defined, did you run the "
                        << "setup program?\n";
+                  gContext->LogEntry("mythfilldatabase",LP_CRITICAL,"No channel sources defined","Could not find any defined channel sources - did you run the setup program?");
                   exit(-1);
              }
         }
@@ -2102,6 +2104,7 @@
         if (!ret)
         {
              cerr << "Failed to fetch some program info\n";
+             gContext->LogEntry("mythfilldatabase",LP_WARNING,"Failed to fetch some program info","");
              exit(1);
         }
     }
@@ -2116,6 +2119,7 @@
 
     ScheduledRecording::signalChange(db);
 
+    gContext->LogEntry("mythfilldatabase",LP_INFO,"Listings Download Finished","");
     delete gContext;
 
     return 0;
Index: programs/mythfrontend/globalsettings.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/programs/mythfrontend/globalsettings.cpp,v
retrieving revision 1.139
diff -u -w -r1.139 globalsettings.cpp
--- programs/mythfrontend/globalsettings.cpp	16 Feb 2004 06:43:43 -0000	1.139
+++ programs/mythfrontend/globalsettings.cpp	22 Feb 2004 03:55:42 -0000
@@ -966,6 +966,18 @@
     };
 };
 
+class LogEnabled: public CheckBoxSetting, public GlobalSetting {
+public:
+    LogEnabled():
+        GlobalSetting("LogEnabled") {
+        setLabel(QObject::tr("DB Logging Enabled"));
+        setValue(false);
+        setHelpText(QObject::tr("If checked, the Myth modules will send event details "
+                    "to the database, where they can be viewed with MythLog or emailed "
+                    "out periodically."));
+    };
+};
+
 class XineramaScreen: public SpinBoxSetting, public GlobalSetting {
 public:
     XineramaScreen():
@@ -1664,6 +1676,7 @@
     general->addChild(new HaltCommand());
     general->addChild(new SetupPinCodeRequired());
     general->addChild(new SetupPinCode());
+    general->addChild(new LogEnabled());
     general->addChild(new EnableMediaMon());
     general->addChild(new EnableXbox());
     addChild(general);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mythmaillog.pl
Type: application/x-perl
Size: 3557 bytes
Desc: not available
Url : http://mythtv.org/pipermail/mythtv-dev/attachments/20040221/90b00766/mythmaillog.pl


More information about the mythtv-dev mailing list