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 
00037 #include <jutils.h>
00038 #include <gui.h>
00039 #include <config.h>
00040 
00041 static int verbosity;
00042 static FILE *logfd = NULL;
00043 static GUI *gui = NULL;
00044 
00045 void set_guimsg(GUI *g) {
00046   gui = g;
00047 }
00048 
00049 void MuseSetDebug(int lev) {
00050   lev = lev<0 ? 0 : lev;
00051   lev = lev>3 ? 3 : lev;
00052   verbosity = lev;
00053 }
00054 
00055 int MuseGetDebug() {
00056   return(verbosity);
00057 }
00058 
00059 
00060 void notice(const char *format, ...) {
00061   char msg[255];
00062   va_list arg;
00063   va_start(arg, format);
00064 
00065   vsnprintf(msg, 254, format, arg);
00066 
00067   if(logfd) { fputs(msg,logfd); fputs("\n",logfd); fflush(logfd); }
00068   else fprintf(stderr,"[*] %s\n",msg);
00069 
00070   if(gui) gui->set_status(msg);
00071 
00072   va_end(arg);
00073 }
00074 
00075 void func(const char *format, ...) {
00076   if(verbosity>=2) {
00077     char msg[255];
00078     va_list arg;
00079     va_start(arg, format);
00080     
00081     vsnprintf(msg, 254, format, arg);
00082     if(logfd) { fputs(msg,logfd); fputs("\n",logfd); fflush(logfd); }
00083     else fprintf(stderr,"[F] %s\n",msg);
00084 
00085     va_end(arg);
00086   }
00087 }
00088 
00089 void error(const char *format, ...) {
00090     char msg[255];
00091     va_list arg;
00092     va_start(arg, format);
00093     
00094     vsnprintf(msg, 254, format, arg);
00095     if(logfd) { fputs(msg,logfd); fputs("\n",logfd); fflush(logfd); }
00096     else {
00097       fprintf(stderr,"[!] %s\n",msg);
00098       if(errno)
00099         fprintf(stderr,"[!] %s\n",strerror(errno));
00100     }
00101     
00102     if(gui) gui->set_status(msg);
00103 
00104     va_end(arg);
00105 }
00106 
00107 void act(const char *format, ...) {
00108   char msg[255];
00109   va_list arg;
00110   va_start(arg, format);
00111   
00112   vsprintf(msg, format, arg);
00113 
00114   if(logfd) { fputs(msg,logfd); fputs("\n",logfd); fflush(logfd); }
00115   else fprintf(stderr," .  %s\n",msg);
00116   
00117   va_end(arg);
00118 }
00119 
00120 void warning(const char *format, ...) {
00121   if(verbosity>=1) {
00122     char msg[255];
00123     va_list arg;
00124     va_start(arg, format);
00125     
00126     vsprintf(msg, format, arg);
00127 
00128     if(logfd) { fputs(msg,logfd); fputs("\n",logfd); fflush(logfd); }
00129     else fprintf(stderr,"[W] %s\n",msg);
00130   
00131     va_end(arg);
00132   }
00133 }
00134 
00135 void MuseSetLog(char *file) {
00136   logfd = fopen(file,"w");
00137   if(!logfd) {
00138     error("can't open logfile %s",file);
00139     error("%s",strerror(errno));
00140   }
00141 }
00142 
00143 void MuseCloseLog() {
00144   if(logfd) fclose(logfd);
00145 }
00146 
00147 
00148 void jsleep(int sec, long nsec) {
00149   struct timespec timelap;
00150   timelap.tv_sec = sec;
00151   timelap.tv_nsec = nsec;
00152   nanosleep(&timelap,NULL);
00153 }
00154 
00155 double dtime() {
00156   struct timeval mytv;
00157   gettimeofday(&mytv,NULL);
00158   return((double)mytv.tv_sec+1.0e-6*(double)mytv.tv_usec);
00159 }
00160 
00161 void chomp(char *str) {
00162   size_t len; //, ilen;
00163   char tmp[MAX_PATH_SIZE], *p = str;
00164   
00165   memset(tmp,'\0',MAX_PATH_SIZE);
00166   
00167   /* eliminate space and tabs at the beginning */
00168   while (*p == ' ' || *p == '\t') p++;
00169   strncpy(tmp, p, MAX_PATH_SIZE);
00170   
00171   /* point *p at the end of string */
00172   len = strlen(tmp); 
00173   p = &tmp[len-1];
00174   
00175   while ((*p == ' ' || *p == '\t' || *p == '\n') && len) {
00176     *p = '\0'; p--; len--;
00177   }
00178 
00179   strncpy(str, tmp, MAX_PATH_SIZE);
00180 }
00181 

Generated on Sat Apr 17 17:38:48 2004 for MuSE by doxygen1.3