Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   File Members  

jutils.cpp

00001 /* MuSE - Multiple Streaming Engine
00002  * Copyright (C) 2000-2002 Denis Roio aka jaromil <jaromil@dyne.org>
00003  *
00004  * This source code is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Public License as published 
00006  * by the Free Software Foundation; either version 2 of the License,
00007  * or (at your option) any later version.
00008  *
00009  * This source code is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00012  * Please refer to the GNU Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Public License along with
00015  * this source code; if not, write to:
00016  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 #include <iostream>
00020 #include <stdio.h>
00021 #include <string.h>
00022 #include <stdarg.h>
00023 #include <stdlib.h>
00024 #include <errno.h>
00025 #include <sched.h>
00026 #include <sys/time.h>
00027 #include <unistd.h>
00028 #include <time.h>
00029 #include <sys/types.h>
00030 #include <sys/stat.h>
00031 #include <fcntl.h>
00032 #include <sys/socket.h>
00033 #include <netinet/in.h>
00034 #include <arpa/inet.h>
00035 #include <netdb.h>
00036 #include <libintl.h>
00037 
00038 #include <jutils.h>
00039 #include <gui.h>
00040 #include <config.h>
00041 
00042 
00043 
00044 static int verbosity;
00045 static FILE *logfd = NULL;
00046 static GUI *gui = NULL;
00047 
00048 void set_guimsg(GUI *g) {
00049   gui = g;
00050 }
00051 
00052 void MuseSetDebug(int lev) {
00053   lev = lev<0 ? 0 : lev;
00054   lev = lev>3 ? 3 : lev;
00055   verbosity = lev;
00056 }
00057 
00058 int MuseGetDebug() {
00059   return(verbosity);
00060 }
00061 
00062 
00063 void notice(const char *format, ...) {
00064   char msg[255];
00065   va_list arg;
00066   va_start(arg, format);
00067 
00068   vsnprintf(msg, 254, format, arg);
00069 
00070   if(logfd) { fputs(msg,logfd); fputs("\n",logfd); fflush(logfd); }
00071   else fprintf(stderr,"[*] %s\n",msg);
00072 
00073   if(gui) gui->set_status(msg);
00074 
00075   va_end(arg);
00076 }
00077 
00078 void func(const char *format, ...) {
00079   if(verbosity>=2) {
00080     char msg[255];
00081     va_list arg;
00082     va_start(arg, format);
00083     
00084     vsnprintf(msg, 254, format, arg);
00085     if(logfd) { fputs(msg,logfd); fputs("\n",logfd); fflush(logfd); }
00086     else fprintf(stderr,"[F] %s\n",msg);
00087 
00088     va_end(arg);
00089   }
00090 }
00091 
00092 void error(const char *format, ...) {
00093     char msg[255];
00094     va_list arg;
00095     va_start(arg, format);
00096     
00097     vsnprintf(msg, 254, format, arg);
00098     if(logfd) { fputs(msg,logfd); fputs("\n",logfd); fflush(logfd); }
00099     else {
00100       fprintf(stderr,"[!] %s\n",msg);
00101       if(errno)
00102         fprintf(stderr,"[!] %s\n",strerror(errno));
00103     }
00104     
00105     if(gui) gui->set_status(msg);
00106 
00107     va_end(arg);
00108 }
00109 
00110 void act(const char *format, ...) {
00111   char msg[255];
00112   va_list arg;
00113   va_start(arg, format);
00114   
00115   vsprintf(msg, format, arg);
00116 
00117   if(logfd) { fputs(msg,logfd); fputs("\n",logfd); fflush(logfd); }
00118   else fprintf(stderr," .  %s\n",msg);
00119   
00120   va_end(arg);
00121 }
00122 
00123 void warning(const char *format, ...) {
00124   if(verbosity>=1) {
00125     char msg[255];
00126     va_list arg;
00127     va_start(arg, format);
00128     
00129     vsprintf(msg, format, arg);
00130 
00131     if(logfd) { fputs(msg,logfd); fputs("\n",logfd); fflush(logfd); }
00132     else fprintf(stderr,"[W] %s\n",msg);
00133   
00134     va_end(arg);
00135   }
00136 }
00137 
00138 void MuseSetLog(char *file) {
00139   logfd = fopen(file,"w");
00140   if(!logfd) {
00141     error("can't open logfile %s",file);
00142     error("%s",strerror(errno));
00143   }
00144 }
00145 
00146 void MuseCloseLog() {
00147   if(logfd) fclose(logfd);
00148 }
00149 
00150 
00151 void jsleep(int sec, long nsec) {
00152   int ret;
00153   struct timespec timelap;
00154   timelap.tv_sec = sec;
00155   timelap.tv_nsec = nsec;
00156   do {ret = nanosleep(&timelap,NULL);} while (ret==-1 && errno==EINTR);
00157 }
00158 
00159 double dtime() {
00160   struct timeval mytv;
00161   gettimeofday(&mytv,NULL);
00162   return((double)mytv.tv_sec+1.0e-6*(double)mytv.tv_usec);
00163 }
00164 
00165 void chomp(char *str) {
00166   size_t len; //, ilen;
00167   char tmp[MAX_PATH_SIZE], *p = str;
00168   
00169   memset(tmp,'\0',MAX_PATH_SIZE);
00170   
00171   /* eliminate space and tabs at the beginning */
00172   while (*p == ' ' || *p == '\t') p++;
00173   strncpy(tmp, p, MAX_PATH_SIZE);
00174   
00175   /* point *p at the end of string */
00176   len = strlen(tmp); 
00177   p = &tmp[len-1];
00178   
00179   while ((*p == ' ' || *p == '\t' || *p == '\n') && len) {
00180     *p = '\0'; p--; len--;
00181   }
00182 
00183   strncpy(str, tmp, MAX_PATH_SIZE);
00184 }
00185 

Generated on Thu Dec 16 12:28:21 2004 for MuSE by doxygen1.3