rollingpolicy.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 "."
  27. #define ROLLINGFILE_DEFAULT_LOG_PREFIX "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 char* 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(
  56. const char* policy_name);
  57. /**
  58. * Use this function to register a rollingpolicy type with log4c.
  59. * Once this is done you may refer to this type by name both
  60. * programmatically and in the log4c configuration file.
  61. *
  62. * @param a_type a pointer to the new rollingpolicy type to register.
  63. * @returns a pointer to the previous rollingpolicy type of same name.
  64. *
  65. * Example code fragment:
  66. * @code
  67. *
  68. * const log4c_rollingpolicy_type_t log4c_rollingpolicy_type_sizewin = {
  69. * "sizewin",
  70. * sizewin_init,
  71. * sizewin_is_triggering_event,
  72. * sizewin_rollover
  73. * };
  74. *
  75. * log4c_rollingpolicy_type_set(&log4c_rollingpolicy_type_sizewin);
  76. * @endcode
  77. *
  78. */
  79. LOG4C_API const log4c_rollingpolicy_type_t* log4c_rollingpolicy_type_set(
  80. const log4c_rollingpolicy_type_t* a_type);
  81. /**
  82. * Configure a rolling policy with a specific policy.
  83. * @param policyp pointer to the rolling policy
  84. * @param udatap a specific policy type, for example sizewin.
  85. * @return zero if successful, non-zero otherwise.
  86. */
  87. LOG4C_API void log4c_rollingpolicy_set_udata(log4c_rollingpolicy_t* policyp,
  88. void *udatap);
  89. /**
  90. * Call the initialization code of a rolling policy.
  91. * @param policyp pointer to the rolling policy
  92. * @param app the rolling appender this policy is used with
  93. * @return zero if successful, non-zero otherwise.
  94. */
  95. LOG4C_API int log4c_rollingpolicy_init(log4c_rollingpolicy_t *policyp,
  96. rollingfile_udata_t* rfup );
  97. /**
  98. * Call the un initialization code of a rolling policy.
  99. * This will call the fini routine of the particular rollingpolicy type
  100. * to allow it to free up resources. If the call to fini in the
  101. * rollingpolicy type fails then the rollingpolicy is not uninitialized.
  102. * Try again later model...
  103. * @param policyp pointer to the rolling policy
  104. * @return zero if successful, non-zero otherwise.
  105. */
  106. LOG4C_API int log4c_rollingpolicy_fini(log4c_rollingpolicy_t *a_this);
  107. /**
  108. * Determine if a logging event should trigger a rollover according to
  109. * the given policy.
  110. * @param policyp pointer to the rolling policy
  111. * @param evtp the logging event pointer.
  112. * @param current_file_size the size of the current file being logged to.
  113. * @return non-zero if rollover required, zero otherwise.
  114. */
  115. LOG4C_API int log4c_rollingpolicy_is_triggering_event(
  116. log4c_rollingpolicy_t* policyp,
  117. const log4c_logging_event_t* evtp,
  118. long current_file_size );
  119. /**
  120. * Effect a rollover according to policyp on the given file stream.
  121. * @param policyp pointer to the rolling policy
  122. * @param fp filestream to rollover.
  123. * @return zero if successful, non-zero otherwise.
  124. * The policy can return an indication that something went wrong but
  125. * that the rollingfile appender can stull go ahead and log by returning an
  126. * error code <= ROLLINGPOLICY_ROLLOVER_ERR_CAN_LOG. Anything greater than
  127. * means that the rolling file appender will not try to log it's message.
  128. */
  129. #define ROLLINGPOLICY_ROLLOVER_ERR_CAN_LOG 0x05
  130. LOG4C_API int log4c_rollingpolicy_rollover(log4c_rollingpolicy_t* policyp,
  131. FILE ** fp);
  132. /**
  133. * sets the rolling policy type
  134. *
  135. * @param a_rollingpolicy the log4c_rollingpolicy_t object
  136. * @param a_type the new rollingpolicy type
  137. * @return the previous appender type
  138. **/
  139. LOG4C_API const log4c_rollingpolicy_type_t* log4c_rollingpolicy_set_type(
  140. log4c_rollingpolicy_t* a_rollingpolicy,
  141. const log4c_rollingpolicy_type_t* a_type);
  142. /**
  143. * Get a pointer to an existing rollingpolicy type.
  144. *
  145. * @param a_name the name of the rollingpolicy type to return.
  146. * @returns a pointer to an existing rollingpolicy type, or NULL if no
  147. * rollingpolicy type with the specified name exists.
  148. */
  149. LOG4C_API const log4c_rollingpolicy_type_t* log4c_rollingpolicy_type_get(
  150. const char* a_name);
  151. /**
  152. * Get the rolling policy configuration.
  153. * @param policyp pointer to the rolling policy
  154. * @return pointer to the rolling policy configuration.
  155. */
  156. LOG4C_API void* log4c_rollingpolicy_get_udata(
  157. const log4c_rollingpolicy_t* policyp);
  158. /**
  159. * Get the rollingfile appender associated with this policy.
  160. * @param policyp pointer to the rolling policy
  161. * @return pointer to the rolling file appender associated with this policy
  162. */
  163. LOG4C_API rollingfile_udata_t* log4c_rollingpolicy_get_rfudata(
  164. const log4c_rollingpolicy_t* policyp);
  165. LOG4C_API void* log4c_rollingpolicy_get_name(const log4c_rollingpolicy_t* a_this);
  166. LOG4C_API log4c_rollingpolicy_t* log4c_rollingpolicy_new(const char* a_name);
  167. LOG4C_API void log4c_rollingpolicy_delete(log4c_rollingpolicy_t* a_this);
  168. LOG4C_API void log4c_rollingpolicy_print(const log4c_rollingpolicy_t* a_this,
  169. FILE* a_stream);
  170. LOG4C_API int log4c_rollingpolicy_is_initialized(log4c_rollingpolicy_t* a_this);
  171. LOG4C_API void log4c_rollingpolicy_types_print(FILE *fp);
  172. /** 删除本rollingpolicy.c文件中定义的全局和静态的指针指向的内存,防止内存泄漏
  173. 如:
  174. 1.gs_types.
  175. 2.
  176. @return void.
  177. 作者:jesse 日期:2008.09.08
  178. */
  179. LOG4C_API void log4c_rollingpolicy_delete_global();
  180. /**
  181. * @internal
  182. **/
  183. struct __sd_factory;
  184. LOG4C_API struct __sd_factory* log4c_rollingpolicy_factory;
  185. __LOG4C_END_DECLS
  186. #endif