userloc_formatter.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_3 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. /* using internal log4c debug function here,
  19. * this is not belong to examples :-) */
  20. #include <sd/error.h>
  21. #include "application_3.h"
  22. /**********************************************************************
  23. *
  24. * Formatted to look for extended user location info
  25. *
  26. **********************************************************************/
  27. static const char* userloc_format(
  28. const log4c_layout_t* a_layout,
  29. const log4c_logging_event_t*a_event)
  30. {
  31. static char buffer[4096];
  32. user_locinfo_t* uloc = NULL;
  33. sd_debug("Formatter s13_userloc checking location info for userdata %X",a_event->evt_loc->loc_data);
  34. if (a_event->evt_loc->loc_data != NULL)
  35. {
  36. sd_debug("Formatter s13_userloc getting a valid user location info pointer");
  37. uloc = (user_locinfo_t*) a_event->evt_loc->loc_data;
  38. sprintf(buffer, "[%s][HOST:%s][PID:%i][FILE:%s][LINE:%i][MSG:%s]",
  39. a_event->evt_category,
  40. uloc->hostname, uloc->pid, a_event->evt_loc->loc_file,
  41. a_event->evt_loc->loc_line,a_event->evt_msg);
  42. }
  43. else
  44. {
  45. sprintf(buffer, "[%s]::[FILE:%s][LINE:%i][MSG::%s]",
  46. a_event->evt_category,
  47. a_event->evt_loc->loc_file,
  48. a_event->evt_loc->loc_line,a_event->evt_msg);
  49. }
  50. return buffer;
  51. }
  52. const log4c_layout_type_t log4c_layout_type_userloc = {
  53. "s13_userloc",
  54. userloc_format,
  55. };
  56. /*****************************/
  57. /*
  58. * Here provide an init routine for this lib
  59. *
  60. ******************************/
  61. static const log4c_layout_type_t * const layout_types[] = {
  62. &log4c_layout_type_userloc,
  63. };
  64. static int nlayout_types =
  65. (int)(sizeof(layout_types) / sizeof(layout_types[0]));
  66. int init_userloc_formatters(){
  67. int rc = 0; int i = 0;
  68. for (i = 0; i < nlayout_types; i++)
  69. log4c_layout_type_set(layout_types[i]);
  70. return(rc);
  71. }