michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * File: logger.c michael@0: * Description: test program for logging's basic functions michael@0: */ michael@0: michael@0: #include "prinit.h" michael@0: #include "prlog.h" michael@0: #include "prlock.h" michael@0: #include "prcvar.h" michael@0: #include "prthread.h" michael@0: #include "prinrval.h" michael@0: michael@0: #include michael@0: michael@0: /* lth. re-define PR_LOG() */ michael@0: #if 0 michael@0: #undef PR_LOG_TEST michael@0: #undef PR_LOG michael@0: #define PR_LOG_TEST(_module,_level) ((_module)->level <= (_level)) michael@0: #define PR_LOG(_module,_level,_args) \ michael@0: { \ michael@0: if (PR_LOG_TEST(_module,_level)) \ michael@0: PR_LogPrint _args ; \ michael@0: } michael@0: #endif michael@0: michael@0: michael@0: static void Error(const char* msg) michael@0: { michael@0: printf("\t%s\n", msg); michael@0: } /* Error */ michael@0: michael@0: static void PR_CALLBACK forked(void *arg) michael@0: { michael@0: PRIntn i; michael@0: PRLock *ml; michael@0: PRCondVar *cv; michael@0: michael@0: PR_LogPrint("%s logging creating mutex\n", (const char*)arg); michael@0: ml = PR_NewLock(); michael@0: PR_LogPrint("%s logging creating condition variable\n", (const char*)arg); michael@0: cv = PR_NewCondVar(ml); michael@0: michael@0: PR_LogPrint("%s waiting on condition timeout 10 times\n", (const char*)arg); michael@0: for (i = 0; i < 10; ++i) michael@0: { michael@0: PR_Lock(ml); michael@0: PR_WaitCondVar(cv, PR_SecondsToInterval(1)); michael@0: PR_Unlock(ml); michael@0: } michael@0: michael@0: PR_LogPrint("%s logging destroying condition variable\n", (const char*)arg); michael@0: PR_DestroyCondVar(cv); michael@0: PR_LogPrint("%s logging destroying mutex\n", (const char*)arg); michael@0: PR_DestroyLock(ml); michael@0: PR_LogPrint("%s forked thread exiting\n", (const char*)arg); michael@0: } michael@0: michael@0: static void UserLogStuff( void ) michael@0: { michael@0: PRLogModuleInfo *myLM; michael@0: PRIntn i; michael@0: michael@0: myLM = PR_NewLogModule( "userStuff" ); michael@0: if (! myLM ) michael@0: { michael@0: printf("UserLogStuff(): can't create new log module\n" ); michael@0: return; michael@0: } michael@0: michael@0: PR_LOG( myLM, PR_LOG_NOTICE, ("Log a Notice %d\n", 1 )); michael@0: michael@0: for (i = 0; i < 10 ; i++ ) michael@0: { michael@0: PR_LOG( myLM, PR_LOG_DEBUG, ("Log Debug number: %d\n", i)); michael@0: PR_Sleep( 300 ); michael@0: } michael@0: michael@0: } /* end UserLogStuff() */ michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRThread *thread; michael@0: michael@0: PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); michael@0: PR_STDIO_INIT(); michael@0: michael@0: if (argc > 1) michael@0: { michael@0: if (!PR_SetLogFile(argv[1])) michael@0: { michael@0: Error("Access: Cannot create log file"); michael@0: goto exit; michael@0: } michael@0: } michael@0: michael@0: /* Start logging something here */ michael@0: PR_LogPrint("%s logging into %s\n", argv[0], argv[1]); michael@0: michael@0: PR_LogPrint("%s creating new thread\n", argv[0]); michael@0: michael@0: /* michael@0: ** Now change buffering. michael@0: */ michael@0: PR_SetLogBuffering( 65500 ); michael@0: thread = PR_CreateThread( michael@0: PR_USER_THREAD, forked, (void*)argv[0], PR_PRIORITY_NORMAL, michael@0: PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); michael@0: PR_LogPrint("%s joining thread\n", argv[0]); michael@0: michael@0: UserLogStuff(); michael@0: michael@0: PR_JoinThread(thread); michael@0: michael@0: PR_LogFlush(); michael@0: return 0; michael@0: michael@0: exit: michael@0: return -1; michael@0: } michael@0: michael@0: /* logger.c */