EMAN2
log.cpp
Go to the documentation of this file.
1/*
2 * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
3 * Copyright (c) 2000-2006 Baylor College of Medicine
4 *
5 * This software is issued under a joint BSD/GNU license. You may use the
6 * source code in this file under either license. However, note that the
7 * complete EMAN2 and SPARX software packages have some GPL dependencies,
8 * so you are responsible for compliance with the licenses of these packages
9 * if you opt to use BSD licensing. The warranty disclaimer below holds
10 * in either instance.
11 *
12 * This complete copyright notice must be included in any revised version of the
13 * source code. Additional authorship citations may be added, but existing
14 * author citations must be preserved.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * */
31
32#include "log.h"
33#include "util.h"
34#include <cstring>
35#include <sys/stat.h>
36#include <sys/types.h>
37#include <cstdio>
38
39#ifdef WIN32
40#include <time.h>
41#include <process.h>
42#else
43#include <unistd.h>
44#endif
45
46using namespace EMAN;
47using std::string;
48
49Log::Log()
50{
51 out = 0;
53#ifdef WIN32
54 char c[2];
55 c[0] = getenv("WINDIR")[0];
56 c[1] = '\0';
57 default_emandir = string(c) + string(":\\.eman");
58#else
59 default_emandir = string(getenv("HOME")) + "/.eman";
60 mkdir(default_emandir.c_str(), 0xffff);
61#endif
62 default_emanlog = ".emanlog";
63 location = "";
64}
65
66Log::Log(const Log &)
67{
68}
69
71{
72 if (out) {
73 fclose(out);
74 out = 0;
75 }
76}
77
79
81{
82 if (!instance) {
83 instance = new Log();
84 }
85 return instance;
86}
87
88void Log::loc(LogLevel level, const string & filename, int linenum, const string & func)
89{
90 if (log_level < level) {
91 return;
92 }
93
94 location = Util::sbasename(filename) + ":" + Util::int2str(linenum);
95 if (func != "") {
96 location +=" " + func + "()";
97 }
98}
99
100void Log::vlog(const char *format, LogLevel level, va_list arg)
101{
102 if (log_level < level) {
103 return;
104 }
105
106 const char *key = "";
107
108 switch (level) {
109 case WARNING_LOG:
110 key = "Warning: ";
111 break;
112 case ERROR_LOG:
113 key = "Error: ";
114 break;
115 default:
116 key = "";
117 }
118
119 FILE *file = stdout;
120 if (out) {
121 file = out;
122 }
123
124 fprintf(file, "%s", key);
125 vfprintf(file, format, arg);
126 if (location != "") {
127 fprintf(file, " at %s", location.c_str());
128 }
129 fprintf(file, "\n");
130}
131
132void Log::variable(const char *format, ...)
133{
134 va_list arg;
135 va_start(arg, format);
136 vlog(format, VARIABLE_LOG, arg);
137 va_end(arg);
138}
139
140void Log::debug(const char *format, ...)
141{
142 va_list arg;
143 va_start(arg, format);
144 vlog(format, DEBUG_LOG, arg);
145 va_end(arg);
146}
147
148void Log::warn(const char *format, ...)
149{
150 va_list arg;
151 va_start(arg, format);
152 vlog(format, WARNING_LOG, arg);
153 va_end(arg);
154}
155
156void Log::error(const char *format, ...)
157{
158 va_list arg;
159 va_start(arg, format);
160 vlog(format, ERROR_LOG, arg);
161 va_end(arg);
162}
163
164
165void Log::set_level(int level)
166{
167 log_level = (LogLevel)level;
168}
169
170void Log::set_logfile(const char *filename)
171{
172 if (filename && !out) {
173 out = fopen(filename, "wb");
174 }
175}
176
177int Log::begin(int argc, char *argv[], int ppid)
178{
179 time_t tm = time(0);
180 const char *pwd = getenv("PWD");
181 int ref = getpid();
182
183 string filename = Util::sbasename(argv[0]);
184
185 char s[4048];
186#ifndef WIN32
187 sprintf(s, "%d\t%d\t%d\t%d\t%s", ref, (int)tm, 0, ppid ? ppid : getppid(), filename.c_str());
188#else
189 sprintf(s, "%d\t%d\t%d\t%d\t%s", ref, (int)tm, 0, ppid, filename.c_str());
190#endif
191 for (int i = 1; i < argc; i++) {
192 sprintf(s + strlen(s), " %s", argv[i]);
193 }
194 sprintf(s + strlen(s), "\n");
195
196 FILE *eman_file = fopen(default_emanlog.c_str(), "a");
197 if (!eman_file) {
198 return 0;
199 }
200
201 //Util::file_lock_wait(eman_file);
202 fprintf(eman_file, "%s", s);
203 fclose(eman_file);
204
205 string dirlist = default_emandir + "./dirlist";
206 FILE *in = fopen(dirlist.c_str(), "r");
207 if (in) {
208 char s[1024];
209 int f = 0;
210 while (fscanf(in, " %1023s", s) == 1) {
211 if (strcmp(s, pwd) == 0) {
212 f = 1;
213 break;
214 }
215 }
216
217 fclose(in);
218 if (!f) {
219 in = 0;
220 }
221 }
222
223 if (!in) {
224 FILE *dirout = fopen(dirlist.c_str(), "a");
225 if (dirout) {
226 fprintf(dirout, "%s\n", pwd);
227 fclose(dirout);
228 }
229 }
230
231 return ref;
232}
233
234
235void Log::end(int ref, const string& file, const string& text)
236{
237 FILE *out = fopen(".emanlog", "a");
238
239 if (out) {
240 time_t tm = time(0);
241 //Util::file_lock_wait(out);
242 fprintf(out, "%d\t%ld\t%s\t%s\n", ref, tm, file.c_str(), text.c_str());
243 fclose(out);
244 }
245}
Log defines a way to output logging information.
Definition: log.h:69
void end(int ref, const string &file="-", const string &text="")
Definition: log.cpp:235
void debug(const char *format,...)
log a debug message.
Definition: log.cpp:140
void vlog(const char *format, LogLevel level, va_list arg)
Definition: log.cpp:100
static Log * instance
Definition: log.h:118
~Log()
Definition: log.cpp:70
string default_emandir
Definition: log.h:121
LogLevel log_level
Definition: log.h:120
void set_level(int level)
Definition: log.cpp:165
void loc(LogLevel level, const string &file, int linenum, const string &func)
Definition: log.cpp:88
int begin(int argc, char *argv[], int ppid)
begin() and start() are used by command-line programs
Definition: log.cpp:177
void error(const char *format,...)
log an error message.
Definition: log.cpp:156
string default_emanlog
Definition: log.h:122
static Log * logger()
Definition: log.cpp:80
void warn(const char *format,...)
log a warning message.
Definition: log.cpp:148
void set_logfile(const char *filename)
set log output file.
Definition: log.cpp:170
string location
Definition: log.h:123
void variable(const char *format,...)
log a very-detailed-level debug message.
Definition: log.cpp:132
LogLevel
Definition: log.h:72
@ WARNING_LOG
Definition: log.h:74
@ DEBUG_LOG
Definition: log.h:75
@ ERROR_LOG
Definition: log.h:73
@ VARIABLE_LOG
Definition: log.h:76
FILE * out
Definition: log.h:119
Log()
Definition: log.cpp:49
static string sbasename(const string &filename)
Get a filename's basename.
Definition: util.cpp:505
static string int2str(int n)
Get a string format of an integer, e.g.
Definition: util.cpp:315
E2Exception class.
Definition: aligner.h:40