example_formatters.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /******************************************************************************
  2. *
  3. * Part of the log4c examples.
  4. *
  5. * Along with example_appenders.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 <stdlib.h>
  16. #include <string.h>
  17. #include <log4c.h>
  18. /* Defined in example_appenders.c */
  19. extern int init_example_appenders(void);
  20. /**********************************************************************
  21. *
  22. * Formatted to put [category] out at the front of the message
  23. *
  24. **********************************************************************/
  25. static const char* cat_format(
  26. const log4c_layout_t* a_layout,
  27. const log4c_logging_event_t*a_event)
  28. {
  29. static char buffer[4096];
  30. /*
  31. * For this formatter we put the category up front in the log message
  32. */
  33. sprintf(buffer, "[%s][LINE:%d][FILE:%s] %s", a_event->evt_category,
  34. a_event->evt_loc->loc_line, a_event->evt_loc->loc_file, a_event->evt_msg);
  35. return buffer;
  36. }
  37. const log4c_layout_type_t log4c_layout_type_cat = {
  38. "s13_cat",
  39. cat_format,
  40. };
  41. static const char* none_format(
  42. const log4c_layout_t* a_layout,
  43. const log4c_logging_event_t*a_event)
  44. {
  45. static char buffer[4096];
  46. return buffer;
  47. }
  48. const log4c_layout_type_t log4c_layout_type_none = {
  49. "s13_none",
  50. none_format,
  51. };
  52. /**********************************************************************/
  53. /*
  54. * Formatted to mock up an xml format.
  55. *
  56. **********************************************************************/
  57. static const char* xml_format(
  58. const log4c_layout_t* a_layout,
  59. const log4c_logging_event_t*a_event)
  60. {
  61. static char buffer[4096];
  62. /*
  63. * For this formatter we put the category up front in the log message
  64. */
  65. sprintf(buffer, "<logmessage><category>%s</category><message>%s</message></logmessage>", a_event->evt_category, a_event->evt_msg);
  66. return buffer;
  67. }
  68. const log4c_layout_type_t log4c_layout_type_xml = {
  69. "s13_xml",
  70. xml_format,
  71. };
  72. /*****************************/
  73. /*
  74. * Here provide an init routine for this lib
  75. *
  76. ******************************/
  77. static const log4c_layout_type_t * const layout_types[] = {
  78. &log4c_layout_type_xml,
  79. &log4c_layout_type_none,
  80. &log4c_layout_type_cat
  81. };
  82. static int nlayout_types =
  83. (int)(sizeof(layout_types) / sizeof(layout_types[0]));
  84. int init_example_formatters(){
  85. int rc = 0; int i = 0;
  86. for (i = 0; i < nlayout_types; i++)
  87. log4c_layout_type_set(layout_types[i]);
  88. return(rc);
  89. }
  90. int init_examples_lib() {
  91. init_example_formatters();
  92. init_example_appenders();
  93. return(0);
  94. }