Newer
Older
/* log.cc
Mark Woolrich, FMRIB Image Analysis Group
Copyright (C) 1999-2000 University of Oxford */
/* CCOPYRIGHT */
#include "log.h"
namespace Utilities {
void Log::makeDir(const string& pdirname, const string& plogfilename, bool pstream_to_logfile, bool pstream_to_cout)
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{
if(logEstablished)
{
logfileout.close();
}
dir = pdirname;
logfilename = plogfilename;
stream_to_logfile = pstream_to_logfile;
stream_to_cout = pstream_to_cout;
// make directory to place results into:
// keep adding "+" until directory is made:
int count = 0;
while(true)
{
if(count >= 20)
{
string s("Cannot create directory " + dir);
throw Exception(s.c_str());
}
int ret = system(("mkdir "+ dir + " 2>/dev/null").c_str());
if(ret == 0)
{
break;
}
dir = dir + "+";
count++;
}
// setup logfile
if(stream_to_logfile)
{
logfileout.open((dir + "/" + logfilename).c_str(), ios::out);
if(logfileout.bad())
{
throw Exception(string(string("Unable to setup logfile ")+logfilename+string(" in directory ")+dir).c_str());
}
}
logEstablished = true;
}
void Log::setDir(const string& pdirname, const string& plogfilename, bool pstream_to_logfile, bool pstream_to_cout)
{
if(logEstablished)
{
logfileout.close();
}
dir = pdirname;
logfilename = plogfilename;
stream_to_logfile = pstream_to_logfile;
stream_to_cout = pstream_to_cout;
// setup logfile
if(stream_to_logfile)
{
logfileout.open((dir + "/" + logfilename).c_str(), ios::out);
if(logfileout.bad())
{
throw Exception(string(string("Unable to setup logfile ")+logfilename+string(" in directory ")+dir).c_str());
}
}
logEstablished = true;
}
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
void Log::setthenmakeDir(const string& pdirname, const string& plogfilename, bool pstream_to_logfile, bool pstream_to_cout)
{
if(logEstablished)
{
logfileout.close();
}
dir = pdirname;
logfilename = plogfilename;
stream_to_logfile = pstream_to_logfile;
stream_to_cout = pstream_to_cout;
ifstream dirif;
dirif.open(dir.c_str(), ios::in);
bool direxists = !logfileout.bad();
cout << "direxists = " << direxists << endl;
if(!direxists)
{
// make directory
int ret = system(("mkdir "+ dir + " 2>/dev/null").c_str());
if(ret == 0)
{
throw Exception(string(string("Unable to make directory ")+dir).c_str());
}
}
// setup logfile
if(stream_to_logfile)
{
logfileout.open((dir + "/" + logfilename).c_str(), ios::out);
if(logfileout.bad())
{
throw Exception(string(string("Unable to setup logfile ")+logfilename+string(" in directory ")+dir).c_str());
}
}
logEstablished = true;
}