application_2.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. This shows how easy it is to
  6. * add in more appenders and formatters to the log4c framework.
  7. *
  8. * With gcc this file is in fact exactly the same as application_1.c--
  9. * only at link time it is linked with a new library.
  10. * With other compilers using explicit initialization, this program needs
  11. * to explicitly tell the custom appender/formatter lib to initialize
  12. * itself and it's appenders and formatters.
  13. *
  14. */
  15. #ifdef HAVE_CONFIG_H
  16. #include "config.h"
  17. #endif
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #ifndef _WIN32
  22. #include <sys/time.h>
  23. #else
  24. #include <time.h>
  25. #include <windows.h>
  26. #include <winsock.h>
  27. #endif
  28. #ifdef HAVE_UNISTD_H
  29. #include <unistd.h>
  30. #endif
  31. #include "log4c.h"
  32. extern int init_examples_lib(void);
  33. #ifdef _WIN32
  34. #if !defined(__MINGW32__) && !defined(__MINGW64__)
  35. int gettimeofday(struct timeval* tp, void* tzp) {
  36. DWORD t;
  37. t = timeGetTime();
  38. tp->tv_sec = t / 1000;
  39. tp->tv_usec = t % 1000;
  40. /* 0 indicates that the call succeeded. */
  41. return 0;
  42. }
  43. #endif
  44. #if !defined(HAVE_SLEEP) || !HAVE_DECL_SLEEP
  45. #define sleep(x) Sleep(x*1000)
  46. #endif
  47. #endif /* _WIN32 */
  48. int main(int argc, char** argv)
  49. {
  50. struct timeval start_time;
  51. struct timeval now_time;
  52. int looptime = 0;
  53. log4c_category_t* mycat = NULL;
  54. if (argc < 2)
  55. {
  56. printf("usage: %s loop_time_in_seconds\n",argv[0]);
  57. exit (1);
  58. }
  59. if (sscanf(argv[1],"%d",&looptime) != 1)
  60. {
  61. printf("could not convert %s to number of seconds to loop\n",argv[1]);
  62. exit(1);
  63. }
  64. /*
  65. * Here, if using explicit initialization (as opposed to implicit via the
  66. * init phase of the library) it's important to initialize the custom appenders
  67. * and layouts before calling log4c_init().
  68. * This is because when log4c_init() parses the config file it looks for the
  69. * types mentioned in the file to set up the relations between categories,
  70. * appenders and layouts. If it does not find a coresponding type in the
  71. * internal hash tables, it creates one with that type name, but the function
  72. * table is not set up--so that at log time nothing happens.
  73. *
  74. */
  75. init_examples_lib();
  76. log4c_init();
  77. /*
  78. * You could choose to wrap the log4c_category_log with some macro
  79. * that then calls an accessor to get your pre-created category
  80. * mycat and then logs to it. But for now we just focus on the fact
  81. * that we are using log4crc to have this application use the new
  82. * formatters and appenders we wrote as examples
  83. */
  84. mycat = log4c_category_get("six13log.log.app.application2");
  85. gettimeofday(&start_time, NULL);
  86. gettimeofday(&now_time, NULL);
  87. while ( (now_time.tv_sec - start_time.tv_sec) < looptime)
  88. {
  89. log4c_category_log(mycat, LOG4C_PRIORITY_DEBUG, "Debugging app 2");
  90. log4c_category_log(mycat, LOG4C_PRIORITY_ERROR,
  91. "some error from app2 at line %d in file %s",
  92. __LINE__, __FILE__);
  93. sleep(3);
  94. gettimeofday(&now_time, NULL);
  95. }
  96. /* Explicitly call the log4c cleanup routine */
  97. if ( log4c_fini()){
  98. printf("log4c_fini() failed");
  99. }
  100. return 0;
  101. }