rollingpolicy.h 7.1 KB

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