application_1.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * This is one of log4c example programs.
  3. *
  4. * Notice how no relationships between the category and a certain
  5. * priority, appender, or formatter are coded here. These are all left
  6. * to the log4crc config file so they can be chaned without recompiling
  7. *
  8. */
  9. #ifdef HAVE_CONFIG_H
  10. #include "config.h"
  11. #endif
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #ifndef _WIN32
  16. #include <sys/time.h>
  17. #else
  18. #include <time.h>
  19. #include <windows.h>
  20. #include <winsock.h>
  21. #endif
  22. #ifdef HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #endif
  25. #include "log4c.h"
  26. #ifdef _WIN32
  27. #if !defined(__MINGW32__) && !defined(__MINGW64__)
  28. int gettimeofday(struct timeval* tp, void* tzp) {
  29. DWORD t;
  30. t = timeGetTime();
  31. tp->tv_sec = t / 1000;
  32. tp->tv_usec = t % 1000;
  33. /* 0 indicates that the call succeeded. */
  34. return 0;
  35. }
  36. #endif
  37. #if !defined(HAVE_SLEEP) || !HAVE_DECL_SLEEP
  38. #define sleep(x) Sleep(x*1000)
  39. #endif
  40. #endif /* _WIN32 */
  41. int main(int argc, char** argv)
  42. {
  43. struct timeval start_time;
  44. struct timeval now_time;
  45. int looptime = 0;
  46. log4c_category_t* mycat = NULL;
  47. int i = 0;
  48. if (argc < 2)
  49. {
  50. printf("usage: %s loop_time_in_seconds\n",argv[0]);
  51. exit (1);
  52. }
  53. if (sscanf(argv[1],"%d",&looptime) != 1)
  54. {
  55. printf("could not convert %s to number of seconds to loop\n",argv[1]);
  56. exit(1);
  57. }
  58. /* You could put your category class into a file with a wrapper macro
  59. * to make calling it easier and more consistent
  60. */
  61. log4c_init();
  62. mycat = log4c_category_get("six13log.log.app.application1");
  63. gettimeofday(&start_time, NULL);
  64. gettimeofday(&now_time, NULL);
  65. i = 0;
  66. while ( (now_time.tv_sec - start_time.tv_sec) < looptime)
  67. {
  68. log4c_category_log(mycat, LOG4C_PRIORITY_DEBUG, "Debugging app 1 - loop %d", i);
  69. log4c_category_log(mycat, LOG4C_PRIORITY_ERROR,
  70. "some error from app1 at line %d in file %s - loop %d",
  71. __LINE__, __FILE__, i);
  72. sleep(3);
  73. gettimeofday(&now_time, NULL);
  74. i++;
  75. }
  76. /* Explicitly call the log4c cleanup routine */
  77. if ( log4c_fini()){
  78. printf("log4c_fini() failed");
  79. }
  80. return 0;
  81. }