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