00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "logging.h"
00011
00012 #include <unistd.h>
00013 #include <cstdlib>
00014 #include <qdatetime.h>
00015
00016 #include <mythtv/mythcontext.h>
00017
00018 MTDLogger::MTDLogger(bool log_stdout)
00019 :QObject()
00020 {
00021 log_to_stdout = log_stdout;
00022
00023
00024
00025
00026
00027 QString logfile_name = gContext->GetSetting("DVDRipLocation");
00028 if(logfile_name.length() < 1)
00029 {
00030 VERBOSE(VB_IMPORTANT, "You do not have a DVD rip directory set."
00031 " Run Setup.");
00032 exit(0);
00033 }
00034 logfile_name.append("/mtd.log");
00035 if(!log_to_stdout)
00036 {
00037 logging_file.setName(logfile_name);
00038 if(!logging_file.open(IO_WriteOnly))
00039 {
00040 VERBOSE(VB_IMPORTANT, QString("Problem opening logfile. Does this"
00041 "look openable to you: %1")
00042 .arg(logfile_name));
00043 exit(0);
00044 }
00045 }
00046 }
00047
00048 void MTDLogger::addEntry(const QString &log_entry)
00049 {
00050 writeStampedString(log_entry);
00051 }
00052
00053 void MTDLogger::addStartup()
00054 {
00055 char hostname[1024];
00056 QString startup_message = "mtd started at "
00057 + QDateTime(QDateTime::currentDateTime()).toString();
00058 writeString(startup_message);
00059
00060 gethostname(hostname, 1024);
00061
00062 startup_message = hostname;
00063 startup_message.prepend("mtd is running on a host called ");
00064
00065 writeString(startup_message);
00066 writeStampedString("Waiting for connections/jobs");
00067 }
00068
00069 void MTDLogger::addShutdown()
00070 {
00071 QString shutdown_message = "mtd shutting down at "
00072 + QDateTime(QDateTime::currentDateTime()).toString();
00073 writeString(shutdown_message);
00074 }
00075
00076 void MTDLogger::socketOpened()
00077 {
00078 writeStampedString("a client socket has been opened");
00079 }
00080
00081 void MTDLogger::socketClosed()
00082 {
00083 writeStampedString("a client socket has been closed");
00084 }
00085
00086 void MTDLogger::writeString(const QString &log_entry)
00087 {
00088 if(log_to_stdout)
00089 {
00090 cout << log_entry << endl;
00091 }
00092 else
00093 {
00094 QTextStream stream(&logging_file);
00095 stream << log_entry << endl << flush;
00096 }
00097 }
00098
00099 void MTDLogger::writeStampedString(const QString &log_entry)
00100 {
00101 QString stamped_log_entry = log_entry;
00102 stamped_log_entry.prepend(": ");
00103 stamped_log_entry.prepend(QTime(QTime::currentTime()).toString());
00104 writeString(stamped_log_entry);
00105 }
00106
00107 MTDLogger::~MTDLogger()
00108 {
00109 logging_file.close();
00110 }
00111