ACE日志根据信息安全级别记录到不同文件

 

ACE的日志系统很强大,通过配置可以将日志定位到系统输出,系统日志,文件等,非常方便。

本篇文章介绍通过ACE_Log_Msg_Backend对日志进行处理使其输出到不同文件。

 

直接上代码

代码
1 #pragma once
2 #include "ace/OS.h"
3 #include "ace/Log_Msg.h"
4 #include "ace/Log_Record.h"
5 #include "ace/Log_Msg_Backend.h"
6 #include "ace/SString.h"
7 #include "ace/Date_Time.h"
8 #include <string>
9 #include <fstream>
10 #include <sstream>
11  using namespace std;
12  class LogMsgBackend : public ACE_Log_Msg_Backend
13 {
14 public:
15 LogMsgBackend()
16 {
17 timeStamp = getTimeName();
18 debugLogName = timeStamp + ACE_TEXT(".debug.log");
19 errorLogName = timeStamp + ACE_TEXT(".error.log");
20 }
21
22 ~LogMsgBackend()
23 {
24 ACE_LOG_MSG->clr_flags(ACE_Log_Msg::CUSTOM);
25 delete debugLog;
26 delete errorLog;
27 }
28
29 int open (const ACE_TCHAR *logger_key)
30 {
31 debugLog = new ofstream((logDir + debugLogName).c_str(), ios::app);
32 errorLog = new ofstream((logDir + errorLogName).c_str(), ios::app);
33 ACE_LOG_MSG->set_flags(ACE_Log_Msg::OSTREAM);
34 ACE_LOG_MSG->clr_flags(ACE_Log_Msg::STDERR);
35 return 0;
36 }
37
38 int reset (void)
39 {
40 return 0;
41 }
42
43 int close (void)
44 {
45 return 0;
46 }
47
48 int log (ACE_Log_Record &log_record)
49 {
50 if (timeStamp != getTimeName())
51 {
52 timeStamp = getTimeName();
53 errorLogName = timeStamp + ACE_TEXT(".error.log");
54 debugLogName = timeStamp + ACE_TEXT(".debug.log");
55 errorLog->flush();
56 debugLog->flush();
57 debugLog = new ofstream((logDir + debugLogName).c_str(), ios::app);
58 errorLog = new ofstream((logDir + errorLogName).c_str(), ios::app);
59 }
60 unsigned long msg_severity = log_record.type ();
61 ACE_Log_Priority prio = ACE_static_cast (ACE_Log_Priority, msg_severity);
62 if (prio == LM_ERROR)
63 {
64 ACE_LOG_MSG->msg_ostream(errorLog, 0);
65 }
66 else
67 {
68 ACE_LOG_MSG->msg_ostream(debugLog, 0);
69 }
70 ostringstream os;
71 os << "[" << ACE_LOG_MSG->file() << "," << ACE_LOG_MSG->linenum() << "] :" << log_record.msg_data() << "\n";
72 log_record.msg_data(os.str().c_str());
73 return 0;
74 }
75
76 void setLogDirName(const ACE_TString & str)
77 {
78 logDir = str;
79 if (!logDir.empty())
80 {
81 if (logDir[logDir.length() - 1] != '/' && logDir[logDir.length() - 1] != '\\')
82 {
83 logDir += '/';
84 }
85 }
86 }
87 private:
88 ACE_TString getTimeName()
89 {
90 ACE_Date_Time nowTime(ACE_OS::gettimeofday());
91 ostringstream os;
92 os << nowTime.year() << "-" << nowTime.month() << "-" << nowTime.day();
93 return os.str().c_str();
94 }
95
96
97 ACE_OSTREAM_TYPE *errorLog;
98 ACE_TString errorLogName;
99 ACE_OSTREAM_TYPE *debugLog;
100 ACE_TString debugLogName;
101 ACE_TString timeStamp;
102 ACE_TString logDir;
103 };
104
105 int ACE_TMAIN(int argc, ACE_TCHAR* argv[])
106 {
107 LogMsgBackend lmb;
108 ACE_LOG_MSG->msg_backend(&lmb);
109 ACE_LOG_MSG->open(argv[0], ACE_Log_Msg::CUSTOM | ACE_Log_Msg::VERBOSE_LITE);
110 for(int i = 0; i != 1000; i++)
111 {
112 ACE_DEBUG((LM_DEBUG, ACE_TEXT("hello1, this should be in debug log: %d"), i));
113 ACE_DEBUG((LM_ERROR, ACE_TEXT("hello1, this should be in error log: %d"), i));
114 }
115
116 }
posted @ 2010-09-27 21:10  seacat  阅读(531)  评论(0编辑  收藏  举报