1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
|
// Boost general library logging.hpp header file ---------------------------//
// (C) Copyright Jean-Daniel Michaud 2007. Permission to copy, use, modify,
// sell and distribute this software is granted provided this copyright notice
// appears in all copies. This software is provided "as is" without express or
// implied warranty, and with no claim as to its suitability for any purpose.
// See http://www.boost.org/LICENSE_1_0.txt for licensing.
// See http://code.google.com/p/loglite/ for library home page.
#ifndef BOOST_LOGGING_HPP
#define BOOST_LOGGING_HPP
#include <list>
#include <stack>
#include <string>
#include <ostream>
#include <sstream>
#include <stdio.h>
#include <algorithm>
#include <exception>
#include <boost/shared_ptr.hpp>
#include <boost/tuple/tuple.hpp>
#ifndef BOOST_CONFIG_HPP
# include <boost/config.hpp>
#endif
#if defined(BOOST_HAS_THREADS)
# include <boost/thread/thread.hpp>
# include <boost/thread/condition.hpp>
#endif // BOOST_HAS_THREADS
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/format.hpp>
#ifndef BOOST_NO_CODE_GENERATION_FOR_LOG
#define BOOST_LOG_INIT( format ) \
{ \
boost::logging::logger *l = boost::logging::logger::get_instance(); \
assert(l); \
l->add_format(format); \
}
#define BOOST_LOG_ADD_OUTPUT_STREAM( sink ) \
{ \
boost::logging::logger *l = boost::logging::logger::get_instance(); \
assert(l); \
l->add_sink(sink); \
}
#define BOOST_LOG(level, qualifier, _trace) \
{ \
boost::logging::logger *l = boost::logging::logger::get_instance(); \
assert(l); \
if (l->get_global_max_log_level() >= level) \
{ \
if (l->m_string_stream.str() != "") \
l->m_string_stack.push(l->m_string_stream.str()); \
\
l->m_string_stream.str(""); \
l->m_string_stream << _trace; \
l->trace(level, qualifier, l->m_string_stream.str(), __FILE__, __LINE__); \
if (!l->m_string_stack.empty()) \
{ \
l->m_string_stream.str(l->m_string_stack.top()); \
l->m_string_stack.pop(); \
} \
} \
}
#define BOOST_LOG_( level, _trace ) \
{ BOOST_LOG(level, boost::logging::log, _trace) }
#define BOOST_LOG_UNFORMATTED(level, qualifier, _trace) \
{ \
boost::logging::logger *l = boost::logging::logger::get_instance(); \
assert(l); \
if (l->get_global_max_log_level() >= level) \
{ \
if (l->m_string_stream.str() != "") \
l->m_string_stack.push(l->m_string_stream.str()); \
\
l->m_string_stream.str(""); \
l->m_string_stream << _trace; \
l->unformatted_trace(level, qualifier, \
l->m_string_stream.str(), __FILE__, __LINE__); \
if (!l->m_string_stack.empty()) \
{ \
l->m_string_stream.str(l->m_string_stack.top()); \
l->m_string_stack.pop(); \
} \
} \
}
#define BOOST_LOG_FINALIZE() \
{ \
boost::logging::logger *l = boost::logging::logger::get_instance(); \
assert(l); \
l->clear(); \
}
#else // !BOOST_NO_CODE_GENERATION_FOR_LOG
#define BOOST_LOG_INIT( format )
#define BOOST_LOG_ADD_OUTPUT_STREAM( sink )
#define BOOST_LOG(level, qualifier, _trace)
#define BOOST_LOG_( level, _trace )
#define BOOST_LOG_UNFORMATTED(level, qualifier, _trace)
#endif // BOOST_NO_CODE_GENERATION_FOR_LOG
#define BOOST_MAX_LINE_STR_SIZE 20 // log(2^64)
#define BOOST_LEVEL_UP_LIMIT 999
namespace boost {
namespace logging {
// Logging forward declarations ---------------------------------------------//
class log_element;
class level_element;
class qualifier;
class trace_element;
class format;
class sink;
class logger;
// Logging typedefs declarations --------------------------------------------//
typedef enum { LEVEL = 0, QUALIFIER, TRACE, FILENAME, LINE } param_e;
typedef enum { SINK = 0, FORMAT } sink_format_assoc_e;
typedef std::list<boost::shared_ptr<log_element> > element_list_t;
typedef std::list<boost::shared_ptr<std::ostream> > stream_list_t;
typedef unsigned short level_t;
typedef tuple<level_t,
const qualifier *,
std::string,
std::string,
unsigned int> log_param_t;
typedef std::list<format> format_list_t;
typedef tuple<sink, format&> sink_format_assoc_t;
typedef std::list<sink_format_assoc_t> sink_format_assoc_list_t;
typedef std::list<qualifier *> qualifier_list_t;
// Used for shared_ptr() on statically allocated log_element ----------------//
struct null_deleter
{ void operator()(void const *) const {} };
// Qualifier class declaration ----------------------------------------------//
class qualifier
{
public:
qualifier() {}
inline std::string to_string() const { return m_identifier; }
virtual bool operator==(const qualifier &q) { return false; }
protected:
std::string m_identifier;
};
class log_qualifier : public qualifier
{
public:
log_qualifier() { m_identifier = "log"; }
bool operator==(const qualifier &q)
{ return (dynamic_cast<const log_qualifier *>(&q) != NULL); }
};
class notice_qualifier : public qualifier
{
public:
notice_qualifier() { m_identifier = "notice"; }
bool operator==(const qualifier &q)
{ return (dynamic_cast<const notice_qualifier *>(&q) != NULL); }
};
class warning_qualifier : public qualifier
{
public:
warning_qualifier() { m_identifier = "warning"; }
bool operator==(const qualifier &q)
{ return (dynamic_cast<const warning_qualifier *>(&q) != NULL); }
};
class error_qualifier : public qualifier
{
public:
error_qualifier() { m_identifier = "error"; }
bool operator==(const qualifier &q)
{ return (dynamic_cast<const error_qualifier *>(&q) != NULL); }
};
// Element classes declaration ---------------------------------------------//
class log_element
{
public:
virtual std::string to_string() { assert(0); return ""; };
virtual std::string visit(format &f, const log_param_t &log_param);
};
class level_element : public log_element
{
public:
std::string to_string(level_t l)
{
return str(boost::format("%i") % l);
};
std::string visit(format &f, const log_param_t &log_param);
};
class filename_element : public log_element
{
public:
std::string to_string(const std::string &f) { return f; }
std::string visit(format &f, const log_param_t &log_param);
};
class line_element : public log_element
{
public:
std::string to_string(unsigned int l)
{
return str(boost::format("%i") % l);
}
std::string visit(format &f, const log_param_t &log_param);
};
class date_element : public log_element
{
public:
std::string to_string()
{
boost::gregorian::date d(boost::gregorian::day_clock::local_day());
return boost::gregorian::to_iso_extended_string(d);
}
};
class time_element : public log_element
{
public:
std::string to_string()
{
boost::posix_time::ptime
t(boost::posix_time::microsec_clock::local_time());
return boost::posix_time::to_simple_string(t);
};
};
class trace_element : public log_element
{
public:
std::string to_string(const std::string& s) { return s; };
std::string visit(format &f, const log_param_t &log_param);
};
class eol_element : public log_element
{
public:
std::string to_string() { return "\n"; };
};
class literal_element : public log_element
{
public:
explicit literal_element(const std::string &l) : m_literal(l) {}
std::string to_string() { return m_literal; };
private:
std::string m_literal;
};
class qualifier_element : public log_element
{
public:
qualifier_element(const qualifier &lq)
{
m_qualifier_identifier = lq.to_string();
}
std::string to_string() { return m_qualifier_identifier; };
private:
std::string m_qualifier_identifier;
};
// Format class declatation -------------------------------------------------//
class format
{
public:
format(log_element &e)
: m_identifier("unnamed")
{
boost::shared_ptr<boost::logging::log_element> p(&e, null_deleter());
m_element_list.push_back(p);
}
format(log_element &e, const std::string &identifier)
: m_identifier(identifier)
{
boost::shared_ptr<boost::logging::log_element> p(&e, null_deleter());
m_element_list.push_back(p);
}
format(element_list_t e)
: m_element_list(e), m_identifier("unnamed") {}
format(element_list_t e, const std::string &identifier)
: m_element_list(e), m_identifier(identifier) {}
std::string produce_trace(const log_param_t &log_param)
{
element_list_t::iterator e_it = m_element_list.begin();
std::stringstream str_stream;
for (; e_it != m_element_list.end(); ++e_it)
{
str_stream << (*e_it)->visit(*this, log_param);
}
return str_stream.str();
}
// Visitors for the log elements
std::string accept(log_element &e)
{
return e.to_string();
}
std::string accept(level_element &e, level_t l)
{
return e.to_string(l);
}
std::string accept(trace_element &e, const std::string& s)
{
return e.to_string(s);
}
std::string accept(filename_element &e, const std::string& s)
{
return e.to_string(s);
}
std::string accept(line_element &e, unsigned int l)
{
return e.to_string(l);
}
private:
element_list_t m_element_list;
std::string m_identifier;
};
// Sink class declaration ---------------------------------------------------//
class sink
{
public:
sink(std::ostream *s, level_t max_log_level = 1)
{
if (s)
if (s == &std::cout || s == &std::cerr || s == &std::clog)
m_output_stream.reset(s, null_deleter());
else
m_output_stream.reset(s);
set_max_log_level(max_log_level);
}
void set_max_log_level(level_t max_log_level)
{
m_max_log_level = ((BOOST_LEVEL_UP_LIMIT < max_log_level)
? BOOST_LEVEL_UP_LIMIT : max_log_level);
}
inline level_t get_max_log_level() const { return m_max_log_level; }
void consume_trace(format &f, const log_param_t &log_param)
{
/* make here check to avoid producing a useless trace */
if (get<LEVEL>(log_param) > m_max_log_level)
return ;
qualifier_list_t::const_iterator it = m_qualifier_list.begin();
bool qualifier_present = false;
for ( ; !qualifier_present && it != m_qualifier_list.end(); ++it)
qualifier_present = (**it == *get<QUALIFIER>(log_param));
if (!qualifier_present)
return ;
*m_output_stream << f.produce_trace(log_param);
}
void attach_qualifier(qualifier &q)
{
m_qualifier_list.push_back(&q);
}
private:
level_t m_max_log_level;
shared_ptr<std::ostream> m_output_stream;
qualifier_list_t m_qualifier_list;
};
// Element static instantiations --------------------------------------------//
static level_element level = level_element();
static filename_element filename = filename_element();
static line_element line = line_element();
static date_element date = date_element();
static time_element time = time_element();
static trace_element trace = trace_element();
static eol_element eol = eol_element();
static log_qualifier log = log_qualifier();
static notice_qualifier notice = notice_qualifier();
static warning_qualifier warning = warning_qualifier();
static error_qualifier error = error_qualifier();
// Logger class declaration ------------------------------------------------//
class logger
{
public:
logger() : m_global_max_log_level(0) {}
void clear()
{
m_format_list.clear();
m_sink_format_assoc.clear();
}
static logger *get_instance()
{
#if defined(BOOST_HAS_THREADS)
static boost::mutex m_inst_mutex;
boost::mutex::scoped_lock scoped_lock(m_inst_mutex);
#endif // BOOST_HAS_THREADS
static logger *l = NULL;
if (!l)
{
l = new logger();
static shared_ptr<logger> s_ptr_l(l);
}
return l;
}
void add_format(const format &f)
{
m_format_list.push_back(f);
}
void add_sink(const sink &s)
{
if (m_format_list.begin() == m_format_list.end())
throw "no format defined";
// Updating global_max_level used for full lazy evaluation
m_global_max_log_level =
(m_global_max_log_level < s.get_max_log_level())
?
s.get_max_log_level()
:
m_global_max_log_level;
m_sink_format_assoc.push_back
(
sink_format_assoc_t(s, *m_format_list.begin())
);
}
void add_sink(const sink &s, format &f)
{
// Updating global_max_level used for full lazy evaluation
m_global_max_log_level =
(m_global_max_log_level < s.get_max_log_level())
?
s.get_max_log_level()
:
m_global_max_log_level;
m_sink_format_assoc.push_back(sink_format_assoc_t(s, f));
}
inline level_t get_global_max_log_level()
{ return m_global_max_log_level; }
void trace(unsigned short l,
const qualifier &q,
const std::string &t,
const std::string &f,
unsigned int ln)
{
#if defined(BOOST_HAS_THREADS)
boost::mutex::scoped_lock scoped_lock(m_mutex);
#endif // BOOST_HAS_THREADS
log_param_t log_param(l, &q, t, f, ln);
sink_format_assoc_list_t::iterator
s_it = m_sink_format_assoc.begin();
for (; s_it != m_sink_format_assoc.end(); ++s_it)
{
get<SINK>(*s_it).consume_trace(get<FORMAT>(*s_it), log_param);
}
}
void unformatted_trace(unsigned short l,
const qualifier &q,
const std::string &t,
const std::string &f,
unsigned int ln);
public:
std::stringstream m_string_stream;
std::stack<std::string> m_string_stack;
private:
format_list_t m_format_list;
sink_format_assoc_list_t m_sink_format_assoc;
// The global max log level is the highest log level on all the link
// added to the logger. If no sink as a log level high enougth for
// a trace, the trace does not need to be evaluated.
level_t m_global_max_log_level;
#if defined(BOOST_HAS_THREADS)
boost::mutex m_mutex;
#endif // BOOST_HAS_THREADS
}; // logger
// Element functions definition ---------------------------------------------//
inline std::string log_element::visit(format &f,
const log_param_t &log_param)
{
return f.accept(*this);
}
inline std::string level_element::visit(format &f,
const log_param_t &log_param)
{
return f.accept(*this, get<LEVEL>(log_param));
}
inline std::string trace_element::visit(format &f,
const log_param_t &log_param)
{
return f.accept(*this, get<TRACE>(log_param));
}
inline std::string filename_element::visit(format &f,
const log_param_t &log_param)
{
return f.accept(*this, get<FILENAME>(log_param));
}
inline std::string line_element::visit(format &f,
const log_param_t &log_param)
{
return f.accept(*this, get<LINE>(log_param));
}
} // !namespace logging
} // !namespace boost
// Element global operators -------------------------------------------------//
inline boost::logging::element_list_t operator>>(
boost::logging::log_element &lhs,
boost::logging::log_element &rhs)
{
boost::logging::element_list_t l;
l.push_back(boost::shared_ptr<boost::logging::log_element>
(&lhs, boost::logging::null_deleter()));
l.push_back(boost::shared_ptr<boost::logging::log_element>
(&rhs, boost::logging::null_deleter()));
return l;
}
inline boost::logging::element_list_t operator>>(
boost::logging::element_list_t lhs,
boost::logging::log_element &rhs)
{
lhs.push_back(boost::shared_ptr<boost::logging::log_element>
(&rhs, boost::logging::null_deleter()));
return lhs;
}
inline boost::logging::element_list_t operator>>(
const std::string &s,
boost::logging::log_element &rhs)
{
boost::logging::element_list_t l;
boost::shared_ptr<boost::logging::literal_element>
p(new boost::logging::literal_element(s));
l.push_back(p);
l.push_back(boost::shared_ptr<boost::logging::log_element>
(&rhs, boost::logging::null_deleter()));
return l;
}
inline boost::logging::element_list_t operator>>(
boost::logging::element_list_t lhs,
const std::string &s)
{
boost::shared_ptr<boost::logging::literal_element>
p(new boost::logging::literal_element(s));
lhs.push_back(p);
return lhs;
}
inline
void boost::logging::logger::unformatted_trace(unsigned short l,
const qualifier &q,
const std::string &t,
const std::string &f,
unsigned int ln)
{
#if defined(BOOST_HAS_THREADS)
boost::mutex::scoped_lock scoped_lock(m_mutex);
#endif // BOOST_HAS_THREADS
log_param_t log_param(l, &q, t, f, ln);
sink_format_assoc_list_t::iterator
s_it = m_sink_format_assoc.begin();
for (; s_it != m_sink_format_assoc.end(); ++s_it)
{
boost::logging::format f(boost::logging::trace);
get<SINK>(*s_it).consume_trace(f, log_param);
}
}
#endif // !BOOST_LOGGING_HPP
|