home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / logger.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  2.8 KB  |  107 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*
  20.  * File:        logger.c
  21.  * Description: test program for logging's basic functions
  22.  */
  23.  
  24. #include "prinit.h"
  25. #include "prlog.h"
  26. #include "prlock.h"
  27. #include "prcvar.h"
  28. #include "prthread.h"
  29. #include "prinrval.h"
  30.  
  31. #include <stdio.h>
  32.  
  33. #ifdef XP_MAC
  34. extern void SetupMacPrintfLog(char *logFile);
  35. #endif
  36.  
  37. static void Error(const char* msg)
  38. {
  39.     printf("\t%s\n", msg);
  40. }  /* Error */
  41.  
  42. static void PR_CALLBACK forked(void *arg)
  43. {
  44.     PRIntn i;
  45.     PRLock *ml;
  46.     PRCondVar *cv;
  47.     
  48.     PR_LogPrint("%s logging creating mutex\n", (const char*)arg);
  49.     ml = PR_NewLock();
  50.     PR_LogPrint("%s logging creating condition variable\n", (const char*)arg);
  51.     cv = PR_NewCondVar(ml);
  52.  
  53.     PR_LogPrint("%s waiting on condition timeout 10 times\n", (const char*)arg);
  54.     for (i = 0; i < 10; ++i)
  55.     {
  56.         PR_Lock(ml);
  57.         PR_WaitCondVar(cv, PR_SecondsToInterval(1));
  58.         PR_Unlock(ml);
  59.     }
  60.     
  61.     PR_LogPrint("%s logging destroying condition variable\n", (const char*)arg);
  62.     PR_DestroyCondVar(cv);
  63.     PR_LogPrint("%s logging destroying mutex\n", (const char*)arg);
  64.     PR_DestroyLock(ml);
  65.     PR_LogPrint("%s forked thread exiting\n", (const char*)arg);
  66. }
  67.  
  68. int main(PRIntn argc, const char **argv)
  69. {
  70.     PRThread *thread;
  71.     
  72.     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  73.     PR_STDIO_INIT();
  74.  
  75. #ifndef XP_MAC
  76.     if (argc > 1)
  77.     {
  78.         if (!PR_SetLogFile(argv[1]))
  79.         {
  80.             Error("Access: Cannot create log file");
  81.             goto exit;
  82.         }
  83.     }
  84. #else
  85.     SetupMacPrintfLog("logger.log");
  86. #endif
  87.  
  88.     /* Start logging something here */
  89.     PR_LogPrint("%s logging into %s\n", argv[0], argv[1]);
  90.  
  91.     PR_LogPrint("%s creating new thread\n", argv[0]);
  92.     thread = PR_CreateThread(
  93.         PR_USER_THREAD, forked, (void*)argv[0], PR_PRIORITY_NORMAL,
  94.         PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
  95.     PR_LogPrint("%s joining thread\n", argv[0]);
  96.  
  97.     PR_JoinThread(thread);
  98.  
  99.     PR_LogFlush();
  100.     return 0;
  101.  
  102. exit:
  103.     return -1;
  104. }
  105.  
  106. /* logger.c */
  107.