(file) Return to Logger.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2