nsprpub/pr/tests/logger.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial