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