|
41 | 41 | /** |
42 | 42 | * @brief Gets the current function signature for different compilers. |
43 | 43 | */ |
| 44 | +#ifdef LOGGING_DISABLE_LOCATION |
| 45 | + |
| 46 | +#define OLP_SDK_LOG_FUNCTION_SIGNATURE "" |
| 47 | +#define OLP_SDK_LOG_FILE "" |
| 48 | +#define OLP_SDK_LOG_LINE 0 |
| 49 | +#define OLP_SDK_LOG_FUNCTION "" |
| 50 | + |
| 51 | +#else // LOGGING_DISABLE_LOCATION |
| 52 | + |
44 | 53 | #if __GNUC__ >= 3 || defined(__clang__) |
45 | 54 | #define OLP_SDK_LOG_FUNCTION_SIGNATURE __PRETTY_FUNCTION__ |
46 | 55 | #elif defined(_MSC_VER) |
|
49 | 58 | #define OLP_SDK_LOG_FUNCTION_SIGNATURE __FUNCTION__ |
50 | 59 | #endif |
51 | 60 |
|
| 61 | +#define OLP_SDK_LOG_FILE __FILE__ |
| 62 | +#define OLP_SDK_LOG_LINE __LINE__ |
| 63 | +#define OLP_SDK_LOG_FUNCTION __FUNCTION__ |
| 64 | + |
| 65 | +#endif // LOGGING_DISABLE_LOCATION |
| 66 | + |
52 | 67 | /** |
53 | 68 | * @brief Logs a message using C++ style streams. |
54 | 69 | * |
|
63 | 78 | do { \ |
64 | 79 | std::ostringstream __strm; \ |
65 | 80 | __strm << message; \ |
66 | | - ::olp::logging::Log::logMessage(level, tag, __strm.str(), __FILE__, \ |
67 | | - __LINE__, __FUNCTION__, \ |
| 81 | + ::olp::logging::Log::logMessage(level, tag, __strm.str(), \ |
| 82 | + OLP_SDK_LOG_FILE, OLP_SDK_LOG_LINE, \ |
| 83 | + OLP_SDK_LOG_FUNCTION, \ |
68 | 84 | OLP_SDK_LOG_FUNCTION_SIGNATURE); \ |
69 | 85 | } \ |
70 | 86 | OLP_SDK_CORE_LOOP_ONCE() |
|
156 | 172 | #define OLP_SDK_DO_LOG_F(level, tag, ...) \ |
157 | 173 | do { \ |
158 | 174 | std::string __message = ::olp::logging::format(__VA_ARGS__); \ |
159 | | - ::olp::logging::Log::logMessage(level, tag, __message, __FILE__, __LINE__, \ |
160 | | - __FUNCTION__, \ |
| 175 | + ::olp::logging::Log::logMessage(level, tag, __message, OLP_SDK_LOG_FILE, \ |
| 176 | + OLP_SDK_LOG_LINE, OLP_SDK_LOG_FUNCTION, \ |
161 | 177 | OLP_SDK_LOG_FUNCTION_SIGNATURE); \ |
162 | 178 | } \ |
163 | 179 | OLP_SDK_CORE_LOOP_ONCE() |
|
392 | 408 | * @brief A namespace for the logging library. |
393 | 409 | */ |
394 | 410 | namespace olp { |
395 | | -namespace logging { |
396 | | -class Configuration; |
397 | | -class FilterGroup; |
398 | | - |
399 | | -/** |
400 | | - * @brief Used for disabled logs at compile time. |
401 | | - */ |
402 | | -class NullLogStream { |
403 | | - public: |
404 | | - template <typename T> |
405 | | - |
406 | | - /** |
407 | | - * @brief The stream operator to print or serialize the given log stream. |
408 | | - */ |
409 | | - NullLogStream& operator<<(const T&) { |
410 | | - return *this; |
411 | | - } |
412 | | -}; |
413 | | - |
414 | | -/** |
415 | | - * @brief A primary interface for log messages. |
416 | | - */ |
417 | | -class CORE_API Log { |
418 | | - public: |
419 | | - /** |
420 | | - * @brief Configures the log system. |
421 | | - * |
422 | | - * It overrides the previous configuration. |
423 | | - * |
424 | | - * @return False if the configuration is invalid. |
425 | | - * The configuration does not change in this case. |
426 | | - */ |
427 | | - static bool configure(Configuration configuration); |
428 | | - |
429 | | - /** |
430 | | - * @brief Gets a copy of the current configuration. |
431 | | - * |
432 | | - * Use it to add an appender and reconfigure the system. |
433 | | - * |
434 | | - * @return A copy of the current configuration. |
435 | | - */ |
436 | | - static Configuration getConfiguration(); |
437 | | - |
438 | | - /** |
439 | | - * @brief Sets the default log level. |
440 | | - * |
441 | | - * No messages below this level are displayed unless overridden by |
442 | | - * specific log tags. |
443 | | - * |
444 | | - * @param level The log level. |
445 | | - */ |
446 | | - static void setLevel(Level level); |
447 | | - |
448 | | - /** |
449 | | - * @brief Gets the default log level. |
450 | | - * |
451 | | - * @return The log level. |
452 | | - */ |
453 | | - static Level getLevel(); |
454 | | - |
455 | | - /** |
456 | | - * @brief Sets the log level for a tag. |
457 | | - * |
458 | | - * It overrides the default configurations. |
459 | | - * |
460 | | - * @param level The log level. |
461 | | - * @param tag The tag for the log component. If empty, it sets the |
462 | | - * default level. |
463 | | - */ |
464 | | - static void setLevel(Level level, const std::string& tag); |
465 | | - |
466 | | - /** |
467 | | - * @brief Sets the log level for a tag. |
468 | | - * |
469 | | - * It overrides the default configurations. |
470 | | - * |
471 | | - * @param level The log level. |
472 | | - * @param tag The tag for the log component. If empty, it sets the |
473 | | - * default level. |
474 | | - */ |
475 | | - static void setLevel(const std::string& level, const std::string& tag); |
476 | | - |
477 | | - /** |
478 | | - * @brief Gets the log level for a tag. |
479 | | - * |
480 | | - * @param tag The tag for the log component. If empty, it sets the |
481 | | - * default level. |
482 | | - * |
483 | | - * @return The log level for the tag or `core::None` if the log level is unset. |
484 | | - */ |
485 | | - static boost::optional<Level> getLevel(const std::string& tag); |
486 | | - |
487 | | - /** |
488 | | - * @brief Clears the log level for a tag and sets it to |
489 | | - * the default value. |
490 | | - * |
491 | | - * @param tag The tag for the log component. |
492 | | - */ |
493 | | - static void clearLevel(const std::string& tag); |
494 | | - |
495 | | - /** |
496 | | - * @brief Clears the log levels for all tags and sets them to |
497 | | - * the default value. |
498 | | - */ |
499 | | - static void clearLevels(); |
500 | | - |
501 | | - /** |
502 | | - * @brief Applies a filter group. |
503 | | - * |
504 | | - * It clears all the log levels for tags and replaces them |
505 | | - * with the levels set in the filter group. If the default log level is set in |
506 | | - * the filter group, it is also applied. |
507 | | - * |
508 | | - * @param filters The filter group to apply. |
509 | | - */ |
510 | | - static void applyFilterGroup(const FilterGroup& filters); |
511 | | - |
512 | | - /** |
513 | | - * @brief Checks whether a level is enabled by default. |
514 | | - * |
515 | | - * @param level The log level. |
516 | | - * |
517 | | - * @return True if the log is enabled; false otherwise. |
518 | | - */ |
519 | | - static bool isEnabled(Level level); |
520 | | - |
521 | | - /** |
522 | | - * @brief Checks whether a log tag is enabled for a level. |
523 | | - * |
524 | | - * @param tag The tag for the log component. |
525 | | - * @param level The log level. |
526 | | - * |
527 | | - * @return True if the log is enabled; false otherwise. |
528 | | - */ |
529 | | - static bool isEnabled(Level level, const std::string& tag); |
530 | | - |
531 | | - /** |
532 | | - * @brief Logs a message to the registered appenders. |
533 | | - * |
534 | | - * Outputting to the appender depends on whether |
535 | | - * the appender is enabled for this level. |
536 | | - * |
537 | | - * @param level The log level. |
538 | | - * @param tag The tag for the log component. |
539 | | - * @param message The log message. |
540 | | - * @param file The file that generated the message. |
541 | | - * @param line The line in the file where the message was logged. |
542 | | - * @param function The function that generated the message. |
543 | | - * @param fullFunction The fully qualified function that generated the message. |
544 | | - */ |
545 | | - static void logMessage(Level level, const std::string& tag, |
546 | | - const std::string& message, const char* file, |
547 | | - unsigned int line, const char* function, |
548 | | - const char* fullFunction); |
549 | | -}; |
550 | | - |
551 | | -} // namespace logging |
552 | | -} // namespace olp |
| 411 | + namespace logging { |
| 412 | + class Configuration; |
| 413 | + class FilterGroup; |
| 414 | + |
| 415 | + /** |
| 416 | + * @brief Used for disabled logs at compile time. |
| 417 | + */ |
| 418 | + class NullLogStream { |
| 419 | + public: |
| 420 | + template<typename T> |
| 421 | + |
| 422 | + /** |
| 423 | + * @brief The stream operator to print or serialize the given log stream. |
| 424 | + */ |
| 425 | + NullLogStream &operator<<(const T &) { |
| 426 | + return *this; |
| 427 | + } |
| 428 | + }; |
| 429 | + |
| 430 | + /** |
| 431 | + * @brief A primary interface for log messages. |
| 432 | + */ |
| 433 | + class CORE_API Log { |
| 434 | + public: |
| 435 | + /** |
| 436 | + * @brief Configures the log system. |
| 437 | + * |
| 438 | + * It overrides the previous configuration. |
| 439 | + * |
| 440 | + * @return False if the configuration is invalid. |
| 441 | + * The configuration does not change in this case. |
| 442 | + */ |
| 443 | + static bool configure(Configuration configuration); |
| 444 | + |
| 445 | + /** |
| 446 | + * @brief Gets a copy of the current configuration. |
| 447 | + * |
| 448 | + * Use it to add an appender and reconfigure the system. |
| 449 | + * |
| 450 | + * @return A copy of the current configuration. |
| 451 | + */ |
| 452 | + static Configuration getConfiguration(); |
| 453 | + |
| 454 | + /** |
| 455 | + * @brief Sets the default log level. |
| 456 | + * |
| 457 | + * No messages below this level are displayed unless overridden by |
| 458 | + * specific log tags. |
| 459 | + * |
| 460 | + * @param level The log level. |
| 461 | + */ |
| 462 | + static void setLevel(Level level); |
| 463 | + |
| 464 | + /** |
| 465 | + * @brief Gets the default log level. |
| 466 | + * |
| 467 | + * @return The log level. |
| 468 | + */ |
| 469 | + static Level getLevel(); |
| 470 | + |
| 471 | + /** |
| 472 | + * @brief Sets the log level for a tag. |
| 473 | + * |
| 474 | + * It overrides the default configurations. |
| 475 | + * |
| 476 | + * @param level The log level. |
| 477 | + * @param tag The tag for the log component. If empty, it sets the |
| 478 | + * default level. |
| 479 | + */ |
| 480 | + static void setLevel(Level level, const std::string &tag); |
| 481 | + |
| 482 | + /** |
| 483 | + * @brief Sets the log level for a tag. |
| 484 | + * |
| 485 | + * It overrides the default configurations. |
| 486 | + * |
| 487 | + * @param level The log level. |
| 488 | + * @param tag The tag for the log component. If empty, it sets the |
| 489 | + * default level. |
| 490 | + */ |
| 491 | + static void setLevel(const std::string &level, const std::string &tag); |
| 492 | + |
| 493 | + /** |
| 494 | + * @brief Gets the log level for a tag. |
| 495 | + * |
| 496 | + * @param tag The tag for the log component. If empty, it sets the |
| 497 | + * default level. |
| 498 | + * |
| 499 | + * @return The log level for the tag or `core::None` if the log level is unset. |
| 500 | + */ |
| 501 | + static boost::optional<Level> getLevel(const std::string &tag); |
| 502 | + |
| 503 | + /** |
| 504 | + * @brief Clears the log level for a tag and sets it to |
| 505 | + * the default value. |
| 506 | + * |
| 507 | + * @param tag The tag for the log component. |
| 508 | + */ |
| 509 | + static void clearLevel(const std::string &tag); |
| 510 | + |
| 511 | + /** |
| 512 | + * @brief Clears the log levels for all tags and sets them to |
| 513 | + * the default value. |
| 514 | + */ |
| 515 | + static void clearLevels(); |
| 516 | + |
| 517 | + /** |
| 518 | + * @brief Applies a filter group. |
| 519 | + * |
| 520 | + * It clears all the log levels for tags and replaces them |
| 521 | + * with the levels set in the filter group. If the default log level is set in |
| 522 | + * the filter group, it is also applied. |
| 523 | + * |
| 524 | + * @param filters The filter group to apply. |
| 525 | + */ |
| 526 | + static void applyFilterGroup(const FilterGroup &filters); |
| 527 | + |
| 528 | + /** |
| 529 | + * @brief Checks whether a level is enabled by default. |
| 530 | + * |
| 531 | + * @param level The log level. |
| 532 | + * |
| 533 | + * @return True if the log is enabled; false otherwise. |
| 534 | + */ |
| 535 | + static bool isEnabled(Level level); |
| 536 | + |
| 537 | + /** |
| 538 | + * @brief Checks whether a log tag is enabled for a level. |
| 539 | + * |
| 540 | + * @param tag The tag for the log component. |
| 541 | + * @param level The log level. |
| 542 | + * |
| 543 | + * @return True if the log is enabled; false otherwise. |
| 544 | + */ |
| 545 | + static bool isEnabled(Level level, const std::string &tag); |
| 546 | + |
| 547 | + /** |
| 548 | + * @brief Logs a message to the registered appenders. |
| 549 | + * |
| 550 | + * Outputting to the appender depends on whether |
| 551 | + * the appender is enabled for this level. |
| 552 | + * |
| 553 | + * @param level The log level. |
| 554 | + * @param tag The tag for the log component. |
| 555 | + * @param message The log message. |
| 556 | + * @param file The file that generated the message. |
| 557 | + * @param line The line in the file where the message was logged. |
| 558 | + * @param function The function that generated the message. |
| 559 | + * @param fullFunction The fully qualified function that generated the message. |
| 560 | + */ |
| 561 | + static void logMessage(Level level, const std::string &tag, |
| 562 | + const std::string &message, const char *file, |
| 563 | + unsigned int line, const char *function, |
| 564 | + const char *fullFunction); |
| 565 | + }; |
| 566 | + } // namespace logging |
| 567 | +} // namespace olp |
0 commit comments