Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / APG / Logging / LogManager.h
blob08695c70c7f8cecb3f92e4f613f59cf35f492395
1 #include "ace/streams.h"
2 #include "ace/Synch.h"
3 #include "ace/Singleton.h"
4 #include "ace/Log_Msg.h"
5 #include "ace/Log_Msg_Callback.h"
7 #ifndef LOG_MANAGER_H
8 #define LOG_MANAGER_H
10 // Listing 1 code/ch03
11 class LogManager
13 public:
14 LogManager ();
15 ~LogManager ();
17 void redirectToDaemon
18 (const ACE_TCHAR *prog_name = ACE_TEXT (""));
19 void redirectToSyslog
20 (const ACE_TCHAR *prog_name = ACE_TEXT (""));
21 void redirectToOStream (ACE_OSTREAM_TYPE *output);
22 void redirectToFile (const char *filename);
23 void redirectToStderr ();
24 ACE_Log_Msg_Callback * redirectToCallback
25 (ACE_Log_Msg_Callback *callback);
27 // Exclude 1
28 private:
29 ofstream *log_stream_;
30 ACE_OSTREAM_TYPE *output_stream_;
31 // Exclude 1
33 // Listing 1
35 // Listing 2 code/ch03
36 LogManager::LogManager ()
37 : log_stream_ (0), output_stream_ (0)
38 { }
40 LogManager::~LogManager ()
42 if (log_stream_)
43 log_stream_->close ();
44 delete log_stream_;
47 void LogManager::redirectToSyslog (const ACE_TCHAR *prog_name)
49 ACE_LOG_MSG->open (prog_name, ACE_Log_Msg::SYSLOG, prog_name);
52 void LogManager::redirectToDaemon (const ACE_TCHAR *prog_name)
54 ACE_LOG_MSG->open (prog_name, ACE_Log_Msg::LOGGER,
55 ACE_DEFAULT_LOGGER_KEY);
58 void LogManager::redirectToOStream (ACE_OSTREAM_TYPE *output)
60 output_stream_ = output;
61 ACE_LOG_MSG->msg_ostream (this->output_stream_);
62 ACE_LOG_MSG->clr_flags
63 (ACE_Log_Msg::STDERR | ACE_Log_Msg::LOGGER);
64 ACE_LOG_MSG->set_flags (ACE_Log_Msg::OSTREAM);
67 void LogManager::redirectToFile (const char *filename)
69 log_stream_ = new ofstream ();
70 log_stream_->open (filename, ios::out | ios::app);
71 this->redirectToOStream ((ACE_OSTREAM_TYPE *)log_stream_);
74 void LogManager::redirectToStderr ()
76 ACE_LOG_MSG->clr_flags
77 (ACE_Log_Msg::OSTREAM | ACE_Log_Msg::LOGGER);
78 ACE_LOG_MSG->set_flags (ACE_Log_Msg::STDERR);
81 ACE_Log_Msg_Callback *
82 LogManager::redirectToCallback (ACE_Log_Msg_Callback * callback)
84 ACE_Log_Msg_Callback *previous =
85 ACE_LOG_MSG->msg_callback (callback);
86 if (callback == 0)
87 ACE_LOG_MSG->clr_flags (ACE_Log_Msg::MSG_CALLBACK);
88 else
89 ACE_LOG_MSG->set_flags (ACE_Log_Msg::MSG_CALLBACK);
90 return previous;
92 // Listing 2
94 // Listing 3 code/ch03
95 typedef ACE_Singleton<LogManager, ACE_Null_Mutex>
96 LogManagerSingleton;
97 #define LOG_MANAGER LogManagerSingleton::instance()
98 // Listing 3
100 #endif /* LOG_MANAGER_H */