log.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #ifndef _LIBCGROUP_LOG_H
  2. #define _LIBCGROUP_LOG_H
  3. #ifndef _LIBCGROUP_H_INSIDE
  4. #error "Only <libcgroup.h> should be included directly."
  5. #endif
  6. #ifndef SWIG
  7. #include <features.h>
  8. #endif
  9. #include <stdarg.h>
  10. __BEGIN_DECLS
  11. /**
  12. * @defgroup group_log 7. Logging
  13. * @{
  14. *
  15. * @name Logging
  16. * @{
  17. * Libcgroup allows applications to register a callback function which
  18. * libcgroup will call when it wants to log something. Each log message
  19. * has associated a log level. As described in previous chapter, most libcgroup
  20. * functions return an error code, which described root cause of the failure
  21. * and log messages might provide further details about these failures and other
  22. * notable events.
  23. *
  24. * @par
  25. * The logging callback can be set at any time, but setting the callback before
  26. * any other libcgroup function (including cgroup_init()) is highly recommended.
  27. * If no logger is set before cgroup_init() is called, default logger is
  28. * automatically set, logging CGROUP_LOG_ERROR messages to stdout.
  29. *
  30. * @par Setting log level
  31. * Some of the functions below set the log level as integer.
  32. * Application can set directly a value of enum #cgroup_log_level or use
  33. * value <tt>-1</tt> to set the log level automatically. In this case, libcgroup
  34. * inspects environment variable <tt>CGROUP_LOGLEVEL</tt> if it is set
  35. * and contains any of these values: <tt>ERROR</tt>, <tt>WARNING</tt>,
  36. * <tt>INFO</tt>, <tt>DEBUG</tt> or integer number representing value from
  37. * enum #cgroup_log_level. If <tt>CGROUP_LOGLEVEL</tt> is not set or its value
  38. * is not valid, <tt>CGROUP_LOG_ERROR</tt> is set as default log level.
  39. *
  40. * @par Example:
  41. * Following short example shows custom libcgroup logger sending all log
  42. * messages to <tt>stderr</tt>:
  43. * @code
  44. * static void my_logger(void *userdata, int level, const char *fmt, va_list ap)
  45. * {
  46. * vfprintf(stderr, fmt, ap);
  47. * }
  48. *
  49. * int main(int argc, char **argv)
  50. * {
  51. * int ret;
  52. *
  53. * cgroup_set_logger(my_logger, -1, NULL);
  54. * ret = cgroup_init();
  55. * if (ret) {
  56. * ...
  57. * }
  58. * ...
  59. * @endcode
  60. */
  61. /**
  62. * Level of importance of a log message.
  63. */
  64. enum cgroup_log_level {
  65. /**
  66. * Something serious happened and libcgroup failed to perform requested
  67. * operation.
  68. */
  69. CGROUP_LOG_ERROR = 1,
  70. /**
  71. * Something bad happened but libcgroup recovered from the error.
  72. */
  73. CGROUP_LOG_WARNING,
  74. /**
  75. * Something interesting happened and the message might be useful to the
  76. * user.
  77. */
  78. CGROUP_LOG_INFO,
  79. /**
  80. * Debugging messages useful to libcgroup developers.
  81. */
  82. CGROUP_LOG_DEBUG,
  83. };
  84. typedef void (*cgroup_logger_callback)(void *userdata, int level,
  85. const char *fmt, va_list ap);
  86. /**
  87. * Set libcgroup logging callback. All log messages with equal or lower log
  88. * level will be sent to the application's callback. There can be only
  89. * one callback logger set, the previous callback is replaced with the new one
  90. * by calling this function.
  91. * Use NULL as the logger callback to completely disable libcgroup logging.
  92. *
  93. * @param logger The callback.
  94. * @param loglevel The log level. Use value -1 to automatically discover the
  95. * level from CGROUP_LOGLEVEL environment variable.
  96. * @param userdata Application's data which will be provided back to the
  97. * callback.
  98. */
  99. extern void cgroup_set_logger(cgroup_logger_callback logger, int loglevel,
  100. void *userdata);
  101. /**
  102. * Set libcgroup logging to stdout. All messages with the given loglevel
  103. * or below will be sent to standard output. Previous logger set by
  104. * cgroup_set_logger() is replaced.
  105. *
  106. * @param loglevel The log level. Use value -1 to automatically discover the
  107. * level from CGROUP_LOGLEVEL environment variable.
  108. */
  109. extern void cgroup_set_default_logger(int loglevel);
  110. /**
  111. * Change current loglevel.
  112. * @param loglevel The log level. Use value -1 to automatically discover the
  113. * level from CGROUP_LOGLEVEL environment variable.
  114. */
  115. extern void cgroup_set_loglevel(int loglevel);
  116. /**
  117. * Libcgroup log function. This is for applications which are too lazy to set
  118. * up their own complex logging and miss-use libcgroup for that purpose.
  119. * I.e. this function should be used only by simple command-line tools.
  120. * This logging automatically benefits from CGROUP_LOGLEVEL env. variable.
  121. */
  122. extern void cgroup_log(int loglevel, const char *fmt, ...);
  123. /**
  124. * Parse levelstr string for information about desired loglevel. The levelstr
  125. * is usually a value of the CGROUP_LOGLEVEL environment variable.
  126. * @param levelstr String containing desired loglevel.
  127. */
  128. extern int cgroup_parse_log_level_str(const char *levelstr);
  129. /**
  130. * @}
  131. * @}
  132. */
  133. __END_DECLS
  134. #endif /* _LIBCGROUP_LOG_H */