application_3.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * This is one of the log4c example programs.
  3. *
  4. * In this example we link against a shared library that has
  5. * additional formatters and appenders where the formatter is
  6. * augmented to expect the extra user location info that we are sending
  7. * in the void* argument the location_info struct
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include "config.h"
  12. #endif
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #ifndef _WIN32
  17. #include <sys/time.h>
  18. #else
  19. #include <time.h>
  20. #include <windows.h>
  21. #include <winsock.h>
  22. #endif
  23. #ifdef HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26. #include "log4c.h"
  27. #include "application_3.h"
  28. extern int init_examples_lib(void);
  29. extern int init_userloc_formatters(void);
  30. #ifdef _WIN32
  31. #if !defined(__MINGW32__) && !defined(__MINGW64__)
  32. int gettimeofday(struct timeval* tp, void* tzp) {
  33. DWORD t;
  34. t = timeGetTime();
  35. tp->tv_sec = t / 1000;
  36. tp->tv_usec = t % 1000;
  37. /* 0 indicates that the call succeeded. */
  38. return 0;
  39. }
  40. #endif
  41. #if !defined(HAVE_SLEEP) || !HAVE_DECL_SLEEP
  42. #define sleep(x) Sleep(x*1000)
  43. #endif
  44. #endif /* _WIN32 */
  45. int main(int argc, char** argv)
  46. {
  47. struct timeval start_time;
  48. struct timeval now_time;
  49. user_locinfo_t userloc;
  50. int looptime = 0;
  51. log4c_category_t* mycat = NULL;
  52. /* setup for the extra user location info */
  53. char hostname[256];
  54. int pid = getpid();
  55. gethostname(hostname,255);
  56. hostname[255] = '\0';
  57. userloc.hostname = hostname;
  58. userloc.pid = pid;
  59. if (argc < 2)
  60. {
  61. printf("usage: %s loop_time_in_seconds\n",argv[0]);
  62. exit (1);
  63. }
  64. if (sscanf(argv[1],"%d",&looptime) != 1)
  65. {
  66. printf("could not convert %s to number of seconds to loop\n",argv[1]);
  67. exit(1);
  68. }
  69. /*
  70. * Here, if using explicit initialization (as opposed to implicit via the
  71. * init phase of the library) it's important to initialize the custom appenders
  72. * and layouts before calling log4c_init().
  73. * This is because when log4c_init() parses the config file it looks for the
  74. * types mentioned in the file to set up the relations between categories,
  75. * appenders and layouts. If it does not find a coresponding type in the
  76. * internal hash tables, it creates one with that type name, but the function
  77. * table is not set up--so that at log time nothing happens.
  78. *
  79. */
  80. init_examples_lib();
  81. init_userloc_formatters();
  82. log4c_init();
  83. /*
  84. * Here we add our own userdefined location info, and then pick that up in our formatter
  85. */
  86. mycat = log4c_category_get("six13log.log.app.application3");
  87. gettimeofday(&start_time, NULL);
  88. gettimeofday(&now_time, NULL);
  89. while ( (now_time.tv_sec - start_time.tv_sec) < looptime)
  90. {
  91. /* LINE and FILE are bad */
  92. log4c_category_log(mycat, LOG4C_PRIORITY_DEBUG, "Debugging app 3, direct log call");
  93. /* using the new API directly */
  94. const log4c_location_info_t locinfo2 = LOG4C_LOCATION_INFO_INITIALIZER(&userloc);
  95. log4c_category_log_locinfo(mycat, &locinfo2,
  96. LOG4C_PRIORITY_DEBUG, "Debugging application number THREE with extra user location");
  97. const log4c_location_info_t locinfo3 = LOG4C_LOCATION_INFO_INITIALIZER(NULL);
  98. log4c_category_log_locinfo(mycat, &locinfo3,
  99. LOG4C_PRIORITY_ERROR,
  100. "some error from app at line %d in file %s with NULL for extra user location info",
  101. __LINE__, __FILE__);
  102. /* using the new API with the define wrapper */
  103. log4c_category_log_userinfo(mycat, &userloc, LOG4C_PRIORITY_DEBUG, "Debug app3 wrapper define");
  104. sleep(3);
  105. gettimeofday(&now_time, NULL);
  106. }
  107. /* Explicitly call the log4c cleanup routine */
  108. if ( log4c_fini()){
  109. printf("log4c_fini() failed");
  110. }
  111. return 0;
  112. }