24 #include <sys/types.h>
41 int fd =
open( filename.c_str(), O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR );
44 std::cerr <<
"Unable to open " << filename <<
" " <<
XrdSysE2T( errno );
45 std::cerr << std::endl;
71 std::cerr <<
"Log file not opened" << std::endl;
74 int ret =
write( pFileDes, message.c_str(), message.length() );
77 std::cerr <<
"Unable to write to the log file: " <<
XrdSysE2T( errno );
78 std::cerr << std::endl;
111 buffer =
new char[size];
112 ret = vsnprintf( buffer, size, format, cp );
117 snprintf( buffer, size,
"Error while processing a log message \"%s\" \n", format);
118 pOutput->
Write(buffer);
122 else if( ret < size )
138 gettimeofday( &ttNow, 0 );
139 localtime_r( &ttNow.tv_sec, &tsNow );
141 strftime( ts, 32,
"%Y-%m-%d %H:%M:%S", &tsNow );
142 strftime( tz, 8,
"%z", &tsNow );
143 snprintf( now, 48,
"%s.%06ld %s", ts, (
long int)ttNow.tv_usec, tz );
147 std::ostringstream out;
148 while( (line = tok.
GetLine()) )
150 out <<
"[" << now <<
"][" << LogLevelToString( level ) <<
"]";
151 out <<
"[" << TopicToString( topic ) <<
"]";
152 if(pPid) out <<
"[" << std::setw(5) << pPid <<
"]";
153 out <<
" " << line << std::endl;
156 pOutput->
Write( out.str() );
165 uint32_t len = name.length();
166 if( len > pTopicMaxLength )
168 pTopicMaxLength = len;
169 TopicMap::iterator it;
170 for( it = pTopicMap.begin(); it != pTopicMap.end(); ++it )
171 it->second.append( len-it->second.length(),
' ' );
174 name.append( pTopicMaxLength-len,
' ' );
175 pTopicMap[topic] = name;
181 std::string Log::LogLevelToString( LogLevel level )
196 return "Unknown Level";
203 bool Log::StringToLogLevel(
const std::string &strLevel, LogLevel &level )
205 if( strLevel ==
"Error" ) level =
ErrorMsg;
206 else if( strLevel ==
"Warning" ) level =
WarningMsg;
207 else if( strLevel ==
"Info" ) level =
InfoMsg;
208 else if( strLevel ==
"Debug" ) level =
DebugMsg;
209 else if( strLevel ==
"Dump" ) level =
DumpMsg;
217 std::string Log::TopicToString( uint64_t topic )
219 TopicMap::iterator it = pTopicMap.find( topic );
220 if( it != pTopicMap.end() )
222 std::ostringstream o;
223 o <<
"0x" << std::setw(pTopicMaxLength-2) << std::setfill(
'0' );
224 o << std::setbase(16) << topic;
240 va_start( argList, format );
257 va_start( argList, format );
265 void Log::Info( uint64_t topic,
const char *format, ... )
274 va_start( argList, format );
291 va_start( argList, format );
299 void Log::Dump( uint64_t topic,
const char *format, ... )
308 va_start( argList, format );
int open(const char *path, int oflag,...)
ssize_t write(int fildes, const void *buf, size_t nbyte)
const char * XrdSysE2T(int errcode)
virtual void Write(const std::string &message)
virtual void Write(const std::string &message)
void Close()
Close the log file.
bool Open(const std::string &fileName)
Open the log file.
virtual void Write(const std::string &message)=0
@ WarningMsg
report warnings
@ DebugMsg
print debug info
@ DumpMsg
print details of the request and responses
void SetTopicName(uint64_t topic, std::string name)
Map a topic number to a string.
void Error(uint64_t topic, const char *format,...)
Report an error.
LogLevel GetLevel() const
Get the log level.
void Warning(uint64_t topic, const char *format,...)
Report a warning.
void Dump(uint64_t topic, const char *format,...)
Print a dump message.
void Info(uint64_t topic, const char *format,...)
Print an info.
void Debug(uint64_t topic, const char *format,...)
Print a debug message.
void Say(LogLevel level, uint64_t topic, const char *format, va_list list)