Lines 1-23
Link Here
|
1 |
#include "log.h" |
1 |
#include "log.h" |
2 |
#include <iostream> |
2 |
#include <iostream> |
3 |
|
3 |
|
|
|
4 |
#include <sys/types.h> |
5 |
#include <sys/stat.h> |
6 |
#include <fcntl.h> |
7 |
|
4 |
bool |
8 |
bool |
5 |
LogUnit::openLog(const char * filename) |
9 |
LogUnit::openLog(const char * filename) |
6 |
{ |
10 |
{ |
7 |
if (logFile.is_open()) { |
11 |
if ( closeLog() ) |
|
|
12 |
{ |
8 |
cerr << APPNAME |
13 |
cerr << APPNAME |
9 |
<< ": opening a new Log file, while another is already open" |
14 |
<< ": opening a new Log file, while another is already open" |
10 |
<< endl; |
15 |
<< endl; |
11 |
logFile.close(); |
|
|
12 |
} |
16 |
} |
13 |
logFile.open(filename, ios_base::app); |
17 |
|
|
|
18 |
// go through quite some trouble to get a close-on-exec file |
19 |
// descriptor and wrapping it into an ostream object for easing |
20 |
// logging |
21 |
|
22 |
fileBuffer.open( filename, ios_base::app ); |
23 |
fcntl(fileBuffer.fd(), F_SETFD, FD_CLOEXEC); |
24 |
logFile.rdbuf( &fileBuffer ); |
14 |
|
25 |
|
15 |
return !(logFile.fail()); |
26 |
return !(logFile.fail()); |
16 |
} |
27 |
} |
17 |
|
28 |
|
18 |
void |
29 |
bool |
19 |
LogUnit::closeLog() |
30 |
LogUnit::closeLog() |
20 |
{ |
31 |
{ |
21 |
if (logFile.is_open()) |
32 |
if( ! fileBuffer.is_open() ) |
22 |
logFile.close(); |
33 |
return false; |
|
|
34 |
|
35 |
fileBuffer.close(); |
36 |
return true; |
23 |
} |
37 |
} |