1 mike 1.13 //%/////////////////////////////////////////////////////////////////////////////
2 //
|
3 kumpf 1.18 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
4 // The Open Group, Tivoli Systems
|
5 mike 1.13 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 kumpf 1.18 // of this software and associated documentation files (the "Software"), to
8 // deal in the Software without restriction, including without limitation the
9 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
10 mike 1.13 // sell copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
|
13 kumpf 1.18 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
|
14 mike 1.13 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
15 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
16 kumpf 1.18 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
19 mike 1.13 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 //==============================================================================
23 //
24 // Author: Mike Brasher (mbrasher@bmc.com)
25 //
|
26 kumpf 1.16 // Modified By: Sushma Fernandes (Hewlett-Packard Company)
27 // sushma_fernandes@hp.com
|
28 mike 1.13 //
29 //%/////////////////////////////////////////////////////////////////////////////
30
31 #include <iostream>
32 #include <fstream>
33 #include "System.h"
34 #include "Logger.h"
35 #include "System.h"
|
36 kumpf 1.16 #include "Destroyer.h"
37
38 #ifdef PEGASUS_OS_HPUX
39 #include <syslog.h>
40 #endif
|
41 mike 1.13
42 PEGASUS_USING_STD;
43
44 PEGASUS_NAMESPACE_BEGIN
45
46 const Uint32 Logger::TRACE = (1 << 0);
47 const Uint32 Logger::INFORMATION = (1 << 1);
48 const Uint32 Logger::WARNING = (1 << 2);
49 const Uint32 Logger::SEVERE = (1 << 3);
50 const Uint32 Logger::FATAL = (1 << 4);
51
52 LoggerRep* Logger::_rep = 0;
53 String Logger::_homeDirectory = ".";
|
54 kumpf 1.16
55 // FUTURE-SF-P3-20020517 : This may need to be configurable. At least needs
56 // to be set to 0xFF by default always.
|
57 mike 1.13 Uint32 Logger::_severityMask = 0xFF; // Set all on by default
|
58 kumpf 1.16
|
59 mike 1.13 Uint32 Logger::_writeControlMask = 0xF; // Set all on by default
60
61 /* _allocLogFileName. Allocates the name from a name set.
62 Today this is static. However, it should be completely
63 configerable and driven from the config file so that
64 Log organization and names are open.
65 ATTN: rewrite this so that names, choice to do logs and
66 mask for level of severity are all driven from configuration
67 input.
68 */
69 static char* _allocLogFileName(
70 const String& homeDirectory,
71 Logger::LogFileType logFileType)
72 {
|
73 mike 1.15 static const char* fileNames[] =
|
74 mike 1.13 {
75 "PegasusTrace.log",
76 "PegasusStandard.log",
77 "PegasusError.log",
78 "PegasusDebug.log"
79 };
80
81 int index = int(logFileType);
82
83 if (index > Logger::NUM_LOGS)
84 index = Logger::ERROR_LOG;
85
86 const char* logFileName = fileNames[index];
87
88 String result;
89 result.reserve(homeDirectory.size() + 1 + strlen(logFileName));
90 result += homeDirectory;
91 result += '/';
92 result += logFileName;
93 return result.allocateCString();
94 }
95 mike 1.13
96 class LoggerRep
97 {
98 public:
99
100 LoggerRep(const String& homeDirectory)
101 {
|
102 kumpf 1.17 #ifndef PEGASUS_OS_HPUX
|
103 mike 1.13 // Add test for home directory set.
104
105 // If home directory does not exist, create it.
106 char* lgDir = homeDirectory.allocateCString();
107
108 if (!System::isDirectory(lgDir))
109 System::makeDirectory(lgDir);
110
|
111 karl 1.14 // KS: I put the second test in just in case some trys to create
|
112 mike 1.13 // a completly erronous directory. At least we will get a message
113 if (!System::isDirectory(lgDir))
114 cout << "Logging Disabled";
115 delete [] lgDir;
116
117
118 char* fileName = _allocLogFileName(homeDirectory, Logger::TRACE_LOG);
119 _logs[Logger::TRACE_LOG].open(fileName, ios::app);
120 delete [] fileName;
121
122 fileName = _allocLogFileName(homeDirectory, Logger::STANDARD_LOG);
123 _logs[Logger::STANDARD_LOG].open(fileName, ios::app);
124 delete [] fileName;
125
126 fileName = _allocLogFileName(homeDirectory, Logger::ERROR_LOG);
127 _logs[Logger::ERROR_LOG].open(fileName, ios::app);
128 delete [] fileName;
129
130 fileName = _allocLogFileName(homeDirectory, Logger::DEBUG_LOG);
131 _logs[Logger::DEBUG_LOG].open(fileName, ios::app);
132 delete [] fileName;
|
133 kumpf 1.17 #endif
|
134 mike 1.13
135 }
136
137 ostream& logOf(Logger::LogFileType logFileType)
138 {
139 int index = int(logFileType);
140
141 if (index > int(Logger::ERROR_LOG))
142 index = Logger::ERROR_LOG;
143
144 return _logs[index];
145 }
146
147 private:
148
149 ofstream _logs[int(Logger::NUM_LOGS)];
150 };
151
152 void Logger::put(
153 LogFileType logFileType,
154 const String& systemId,
155 mike 1.13 Uint32 severity,
156 const String& formatString,
157 const Formatter::Arg& arg0,
158 const Formatter::Arg& arg1,
159 const Formatter::Arg& arg2,
160 const Formatter::Arg& arg3,
161 const Formatter::Arg& arg4,
162 const Formatter::Arg& arg5,
163 const Formatter::Arg& arg6,
164 const Formatter::Arg& arg7,
165 const Formatter::Arg& arg8,
166 const Formatter::Arg& arg9)
167 {
168 // Test for severity against severity mask to determine
169 // if we write this log.
170 if ((_severityMask & severity) != 0)
171 {
172 if (!_rep)
173 _rep = new LoggerRep(_homeDirectory);
174
175 // Get the Severity String
176 mike 1.13 // This converts bitmap to string based on highest order
177 // bit set
178 // ATTN: KS Fix this more efficiently.
|
179 mike 1.15 static const char* svNames[] =
180 {
|
181 mike 1.13 "TRACE ",
182 "INFO ",
183 "WARNING ",
184 "SEVERE ",
185 "FATAL "
|
186 mike 1.15 };
|
187 mike 1.13 // NUM_LEVELS = 5
188 int sizeSvNames = sizeof(svNames) / sizeof(svNames[0]) - 1;
189
|
190 kumpf 1.16 String logMsg = Formatter::format(formatString,
191 arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
192 ArrayDestroyer<char> logMsgCString(logMsg.allocateCString());
193
194 #ifdef PEGASUS_OS_HPUX
195 // FUTURE-SF-P3-20020517 : Use the Syslog on HP-UX. Eventually only
196 // certain messages will go to the Syslog and others to the
197 // Pegasus Logger.
198 Uint32 syslogLevel = LOG_DEBUG;
199
200 // Map the log levels.
201 if (severity & Logger::TRACE) syslogLevel = LOG_DEBUG;
202 if (severity & Logger::INFORMATION) syslogLevel = LOG_INFO;
203 if (severity & Logger::WARNING) syslogLevel = LOG_WARNING;
204 if (severity & Logger::SEVERE) syslogLevel = LOG_ERR;
205 if (severity & Logger::FATAL) syslogLevel = LOG_CRIT;
206
207 // Open the syslog.
208 // Ignore the systemId and open the log as cimserver
209 openlog("cimserver", LOG_PID|LOG_CONS, LOG_DAEMON);
210
211 kumpf 1.16 // Log the message
212 syslog(syslogLevel, "%s", logMsgCString.getPointer());
213
214 // Close the syslog.
215 closelog();
216 #else
217 const char* tmp = "";
218 if (severity & Logger::TRACE) tmp = "TRACE ";
219 if (severity & Logger::INFORMATION) tmp = "INFO ";
220 if (severity & Logger::WARNING) tmp = "WARNING ";
221 if (severity & Logger::SEVERE) tmp = "SEVERE ";
222 if (severity & Logger::FATAL) tmp = "FATAL ";
223 _rep->logOf(logFileType) << System::getCurrentASCIITime()
224 << " " << tmp << logMsgCString.getPointer() << endl;
225 #endif
|
226 mike 1.13
227 }
228 }
229
230 void Logger::clean(const String& directory)
231 {
232 //String logFiles = logsDirectory;
233 //logFiles.append("/PegasusTrace.log");
234 //char* lgFiles = logFiles.allocateCString();
235 //cout << "Delete logs in " << logFiles << endl;
236 //System::removeFile(lgFiles);
237 //delete [] lgFiles;
238 //for (i = xx; i < yy; i++)
239 //(
240 //_allocateLogFileName(directory, i)
241 //removeFile(
242 //}
243 }
244
245 void Logger::setHomeDirectory(const String& homeDirectory)
246 {
247 mike 1.13 _homeDirectory = homeDirectory;
248 }
249
250 PEGASUS_NAMESPACE_END
|