logger.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. * File: logger.c
  7. * Description: test program for logging's basic functions
  8. */
  9. #include "prinit.h"
  10. #include "prlog.h"
  11. #include "prlock.h"
  12. #include "prcvar.h"
  13. #include "prthread.h"
  14. #include "prinrval.h"
  15. #include <stdio.h>
  16. /* lth. re-define PR_LOG() */
  17. #if 0
  18. #undef PR_LOG_TEST
  19. #undef PR_LOG
  20. #define PR_LOG_TEST(_module,_level) ((_module)->level <= (_level))
  21. #define PR_LOG(_module,_level,_args) \
  22. { \
  23. if (PR_LOG_TEST(_module,_level)) \
  24. PR_LogPrint _args ; \
  25. }
  26. #endif
  27. static void Error(const char* msg)
  28. {
  29. printf("\t%s\n", msg);
  30. } /* Error */
  31. static void PR_CALLBACK forked(void *arg)
  32. {
  33. PRIntn i;
  34. PRLock *ml;
  35. PRCondVar *cv;
  36. PR_LogPrint("%s logging creating mutex\n", (const char*)arg);
  37. ml = PR_NewLock();
  38. PR_LogPrint("%s logging creating condition variable\n", (const char*)arg);
  39. cv = PR_NewCondVar(ml);
  40. PR_LogPrint("%s waiting on condition timeout 10 times\n", (const char*)arg);
  41. for (i = 0; i < 10; ++i)
  42. {
  43. PR_Lock(ml);
  44. PR_WaitCondVar(cv, PR_SecondsToInterval(1));
  45. PR_Unlock(ml);
  46. }
  47. PR_LogPrint("%s logging destroying condition variable\n", (const char*)arg);
  48. PR_DestroyCondVar(cv);
  49. PR_LogPrint("%s logging destroying mutex\n", (const char*)arg);
  50. PR_DestroyLock(ml);
  51. PR_LogPrint("%s forked thread exiting\n", (const char*)arg);
  52. }
  53. static void UserLogStuff( void )
  54. {
  55. PRLogModuleInfo *myLM;
  56. PRIntn i;
  57. myLM = PR_NewLogModule( "userStuff" );
  58. if (! myLM )
  59. {
  60. printf("UserLogStuff(): can't create new log module\n" );
  61. return;
  62. }
  63. PR_LOG( myLM, PR_LOG_NOTICE, ("Log a Notice %d\n", 1 ));
  64. for (i = 0; i < 10 ; i++ )
  65. {
  66. PR_LOG( myLM, PR_LOG_DEBUG, ("Log Debug number: %d\n", i));
  67. PR_Sleep( 300 );
  68. }
  69. } /* end UserLogStuff() */
  70. int main(int argc, char **argv)
  71. {
  72. PRThread *thread;
  73. PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  74. PR_STDIO_INIT();
  75. if (argc > 1)
  76. {
  77. if (!PR_SetLogFile(argv[1]))
  78. {
  79. Error("Access: Cannot create log file");
  80. goto exit;
  81. }
  82. }
  83. /* Start logging something here */
  84. PR_LogPrint("%s logging into %s\n", argv[0], argv[1]);
  85. PR_LogPrint("%s creating new thread\n", argv[0]);
  86. /*
  87. ** Now change buffering.
  88. */
  89. PR_SetLogBuffering( 65500 );
  90. thread = PR_CreateThread(
  91. PR_USER_THREAD, forked, (void*)argv[0], PR_PRIORITY_NORMAL,
  92. PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
  93. PR_LogPrint("%s joining thread\n", argv[0]);
  94. UserLogStuff();
  95. PR_JoinThread(thread);
  96. PR_LogFlush();
  97. return 0;
  98. exit:
  99. return -1;
  100. }
  101. /* logger.c */