example_appenders.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /******************************************************************************
  2. *
  3. * Part of the log4c examples.
  4. *
  5. * Along with example_formatters.c this file is used to create a small
  6. * library of custom appenders and formatters.
  7. *
  8. * This library is excercised using application_2 and a sample log4crc
  9. * config file.
  10. *
  11. *****************************************************************************/
  12. #ifdef HAVE_CONFIG_H
  13. #include <config.h>
  14. #endif
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #ifdef HAVE_UNISTD_H
  18. #include <unistd.h>
  19. #endif
  20. #ifdef HAVE_SYSLOG_H
  21. #include <syslog.h>
  22. #endif
  23. #include <time.h>
  24. #include <log4c.h>
  25. /* global var to force linking with the appender when using syslog */
  26. static int log4c_to_syslog_priority(int a_priority);
  27. /*
  28. * This one is experimental and not in use. But its a good example to see all the things
  29. * you have access to during the call to the appender
  30. */
  31. static int s13_file_append(log4c_appender_t* this,
  32. const log4c_logging_event_t* a_event)
  33. {
  34. FILE* fp = log4c_appender_get_udata(this);
  35. /*
  36. return fprintf(fp, "[%s] [%s] [%d] [%d] [%s] [%s] [%d]\n%s",
  37. log4c_appender_get_name(this),
  38. a_event->evt_category,
  39. a_event->evt_priority,
  40. a_event->evt_timestamp.tv_sec*1000000 + a_event->evt_timestamp.tv_usec,
  41. a_event->evt_msg,
  42. a_event->evt_loc->loc_file,
  43. a_event->evt_loc->loc_line,
  44. a_event->evt_rendered_msg);
  45. */
  46. return fprintf(fp, "%s\n",
  47. a_event->evt_rendered_msg
  48. );
  49. }
  50. /*******************************************************************************/
  51. static int etf_open(log4c_appender_t* this)
  52. {
  53. FILE* fp = log4c_appender_get_udata(this);
  54. if (fp)
  55. return 0;
  56. if ( (fp = fopen(log4c_appender_get_name(this), "a+")) == NULL)
  57. fp = stderr;
  58. /* unbuffered mode */
  59. setbuf(fp, NULL);
  60. log4c_appender_set_udata(this, fp);
  61. return 0;
  62. }
  63. /*******************************************************************************/
  64. static int etf_close(log4c_appender_t* this)
  65. {
  66. FILE* fp = log4c_appender_get_udata(this);
  67. return (fp ? fclose(fp) : 0);
  68. }
  69. const log4c_appender_type_t log4c_appender_type_s13_file = {
  70. "s13_file",
  71. etf_open,
  72. s13_file_append,
  73. etf_close,
  74. };
  75. /*******************************************************************************/
  76. /*
  77. * The log4c stderr adds a "[stderr] " in front of the message
  78. * it is logging. this one doesn't, and leaves the formatting
  79. * descisions up to the formatter
  80. */
  81. static int s13_stderr_append(log4c_appender_t* this,
  82. const log4c_logging_event_t* a_event)
  83. {
  84. return fprintf(stderr, "%s\n",
  85. a_event->evt_rendered_msg
  86. );
  87. }
  88. static int s13_stderr_open(log4c_appender_t* this)
  89. {
  90. /* fprintf (stderr,"running s13_stderr appender open command now\n"); */
  91. return 0;
  92. }
  93. const log4c_appender_type_t log4c_appender_type_s13_stderr = {
  94. "s13_stderr",
  95. s13_stderr_open,
  96. s13_stderr_append,
  97. NULL,
  98. };
  99. #ifdef HAVE_SYSLOG_H
  100. /**************************/
  101. /* User appender */
  102. /**************************/
  103. /*******************************************************************************/
  104. static int syslog_user_open(log4c_appender_t* this)
  105. {
  106. openlog(log4c_appender_get_name(this), LOG_PID, LOG_USER);
  107. return 0;
  108. }
  109. /*******************************************************************************/
  110. static int syslog_user_append(log4c_appender_t* this,
  111. const log4c_logging_event_t* a_event)
  112. {
  113. syslog(log4c_to_syslog_priority(a_event->evt_priority) | LOG_USER,
  114. "%s", a_event->evt_rendered_msg);
  115. return 0;
  116. }
  117. /*******************************************************************************/
  118. static int syslog_user_close(log4c_appender_t* this)
  119. {
  120. closelog();
  121. return 0;
  122. }
  123. /*******************************************************************************/
  124. const log4c_appender_type_t log4c_appender_type_syslog_user = {
  125. "syslog_user",
  126. syslog_user_open,
  127. syslog_user_append,
  128. syslog_user_close,
  129. };
  130. /**************************/
  131. /* Local 0 appender */
  132. /**************************/
  133. /*******************************************************************************/
  134. static int syslog_local0_open(log4c_appender_t* this)
  135. {
  136. openlog(log4c_appender_get_name(this), LOG_PID, LOG_LOCAL0);
  137. return 0;
  138. }
  139. /*******************************************************************************/
  140. static int syslog_local0_append(log4c_appender_t* this,
  141. const log4c_logging_event_t* a_event)
  142. {
  143. syslog(log4c_to_syslog_priority(a_event->evt_priority) | LOG_LOCAL0,
  144. "%s", a_event->evt_rendered_msg);
  145. return 0;
  146. }
  147. /*******************************************************************************/
  148. static int syslog_local0_close(log4c_appender_t* this)
  149. {
  150. closelog();
  151. return 0;
  152. }
  153. /*******************************************************************************/
  154. const log4c_appender_type_t log4c_appender_type_syslog_local0 = {
  155. "syslog_local0",
  156. syslog_local0_open,
  157. syslog_local0_append,
  158. syslog_local0_close,
  159. };
  160. static int log4c_to_syslog_priority(int a_priority)
  161. {
  162. static int priorities[] = {
  163. LOG_EMERG,
  164. LOG_ALERT,
  165. LOG_CRIT,
  166. LOG_ERR,
  167. LOG_WARNING,
  168. LOG_NOTICE,
  169. LOG_INFO,
  170. LOG_DEBUG
  171. };
  172. int result;
  173. a_priority++;
  174. a_priority /= 100;
  175. if (a_priority < 0) {
  176. result = LOG_EMERG;
  177. } else if (a_priority > 7) {
  178. result = LOG_DEBUG;
  179. } else {
  180. result = priorities[a_priority];
  181. }
  182. return result;
  183. }
  184. #endif /* HAVE_SYSLOG_H */
  185. /*****************************************************/
  186. static const log4c_appender_type_t * const appender_types[] = {
  187. #ifdef HAVE_SYSLOG_H
  188. &log4c_appender_type_syslog_local0,
  189. &log4c_appender_type_syslog_user,
  190. #endif
  191. &log4c_appender_type_s13_file,
  192. &log4c_appender_type_s13_stderr
  193. };
  194. int nappender_types =
  195. (int)(sizeof(appender_types) / sizeof(appender_types[0]));
  196. int init_example_appenders(){
  197. int rc = 0; int i = 0;
  198. for (i = 0; i < nappender_types; i++)
  199. log4c_appender_type_set(appender_types[i]);
  200. return(rc);
  201. }