From bddbe1054756d13f00aa65d7777de89fec190a5f Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Mon, 1 Aug 2005 22:06:57 +0000 Subject: [PATCH] New logging system, bug fixed where client could gazump recordings or other players with higher priority --- Makefile | 4 +- config.c | 51 +++++++++---------- config.h | 3 ++ dsock.c | 90 +++++++++++++-------------------- dsock.h | 6 ++- mvpclient.c | 135 ++++++++++++++++++++++++++++++-------------------- mvpclient.h | 1 + mvpserver.c | 9 ++-- mvpserver.h | 1 + recplayer.c | 17 ++++--- recplayer.h | 2 + tcp.c | 27 +++++----- tcp.h | 3 ++ transceiver.c | 23 +++++---- transceiver.h | 6 ++- udpreplier.c | 2 +- udpreplier.h | 1 + vompserver.c | 3 -- 18 files changed, 206 insertions(+), 178 deletions(-) diff --git a/Makefile b/Makefile index fdea30e..88a268b 100644 --- a/Makefile +++ b/Makefile @@ -48,7 +48,7 @@ DEFINES += -DPLUGIN_NAME_I18N='"$(PLUGIN)"' OBJS = $(PLUGIN).o dsock.o mvpserver.o udpreplier.o mvpclient.o tcp.o \ transceiver.o remux/ts2ps.o remux/ts2es.o remux/tsremux.o ringbuffer.o \ - recplayer.o config.o + recplayer.o config.o log.o libdvbmpeg/libdvbmpegtools.a: libdvbmpeg/*.c libdvbmpeg/*.cc libdvbmpeg/*.h libdvbmpeg/*.hh @@ -59,7 +59,7 @@ libdvbmpeg/libdvbmpegtools.a: libdvbmpeg/*.c libdvbmpeg/*.cc libdvbmpeg/*.h libd %.o: %.c $(CXX) $(CXXFLAGS) -c $(DEFINES) $(INCLUDES) -o $@ $< - + # Dependencies: MAKEDEP = g++ -MM -MG diff --git a/config.c b/config.c index 868ff8f..272f352 100644 --- a/config.c +++ b/config.c @@ -25,6 +25,7 @@ Config::Config() { initted = 0; lastLineLength = 0; + log = Log::getInstance(); } int Config::init(char* takeFileName) @@ -35,7 +36,7 @@ int Config::init(char* takeFileName) if (strlen(takeFileName) > (MAX_FILENAME_LENGTH - 1)) { - printf("Config error: Config filename too long\n"); + log->log("Config", Log::DEBUG, "Config error: Config filename too long"); return 0; } @@ -49,7 +50,7 @@ int Config::init(char* takeFileName) file = fopen(fileName, "w"); if (!file) { - printf("Config error: Could not access config file\n"); + log->log("Config", Log::DEBUG, "Config error: Could not access config file"); return 0; } } @@ -77,12 +78,12 @@ int Config::openFile() if (!initted) return 0; if (pthread_mutex_lock(&fileLock)) { - printf("Config error: Could not get lock\n"); + log->log("Config", Log::DEBUG, "Config error: Could not get lock"); return 0; } if (!initted) { - printf("Config error: Initted 0 after lock\n"); + log->log("Config", Log::DEBUG, "Config error: Initted 0 after lock"); pthread_mutex_unlock(&fileLock); return 0; } @@ -90,7 +91,7 @@ int Config::openFile() file = fopen(fileName, "r"); if (!file) { - printf("Config error: Could not open config file\n"); + log->log("Config", Log::DEBUG, "Config error: Could not open config file"); pthread_mutex_unlock(&fileLock); return 0; } @@ -108,12 +109,12 @@ void Config::closeFile() int Config::readLine() { - if (!initted || !file) { printf("1\n"); return 0; } - if (!fgets(buffer, BUFFER_LENGTH-1, file)) { printf("2\n"); return 0; } + if (!initted || !file) { log->log("Config", Log::DEBUG, "1"); return 0; } + if (!fgets(buffer, BUFFER_LENGTH-1, file)) { log->log("Config", Log::DEBUG, "2"); return 0; } lastLineLength = strlen(buffer); - printf("buffer before trim: '%s'\n", buffer); + log->log("Config", Log::DEBUG, "buffer before trim: '%s'", buffer); trim(buffer); - printf("buffer after trim: '%s'\n", buffer); + log->log("Config", Log::DEBUG, "buffer after trim: '%s'", buffer); return 1; } @@ -165,13 +166,13 @@ int Config::deleteValue(char* section, char* key) if (!findSection(section)) { closeFile(); - printf("Config error: Section %s not found\n", section); + log->log("Config", Log::DEBUG, "Config error: Section %s not found", section); return 0; } if (!findKey(key)) { closeFile(); - printf("Config error: Key %s not found\n", key); + log->log("Config", Log::DEBUG, "Config error: Key %s not found", key); return 0; } @@ -215,7 +216,7 @@ int Config::setValueString(char* section, char* key, char* newValue) if (!newFile) { closeFile(); - printf("Config error: Could not write temp config file\n"); + log->log("Config", Log::DEBUG, "Config error: Could not write temp config file"); return 0; } @@ -231,7 +232,7 @@ int Config::setValueString(char* section, char* key, char* newValue) if (!newFile) { closeFile(); - printf("Config error: Could not write temp config file\n"); + log->log("Config", Log::DEBUG, "Config error: Could not write temp config file"); return 0; } @@ -247,7 +248,7 @@ int Config::setValueString(char* section, char* key, char* newValue) if (!newFile) { closeFile(); - printf("Config error: Could not write temp config file\n"); + log->log("Config", Log::DEBUG, "Config error: Could not write temp config file"); return 0; } @@ -303,7 +304,7 @@ int Config::findSection(char* section) if (!initted || !file) return 0; if (strlen(section) > (BUFFER_LENGTH-2)) { - printf("Config error: Section given exceeds max length\n"); + log->log("Config", Log::DEBUG, "Config error: Section given exceeds max length"); return 0; } @@ -315,7 +316,7 @@ int Config::findSection(char* section) while(readLine()) { - printf("to find '%s' this line '%s'\n", toFind, buffer); + log->log("Config", Log::DEBUG, "to find '%s' this line '%s'", toFind, buffer); if (!strcmp(toFind, buffer)) return 1; } return 0; @@ -327,7 +328,7 @@ int Config::findKey(char* key) if (strlen(key) > (BUFFER_LENGTH-1)) { - printf("Config error: Key given exceeds max length\n"); + log->log("Config", Log::DEBUG, "Config error: Key given exceeds max length"); return 0; } @@ -370,13 +371,13 @@ char* Config::getValueString(char* section, char* key) if (!findSection(section)) { closeFile(); - printf("Config error: Section %s not found\n", section); + log->log("Config", Log::DEBUG, "Config error: Section %s not found", section); return 0; } if (!findKey(key)) { closeFile(); - printf("Config error: Key %s not found\n", key); + log->log("Config", Log::DEBUG, "Config error: Key %s not found", key); return 0; } @@ -397,13 +398,13 @@ long Config::getValueLong(char* section, char* key, int* failure) if (!findSection(section)) { closeFile(); - printf("Config error: Section %s not found\n", section); + log->log("Config", Log::DEBUG, "Config error: Section %s not found", section); return 0; } if (!findKey(key)) { closeFile(); - printf("Config error: Key %s not found\n", key); + log->log("Config", Log::DEBUG, "Config error: Key %s not found", key); return 0; } *failure = 0; @@ -425,13 +426,13 @@ long long Config::getValueLongLong(char* section, char* key, int* failure) if (!findSection(section)) { closeFile(); - printf("Config error: Section %s not found\n", section); + log->log("Config", Log::DEBUG, "Config error: Section %s not found", section); return 0; } if (!findKey(key)) { closeFile(); - printf("Config error: Key %s not found\n", key); + log->log("Config", Log::DEBUG, "Config error: Key %s not found", key); return 0; } *failure = 0; @@ -453,13 +454,13 @@ double Config::getValueDouble(char* section, char* key, int* failure) if (!findSection(section)) { closeFile(); - printf("Config error: Section %s not found\n", section); + log->log("Config", Log::DEBUG, "Config error: Section %s not found", section); return 0; } if (!findKey(key)) { closeFile(); - printf("Config error: Key %s not found\n", key); + log->log("Config", Log::DEBUG, "Config error: Key %s not found", key); return 0; } diff --git a/config.h b/config.h index 161a497..58c92d4 100644 --- a/config.h +++ b/config.h @@ -28,6 +28,8 @@ #include #include +#include "log.h" + #define MAX_FILENAME_LENGTH 500 #define BUFFER_LENGTH 1500 @@ -57,6 +59,7 @@ class Config pthread_mutex_t fileLock; int initted; int lastLineLength; + Log* log; char fileName[MAX_FILENAME_LENGTH]; char fileNameTemp[MAX_FILENAME_LENGTH]; diff --git a/dsock.c b/dsock.c index 77755b4..8a1ee53 100644 --- a/dsock.c +++ b/dsock.c @@ -24,16 +24,26 @@ DatagramSocket::DatagramSocket(short port) { myPort = port; addrlen = sizeof(struct sockaddr); + log = Log::getInstance(); + initted = 0; if ((socketnum = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) - { perror("socket"); exit(1); } + { + log->log("UDP", Log::CRIT, "Socket error"); + return; + } myAddr.sin_family = AF_INET; // host byte order myAddr.sin_port = htons(myPort); // short, network byte order myAddr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP memset(&(myAddr.sin_zero), 0, 8); // zero the rest of the struct if (bind(socketnum, (struct sockaddr *)&myAddr, addrlen) == -1) - { perror("bind"); printf(" %s ", strerror(errno)); exit(1); } + { + log->log("UDP", Log::CRIT, "Bind error"); + close(socketnum); + return; + } + initted = 1; FD_ZERO(&readfds); FD_SET(socketnum, &readfds); @@ -43,11 +53,14 @@ DatagramSocket::DatagramSocket(short port) DatagramSocket::~DatagramSocket() { - close(socketnum); + if (initted) close(socketnum); + initted = 0; } unsigned char DatagramSocket::waitforMessage(unsigned char how) { + if (!initted) return 0; + /* how = 0 - block how = 1 - start new wait how = 2 - continue wait @@ -78,26 +91,19 @@ unsigned char DatagramSocket::waitforMessage(unsigned char how) FD_ZERO(&readfds); FD_SET(socketnum, &readfds); - if (select(socketnum + 1, &readfds, NULL, NULL, passToSelect) <= 0) - { return 1; } + if (select(socketnum + 1, &readfds, NULL, NULL, passToSelect) <= 0) return 1; - if ((mlength = recvfrom(socketnum, buf, MAXBUFLEN, 0, - (struct sockaddr *)&theirAddr, &addrlen)) == -1) - { perror("recvfrom"); return 0; } + if ((mlength = recvfrom(socketnum, buf, MAXBUFLEN, 0, (struct sockaddr *)&theirAddr, &addrlen)) == -1) + { + log->log("UDP", Log::DEBUG, "recvfrom error"); + return 0; + } else { memset(&buf[mlength], 0, MAXBUFLEN - mlength); strcpy(fromIPA, inet_ntoa(theirAddr.sin_addr)); fromPort = ntohs(theirAddr.sin_port); - - if (DSOCKDEBUG) - { - printf("%s:%i\tIN %i\t", fromIPA, fromPort, mlength); - int k; - for(k = 0; k < mlength; k++) - printf("%u ", (unsigned char)buf[k]); - printf("\n"); - } + log->log("UDP", Log::DEBUG, "%s:%i received length %i", fromIPA, fromPort, mlength); return 2; } @@ -118,15 +124,6 @@ short DatagramSocket::getFromPort(void) const { return fromPort; } void DatagramSocket::send(char *ipa, short port, char *message, int length) { - if (DSOCKDEBUG) - { - printf("%s:%i\tOUT %i\t", ipa, port, length); - int k; - uchar l; - for (k = 0; k < length; k++) - { l = (uchar)message[k]; printf("%u ", l); } - } - int sentLength = 0; theirAddr.sin_family = AF_INET; // host byte order @@ -136,39 +133,22 @@ void DatagramSocket::send(char *ipa, short port, char *message, int length) theirAddr.sin_addr = tad; // address memset(&(theirAddr.sin_zero), 0, 8); // zero the rest of the struct - unsigned char crypt[MAXBUFLEN]; - memcpy(crypt, message, length); - - sentLength = sendto(socketnum, crypt, length, 0, (struct sockaddr *)&theirAddr, addrlen); + sentLength = sendto(socketnum, message, length, 0, (struct sockaddr *)&theirAddr, addrlen); if (sentLength == length) { - printf(" GOOD\n"); + log->log("UDP", Log::DEBUG, "%s:%i sent length %i", ipa, port, length); + return; } - else + + log->log("UDP", Log::DEBUG, "%s:%i send failed %i", ipa, port, length); + + sentLength = sendto(socketnum, message, length, 0, (struct sockaddr *)&theirAddr, addrlen); + if (sentLength == length) { - printf(" --BAD--"); fflush(stdout); - sentLength = sendto(socketnum, crypt, length, 0, (struct sockaddr *)&theirAddr, addrlen); - if (sentLength == length) - printf(" GOOD\n"); - else - { - printf(" -#-FAILED-#-\n"); - - if (DSOCKDEBUG && (sentLength != length)) - { - printf("--------------\n"); - printf("Sendto failure\n"); - printf("--------------\n"); - printf("%s:%i\tOUT %i %i ...\n", ipa, port, length, sentLength); - perror("Perror reports"); - printf("errno value: %d\n", errno); - printf("errno translated: %s\n", strerror(errno)); - // printf("h_errno value: %d\n", h_errno); - // printf("\nActual address: %s\n", inet_ntoa(tad)); - // printf("Actual port: %i\n", ntohs(theirAddr.sin_port)); - printf("continuing...\n\n"); - } - } + log->log("UDP", Log::DEBUG, "%s:%i sent length %i 2nd try", ipa, port, length); + return; } + + log->log("UDP", Log::DEBUG, "%s:%i send failed %i 2nd try", ipa, port, length); } diff --git a/dsock.h b/dsock.h index 28ddf5c..29389b9 100644 --- a/dsock.h +++ b/dsock.h @@ -32,8 +32,10 @@ #include #include +#include "log.h" + #define MAXBUFLEN 2000 -const char DSOCKDEBUG = 1; +const char DSOCKDEBUG = 0; typedef unsigned char uchar; class DatagramSocket @@ -49,6 +51,8 @@ class DatagramSocket void send(char *, short, char *, int); // send wants: IP Address ddn style, port, // data, length of data private: + Log* log; + int initted; int socketnum; // Socket descriptor short myPort; // My port number struct sockaddr_in myAddr; // My address diff --git a/mvpclient.c b/mvpclient.c index 1a66fc4..ef19bbf 100644 --- a/mvpclient.c +++ b/mvpclient.c @@ -26,6 +26,7 @@ MVPClient::MVPClient(int tsocket) cm = NULL; rp = NULL; recordingManager = NULL; + log = Log::getInstance(); // Get IP address of client for config module @@ -39,13 +40,13 @@ MVPClient::MVPClient(int tsocket) else { ipa[0] = '\0'; - printf("Cannot get peer name!\n"); + log->log("Client", Log::DEBUG, "Cannot get peer name!"); } const char* configDir = cPlugin::ConfigDirectory(); if (!configDir) { - printf("No config dir!\n"); + log->log("Client", Log::DEBUG, "No config dir!"); return; } @@ -53,18 +54,17 @@ MVPClient::MVPClient(int tsocket) snprintf(configFileName, PATH_MAX - strlen(configDir) - strlen(ipa) - 20, "%s/vomp-%s.conf", configDir, ipa); config.init(configFileName); - printf("Config file name: %s\n", configFileName); + log->log("Client", Log::DEBUG, "Config file name: %s", configFileName); // processGetChannelSchedule(NULL, 0); -// printf("here\n"); //test(); } MVPClient::~MVPClient() { - printf("MVP client destructor\n"); + log->log("Client", Log::DEBUG, "MVP client destructor"); if (cm) { cm->Stop(); @@ -92,7 +92,7 @@ cChannel* MVPClient::channelFromNumber(unsigned long channelNumber) { if (!channel->GroupSep()) { - printf("Looking for channel %lu::: number: %i name: '%s'\n", channelNumber, channel->Number(), channel->Name()); + log->log("Client", Log::DEBUG, "Looking for channel %lu::: number: %i name: '%s'", channelNumber, channel->Number(), channel->Name()); if (channel->Number() == (int)channelNumber) { @@ -102,7 +102,7 @@ cChannel* MVPClient::channelFromNumber(unsigned long channelNumber) #else int apid1 = channel->Apid(0); #endif - printf("Found channel number %lu, vpid = %i, apid1 = %i\n", channelNumber, vpid, apid1); + log->log("Client", Log::DEBUG, "Found channel number %lu, vpid = %i, apid1 = %i", channelNumber, vpid, apid1); return channel; } } @@ -110,7 +110,7 @@ cChannel* MVPClient::channelFromNumber(unsigned long channelNumber) if (!channel) { - printf("Channel not found\n"); + log->log("Client", Log::DEBUG, "Channel not found"); } return channel; @@ -129,7 +129,7 @@ void MVPClient::sendULONG(ULONG ul) *(unsigned long*)&sendBuffer[4] = htonl(ul); tcp.sendPacket(sendBuffer, 8); - printf("written ULONG %lu\n", ul); + log->log("Client", Log::DEBUG, "written ULONG %lu", ul); } void MVPClientStartThread(void* arg) @@ -145,7 +145,7 @@ void MVPClientStartThread(void* arg) int MVPClient::run() { if (pthread_create(&runThread, NULL, (void*(*)(void*))MVPClientStartThread, (void *)this) == -1) return 0; - printf("MVPClient run success\n"); + log->log("Client", Log::DEBUG, "MVPClient run success"); return 1; } @@ -169,12 +169,12 @@ void MVPClient::run2() while(1) { - printf("starting wait\n"); + log->log("Client", Log::DEBUG, "Waiting"); buffer = (unsigned char*)tcp.receivePacket(); - printf("back from wait\n"); + log->log("Client", Log::DEBUG, "Received packet, length = %u", tcp.getDataLength()); if (buffer == NULL) { - printf("Detected connection closed\n"); + log->log("Client", Log::DEBUG, "Detected connection closed"); break; } @@ -239,7 +239,7 @@ void MVPClient::processLogin(unsigned char* buffer, int length) *(signed int*)&sendBuffer[8] = htonl(timeOffset); tcp.sendPacket(sendBuffer, 12); - printf("written login reply\n"); + log->log("Client", Log::DEBUG, "written login reply"); } void MVPClient::processGetRecordingsList(unsigned char* data, int length) @@ -281,11 +281,11 @@ void MVPClient::processGetRecordingsList(unsigned char* data, int length) *(unsigned long*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field - printf("recorded size as %u\n", ntohl(*(unsigned long*)&sendBuffer[0])); + log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(unsigned long*)&sendBuffer[0])); tcp.sendPacket(sendBuffer, count); delete[] sendBuffer; - printf("Written list\n"); + log->log("Client", Log::DEBUG, "Written list"); } void MVPClient::processDeleteRecording(unsigned char* data, int length) @@ -297,11 +297,11 @@ void MVPClient::processDeleteRecording(unsigned char* data, int length) cRecording* recording = Recordings.GetByName((char*)data); - printf("recording pointer %p\n", recording); + log->log("Client", Log::DEBUG, "recording pointer %p", recording); if (recording) { - printf("deleting recording: %s\n", recording->Name()); + log->log("Client", Log::DEBUG, "deleting recording: %s", recording->Name()); recording->Delete(); sendULONG(1); } @@ -320,7 +320,7 @@ void MVPClient::processGetSummary(unsigned char* data, int length) cRecording *recording = Recordings.GetByName((char*)data); - printf("recording pointer %p\n", recording); + log->log("Client", Log::DEBUG, "recording pointer %p", recording); if (recording) { @@ -333,22 +333,22 @@ void MVPClient::processGetSummary(unsigned char* data, int length) #else const cRecordingInfo *Info = recording->Info(); point = (char*)Info->ShortText(); - printf("info pointer %p\nsummary pointer %p\n", Info, point); + log->log("Client", Log::DEBUG, "info pointer %p summary pointer %p", Info, point); if (isempty(point)) { point = (char*)Info->Description(); - printf("description pointer %p\n", point); + log->log("Client", Log::DEBUG, "description pointer %p", point); } #endif strcpy((char*)&sendBuffer[count], point); count += strlen(point) + 1; *(unsigned long*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field - printf("recorded size as %u\n", ntohl(*(unsigned long*)&sendBuffer[0])); + log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(unsigned long*)&sendBuffer[0])); tcp.sendPacket(sendBuffer, count); delete[] sendBuffer; - printf("Written summary\n"); + log->log("Client", Log::DEBUG, "Written summary"); } @@ -373,7 +373,7 @@ void MVPClient::processGetChannelsList(unsigned char* data, int length) if (!channel->GroupSep() && !channel->Ca(0)) #endif { - printf("name: '%s'\n", channel->Name()); + log->log("Client", Log::DEBUG, "name: '%s'", channel->Name()); if (channel->Vpid()) type = 1; #if VDRVERSNUM < 10300 @@ -398,16 +398,16 @@ void MVPClient::processGetChannelsList(unsigned char* data, int length) *(unsigned long*)&sendBuffer[0] = htonl(count - 4); // -4 : take off the size field - printf("recorded size as %u\n", ntohl(*(unsigned long*)&sendBuffer[0])); + log->log("Client", Log::DEBUG, "recorded size as %u", ntohl(*(unsigned long*)&sendBuffer[0])); tcp.sendPacket(sendBuffer, count); delete[] sendBuffer; - printf("Written channels list\n"); + log->log("Client", Log::DEBUG, "Written channels list"); } void MVPClient::processStartStreamingChannel(unsigned char* data, int length) { - printf("length = %i\n", length); + log->log("Client", Log::DEBUG, "length = %i", length); unsigned long channelNumber = ntohl(*(unsigned long*)data); cChannel* channel = channelFromNumber(channelNumber); @@ -417,17 +417,45 @@ void MVPClient::processStartStreamingChannel(unsigned char* data, int length) return; } -// MVPReceiver* m = new MVPReceiver(channel->Vpid(), channel->Apid1()); - cm = new cMediamvpTransceiver(channel, 0, 0, cDevice::ActualDevice()); - cDevice::ActualDevice()->AttachReceiver(cm); - //cDevice::ActualDevice()->SwitchChannel(channel, false); - sendULONG(1); +// static cDevice *GetDevice(const cChannel *Channel, int Priority = -1, bool *NeedsDetachReceivers = NULL); + ///< Returns a device that is able to receive the given Channel at the + ///< given Priority. + ///< See ProvidesChannel() for more information on how + ///< priorities are handled, and the meaning of NeedsDetachReceivers. + + bool NeedsDetachReceivers; + cDevice* device = cDevice::GetDevice(channel, 0, &NeedsDetachReceivers); + if (!device) + { + log->log("Client", Log::DEBUG, "No device found to receive this channel at this priority"); + sendULONG(0); + } + else if (NeedsDetachReceivers) + { + // can't really happen since we stream with priority zero. if a rec has pri zero maybe + log->log("Client", Log::DEBUG, "Needs detach receivers"); + sendULONG(0); + } + else + { + cm = new cMediamvpTransceiver(channel, 0, 0, device); + device->AttachReceiver(cm); + sendULONG(1); + } + + +////// MVPReceiver* m = new MVPReceiver(channel->Vpid(), channel->Apid1()); +// cm = new cMediamvpTransceiver(channel, 0, 0, cDevice::ActualDevice()); +// cDevice::ActualDevice()->AttachReceiver(cm); +//// //cDevice::ActualDevice()->SwitchChannel(channel, false); + +// sendULONG(1); } void MVPClient::processStopStreaming(unsigned char* data, int length) { - printf("STOP STREAMING RECEIVED\n"); + log->log("Client", Log::DEBUG, "STOP STREAMING RECEIVED"); if (cm) { delete cm; @@ -450,35 +478,32 @@ void MVPClient::processGetBlock(unsigned char* data, int length) { if (!cm && !rp) { - printf("Get block called when no streaming happening!\n"); + log->log("Client", Log::DEBUG, "Get block called when no streaming happening!"); return; } ULLONG position = ntohll(*(ULLONG*)data); - printf("getblock called for position = %llu\n", position); - data += sizeof(ULLONG); - unsigned long amount = ntohl(*(unsigned long*)data); - printf("getblock called for length = %lu\n", amount); + + log->log("Client", Log::DEBUG, "getblock pos = %llu length = %lu", position, amount); unsigned char sendBuffer[amount + 4]; unsigned long amountReceived = 0; // compiler moan. if (cm) { - printf("getting from live\n"); + log->log("Client", Log::DEBUG, "getting from live"); amountReceived = cm->getBlock(&sendBuffer[4], amount); } else if (rp) { - printf("getting from recording\n"); + log->log("Client", Log::DEBUG, "getting from recording"); amountReceived = rp->getBlock(&sendBuffer[4], position, amount); } *(unsigned long*)&sendBuffer[0] = htonl(amountReceived); - printf("sendpacket go\n"); tcp.sendPacket(sendBuffer, amountReceived + 4); - printf("written ok %lu\n", amountReceived); + log->log("Client", Log::DEBUG, "written ok %lu", amountReceived); } void MVPClient::processStartStreamingRecording(unsigned char* data, int length) @@ -490,7 +515,7 @@ void MVPClient::processStartStreamingRecording(unsigned char* data, int length) cRecording* recording = recordingManager->GetByName((char*)data); - printf("recording pointer %p\n", recording); + log->log("Client", Log::DEBUG, "recording pointer %p", recording); if (recording) { @@ -501,7 +526,7 @@ void MVPClient::processStartStreamingRecording(unsigned char* data, int length) *(ULLONG*)&sendBuffer[4] = htonll(rp->getTotalLength()); tcp.sendPacket(sendBuffer, 12); - printf("written totalLength\n"); + log->log("Client", Log::DEBUG, "written totalLength"); } else { @@ -513,7 +538,7 @@ void MVPClient::processStartStreamingRecording(unsigned char* data, int length) void MVPClient::processGetChannelSchedule(unsigned char* data, int length) { ULONG channelNumber = ntohl(*(ULLONG*)data); - printf("get schedule called for channel %lu\n", channelNumber); + log->log("Client", Log::DEBUG, "get schedule called for channel %lu", channelNumber); cChannel* channel = channelFromNumber(channelNumber); if (!channel) @@ -521,7 +546,7 @@ void MVPClient::processGetChannelSchedule(unsigned char* data, int length) unsigned char sendBuffer[4]; *(unsigned long*)&sendBuffer[0] = htonl(0); tcp.sendPacket(sendBuffer, 4); - printf("written null\n"); + log->log("Client", Log::DEBUG, "written null"); return; } @@ -538,7 +563,7 @@ void MVPClient::processGetChannelSchedule(unsigned char* data, int length) *(unsigned long*)&sendBuffer[0] = htonl(4); *(unsigned long*)&sendBuffer[4] = htonl(0); tcp.sendPacket(sendBuffer, 8); - printf("written 0\n"); + log->log("Client", Log::DEBUG, "written 0"); return; } @@ -546,7 +571,7 @@ void MVPClient::processGetChannelSchedule(unsigned char* data, int length) *(unsigned long*)&sendBuffer[0] = htonl(4); *(unsigned long*)&sendBuffer[4] = htonl(1); tcp.sendPacket(sendBuffer, 8); - printf("written 1\n"); + log->log("Client", Log::DEBUG, "written 1"); } @@ -716,7 +741,7 @@ void MVPClient::processConfigSave(unsigned char* buffer, int length) // if the last string (value) doesnt have null terminator, give up if (buffer[length - 1] != '\0') return; - printf("Config save:\n%s\n%s\n%s\n", section, key, value); + log->log("Client", Log::DEBUG, "Config save: %s %s %s", section, key, value); if (config.setValueString(section, key, value)) { sendULONG(1); @@ -750,7 +775,7 @@ void MVPClient::processConfigLoad(unsigned char* buffer, int length) strcpy((char*)&sendBuffer[4], value); tcp.sendPacket(sendBuffer, 4 + strlen(value) + 1); - printf("Written config load packet\n"); + log->log("Client", Log::DEBUG, "Written config load packet"); delete[] value; } else @@ -760,13 +785,13 @@ void MVPClient::processConfigLoad(unsigned char* buffer, int length) *(unsigned long*)&sendBuffer[4] = htonl(0); tcp.sendPacket(sendBuffer, 8); - printf("Written config load failed packet\n"); + log->log("Client", Log::DEBUG, "Written config load failed packet"); } } void MVPClient::cleanConfig() { - printf("Clean config\n"); + log->log("Client", Log::DEBUG, "Clean config"); cRecordings Recordings; Recordings.Load(); @@ -777,18 +802,18 @@ void MVPClient::cleanConfig() char* position = resumes; for(int k = 0; k < numReturns; k++) { - printf("EXAMINING: %i %i %p %s\n", k, numReturns, position, position); + log->log("Client", Log::DEBUG, "EXAMINING: %i %i %p %s", k, numReturns, position, position); cRecording* recording = Recordings.GetByName(position); if (!recording) { // doesn't exist anymore - printf("Found a recording that doesn't exist anymore\n"); + log->log("Client", Log::DEBUG, "Found a recording that doesn't exist anymore"); config.deleteValue("ResumeData", position); } else { - printf("This recording still exists\n"); + log->log("Client", Log::DEBUG, "This recording still exists"); } position += strlen(position) + 1; diff --git a/mvpclient.h b/mvpclient.h index b31ce31..c5cbcc1 100644 --- a/mvpclient.h +++ b/mvpclient.h @@ -56,6 +56,7 @@ class MVPClient cMediamvpTransceiver* cm; cRecordings* recordingManager; RecPlayer* rp; + Log* log; void processLogin(unsigned char* buffer, int length); void processGetRecordingsList(unsigned char* data, int length); diff --git a/mvpserver.c b/mvpserver.c index 35bb3f1..46fb62f 100644 --- a/mvpserver.c +++ b/mvpserver.c @@ -43,6 +43,7 @@ int MVPServer::stop() { if (!running) return 0; + log.shutdown(); udpr.stop(); pthread_cancel(runThread); @@ -57,12 +58,12 @@ int MVPServer::run() { if (running) return 1; -// logger.init(Log::DEBUG, "/tmp/vompserver.log"); + log.init(Log::DEBUG, "/tmp/vompserver.log", 0); if (udpr.run() == 0) return 0; if (pthread_create(&runThread, NULL, (void*(*)(void*))MVPServerStartThread, (void *)this) == -1) return 0; - printf("MVPServer run success\n"); + log.log("MVPServer", Log::DEBUG, "MVPServer run success"); return 1; } @@ -85,7 +86,7 @@ void MVPServer::run2() listeningSocket = socket(AF_INET, SOCK_STREAM, 0); if (listeningSocket < 0) { - printf("Could not get TCP socket in vompserver\n"); + log.log("MVPServer", Log::DEBUG, "Could not get TCP socket in vompserver"); return; } @@ -94,7 +95,7 @@ void MVPServer::run2() if (bind(listeningSocket,(struct sockaddr *)&address,sizeof(address)) < 0) { - printf("Could not bind to socket in vompserver\n"); + log.log("MVPServer", Log::DEBUG, "Could not bind to socket in vompserver"); close(listeningSocket); return; } diff --git a/mvpserver.h b/mvpserver.h index 64caaff..0dbcac3 100644 --- a/mvpserver.h +++ b/mvpserver.h @@ -46,6 +46,7 @@ class MVPServer pthread_t runThread; int running; + Log log; UDPReplier udpr; int listeningSocket; }; diff --git a/recplayer.c b/recplayer.c index 6c24033..8e688ca 100644 --- a/recplayer.c +++ b/recplayer.c @@ -25,6 +25,7 @@ RecPlayer::RecPlayer(cRecording* rec) file = NULL; totalLength = 0; lastPosition = 0; + log = Log::getInstance(); recording = rec; // FIXME find out max file path / name lengths @@ -34,7 +35,7 @@ RecPlayer::RecPlayer(cRecording* rec) for(int i = 1; i < 1001; i++) { snprintf(fileName, 2047, "%s/%03i.vdr", rec->FileName(), i); - printf("FILENAME: %s\n", fileName); + log->log("RecPlayer", Log::DEBUG, "FILENAME: %s", fileName); file = fopen(fileName, "r"); if (file) { @@ -43,7 +44,7 @@ RecPlayer::RecPlayer(cRecording* rec) fseek(file, 0, SEEK_END); totalLength += ftell(file); - printf("File %i found, totalLength now %llu\n", i, totalLength); + log->log("RecPlayer", Log::DEBUG, "File %i found, totalLength now %llu", i, totalLength); segments[i]->end = totalLength; fclose(file); } @@ -58,7 +59,7 @@ RecPlayer::RecPlayer(cRecording* rec) RecPlayer::~RecPlayer() { - printf("RecPlayer destructor\n"); + log->log("RecPlayer", Log::DEBUG, "destructor"); int i = 1; while(segments[i++]) delete segments[i]; if (file) fclose(file); @@ -70,12 +71,12 @@ int RecPlayer::openFile(int index) char fileName[2048]; snprintf(fileName, 2047, "%s/%03i.vdr", recording->FileName(), index); - printf("openFile called for index %i string:%s\n", index, fileName); + log->log("RecPlayer", Log::DEBUG, "openFile called for index %i string:%s", index, fileName); file = fopen(fileName, "r"); if (!file) { - printf("file failed to open\n"); + log->log("RecPlayer", Log::DEBUG, "file failed to open"); fileOpen = 0; return 0; } @@ -92,19 +93,19 @@ unsigned long RecPlayer::getBlock(unsigned char* buffer, ULLONG position, unsign { if ((amount > totalLength) || (amount > 100000)) { - printf("Amount %lu requested and rejected\n", amount); + log->log("RecPlayer", Log::DEBUG, "Amount %lu requested and rejected", amount); return 0; } if (position >= totalLength) { - printf("Client asked for data starting past end of recording!\n"); + log->log("RecPlayer", Log::DEBUG, "Client asked for data starting past end of recording!"); return 0; } if ((position + amount) > totalLength) { - printf("Client asked for some data past the end of recording, adjusting amount\n"); + log->log("RecPlayer", Log::DEBUG, "Client asked for some data past the end of recording, adjusting amount"); amount = totalLength - position; } diff --git a/recplayer.h b/recplayer.h index 0a48b41..d2ad4ec 100644 --- a/recplayer.h +++ b/recplayer.h @@ -25,6 +25,7 @@ #include #include "defines.h" +#include "log.h" class Segment { @@ -45,6 +46,7 @@ class RecPlayer cRecording* getCurrentRecording(); private: + Log* log; cRecording* recording; FILE* file; int fileOpen; diff --git a/tcp.c b/tcp.c index b1d09c3..0a6e402 100644 --- a/tcp.c +++ b/tcp.c @@ -23,6 +23,7 @@ TCP::TCP(int tsocket) { + log = Log::getInstance(); sock = -1; connected = 0; readTimeoutEnabled = 1; @@ -44,7 +45,7 @@ void TCP::cleanup() close(sock); sock = -1; connected = 0; - printf("TCP has closed socket\n"); + log->log("TCP", Log::DEBUG, "TCP has closed socket"); } @@ -95,18 +96,18 @@ int TCP::setSoKeepTime(int timeOut) option = 1; s1 = setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &option, sizeof(option)); - printf("SO_KEEPALIVE = %i\n", s1); + log->log("TCP", Log::DEBUG, "SO_KEEPALIVE = %i", s1); option = timeOut; s2 = setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, &option, sizeof(option)); - printf("TCP_KEEPIDLE = %i\n", s2); + log->log("TCP", Log::DEBUG, "TCP_KEEPIDLE = %i", s2); s3 = setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, &option, sizeof(option)); - printf("TCP_KEEPINTVL = %i\n", s3); + log->log("TCP", Log::DEBUG, "TCP_KEEPINTVL = %i", s3); option = 2; s4 = setsockopt(sock, SOL_TCP, TCP_KEEPCNT, &option, sizeof(option)); - printf("TCP_KEEPCNT = %i\n", s4); + log->log("TCP", Log::DEBUG, "TCP_KEEPCNT = %i", s4); if (s1 || s2 || s3 || s4) return 0; return 1; @@ -150,7 +151,7 @@ UCHAR* TCP::receivePacket() return NULL; } - dump((unsigned char*)buffer, packetLength); +// dump((unsigned char*)buffer, packetLength); dataLength = packetLength; return buffer; @@ -186,7 +187,7 @@ int TCP::readData(UCHAR* buffer, int totalBytes) if (success < 1) { cleanup(); - printf("TCP: error or timeout\n"); + log->log("TCP", Log::DEBUG, "TCP: error or timeout"); return 0; // error, or timeout } @@ -201,7 +202,7 @@ int TCP::readData(UCHAR* buffer, int totalBytes) } bytesRead += thisRead; - printf("Bytes read now: %u\n", bytesRead); +// log->log("TCP", Log::DEBUG, "Bytes read now: %u", bytesRead); if (bytesRead == totalBytes) { return 1; @@ -211,7 +212,7 @@ int TCP::readData(UCHAR* buffer, int totalBytes) if (++readTries == 100) { cleanup(); - printf("too many reads\n"); + log->log("TCP", Log::DEBUG, "too many reads"); return 0; } } @@ -239,7 +240,7 @@ int TCP::sendPacket(UCHAR* buf, size_t count) if (success < 1) { cleanup(); - printf("TCP: error or timeout\n"); + log->log("TCP", Log::DEBUG, "TCP: error or timeout"); return 0; // error, or timeout } @@ -250,12 +251,12 @@ int TCP::sendPacket(UCHAR* buf, size_t count) // in non-blocking mode if read is called with no data available, it returns -1 // and sets errno to EGAGAIN. but we use select so it wouldn't do that anyway. cleanup(); - printf("Detected connection closed\n"); + log->log("TCP", Log::DEBUG, "Detected connection closed"); return 0; } bytesWritten += thisWrite; - printf("Bytes written now: %u\n", bytesWritten); +// log->log("TCP", Log::DEBUG, "Bytes written now: %u", bytesWritten); if (bytesWritten == count) { return 1; @@ -265,7 +266,7 @@ int TCP::sendPacket(UCHAR* buf, size_t count) if (++writeTries == 100) { cleanup(); - printf("too many writes\n"); + log->log("TCP", Log::DEBUG, "too many writes"); return 0; } } diff --git a/tcp.h b/tcp.h index f736707..489dfa4 100644 --- a/tcp.h +++ b/tcp.h @@ -37,6 +37,8 @@ #include #include +#include "log.h" + typedef unsigned char UCHAR; typedef unsigned short USHORT; @@ -66,6 +68,7 @@ class TCP static UCHAR dcc(UCHAR c); private: + Log* log; int sock; int connected; int readTimeoutEnabled; diff --git a/transceiver.c b/transceiver.c index 13bf886..46a2f23 100644 --- a/transceiver.c +++ b/transceiver.c @@ -47,12 +47,15 @@ cMediamvpTransceiver::cMediamvpTransceiver(const cChannel *Channel, int Priority cMediamvpTransceiver::cMediamvpTransceiver(const cChannel *Channel, int Priority, int Socket, cDevice *Device) : cReceiver(Channel->Ca(), Priority, Channel->Vpid(), Channel->Apids(), Channel->Dpids(), Channel->Spids()) { -#endif - m_Active = false; +#endif + m_Active = false; m_Socket = Socket; m_Remux = NULL; m_Device = Device; +//cjt + log = Log::getInstance(); + m_RingBuffer = new cRingBufferLinear(VIDEOBUFSIZE, TS_SIZE * 2, true); // m_RingBuffer = new cRingBufferLinear(VIDEOBUFSIZE, TS_SIZE * 20, true); @@ -70,7 +73,7 @@ cMediamvpTransceiver::cMediamvpTransceiver(const cChannel *Channel, int Priority m_Remux = new cTS2PSRemux(Channel->Vpid(), Channel->Apid(0), 0, 0, 0, 0); } #endif - printf("Created transceiver at %p, remux @%p ringbuffer %p\n",this,m_Remux,m_RingBuffer); + log->log("Transciever", Log::DEBUG, "Created transceiver at %p, remux @%p ringbuffer %p",this,m_Remux,m_RingBuffer); /* Suggested by Peter Wagner to assist single DVB card systems */ #ifdef SINGLE_DEVICE @@ -89,7 +92,7 @@ cMediamvpTransceiver::cMediamvpTransceiver(const cChannel *Channel, int Priority cMediamvpTransceiver::~cMediamvpTransceiver(void) { - printf("Deleting transceiver at %p, remux @%p ringbuffer %p\n",this,m_Remux,m_RingBuffer); + log->log("Transciever", Log::DEBUG, "Deleting transceiver at %p, remux @%p ringbuffer %p",this,m_Remux,m_RingBuffer); Detach(); if (m_Remux) @@ -128,7 +131,7 @@ void cMediamvpTransceiver::Receive(uchar *Data, int Length) if (p != Length) { ++errcnt; #if VDRVERSNUM < 10300 - if (showerr) { + if (showerr) { if (firsterr == 0) firsterr = time_ms(); else if (firsterr + BUFOVERTIME > time_ms() && errcnt > BUFOVERCOUNT) { @@ -147,11 +150,11 @@ void cMediamvpTransceiver::Receive(uchar *Data, int Length) else firsterr = time_ms(); #else - if (showerr) { + if (showerr) { if (firsterr == 0) { firsterr = 1; - lastTime.Set(); - } + lastTime.Set(); + } else if (lastTime.Elapsed() > BUFOVERTIME && errcnt > BUFOVERCOUNT) { esyslog("ERROR: too many buffer overflows, logging stopped"); showerr = false; @@ -176,7 +179,7 @@ void cMediamvpTransceiver::Action(void) int max = 0; - printf("Mediamvp: Transceiver thread started (pid=%d)", getpid()); + log->log("Transciever", Log::DEBUG, "Mediamvp: Transceiver thread started (pid=%d)", getpid()); m_Active = true; @@ -213,7 +216,7 @@ void cMediamvpTransceiver::Action(void) } - printf("Mediamvp: Transceiver thread ended"); + log->log("Transciever", Log::DEBUG, "Mediamvp: Transceiver thread ended"); } unsigned long cMediamvpTransceiver::getBlock(unsigned char* buffer, unsigned long amount) diff --git a/transceiver.h b/transceiver.h index cbfe50a..08e721e 100644 --- a/transceiver.h +++ b/transceiver.h @@ -1,6 +1,8 @@ /* Edited for VOMP by Chris Tallon Edits Copyright 2004-2005 Chris Tallon + + This class will be replaced soon. */ /* @@ -32,6 +34,7 @@ class cChannel; #include #include "ringbuffer.h" +#include "log.h" class cMediamvpTransceiver: public cReceiver, public cThread { // friend class cMediamvpVdrURL; @@ -43,11 +46,12 @@ private: bool m_Active; #if VDRVERSNUM >= 10300 - cTimeMs lastTime; + cTimeMs lastTime; #endif // CJT Ringbuffer rb; pthread_mutex_t ringLock; + Log* log; protected: diff --git a/udpreplier.c b/udpreplier.c index d6a352f..e61612d 100644 --- a/udpreplier.c +++ b/udpreplier.c @@ -56,7 +56,7 @@ int UDPReplier::run() if (running) return 1; running = 1; if (pthread_create(&runThread, NULL, (void*(*)(void*))UDPReplierStartThread, (void *)this) == -1) return 0; - printf("UDPReplier run success\n"); + Log::getInstance()->log("UDP", Log::DEBUG, "UDP replier started"); return 1; } diff --git a/udpreplier.h b/udpreplier.h index 4d82ba8..3ef169a 100644 --- a/udpreplier.h +++ b/udpreplier.h @@ -25,6 +25,7 @@ #include #include +#include "log.h" #include "dsock.h" class UDPReplier diff --git a/vompserver.c b/vompserver.c index 1845937..45792f2 100644 --- a/vompserver.c +++ b/vompserver.c @@ -80,15 +80,12 @@ bool cPluginVompserver::ProcessArgs(int argc, char *argv[]) bool cPluginVompserver::Initialize(void) { // Initialize any background activities the plugin shall perform. - printf("VOMP Plugin init\n"); return true; } bool cPluginVompserver::Start(void) { // Start any background activities the plugin shall perform. - printf("VOMP Plugin start\n"); - int success = mvpserver.run(); if (success) return true; else return false; -- 2.39.5