rollingpolicy.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * rollingpolicy.h
  3. *
  4. * See the COPYING file for the terms of usage and distribution.
  5. */
  6. #ifndef log4c_rollingpolicy_h
  7. #define log4c_rollingpolicy_h
  8. /**
  9. * @file rollingpolicy.h
  10. *
  11. * @brief Log4c rolling policy interface. Defines the interface for
  12. * managing and providing rolling policies.
  13. *
  14. * A rolling policy is used to confogure a rollingfile appender to tell
  15. * it when to trigger a rolover event.
  16. */
  17. #include <stdio.h>
  18. #include "log4c/defs.h"
  19. #include "log4c/layout.h"
  20. __LOG4C_BEGIN_DECLS
  21. struct __log4c_rollingpolicy;
  22. /**
  23. * log4c rollingpolicy type
  24. */
  25. typedef struct __log4c_rollingpolicy log4c_rollingpolicy_t;
  26. #define ROLLINGFILE_DEFAULT_LOG_DIR _T(".")
  27. #define ROLLINGFILE_DEFAULT_LOG_PREFIX _T("log")
  28. typedef struct __rollingfile_udata rollingfile_udata_t; /* opaque */
  29. /**
  30. * @brief log4c rollingpolicy type. Defines the interface a specific policy
  31. * must provide to the rollingfile appender.
  32. *
  33. * Attributes description:
  34. *
  35. * @li @c name rollingpolicy type name
  36. * @li @c init() init the rollingpolicy
  37. * @li @c is_triggering_event()
  38. * @li @c rollover()
  39. *
  40. **/
  41. typedef struct log4c_rollingpolicy_type {
  42. const TCHAR* name;
  43. int (*init)(log4c_rollingpolicy_t *a_this, rollingfile_udata_t* rfudatap );
  44. int (*is_triggering_event)( log4c_rollingpolicy_t* a_policy,
  45. const log4c_logging_event_t*,
  46. long current_file_size );
  47. int (*rollover)(log4c_rollingpolicy_t* a_policy, FILE **);
  48. int (*fini)(log4c_rollingpolicy_t *a_this);
  49. } log4c_rollingpolicy_type_t;
  50. /**
  51. * Get a new rolling policy
  52. * @param policy_name a name for the policy
  53. * @return a new rolling policy, otherwise NULL.
  54. */
  55. LOG4C_API log4c_rollingpolicy_t* log4c_rollingpolicy_get(const TCHAR* policy_name);
  56. /**
  57. * Use this function to register a rollingpolicy type with log4c.
  58. * Once this is done you may refer to this type by name both
  59. * programmatically and in the log4c configuration file.
  60. *
  61. * @param a_type a pointer to the new rollingpolicy type to register.
  62. * @returns a pointer to the previous rollingpolicy type of same name.
  63. *
  64. * Example code fragment:
  65. * @code
  66. *
  67. * const log4c_rollingpolicy_type_t log4c_rollingpolicy_type_sizewin = {
  68. * "sizewin",
  69. * sizewin_init,
  70. * sizewin_is_triggering_event,
  71. * sizewin_rollover
  72. * };
  73. *
  74. * log4c_rollingpolicy_type_set(&log4c_rollingpolicy_type_sizewin);
  75. * @endcode
  76. *
  77. */
  78. LOG4C_API const log4c_rollingpolicy_type_t* log4c_rollingpolicy_type_set(const log4c_rollingpolicy_type_t* a_type);
  79. /**
  80. * Configure a rolling policy with a specific policy.
  81. * @param policyp pointer to the rolling policy
  82. * @param udatap a specific policy type, for example sizewin.
  83. * @return zero if successful, non-zero otherwise.
  84. */
  85. LOG4C_API void log4c_rollingpolicy_set_udata(log4c_rollingpolicy_t* policyp, void *udatap);
  86. /**
  87. * Call the initialization code of a rolling policy.
  88. * @param policyp pointer to the rolling policy
  89. * @param app the rolling appender this policy is used with
  90. * @return zero if successful, non-zero otherwise.
  91. */
  92. LOG4C_API int log4c_rollingpolicy_init(log4c_rollingpolicy_t *policyp, rollingfile_udata_t* rfup );
  93. /**
  94. * Call the un initialization code of a rolling policy.
  95. * This will call the fini routine of the particular rollingpolicy type
  96. * to allow it to free up resources. If the call to fini in the
  97. * rollingpolicy type fails then the rollingpolicy is not uninitialized.
  98. * Try again later model...
  99. * @param policyp pointer to the rolling policy
  100. * @return zero if successful, non-zero otherwise.
  101. */
  102. LOG4C_API int log4c_rollingpolicy_fini(log4c_rollingpolicy_t *a_this);
  103. /**
  104. * Determine if a logging event should trigger a rollover according to
  105. * the given policy.
  106. * @param policyp pointer to the rolling policy
  107. * @param evtp the logging event pointer.
  108. * @param current_file_size the size of the current file being logged to.
  109. * @return non-zero if rollover required, zero otherwise.
  110. */
  111. LOG4C_API int log4c_rollingpolicy_is_triggering_event(
  112. log4c_rollingpolicy_t* policyp,
  113. const log4c_logging_event_t* evtp,
  114. long current_file_size );
  115. /**
  116. * Effect a rollover according to policyp on the given file stream.
  117. * @param policyp pointer to the rolling policy
  118. * @param fp filestream to rollover.
  119. * @return zero if successful, non-zero otherwise.
  120. * The policy can return an indication that something went wrong but
  121. * that the rollingfile appender can stull go ahead and log by returning an
  122. * error code <= ROLLINGPOLICY_ROLLOVER_ERR_CAN_LOG. Anything greater than
  123. * means that the rolling file appender will not try to log it's message.
  124. */
  125. #define ROLLINGPOLICY_ROLLOVER_ERR_CAN_LOG 0x05
  126. LOG4C_API int log4c_rollingpolicy_rollover(log4c_rollingpolicy_t* policyp, FILE ** fp);
  127. /**
  128. * sets the rolling policy type
  129. *
  130. * @param a_rollingpolicy the log4c_rollingpolicy_t object
  131. * @param a_type the new rollingpolicy type
  132. * @return the previous appender type
  133. **/
  134. LOG4C_API const log4c_rollingpolicy_type_t* log4c_rollingpolicy_set_type(
  135. log4c_rollingpolicy_t* a_rollingpolicy,
  136. const log4c_rollingpolicy_type_t* a_type);
  137. /**
  138. * Get a pointer to an existing rollingpolicy type.
  139. *
  140. * @param a_name the name of the rollingpolicy type to return.
  141. * @returns a pointer to an existing rollingpolicy type, or NULL if no
  142. * rollingpolicy type with the specified name exists.
  143. */
  144. LOG4C_API const log4c_rollingpolicy_type_t* log4c_rollingpolicy_type_get(const TCHAR* a_name);
  145. /**
  146. * Get the rolling policy configuration.
  147. * @param policyp pointer to the rolling policy
  148. * @return pointer to the rolling policy configuration.
  149. */
  150. LOG4C_API void* log4c_rollingpolicy_get_udata(const log4c_rollingpolicy_t* policyp);
  151. /**
  152. * Get the rollingfile appender associated with this policy.
  153. * @param policyp pointer to the rolling policy
  154. * @return pointer to the rolling file appender associated with this policy
  155. */
  156. LOG4C_API rollingfile_udata_t* log4c_rollingpolicy_get_rfudata(const log4c_rollingpolicy_t* policyp);
  157. LOG4C_API void* log4c_rollingpolicy_get_name(const log4c_rollingpolicy_t* a_this);
  158. LOG4C_API log4c_rollingpolicy_t* log4c_rollingpolicy_new(const TCHAR* a_name);
  159. LOG4C_API void log4c_rollingpolicy_delete(log4c_rollingpolicy_t* a_this);
  160. LOG4C_API void log4c_rollingpolicy_print(const log4c_rollingpolicy_t* a_this, FILE* a_stream);
  161. LOG4C_API int log4c_rollingpolicy_is_initialized(log4c_rollingpolicy_t* a_this);
  162. LOG4C_API void log4c_rollingpolicy_types_print(FILE *fp);
  163. /** 删除本rollingpolicy.c文件中定义的全局和静态的指针指向的内存,防止内存泄漏
  164. 如:
  165. 1.gs_types.
  166. 2.
  167. @return void.
  168. 作者:jesse 日期:2008.09.08
  169. */
  170. LOG4C_API void log4c_rollingpolicy_delete_global();
  171. /**
  172. * @internal
  173. **/
  174. struct __sd_factory;
  175. LOG4C_API struct __sd_factory* log4c_rollingpolicy_factory;
  176. __LOG4C_END_DECLS
  177. #endif