log.cc

00001 /* 
00002    EyeDB Object Database Management System
00003    Copyright (C) 1994-2008 SYSRA
00004    
00005    EyeDB is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009    
00010    EyeDB is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014    
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA 
00018 */
00019 
00020 /*
00021    Author: Eric Viara <viara@sysra.com>
00022 */
00023 
00024 
00025 #include <eyedbconfig.h>
00026 
00027 #include <pthread.h>
00028 #include <string.h>
00029 #include <stdlib.h>
00030 #include <stdarg.h>
00031 #include <unistd.h>
00032 #if TIME_WITH_SYS_TIME
00033 #include <sys/time.h>
00034 #include <time.h>
00035 #else
00036 #if HAVE_SYS_TIME_H
00037 #include <sys/time.h>
00038 #else
00039 #include <time.h>
00040 #endif
00041 #endif
00042 
00043 #include <eyedblib/log.h>
00044 #include <eyedblib/rpc_lib.h>
00045 
00046 #ifndef HAVE_CTIME_R
00047 #define CTIME_R(T, S, L) S = ctime(T)
00048 #else
00049 #ifdef HAVE_CTIME_R_3
00050 #define CTIME_R(T, S, L) ctime_r(T, S, L)
00051 #else
00052 #define CTIME_R(T, S, L) ctime_r(T, S)
00053 #endif
00054 #endif
00055 
00056 static FILE *logfd;
00057 static char *logProgName;
00058 static char *logDevName;
00059 static FILE *fdnull;
00060 
00061 eyedblib::LogMask eyedblib::log_mask;
00062 
00063 void
00064 utlogInit(const char *progName, const char *devname)
00065 {
00066   if (!fdnull)
00067     fdnull = fopen("/dev/null", "w");
00068 
00069   if (logfd && logfd != stdout && logfd != stderr)
00070     fclose(logfd);
00071 
00072   logfd = (FILE *)0;
00073 
00074   if (!devname)
00075     return;
00076 
00077   free(logDevName);
00078   logDevName = strdup(devname);
00079 
00080   free(logProgName);
00081   logProgName = strdup(progName);
00082 
00083   if (!strcmp(devname, "stderr"))
00084     logfd = stderr;
00085   else if (!strcmp(devname, "stdout"))
00086     logfd = stdout;
00087   else {
00088     logfd = fopen(devname, "w");
00089 
00090     if (!logfd) {
00091       fprintf(stderr, "%s: cannot open log file '%s' for writing\n",
00092               logProgName, devname);
00093       /*exit(1);*/
00094     }
00095   }
00096 }
00097 
00098 FILE *
00099 utlogFDGet()
00100 {
00101   return (logfd ? logfd : fdnull);
00102 }
00103 
00104 FILE *
00105 utlogFDSet(FILE *fd)
00106 {
00107   FILE *oldlogfd = logfd;
00108   logfd = fd;
00109   return oldlogfd;
00110 }
00111 
00112 const char *utlogDevNameGet()
00113 {
00114   /*  check(); */
00115   return logDevName;
00116 }
00117 
00118 int rpc_from_core;
00119 
00120 static unsigned long long ms_start;
00121 static int log_date = 1;
00122 static int log_timer;
00123 static int log_progname;
00124 static int log_pid;
00125 
00126 void
00127 utlogResetTimer()
00128 {
00129   ms_start = 0;
00130 }
00131 
00132 void
00133 utlogSetLogDate(int on)
00134 {
00135   log_date = on;
00136 }
00137 
00138 void
00139 utlogSetLogTimer(int on)
00140 {
00141   log_timer = on;
00142 }
00143 
00144 void
00145 utlogSetLogPid(int on)
00146 {
00147   log_pid = on;
00148 }
00149 
00150 void
00151 utlogSetLogProgName(int on)
00152 {
00153   log_progname = on;
00154 }
00155 
00156 void
00157 utlog_p(const char *s)
00158 {
00159   static const char prefix[] = "IDB_LOG_";
00160   static const int prefix_len = 8;
00161   const char *x;
00162 
00163   if (!logfd || rpc_from_core)
00164     return;
00165 
00166   if (!strncmp(s, prefix, prefix_len))
00167     x = s + prefix_len;
00168   else
00169     x = s;
00170 
00171   fprintf(logfd, "%s ", x);
00172 }
00173 
00174 
00175 void
00176 utlog(const char *fmt, ...)
00177 {
00178   va_list ap;
00179   struct timeval tv;
00180   unsigned long long ms;
00181   time_t t;
00182 #ifdef HAVE_CTIME_R
00183   char time_str[64];
00184 #else
00185   char *time_str;
00186 #endif
00187 
00188   if (!logfd || rpc_from_core)
00189     return;
00190 
00191   if (log_date) {
00192     time(&t);
00193     CTIME_R(&t, time_str, sizeof(time_str)-1);
00194     time_str[strlen(time_str)-1] = 0;
00195     fprintf(logfd, "%s ", time_str);
00196   }
00197 
00198   if (log_timer) {
00199     gettimeofday(&tv, NULL);
00200     ms = (unsigned long long)tv.tv_sec * 1000ULL +
00201       (unsigned long long)tv.tv_usec / 1000ULL;
00202     if (!ms_start)
00203       ms_start = ms;
00204     fprintf(logfd, "%llu ms ", ms-ms_start);
00205   }
00206 
00207 
00208   if (log_pid)
00209     fprintf(logfd, "[thread %d#%d] ", rpc_getpid(), pthread_self());
00210 
00211   if (log_progname)
00212     fprintf(logfd, "%s ", logProgName);
00213 
00214   if (log_date || log_timer || log_pid || log_progname)
00215     fprintf(logfd, ": ");
00216 
00217   va_start(ap, fmt);
00218   vfprintf(logfd, fmt, ap);
00219   va_end(ap);
00220 
00221   fflush(logfd);
00222 }

Generated on Mon Dec 22 18:15:58 2008 for eyedb by  doxygen 1.5.3