OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimNotify.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 // License: See top level LICENSE.txt file.
3 //
4 // Author: Garrett Potts
5 //
6 // Description:
7 //
8 // Contains class definition for ossimNotify.
9 //*******************************************************************
10 // $Id: ossimNotify.cpp 23467 2015-08-14 13:59:09Z gpotts $
11 
12 #include <iostream>
13 #include <cstdio>
14 #include <cstdarg>
15 #include <stack>
16 #include <cstddef>
17 
18 #include <ossim/base/ossimNotify.h>
19 #include <mutex>
20 static std::ostream* theOssimFatalStream = &std::cerr;
21 static std::ostream* theOssimWarnStream = &std::cerr;
22 static std::ostream* theOssimInfoStream = &std::cout;
23 static std::ostream* theOssimNoticeStream = &std::cout;
24 static std::ostream* theOssimDebugStream = &std::cout;
25 static std::ostream* theOssimAlwaysStream = &std::cout;
26 
27 static std::mutex theMutex;
28 static ossimNotifyFlags theNotifyFlags = ossimNotifyFlags_ALL;
29 std::stack<ossimNotifyFlags> theNotifyFlagsStack;
30 
31 template <class charT, class traits = std::char_traits<charT> >
32 class ossimNullBufferStream : public std::basic_streambuf<charT, traits>
33 {
34 public:
36 
37 
38 protected:
39 
40  std::streamsize xsputn(const charT * /* pChar */, std::streamsize /* n */)
41  {
42  return 0;
43  }
44 
45 private:
48 };
49 
50 template <class charT, class traits = std::char_traits<charT> >
51 class ossimLogFileBufferStream : public std::basic_streambuf<charT, traits>
52 {
53 public:
55 
56  void setLogFilename(const ossimFilename& file)
57  {
58  theLogFilename = file;
59  }
61  {
62  return theLogFilename;
63  }
64 
65 protected:
67  std::mutex fileMutex;
68  virtual int overflow(int c)
69  {
70  std::lock_guard<std::mutex> scopedLock(fileMutex);
71  if(!traits::eq_int_type(c, traits::eof()))
72  {
73  tempString = tempString + ossimString((char)c);
74  }
75 
76  return c;
77  }
78 
79  virtual std::streamsize xsputn(const charT * pChar, std::streamsize n)
80  {
81  std::lock_guard<std::mutex> scopedLock(fileMutex);
82 
83  tempString = tempString + ossimString(pChar, pChar + n);
84 
85  return n;
86  }
87 
88  virtual int sync()
89  {
90  std::lock_guard<std::mutex> scopedLock(fileMutex);
91  flushBuffer();
92  return 0;
93  }
94 
95 private:
97 
98  void flushBuffer()
99  {
100  if((theLogFilename != "") && (tempString!=""))
101  {
103  std::ios::app|std::ios::out);
104  if(outFile)
105  {
106  outFile.write(tempString.c_str(), (std::streamsize)tempString.length());
107  }
108  else
109  {
110  }
111 
112  tempString = "";
113  }
114  }
117 };
118 
119 
121 {
122 public:
125  {
126  nullBufferStream.pubsync();
127  }
128 
129 private:
131  // Copy & assignment are undefined in iostreams
134 };
135 
137 {
138 public:
141  {
142  theLogFileBufferStream.pubsync();
143  }
144  void setLogFilename(const ossimFilename& filename)
145  {
147  }
149  {
151  }
152 
153 private:
155  // Copy & assignment are undefined in iostreams
158 };
159 
160 static ossimNullStream theOssimNullStream;
161 static ossimLogFileStream theLogFileStream;
162 
164 {
165  std::lock_guard<std::mutex> lock(theMutex);
166  theOssimFatalStream = &std::cerr;
167  theOssimWarnStream = &std::cout;
168  theOssimInfoStream = &std::cout;
169  theOssimNoticeStream = &std::cout;
170  theOssimDebugStream = &std::cout;
171  theOssimAlwaysStream = &std::cout;
172 }
173 
175  ossimNotifyFlags whichLevelsToRedirect)
176 {
177  std::lock_guard<std::mutex> lock(theMutex);
178  if(whichLevelsToRedirect&ossimNotifyFlags_FATAL)
179  {
180  theOssimFatalStream = outputStream;
181  }
182  if(whichLevelsToRedirect&ossimNotifyFlags_WARN)
183  {
184  theOssimWarnStream = outputStream;
185  }
186  if(whichLevelsToRedirect&ossimNotifyFlags_INFO)
187  {
188  theOssimInfoStream = outputStream;
189  }
190  if(whichLevelsToRedirect&ossimNotifyFlags_NOTICE)
191  {
192  theOssimNoticeStream = outputStream;
193  }
194  if(whichLevelsToRedirect&ossimNotifyFlags_DEBUG)
195  {
196  theOssimDebugStream = outputStream;
197  }
198 }
199 
201 {
202  std::lock_guard<std::mutex> lock(theMutex);
203  std::ostream* notifyStream = &theOssimNullStream;
204 
205  switch(whichLevel)
206  {
208  {
209  notifyStream = theOssimAlwaysStream;
210  break;
211  }
213  {
214  notifyStream = theOssimFatalStream;
215  break;
216  }
218  {
219  notifyStream = theOssimWarnStream;
220  break;
221  }
223  {
224  notifyStream = theOssimInfoStream;
225  break;
226  }
228  {
229  notifyStream = theOssimNoticeStream;
230  break;
231  }
233  {
234  notifyStream = theOssimDebugStream;
235  break;
236  }
237  }
238  return notifyStream;
239 }
240 
242 {
244  {
245  theMutex.lock();
246  if(theLogFileStream.getLogFilename() != "")
247  {
248  theMutex.unlock();
249  return theLogFileStream;
250  }
251  else
252  {
253  bool reportMessageFlag = false;
254  switch(level)
255  {
257  {
258  reportMessageFlag = true;
259  break;
260  }
262  {
263  if(theNotifyFlags&ossimNotifyFlags_FATAL)
264  {
265  reportMessageFlag = true;
266  }
267  break;
268  }
270  {
271  if(theNotifyFlags&ossimNotifyFlags_WARN)
272  {
273  reportMessageFlag = true;
274  }
275  break;
276  }
278  {
279  if(theNotifyFlags&ossimNotifyFlags_INFO)
280  {
281  reportMessageFlag = true;
282  }
283  break;
284  }
286  {
287  if(theNotifyFlags&ossimNotifyFlags_NOTICE)
288  {
289  reportMessageFlag = true;
290  }
291  break;
292  }
294  {
295  if(theNotifyFlags&ossimNotifyFlags_DEBUG)
296  {
297  reportMessageFlag = true;
298  }
299  break;
300  }
301  }
302  if(reportMessageFlag)
303  {
304  theMutex.unlock();
305  return *ossimGetNotifyStream(level);
306  }
307  }
308 
309  theMutex.unlock();
310 
311  } // matches: if(ossimIsReportingEnabled())
312 
313  return theOssimNullStream;
314 }
315 
316 void ossimSetLogFilename(const ossimFilename& filename)
317 {
318  std::lock_guard<std::mutex> lock(theMutex);
319  theLogFileStream.setLogFilename(filename);
320 }
321 
322 /*
323 const char* ossimGetLogFilename()
324 {
325  return theLogFileStream.getLogFilename().c_str();
326 }
327 */
328 
330 {
331  logFile = theLogFileStream.getLogFilename();
332 }
333 
334 ossimString ossimErrorV(const char *fmt, va_list args )
335 {
336  char temp[2024];
337  if(fmt)
338  {
339  vsprintf(temp, fmt, args);
340  }
341  else
342  {
343  sprintf(temp,"%s", "");
344  }
345 
346  return temp;
347 }
348 
350 {
351  std::lock_guard<std::mutex> lock(theMutex);
352  theNotifyFlags = (ossimNotifyFlags)(theNotifyFlags | flags);
353 }
354 
356 {
357  std::lock_guard<std::mutex> lock(theMutex);
358  theNotifyFlags = (ossimNotifyFlags)((ossimNotifyFlags_ALL^flags)&
359  theNotifyFlags);
360 }
361 
363 {
364  std::lock_guard<std::mutex> lock(theMutex);
365  theNotifyFlags = notifyFlags;
366 }
367 
369 {
370  std::lock_guard<std::mutex> lock(theMutex);
371  theNotifyFlagsStack.push(theNotifyFlags);
372 }
373 
375 {
376  std::lock_guard<std::mutex> lock(theMutex);
377  if(theNotifyFlagsStack.empty())
378  {
379  return;
380  }
381  theNotifyFlags = theNotifyFlagsStack.top();
382  theNotifyFlagsStack.pop();
383 }
384 
386 {
387  std::lock_guard<std::mutex> lock(theMutex);
388  return theNotifyFlags;
389 }
390 
391 
392 
394 {
395  std::lock_guard<std::mutex> lock(theMutex);
396  return (theNotifyFlags != ossimNotifyFlags_NONE);
397 }
398 
399 
401  ossimNotifyLevel notifyLevel)
402 {
403  std::lock_guard<std::mutex> lock(theMutex);
404  ossimNotify(notifyLevel) << msg << "\n";
405 }
406 
407 void ossimSetError( const char* /* className */,
408  ossim_int32 /* error */,
409  const char *fmtString, ...)
410 {
411  // NOTE: This code has an infinite loop in it!!! (drb)
412  //std::lock_guard<std::mutex> lock(theMutex);
413  theMutex.lock();
414  va_list args;
415 
416  va_start(args, fmtString);
417  ossimString result = ossimErrorV(fmtString, args );
418  va_end(args);
419  theMutex.unlock();
420  ossimNotify(ossimNotifyLevel_WARN) << result << "\n";
421 }
422 
423 void ossimSetInfo( const char* /* className */,
424  const char *fmtString, ...)
425 {
426  theMutex.lock();
427  va_list args;
428 
429  va_start(args, fmtString);
430  ossimString result = ossimErrorV(fmtString, args );
431  va_end(args);
432  theMutex.unlock();
433  ossimNotify(ossimNotifyLevel_WARN) << result << "\n";
434 }
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level)
void ossimSetInfo(const char *, const char *fmtString,...)
This is for general warnings and information feedback.
void setLogFilename(const ossimFilename &filename)
ossimNullBufferStream & operator=(const ossimNullBufferStream &)
#define OSSIMDLLEXPORT
void ossimEnableNotify(ossimNotifyFlags flags)
virtual ~ossimNullStream()
void ossimSetDefaultNotifyHandlers()
ossimFilename getLogFilename() const
void ossimPushNotifyFlags()
ossimFilename getLogFilename() const
Definition: ossimNotify.cpp:60
void setLogFilename(const ossimFilename &file)
Definition: ossimNotify.cpp:56
ossimNotifyLevel
Notification level enumeration.
Definition: ossimNotify.h:24
std::string::size_type length() const
Definition: ossimString.h:408
void ossimGetLogFilename(ossimFilename &logFile)
Returns the log filename if set.
os2<< "> n<< " > nendobj n
std::stack< ossimNotifyFlags > theNotifyFlagsStack
Definition: ossimNotify.cpp:29
virtual std::streamsize xsputn(const charT *pChar, std::streamsize n)
Definition: ossimNotify.cpp:79
std::streamsize xsputn(const charT *, std::streamsize)
Definition: ossimNotify.cpp:40
void ossimSetNotifyFlag(ossimNotifyFlags notifyFlags)
virtual ~ossimLogFileStream()
void ossimSetNotifyStream(std::ostream *outputStream, ossimNotifyFlags whichLevelsToRedirect)
ossimNotifyFlags
Flags to allow the user to turn off certain notification levels.
Definition: ossimNotify.h:38
void ossimPopNotifyFlags()
ossimLogFileBufferStream< char > theLogFileBufferStream
ossimNullStream & operator=(const ossimNullStream &)
const char * c_str() const
Returns a pointer to a null-terminated array of characters representing the string&#39;s contents...
Definition: ossimString.h:396
virtual int overflow(int c)
Definition: ossimNotify.cpp:68
void ossimDisableNotify(ossimNotifyFlags flags)
std::ostream * ossimGetNotifyStream(ossimNotifyLevel whichLevel)
std::basic_ofstream< char > ofstream
Class for char output file streams.
Definition: ossimIosFwd.h:47
bool ossimIsReportingEnabled()
ossimString ossimErrorV(const char *fmt, va_list args)
void ossimSetLogFilename(const ossimFilename &filename)
ossimLogFileBufferStream & operator=(const ossimLogFileBufferStream &)
void ossimSetError(const char *, ossim_int32, const char *fmtString,...)
ossimLogFileStream & operator=(const ossimLogFileStream &)
ossimFilename theLogFilename
Definition: ossimNotify.cpp:66
ossimNotifyFlags ossimGetNotifyFlags()
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23
ossimNullBufferStream< char > nullBufferStream
int ossim_int32