1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998-2000
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | /*
|
---|
39 | * File: logger.c
|
---|
40 | * Description: test program for logging's basic functions
|
---|
41 | */
|
---|
42 |
|
---|
43 | #include "prinit.h"
|
---|
44 | #include "prlog.h"
|
---|
45 | #include "prlock.h"
|
---|
46 | #include "prcvar.h"
|
---|
47 | #include "prthread.h"
|
---|
48 | #include "prinrval.h"
|
---|
49 |
|
---|
50 | #include <stdio.h>
|
---|
51 |
|
---|
52 | #ifdef XP_MAC
|
---|
53 | extern void SetupMacPrintfLog(char *logFile);
|
---|
54 | #endif
|
---|
55 |
|
---|
56 | /* lth. re-define PR_LOG() */
|
---|
57 | #if 0
|
---|
58 | #undef PR_LOG_TEST
|
---|
59 | #undef PR_LOG
|
---|
60 | #define PR_LOG_TEST(_module,_level) ((_module)->level <= (_level))
|
---|
61 | #define PR_LOG(_module,_level,_args) \
|
---|
62 | { \
|
---|
63 | if (PR_LOG_TEST(_module,_level)) \
|
---|
64 | PR_LogPrint _args ; \
|
---|
65 | }
|
---|
66 | #endif
|
---|
67 |
|
---|
68 |
|
---|
69 | static void Error(const char* msg)
|
---|
70 | {
|
---|
71 | printf("\t%s\n", msg);
|
---|
72 | } /* Error */
|
---|
73 |
|
---|
74 | static void PR_CALLBACK forked(void *arg)
|
---|
75 | {
|
---|
76 | PRIntn i;
|
---|
77 | PRLock *ml;
|
---|
78 | PRCondVar *cv;
|
---|
79 |
|
---|
80 | PR_LogPrint("%s logging creating mutex\n", (const char*)arg);
|
---|
81 | ml = PR_NewLock();
|
---|
82 | PR_LogPrint("%s logging creating condition variable\n", (const char*)arg);
|
---|
83 | cv = PR_NewCondVar(ml);
|
---|
84 |
|
---|
85 | PR_LogPrint("%s waiting on condition timeout 10 times\n", (const char*)arg);
|
---|
86 | for (i = 0; i < 10; ++i)
|
---|
87 | {
|
---|
88 | PR_Lock(ml);
|
---|
89 | PR_WaitCondVar(cv, PR_SecondsToInterval(1));
|
---|
90 | PR_Unlock(ml);
|
---|
91 | }
|
---|
92 |
|
---|
93 | PR_LogPrint("%s logging destroying condition variable\n", (const char*)arg);
|
---|
94 | PR_DestroyCondVar(cv);
|
---|
95 | PR_LogPrint("%s logging destroying mutex\n", (const char*)arg);
|
---|
96 | PR_DestroyLock(ml);
|
---|
97 | PR_LogPrint("%s forked thread exiting\n", (const char*)arg);
|
---|
98 | }
|
---|
99 |
|
---|
100 | static void UserLogStuff( void )
|
---|
101 | {
|
---|
102 | PRLogModuleInfo *myLM;
|
---|
103 | PRIntn i;
|
---|
104 |
|
---|
105 | myLM = PR_NewLogModule( "userStuff" );
|
---|
106 | if (! myLM )
|
---|
107 | {
|
---|
108 | printf("UserLogStuff(): can't create new log module\n" );
|
---|
109 | return;
|
---|
110 | }
|
---|
111 |
|
---|
112 | PR_LOG( myLM, PR_LOG_NOTICE, ("Log a Notice %d\n", 1 ));
|
---|
113 |
|
---|
114 | for (i = 0; i < 10 ; i++ )
|
---|
115 | {
|
---|
116 | PR_LOG( myLM, PR_LOG_DEBUG, ("Log Debug number: %d\n", i));
|
---|
117 | PR_Sleep( 300 );
|
---|
118 | }
|
---|
119 |
|
---|
120 | } /* end UserLogStuff() */
|
---|
121 |
|
---|
122 | int main(PRIntn argc, const char **argv)
|
---|
123 | {
|
---|
124 | PRThread *thread;
|
---|
125 |
|
---|
126 | PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
|
---|
127 | PR_STDIO_INIT();
|
---|
128 |
|
---|
129 | #ifndef XP_MAC
|
---|
130 | if (argc > 1)
|
---|
131 | {
|
---|
132 | if (!PR_SetLogFile(argv[1]))
|
---|
133 | {
|
---|
134 | Error("Access: Cannot create log file");
|
---|
135 | goto exit;
|
---|
136 | }
|
---|
137 | }
|
---|
138 | #else
|
---|
139 | SetupMacPrintfLog("logger.log");
|
---|
140 | #endif
|
---|
141 |
|
---|
142 | /* Start logging something here */
|
---|
143 | PR_LogPrint("%s logging into %s\n", argv[0], argv[1]);
|
---|
144 |
|
---|
145 | PR_LogPrint("%s creating new thread\n", argv[0]);
|
---|
146 |
|
---|
147 | /*
|
---|
148 | ** Now change buffering.
|
---|
149 | */
|
---|
150 | PR_SetLogBuffering( 65500 );
|
---|
151 | thread = PR_CreateThread(
|
---|
152 | PR_USER_THREAD, forked, (void*)argv[0], PR_PRIORITY_NORMAL,
|
---|
153 | PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
|
---|
154 | PR_LogPrint("%s joining thread\n", argv[0]);
|
---|
155 |
|
---|
156 | UserLogStuff();
|
---|
157 |
|
---|
158 | PR_JoinThread(thread);
|
---|
159 |
|
---|
160 | PR_LogFlush();
|
---|
161 | return 0;
|
---|
162 |
|
---|
163 | exit:
|
---|
164 | return -1;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /* logger.c */
|
---|