EMAN2
log.cpp
Go to the documentation of this file.
00001 
00005 /*
00006  * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
00007  * Copyright (c) 2000-2006 Baylor College of Medicine
00008  *
00009  * This software is issued under a joint BSD/GNU license. You may use the
00010  * source code in this file under either license. However, note that the
00011  * complete EMAN2 and SPARX software packages have some GPL dependencies,
00012  * so you are responsible for compliance with the licenses of these packages
00013  * if you opt to use BSD licensing. The warranty disclaimer below holds
00014  * in either instance.
00015  *
00016  * This complete copyright notice must be included in any revised version of the
00017  * source code. Additional authorship citations may be added, but existing
00018  * author citations must be preserved.
00019  *
00020  * This program is free software; you can redistribute it and/or modify
00021  * it under the terms of the GNU General Public License as published by
00022  * the Free Software Foundation; either version 2 of the License, or
00023  * (at your option) any later version.
00024  *
00025  * This program is distributed in the hope that it will be useful,
00026  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00028  * GNU General Public License for more details.
00029  *
00030  * You should have received a copy of the GNU General Public License
00031  * along with this program; if not, write to the Free Software
00032  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00033  *
00034  * */
00035 
00036 #include "log.h"
00037 #include "util.h"
00038 #include <cstring>
00039 #include <sys/stat.h>
00040 #include <sys/types.h>
00041 #include <cstdio>
00042 
00043 #ifdef WIN32
00044 #include <time.h>
00045 #include <process.h>
00046 #else
00047 #include <unistd.h>
00048 #endif
00049 
00050 using namespace EMAN;
00051 using std::string;
00052 
00053 Log::Log()
00054 {
00055         out = 0;
00056         log_level = ERROR_LOG;
00057 #ifdef WIN32
00058         char c[2];
00059         c[0] = getenv("WINDIR")[0];
00060         c[1] = '\0';
00061         default_emandir = string(c) + string(":\\.eman");
00062 #else
00063         default_emandir = string(getenv("HOME")) + "/.eman";
00064         mkdir(default_emandir.c_str(), 0xffff);
00065 #endif
00066         default_emanlog = ".emanlog";
00067         location = "";
00068 }
00069 
00070 Log::Log(const Log &)
00071 {
00072 }
00073 
00074 Log::~Log()
00075 {
00076         if (out) {
00077                 fclose(out);
00078                 out = 0;
00079         }
00080 }
00081 
00082 Log *Log::instance = 0;
00083 
00084 Log *Log::logger()
00085 {
00086         if (!instance) {
00087                 instance = new Log();
00088         }
00089         return instance;
00090 }
00091 
00092 void Log::loc(LogLevel level, const string & filename, int linenum, const string & func)
00093 {
00094         if (log_level < level) {
00095                 return;
00096         }
00097 
00098         location = Util::sbasename(filename) + ":" + Util::int2str(linenum);
00099         if (func != "") {
00100                 location +=" " + func + "()";
00101         }
00102 }
00103 
00104 void Log::vlog(const char *format, LogLevel level, va_list arg)
00105 {
00106         if (log_level < level) {
00107                 return;
00108         }
00109 
00110         const char *key = "";
00111 
00112         switch (level) {
00113         case WARNING_LOG:
00114                 key = "Warning: ";
00115                 break;
00116         case ERROR_LOG:
00117                 key = "Error: ";
00118                 break;
00119         default:
00120                 key = "";
00121         }
00122 
00123         FILE *file = stdout;
00124         if (out) {
00125                 file = out;
00126         }
00127 
00128         fprintf(file, "%s", key);
00129         vfprintf(file, format, arg);
00130         if (location != "") {
00131                 fprintf(file, " at %s", location.c_str());
00132         }
00133         fprintf(file, "\n");
00134 }
00135 
00136 void Log::variable(const char *format, ...)
00137 {
00138         va_list arg;
00139         va_start(arg, format);
00140         vlog(format, VARIABLE_LOG, arg);
00141         va_end(arg);
00142 }
00143 
00144 void Log::debug(const char *format, ...)
00145 {
00146         va_list arg;
00147         va_start(arg, format);
00148         vlog(format, DEBUG_LOG, arg);
00149         va_end(arg);
00150 }
00151 
00152 void Log::warn(const char *format, ...)
00153 {
00154         va_list arg;
00155         va_start(arg, format);
00156         vlog(format, WARNING_LOG, arg);
00157         va_end(arg);
00158 }
00159 
00160 void Log::error(const char *format, ...)
00161 {
00162         va_list arg;
00163         va_start(arg, format);
00164         vlog(format, ERROR_LOG, arg);
00165         va_end(arg);
00166 }
00167 
00168 
00169 void Log::set_level(int level)
00170 {
00171         log_level = (LogLevel)level;
00172 }
00173 
00174 void Log::set_logfile(const char *filename)
00175 {
00176         if (filename && !out) {
00177                 out = fopen(filename, "wb");
00178         }
00179 }
00180 
00181 int Log::begin(int argc, char *argv[], int ppid)
00182 {
00183         time_t tm = time(0);
00184         const char *pwd = getenv("PWD");
00185 #ifdef  _WIN32
00186         int ref = _getpid();
00187 #else
00188         int ref = getpid();
00189 #endif
00190 
00191         string filename = Util::sbasename(argv[0]);
00192 
00193         char s[4048];
00194 #ifndef WIN32
00195         sprintf(s, "%d\t%d\t%d\t%d\t%s", ref, (int)tm, 0, ppid ? ppid : getppid(), filename.c_str());
00196 #else
00197         sprintf(s, "%d\t%d\t%d\t%d\t%s", ref, (int)tm, 0, ppid, filename.c_str());
00198 #endif
00199         for (int i = 1; i < argc; i++) {
00200                 sprintf(s + strlen(s), " %s", argv[i]);
00201         }
00202         sprintf(s + strlen(s), "\n");
00203 
00204         FILE *eman_file = fopen(default_emanlog.c_str(), "a");
00205         if (!eman_file) {
00206                 return 0;
00207         }
00208 
00209         //Util::file_lock_wait(eman_file);
00210         fprintf(eman_file, "%s", s);
00211         fclose(eman_file);
00212 
00213         string dirlist = default_emandir + "./dirlist";
00214         FILE *in = fopen(dirlist.c_str(), "r");
00215         if (in) {
00216                 char s[1024];
00217                 int f = 0;
00218                 while (fscanf(in, " %1023s", s) == 1) {
00219                         if (strcmp(s, pwd) == 0) {
00220                                 f = 1;
00221                                 break;
00222                         }
00223                 }
00224 
00225                 fclose(in);
00226                 if (!f) {
00227                         in = 0;
00228                 }
00229         }
00230 
00231         if (!in) {
00232                 FILE *dirout = fopen(dirlist.c_str(), "a");
00233                 if (dirout) {
00234                         fprintf(dirout, "%s\n", pwd);
00235                         fclose(dirout);
00236                 }
00237         }
00238 
00239         return ref;
00240 }
00241 
00242 
00243 void Log::end(int ref, const string& file, const string& text)
00244 {
00245         FILE *out = fopen(".emanlog", "a");
00246 
00247         if (out) {
00248                 time_t tm = time(0);
00249                 //Util::file_lock_wait(out);
00250                 fprintf(out, "%d\t%ld\t%s\t%s\n", ref, tm, file.c_str(), text.c_str());
00251                 fclose(out);
00252         }
00253 }