/*************************************************************************** * Copyright (C) 2006 by couriousous, blino, trem * * couriousous@mandriva.org * * oblin@mandriva.com * * trem@mandriva.org * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include #include "prcsys.h" #include "prcsys_log.h" #include #include #include static FILE * logf = NULL; static struct timeval starttime; void openlog(char * file) { int fd = 0; int flag; gettimeofday(&starttime,NULL); if(logf) closelog(); if(file == NULL) file = "/var/log/prcsys.log"; logf= fopen(file,"w"); if(logf == NULL) { if(!test_mode) printf("Cannot open log file %s ! \n",file); return; } /* set the close-on-exec flag */ fd = fileno(logf); if(fd < 0) return; flag = fcntl(fd, F_GETFD); if(flag < 0) return; fcntl(fd,F_SETFD,flag | FD_CLOEXEC); } void closelog(void) { struct timeval stoptime; gettimeofday(&stoptime,NULL); if(logf) { fprintf(logf,"Total time: %d seconds\n",(int) (stoptime.tv_sec - starttime.tv_sec)); fclose(logf); } logf = NULL; } void printandlog(char * format, ...) { va_list list; if(logf) { va_start(list,format); vfprintf(logf,format,list); va_end(list); fflush(logf); } va_start(list,format); vprintf(format,list); va_end(list); } void dbg(char * format, ...) { va_list list; if(logf && debug) { if(debug == 2) { va_start(list,format); vprintf(format,list); va_end(list); } va_start(list,format); vfprintf(logf,format,list); va_end(list); fflush(logf); } } void writetolog(char * buffer,size_t s) { if(logf) { fwrite(buffer,1,s,logf); fflush(logf); } }