Re: Patch for logging into file + Makefile NO_DEBUG switch
On Fri, May 07, 2010 at 02:00:36PM +0400, Михаил Продан wrote:
I don't known if this is a right place to post this thing. But. Here is a patch for logging into file using -f switch Also a small change of Makefile for turning on\off optimizations (-O2 -g compiler switches). Usage: make (Normal build with -O0 -g). make NO_DEBUG=1 (for -O2)
Getting back to this - there are certain things to note on this patch.
diff --git a/.gitignore b/.gitignore index 70bce91..42ef55e 100644 --- a/.gitignore +++ b/.gitignore @@ -8,5 +8,6 @@ core* *.rej tsdecode getstream - +getstream.conf +gs.sh !.gitignore
Unrelated hunk
diff --git a/Makefile b/Makefile index 345a635..47cc29d 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,9 @@ CC=gcc -CFLAGS=-O0 -g -Wall -I. -I/usr/include/glib-2.0/ -I/usr/lib/glib-2.0/include/ +ifdef NO_DEBUG + CFLAGS=-O2 -Wall -I. -I/usr/include/glib-2.0/ -I/usr/lib/glib-2.0/include/ +else + CFLAGS=-O0 -g -Wall -I. -I/usr/include/glib-2.0/ -I/usr/lib/glib-2.0/include/ +endif LDFLAGS=-levent -lglib-2.0 -lpthread OBJ-getstream=getstream.o fe.o crc32.o \ libhttp.o libconf.o config.o util.o logging.o \ diff --git a/getstream.c b/getstream.c index b56fa3e..45c5502 100644 --- a/getstream.c +++ b/getstream.c @@ -98,7 +98,7 @@ static void terminate_init(int timeout) {
static void usage(void ) { - fprintf(stderr, "-c <config file> -d -t <timeout>\n"); + fprintf(stderr, "-c <config file> -d -t <timeout> -f <logfile>\n"); exit(-1); }
@@ -110,8 +110,7 @@ int main(int argc, char **argv) { int timeout=0; GList *al; struct config_s *config=NULL; - - while((ch=getopt(argc, argv, "c:dt:")) != -1) { + while((ch=getopt(argc, argv, "c:dt:f:")) != -1) { switch(ch) { case 'c': config=readconfig(optarg); @@ -124,6 +123,9 @@ int main(int argc, char **argv) { case 't': timeout=strtol(optarg, NULL, 10); break; + case 'f': + preparelogfile(optarg); + break; default: usage(); break; diff --git a/getstream.h b/getstream.h index 5aec52c..6226b10 100644 --- a/getstream.h +++ b/getstream.h @@ -165,6 +165,7 @@ enum { */
extern int loglevel; +//extern int uselogfile;
Debugging/First incarnation left over?
#define MAX_MCAST_PAYLOAD (1500-40) #define TS_PACKET_SIZE 188 diff --git a/logging.c b/logging.c index 3c62f16..91d3582 100644 --- a/logging.c +++ b/logging.c @@ -3,11 +3,22 @@ #include <stdlib.h> #include <sys/param.h> #include <time.h> - +#include <fcntl.h> #include "getstream.h"
int loglevel=LOG_ERROR; - +int uselogfile=0;
static? And in the end one could check for lfd beeing non null.
+FILE * lfd; +void preparelogfile(char *file) { + lfd=fopen(file,"a"); + if(lfd==NULL) + { + logwrite(LOG_ERROR,"error opening log file '%s' for write",file); + uselogfile=0; + return; + } + uselogfile=1; +} void logwrite_inc_level() { loglevel++; } @@ -36,4 +47,11 @@ void logwrite(int level, const char *format, ...) { timedate, (int) tv.tv_usec/1000, logbuffer); + if(uselogfile==1) { + fprintf(lfd,"%s.%03d %s\n", + timedate, + (int) tv.tv_usec/1000, + logbuffer); + fflush(lfd); + } }
This is "strange" - you are sending the output to file _AND_ to the screen? I would have expected -f to send it to the file only? And i'd discourage use of files at all - in the end we are talking about a real time process which needs to handle the TS Packets with the least delay possible - file access on the other hand may block forever when your machine is under heavy i/o load. And the fflush makes it even worse. When doing this you'd better be using a seperate thread for writing out the log and using something like g_async_push from the packet forwarding thread to the log writer or better use syslog - it was designed for logging ... Flo -- Florian Lohoff f@zz.de "Es ist ein grobes Missverständnis und eine Fehlwahrnehmung, dem Staat im Internet Zensur- und Überwachungsabsichten zu unterstellen." - - Bundesminister Dr. Wolfgang Schäuble -- 10. Juli in Berlin
participants (1)
-
Florian Lohoff