Back to index

lightning-sunbird  0.9+nobinonly
logger.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * File:        logger.c
00040  * Description: test program for logging's basic functions
00041  */
00042 
00043 #include "prinit.h"
00044 #include "prlog.h"
00045 #include "prlock.h"
00046 #include "prcvar.h"
00047 #include "prthread.h"
00048 #include "prinrval.h"
00049 
00050 #include <stdio.h>
00051 
00052 #ifdef XP_MAC
00053 extern void SetupMacPrintfLog(char *logFile);
00054 #endif
00055 
00056 /* lth. re-define PR_LOG() */
00057 #if 0
00058 #undef PR_LOG_TEST
00059 #undef PR_LOG
00060 #define PR_LOG_TEST(_module,_level) ((_module)->level <= (_level))
00061 #define PR_LOG(_module,_level,_args)    \
00062   {                                     \
00063     if (PR_LOG_TEST(_module,_level))    \
00064        PR_LogPrint _args   ;             \
00065   }
00066 #endif
00067 
00068 
00069 static void Error(const char* msg)
00070 {
00071     printf("\t%s\n", msg);
00072 }  /* Error */
00073 
00074 static void PR_CALLBACK forked(void *arg)
00075 {
00076     PRIntn i;
00077        PRLock *ml;
00078        PRCondVar *cv;
00079        
00080     PR_LogPrint("%s logging creating mutex\n", (const char*)arg);
00081     ml = PR_NewLock();
00082     PR_LogPrint("%s logging creating condition variable\n", (const char*)arg);
00083     cv = PR_NewCondVar(ml);
00084 
00085     PR_LogPrint("%s waiting on condition timeout 10 times\n", (const char*)arg);
00086     for (i = 0; i < 10; ++i)
00087     {
00088         PR_Lock(ml);
00089         PR_WaitCondVar(cv, PR_SecondsToInterval(1));
00090         PR_Unlock(ml);
00091     }
00092     
00093     PR_LogPrint("%s logging destroying condition variable\n", (const char*)arg);
00094     PR_DestroyCondVar(cv);
00095     PR_LogPrint("%s logging destroying mutex\n", (const char*)arg);
00096     PR_DestroyLock(ml);
00097     PR_LogPrint("%s forked thread exiting\n", (const char*)arg);
00098 }
00099 
00100 static void UserLogStuff( void )
00101 {
00102     PRLogModuleInfo *myLM;
00103     PRIntn i;
00104 
00105     myLM = PR_NewLogModule( "userStuff" );
00106     if (! myLM )
00107       {
00108         printf("UserLogStuff(): can't create new log module\n" );
00109         return;
00110       }
00111 
00112     PR_LOG( myLM, PR_LOG_NOTICE, ("Log a Notice %d\n", 1 ));
00113 
00114     for (i = 0; i < 10 ; i++ )
00115       {
00116         PR_LOG( myLM, PR_LOG_DEBUG, ("Log Debug number: %d\n", i));
00117         PR_Sleep( 300 );
00118       }
00119 
00120 } /* end UserLogStuff() */
00121 
00122 int main(PRIntn argc, const char **argv)
00123 {
00124     PRThread *thread;
00125 
00126     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
00127     PR_STDIO_INIT();
00128 
00129 #ifndef XP_MAC
00130     if (argc > 1)
00131     {
00132         if (!PR_SetLogFile(argv[1]))
00133         {
00134             Error("Access: Cannot create log file");
00135             goto exit;
00136         }
00137     }
00138 #else
00139        SetupMacPrintfLog("logger.log");
00140 #endif
00141 
00142     /* Start logging something here */
00143     PR_LogPrint("%s logging into %s\n", argv[0], argv[1]);
00144 
00145     PR_LogPrint("%s creating new thread\n", argv[0]);
00146 
00147     /*
00148     ** Now change buffering.
00149     */
00150     PR_SetLogBuffering( 65500 );    
00151        thread = PR_CreateThread(
00152            PR_USER_THREAD, forked, (void*)argv[0], PR_PRIORITY_NORMAL,
00153            PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
00154     PR_LogPrint("%s joining thread\n", argv[0]);
00155 
00156     UserLogStuff();
00157 
00158     PR_JoinThread(thread);
00159 
00160     PR_LogFlush();
00161     return 0;
00162 
00163 exit:
00164     return -1;
00165 }
00166 
00167 /* logger.c */