1 |
Index: applog.cpp |
2 |
=================================================================== |
3 |
--- src/applog.cpp (revisione 1570) |
4 |
+++ src/applog.cpp (revisione 1907) |
5 |
@@ -119,6 +119,7 @@ |
6 |
string _nomeFile; |
7 |
std::fstream _logfs; |
8 |
bool _usePipe; |
9 |
+ bool _closedByApplog; |
10 |
|
11 |
protected: |
12 |
// to dequeue log messages and write them to file if not log_directly |
13 |
@@ -127,6 +128,7 @@ |
14 |
virtual void stopQueue(void); |
15 |
virtual void onTimer(void); |
16 |
virtual void final(void); |
17 |
+ void _openFile(); |
18 |
|
19 |
public: |
20 |
logger(const char* logFileName = NULL, bool usePipe = false); |
21 |
@@ -135,6 +137,9 @@ |
22 |
// To change log file name |
23 |
void logFileName(const char* FileName, bool usePipe = false); |
24 |
|
25 |
+ void openFile(); |
26 |
+ void closeFile(); |
27 |
+ |
28 |
}; |
29 |
|
30 |
|
31 |
@@ -285,36 +290,15 @@ |
32 |
} |
33 |
|
34 |
// class logger |
35 |
-logger::logger(const char* logFileName, bool usePipe) : ThreadQueue(NULL, 0, 0), _usePipe(usePipe) |
36 |
+logger::logger(const char* logFileName, bool usePipe) : ThreadQueue(NULL, 0, 0), _usePipe(usePipe), _closedByApplog(false) |
37 |
{ |
38 |
_nomeFile = ""; |
39 |
|
40 |
if (logFileName) |
41 |
_nomeFile = logFileName; |
42 |
- if (!_nomeFile.empty()) |
43 |
- { |
44 |
- if (!_usePipe) |
45 |
- { |
46 |
- _logfs.open(_nomeFile.c_str(), std::ofstream::out | std::ofstream::app | std::ofstream::ate); |
47 |
+ |
48 |
+ openFile(); |
49 |
} |
50 |
-#ifndef WIN32 |
51 |
- else |
52 |
- { |
53 |
- // create pipe |
54 |
- int err = mkfifo(_nomeFile.c_str(), S_IREAD | S_IWRITE); |
55 |
- if (err == 0 || errno == EEXIST) |
56 |
- { |
57 |
- // and open it |
58 |
- _logfs.open(_nomeFile.c_str(), std::fstream::in | std::fstream::out); |
59 |
- } |
60 |
- else |
61 |
- THROW(AppLogException("Can't create pipe")); |
62 |
- } |
63 |
-#endif |
64 |
- if (_logfs.fail()) |
65 |
- THROW(AppLogException("Can't open log file name")); |
66 |
- } |
67 |
-} |
68 |
|
69 |
logger::~logger() |
70 |
{ |
71 |
@@ -336,8 +320,24 @@ |
72 |
if (_logfs.is_open()) |
73 |
_logfs.close(); |
74 |
|
75 |
+ openFile(); |
76 |
+} |
77 |
+ |
78 |
+/// open also logger if applog->open() is invoked |
79 |
+void logger::openFile() |
80 |
+{ |
81 |
+ _closedByApplog=false; |
82 |
+} |
83 |
+ |
84 |
+///internal logger openFile needed to use pipe and avoid stream buffering in the case |
85 |
+/// the consumer is not connected to pipe |
86 |
+void logger::_openFile() |
87 |
+{ |
88 |
+ if (!_closedByApplog && !_logfs.is_open()) |
89 |
+ { |
90 |
if (!_nomeFile.empty()) |
91 |
{ |
92 |
+ _logfs.clear(); |
93 |
if (!_usePipe) |
94 |
{ |
95 |
_logfs.open(_nomeFile.c_str(), std::ofstream::out | std::ofstream::app | std::ofstream::ate); |
96 |
@@ -359,14 +359,34 @@ |
97 |
if (_logfs.fail()) |
98 |
THROW(AppLogException("Can't open log file name")); |
99 |
} |
100 |
+ } |
101 |
+} |
102 |
|
103 |
+/// close also logger if applog->close() is invoked |
104 |
+void logger::closeFile() |
105 |
+{ |
106 |
+ _closedByApplog = true; |
107 |
} |
108 |
|
109 |
+ |
110 |
// writes into filename enqueued messages |
111 |
void logger::runQueue(void * data) |
112 |
{ |
113 |
char *str = (char *) data; |
114 |
|
115 |
+ // if for some internal reasons file has been closed |
116 |
+ // reopen it |
117 |
+ try |
118 |
+ { |
119 |
+ _openFile(); |
120 |
+ } |
121 |
+ catch (AppLogException e) |
122 |
+ { |
123 |
+ std::cerr << e.what() << std::endl; |
124 |
+ slog.emerg("%s\n", e.what()); |
125 |
+ std::cerr.flush(); |
126 |
+ } |
127 |
+ |
128 |
if (_logfs.is_open()) |
129 |
{ |
130 |
Thread::setCancel(cancelDisabled); |
131 |
@@ -374,7 +394,15 @@ |
132 |
_logfs.flush(); |
133 |
Thread::setCancel(cancelImmediate); |
134 |
} |
135 |
+ |
136 |
+ //if we use a pipe to avoid increasing of stream buffer |
137 |
+ // without a consumer, we open, use and close it |
138 |
+ if ((_usePipe || _closedByApplog) && _logfs.is_open()) |
139 |
+ { |
140 |
+ _logfs.flush(); |
141 |
+ _logfs.close(); |
142 |
} |
143 |
+} |
144 |
|
145 |
void logger::startQueue() |
146 |
{ |
147 |
@@ -545,7 +573,6 @@ |
148 |
#endif |
149 |
if (!d->_logDirectly) |
150 |
{ |
151 |
- d->_nomeFile = FileName; |
152 |
if (d->_pLogger) |
153 |
d->_pLogger->logFileName(FileName, d->_logPipe); |
154 |
else |
155 |
@@ -728,7 +755,12 @@ |
156 |
} |
157 |
d->_lock.leaveMutex(); |
158 |
} |
159 |
+ else |
160 |
+ { |
161 |
+ if (d->_pLogger) |
162 |
+ d->_pLogger->closeFile(); |
163 |
} |
164 |
+} |
165 |
|
166 |
void AppLog::open(const char *ident) |
167 |
{ |
168 |
@@ -760,6 +792,11 @@ |
169 |
} |
170 |
d->_lock.leaveMutex(); |
171 |
} |
172 |
+ else |
173 |
+ { |
174 |
+ if (d->_pLogger) |
175 |
+ d->_pLogger->openFile(); |
176 |
+ } |
177 |
if (ident != NULL) |
178 |
logIt->second._ident = ident; |
179 |
|