1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/logger.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,127 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * File: logger.c 1.11 + * Description: test program for logging's basic functions 1.12 + */ 1.13 + 1.14 +#include "prinit.h" 1.15 +#include "prlog.h" 1.16 +#include "prlock.h" 1.17 +#include "prcvar.h" 1.18 +#include "prthread.h" 1.19 +#include "prinrval.h" 1.20 + 1.21 +#include <stdio.h> 1.22 + 1.23 +/* lth. re-define PR_LOG() */ 1.24 +#if 0 1.25 +#undef PR_LOG_TEST 1.26 +#undef PR_LOG 1.27 +#define PR_LOG_TEST(_module,_level) ((_module)->level <= (_level)) 1.28 +#define PR_LOG(_module,_level,_args) \ 1.29 + { \ 1.30 + if (PR_LOG_TEST(_module,_level)) \ 1.31 + PR_LogPrint _args ; \ 1.32 + } 1.33 +#endif 1.34 + 1.35 + 1.36 +static void Error(const char* msg) 1.37 +{ 1.38 + printf("\t%s\n", msg); 1.39 +} /* Error */ 1.40 + 1.41 +static void PR_CALLBACK forked(void *arg) 1.42 +{ 1.43 + PRIntn i; 1.44 + PRLock *ml; 1.45 + PRCondVar *cv; 1.46 + 1.47 + PR_LogPrint("%s logging creating mutex\n", (const char*)arg); 1.48 + ml = PR_NewLock(); 1.49 + PR_LogPrint("%s logging creating condition variable\n", (const char*)arg); 1.50 + cv = PR_NewCondVar(ml); 1.51 + 1.52 + PR_LogPrint("%s waiting on condition timeout 10 times\n", (const char*)arg); 1.53 + for (i = 0; i < 10; ++i) 1.54 + { 1.55 + PR_Lock(ml); 1.56 + PR_WaitCondVar(cv, PR_SecondsToInterval(1)); 1.57 + PR_Unlock(ml); 1.58 + } 1.59 + 1.60 + PR_LogPrint("%s logging destroying condition variable\n", (const char*)arg); 1.61 + PR_DestroyCondVar(cv); 1.62 + PR_LogPrint("%s logging destroying mutex\n", (const char*)arg); 1.63 + PR_DestroyLock(ml); 1.64 + PR_LogPrint("%s forked thread exiting\n", (const char*)arg); 1.65 +} 1.66 + 1.67 +static void UserLogStuff( void ) 1.68 +{ 1.69 + PRLogModuleInfo *myLM; 1.70 + PRIntn i; 1.71 + 1.72 + myLM = PR_NewLogModule( "userStuff" ); 1.73 + if (! myLM ) 1.74 + { 1.75 + printf("UserLogStuff(): can't create new log module\n" ); 1.76 + return; 1.77 + } 1.78 + 1.79 + PR_LOG( myLM, PR_LOG_NOTICE, ("Log a Notice %d\n", 1 )); 1.80 + 1.81 + for (i = 0; i < 10 ; i++ ) 1.82 + { 1.83 + PR_LOG( myLM, PR_LOG_DEBUG, ("Log Debug number: %d\n", i)); 1.84 + PR_Sleep( 300 ); 1.85 + } 1.86 + 1.87 +} /* end UserLogStuff() */ 1.88 + 1.89 +int main(int argc, char **argv) 1.90 +{ 1.91 + PRThread *thread; 1.92 + 1.93 + PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); 1.94 + PR_STDIO_INIT(); 1.95 + 1.96 + if (argc > 1) 1.97 + { 1.98 + if (!PR_SetLogFile(argv[1])) 1.99 + { 1.100 + Error("Access: Cannot create log file"); 1.101 + goto exit; 1.102 + } 1.103 + } 1.104 + 1.105 + /* Start logging something here */ 1.106 + PR_LogPrint("%s logging into %s\n", argv[0], argv[1]); 1.107 + 1.108 + PR_LogPrint("%s creating new thread\n", argv[0]); 1.109 + 1.110 + /* 1.111 + ** Now change buffering. 1.112 + */ 1.113 + PR_SetLogBuffering( 65500 ); 1.114 + thread = PR_CreateThread( 1.115 + PR_USER_THREAD, forked, (void*)argv[0], PR_PRIORITY_NORMAL, 1.116 + PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); 1.117 + PR_LogPrint("%s joining thread\n", argv[0]); 1.118 + 1.119 + UserLogStuff(); 1.120 + 1.121 + PR_JoinThread(thread); 1.122 + 1.123 + PR_LogFlush(); 1.124 + return 0; 1.125 + 1.126 +exit: 1.127 + return -1; 1.128 +} 1.129 + 1.130 +/* logger.c */