38 #include <android/log.h>
47 Logger::Logger(LogLevel level,
const std::string &tag,
int debug_verbosity)
48 : _level(level), _tag(tag), _debug_verbosity(debug_verbosity)
50 ::gettimeofday(&_logtime, NULL);
54 : _level(obj._level), _tag(obj._tag), _debug_verbosity(obj._debug_verbosity), _logtime(obj._logtime), _data(obj._data)
119 _verbosity = verbosity;
134 return _global_logmask;
142 void Logger::addStream(std::ostream &stream,
const unsigned char logmask,
const unsigned char options)
154 _global_logmask |= logmask;
155 _logger.push_back( LogWriter::LoggerOutput(stream, logmask, options) );
170 #if ( defined HAVE_SYSLOG_H || defined ANDROID )
173 ::openlog(name, option, facility);
176 _syslog_mask = logmask;
177 _global_logmask |= logmask;
185 for (std::list<LoggerOutput>::iterator iter = _logger.begin(); iter != _logger.end(); ++iter)
187 LoggerOutput &output = (*iter);
194 if (_logfile_output != NULL)
196 _logfile_output->log(logger);
200 #if ( defined HAVE_SYSLOG_H || defined ANDROID )
204 if (logger.
getLevel() & _syslog_mask)
208 if (logger.
getTag().length() > 0) {
209 log_tag = logger.
getTag();
213 log_tag = _android_tag_prefix + log_tag;
218 __android_log_print(ANDROID_LOG_FATAL, log_tag.c_str(),
"%s", logger.
str().c_str());
222 __android_log_print(ANDROID_LOG_FATAL, log_tag.c_str(),
"%s", logger.
str().c_str());
226 __android_log_print(ANDROID_LOG_FATAL, log_tag.c_str(),
"%s", logger.
str().c_str());
230 __android_log_print(ANDROID_LOG_ERROR, log_tag.c_str(),
"%s", logger.
str().c_str());
234 __android_log_print(ANDROID_LOG_WARN, log_tag.c_str(),
"%s", logger.
str().c_str());
238 __android_log_print(ANDROID_LOG_INFO, log_tag.c_str(),
"%s", logger.
str().c_str());
242 __android_log_print(ANDROID_LOG_INFO, log_tag.c_str(),
"%s", logger.
str().c_str());
246 __android_log_print(ANDROID_LOG_DEBUG, log_tag.c_str(),
"%s", logger.
str().c_str());
250 __android_log_print(ANDROID_LOG_INFO, log_tag.c_str(),
"%s", logger.
str().c_str());
257 ::syslog( LOG_EMERG,
"%s", logger.
str().c_str() );
261 ::syslog( LOG_ALERT,
"%s", logger.
str().c_str() );
265 ::syslog( LOG_CRIT,
"%s", logger.
str().c_str() );
269 ::syslog( LOG_ERR,
"%s", logger.
str().c_str() );
273 ::syslog( LOG_WARNING,
"%s", logger.
str().c_str() );
277 ::syslog( LOG_NOTICE,
"%s", logger.
str().c_str() );
281 ::syslog( LOG_INFO,
"%s", logger.
str().c_str() );
285 ::syslog( LOG_DEBUG,
"%s", logger.
str().c_str() );
289 ::syslog( LOG_NOTICE,
"%s", logger.
str().c_str() );
299 void LogWriter::LoggerOutput::log(
const Logger &log)
301 if (_level & log.getLevel())
303 std::list<std::string> prefixes;
309 time_t secs = log.getLogTime().tv_sec;
310 std::string timestamp(asctime( localtime(&secs) ));
311 timestamp.erase(std::remove(timestamp.begin(), timestamp.end(),
'\n'), timestamp.end());
312 prefixes.push_back(timestamp);
318 std::stringstream ss;
320 ss << log.getLogTime().tv_sec <<
"." << std::setw(6) << log.getLogTime().tv_usec;
321 prefixes.push_back(ss.str());
327 switch (log.getLevel())
330 prefixes.push_back(
"EMERGENCY");
334 prefixes.push_back(
"ALERT");
338 prefixes.push_back(
"CRTITICAL");
342 prefixes.push_back(
"ERROR");
346 prefixes.push_back(
"WARNING");
350 prefixes.push_back(
"NOTICE");
354 prefixes.push_back(
"INFO");
359 std::stringstream ss;
360 ss <<
"DEBUG." << log.getDebugVerbosity();
361 prefixes.push_back(ss.str());
372 std::vector<char> hostname_array(64);
373 if ( gethostname(&hostname_array[0], 64) == 0 )
375 std::string hostname(&hostname_array[0]);
376 prefixes.push_back(hostname);
382 if (log.getTag().length() > 0) {
383 prefixes.push_back(log.getTag());
390 for (std::list<std::string>::const_iterator iter = prefixes.begin(); iter != prefixes.end(); ++iter)
392 if (iter == prefixes.begin())
398 _stream <<
" " << (*iter);
402 if (!prefixes.empty())
407 _stream << log.str() << std::endl;
411 LogWriter::LoggerOutput::LoggerOutput(std::ostream &stream,
const unsigned char logmask,
const unsigned char options)
412 : _stream(stream), _level(logmask), _options(options)
416 LogWriter::LoggerOutput::~LoggerOutput()
477 return _debug_verbosity;
480 struct timeval
Logger::getLogTime() const
491 LogWriter::LogWriter()
492 : _global_logmask(0), _verbosity(0), _syslog(0), _syslog_mask(0), _queue(50), _use_queue(false), _buffer_size(0), _buffer(NULL),
493 _logfile_output(NULL), _logfile_logmask(0), _logfile_options(0), _default_tag(
"Core"), _android_tag_prefix(
"IBR-DTN/")
501 if (_use_queue)
stop();
508 if (_buffer != NULL)
delete _buffer;
515 _buffer =
new std::list<Logger>();
521 if (_logfile_output != NULL)
524 delete _logfile_output;
525 _logfile_output = NULL;
528 _logfile_stream.close();
533 if (logmask == 0)
return;
536 _logfile_logmask = logmask;
537 _logfile_options = options;
538 _global_logmask |= logmask;
541 _logfile_stream.open(_logfile.
getPath().c_str(), std::ios::out | std::ios::app);
544 if (_logfile_stream.good())
546 _logfile_output =
new LoggerOutput(_logfile_stream, _logfile_logmask, _logfile_options);
550 _logfile_stream.close();
557 if (_logfile_output != NULL)
560 delete _logfile_output;
561 _logfile_output = NULL;
564 _logfile_stream.close();
567 _logfile_stream.open(_logfile.
getPath().c_str(), std::ios::out | std::ios::app);
570 if (_logfile_stream.good())
572 _logfile_output =
new LoggerOutput(_logfile_stream, _logfile_logmask, _logfile_options);
576 _logfile_stream.close();
584 if (_buffer == NULL)
return;
586 LoggerOutput output(stream, logmask, options);
588 for (std::list<Logger>::const_iterator iter = _buffer->begin(); iter != _buffer->end(); ++iter)
590 const Logger &l = (*iter);
602 _default_tag = value;
622 Logger log = _queue.getnpop(
true);
629 _buffer->push_back(log);
630 while (_buffer->size() > _buffer_size)
632 _buffer->pop_front();
636 }
catch (
const std::exception&) {
646 try {
flush(log); }
catch (
const std::exception&) {};
650 }
catch (
const std::exception&) {