~ubuntu-branches/ubuntu/saucy/resiprocate/saucy-proposed

« back to all changes in this revision

Viewing changes to rutil/test/testLogger.cxx

  • Committer: Package Import Robot
  • Author(s): Daniel Pocock
  • Date: 2012-05-17 19:29:59 UTC
  • Revision ID: package-import@ubuntu.com-20120517192959-vv00m77isztdy64q
Tags: upstream-1.8.2
ImportĀ upstreamĀ versionĀ 1.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include "rutil/Logger.hxx"
 
3
#include "rutil/Data.hxx"
 
4
#include "rutil/ThreadIf.hxx"
 
5
#include "rutil/Timer.hxx"
 
6
 
 
7
#include "TestSubsystemLogLevel.hxx"
 
8
#include "rutil/WinLeakCheck.hxx"
 
9
 
 
10
#ifdef WIN32
 
11
#define usleep(x) Sleep(x/1000)
 
12
#define sleep(x) Sleep(x*1000)
 
13
#endif
 
14
 
 
15
using namespace resip;
 
16
using namespace std;
 
17
 
 
18
#define RESIPROCATE_SUBSYSTEM Subsystem::SIP
 
19
 
 
20
 
 
21
class LogThread : public ThreadIf
 
22
{
 
23
    public:
 
24
      LogThread(const Data& description, const Log::ThreadSetting& s,
 
25
                Log::LocalLoggerId id = 0)
 
26
         : mDescription(description),
 
27
           mSetting(s),
 
28
           mId(id)
 
29
      {}
 
30
 
 
31
      void thread()
 
32
      {
 
33
         Log::setThreadSetting(mSetting);
 
34
         int reval = Log::setThreadLocalLogger(mId);
 
35
         (void)reval;   // make gcc4 happy
 
36
//         InfoLog(<< "setThreadLocalLogger(" << mId << ") returned " << reval);
 
37
         while(!waitForShutdown(1000))
 
38
         {
 
39
            StackLog(<< mDescription << "  STACK");
 
40
            DebugLog(<< mDescription << "  DEBUG");
 
41
            InfoLog(<< mDescription << "  INFO");
 
42
         }
 
43
      }
 
44
   private:
 
45
      Data mDescription;
 
46
      Log::ThreadSetting mSetting;
 
47
      Log::LocalLoggerId mId;
 
48
};
 
49
 
 
50
int logsInCall()
 
51
{
 
52
   InfoLog(<< "Got here?");
 
53
   return 17;
 
54
}
 
55
 
 
56
 
 
57
int debugLogsInCall()
 
58
{
 
59
   DebugLog(<< "Got here?");
 
60
   return 17;
 
61
}
 
62
 
 
63
class TestExternalLogger : public ExternalLogger
 
64
{
 
65
   public:
 
66
      virtual bool operator()(Log::Level level,
 
67
                              const Subsystem& subsystem, 
 
68
                              const Data& appName,
 
69
                              const char* file,
 
70
                              int line,
 
71
                              const Data& message,
 
72
                              const Data& messageWithHeaders)
 
73
      {
 
74
         cout << "From TestExternalLogger: " << message << endl;
 
75
         return true;         
 
76
      }
 
77
};
 
78
 
 
79
void
 
80
testThreadLocalLoggers(const char *appname)
 
81
{
 
82
   Log::initialize(Log::Cout, Log::Info, appname);
 
83
 
 
84
   InfoLog(<<"Going to test thread local loggers.");
 
85
   Noisy::outputLogMessages();
 
86
 
 
87
   Log::LocalLoggerId id1 = Log::localLoggerCreate(Log::File, Log::Info, "testLogger-local1.txt");
 
88
   Log::LocalLoggerId id2 = Log::localLoggerCreate(Log::File, Log::Info, "testLogger-local2.txt");
 
89
 
 
90
   LogThread serviceErr("service with wrong local logger Id",
 
91
                        Log::ThreadSetting(1, Log::Debug), id2+10);
 
92
 
 
93
   LogThread service0("global", Log::ThreadSetting(1, Log::Debug));
 
94
   LogThread service1a("local1----A", Log::ThreadSetting(1, Log::Debug), id1);
 
95
   LogThread service1b("local1-------B", Log::ThreadSetting(1, Log::Debug), id1);
 
96
   LogThread service2a("local2---------C", Log::ThreadSetting(1, Log::Debug), id2);
 
97
   LogThread service2b("local2-----------D", Log::ThreadSetting(1, Log::Debug), id2);
 
98
 
 
99
   service0.run();
 
100
   service1a.run();
 
101
   service1b.run();
 
102
   service2a.run();
 
103
   service2b.run();
 
104
 
 
105
   sleep(3);
 
106
 
 
107
   service0.shutdown();
 
108
   service1a.shutdown();
 
109
   service1b.shutdown();
 
110
   service2a.shutdown();
 
111
   service2b.shutdown();
 
112
 
 
113
   service0.join();
 
114
   service1a.join();
 
115
   service1b.join();
 
116
   service2a.join();
 
117
   service2b.join();
 
118
 
 
119
   int retval = Log::localLoggerRemove(id1);
 
120
   if (retval > 0)
 
121
   {
 
122
      std::cerr << "Local logger 1 hasn't been cleaned up correctly! "
 
123
                   "Log::localLoggerRemove() return code is " << retval << std::endl;
 
124
   }
 
125
   retval = Log::localLoggerRemove(id2);
 
126
   if (retval > 0)
 
127
   {
 
128
      std::cerr << "Local logger 2 hasn't been cleaned up correctly! "
 
129
                   "Log::localLoggerRemove() return code is " << retval << std::endl;
 
130
   }
 
131
}
 
132
 
 
133
int
 
134
main(int argc, char* argv[])
 
135
{
 
136
#if defined(WIN32) && defined(_DEBUG) && defined(LEAK_CHECK) 
 
137
   FindMemoryLeaks fml;
 
138
#endif
 
139
 
 
140
   CritLog(<< "logging before initializing is ok");
 
141
 
 
142
   Log::initialize(Log::Cout, Log::Info, argv[0]);
 
143
 
 
144
   InfoLog(<<"Subsystem level for TEST subsystem, not level set, global is info.");
 
145
   Noisy::outputLogMessages();
 
146
   Log::setLevel(Log::Debug, Subsystem::TEST);
 
147
   InfoLog(<<"Subsystem level for TEST subsystem, TEST set to Debug, global is info.");
 
148
   Noisy::outputLogMessages();
 
149
   Log::setLevel(Log::Crit, Subsystem::TEST);
 
150
   InfoLog(<<"Subsystem level for TEST subsystem, TEST set to Crit, global is info.");
 
151
   Noisy::outputLogMessages();
 
152
 
 
153
   DebugLog(<<"This should not appear.");
 
154
   InfoLog(<<"This should appear.");
 
155
 
 
156
   LogThread service1a("service1----A", Log::ThreadSetting(1, Log::Debug));
 
157
   LogThread service1b("service1-------B", Log::ThreadSetting(1, Log::Debug));
 
158
   LogThread service1c("service1---------C", Log::ThreadSetting(1, Log::Debug));
 
159
 
 
160
   LogThread service2a("service2-----------A", Log::ThreadSetting(2, Log::Debug));
 
161
   LogThread service2b("service2------------------B", Log::ThreadSetting(2, Log::Err));
 
162
 
 
163
   service1a.run();
 
164
   service1b.run();
 
165
   service1c.run();
 
166
   service2a.run();
 
167
   service2b.run();
 
168
 
 
169
   sleep(2);
 
170
 
 
171
   InfoLog(<<"Setting service 1 to INFO\n");
 
172
   Log::setServiceLevel(1, Log::Info);
 
173
   sleep(2);
 
174
 
 
175
   InfoLog(<<"Setting service 1 to CRIT\n");
 
176
   Log::setServiceLevel(1, Log::Crit);
 
177
   sleep(2);
 
178
 
 
179
   InfoLog(<<"Setting service 2 to STACK\n");
 
180
   Log::setServiceLevel(2, Log::Stack);
 
181
   sleep(2);
 
182
 
 
183
   InfoLog(<<"Setting service 1 to DEBUG\n");
 
184
   Log::setServiceLevel(1, Log::Debug);
 
185
   sleep(2);
 
186
 
 
187
   DebugLog(<<"This should still not appear.");
 
188
   InfoLog(<<"This should still appear.");
 
189
 
 
190
   InfoLog(<<"Log will now be written to testLoggerOut.txt.");
 
191
 
 
192
   Log::initialize(Log::File, Log::Info, argv[0], "testLoggerOut.txt");  
 
193
   InfoLog(<<"This should be in the file");
 
194
   sleep(2);
 
195
   
 
196
   Log::initialize(Log::Cout, Log::Info, argv[0]);
 
197
   InfoLog(<<"This should appear-back to Cout");
 
198
   sleep(2);   
 
199
 
 
200
   TestExternalLogger tel;
 
201
   Log::initialize(Log::OnlyExternal, Log::Info, argv[0], tel);
 
202
   InfoLog(<<"This should appear-back in cout through the extenal logger, and nowhere else");
 
203
   sleep(2);   
 
204
 
 
205
   service1a.shutdown();
 
206
   service1b.shutdown();
 
207
   service1c.shutdown();
 
208
   service2a.shutdown();
 
209
   service2b.shutdown();
 
210
 
 
211
   service1a.join();
 
212
   service1b.join();
 
213
   service1c.join();
 
214
   service2a.join();
 
215
   service2b.join();
 
216
   
 
217
   Log::initialize(Log::Cout, Log::Info, argv[0]);
 
218
   InfoLog(<<"This should appear-back to Cout");
 
219
   sleep(2);   
 
220
 
 
221
   Log::setLevel(Log::Info);
 
222
 
 
223
   if (false)
 
224
   {
 
225
      UInt64 start = Timer::getTimeMs();
 
226
      for (int i = 0; i < 10000; i++)
 
227
      {
 
228
         InfoLog(<< "string");
 
229
      }
 
230
      cerr << "Info Took: " << Timer::getTimeMs() - start << endl;
 
231
   }
 
232
 
 
233
   if (false)
 
234
   {
 
235
      UInt64 start = Timer::getTimeMs();
 
236
      for (int i = 0; i < 10000; i++)
 
237
      {
 
238
         DebugLog(<< "string");
 
239
      }
 
240
      cerr << "Debug Took: " << Timer::getTimeMs() - start << endl;
 
241
   }
 
242
 
 
243
   InfoLog(<< "Recursive debug: " << debugLogsInCall());
 
244
   DebugLog(<< "Recursive non-debug OK: " << logsInCall());
 
245
 
 
246
   InfoLog(<< "Recursive non-debug OK!: " << logsInCall());
 
247
 
 
248
   cout << endl;
 
249
   testThreadLocalLoggers(argv[0]);
 
250
 
 
251
   return 0;
 
252
}
 
253
 
 
254
/* ====================================================================
 
255
 * The Vovida Software License, Version 1.0 
 
256
 * 
 
257
 * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
 
258
 * 
 
259
 * Redistribution and use in source and binary forms, with or without
 
260
 * modification, are permitted provided that the following conditions
 
261
 * are met:
 
262
 * 
 
263
 * 1. Redistributions of source code must retain the above copyright
 
264
 *    notice, this list of conditions and the following disclaimer.
 
265
 * 
 
266
 * 2. Redistributions in binary form must reproduce the above copyright
 
267
 *    notice, this list of conditions and the following disclaimer in
 
268
 *    the documentation and/or other materials provided with the
 
269
 *    distribution.
 
270
 * 
 
271
 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
 
272
 *    and "Vovida Open Communication Application Library (VOCAL)" must
 
273
 *    not be used to endorse or promote products derived from this
 
274
 *    software without prior written permission. For written
 
275
 *    permission, please contact vocal@vovida.org.
 
276
 *
 
277
 * 4. Products derived from this software may not be called "VOCAL", nor
 
278
 *    may "VOCAL" appear in their name, without prior written
 
279
 *    permission of Vovida Networks, Inc.
 
280
 * 
 
281
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
 
282
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
283
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
 
284
 * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
 
285
 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
 
286
 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
 
287
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
288
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
289
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 
290
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
291
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 
292
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 
293
 * DAMAGE.
 
294
 * 
 
295
 * ====================================================================
 
296
 * 
 
297
 * This software consists of voluntary contributions made by Vovida
 
298
 * Networks, Inc. and many individuals on behalf of Vovida Networks,
 
299
 * Inc.  For more information on Vovida Networks, Inc., please see
 
300
 * <http://www.vovida.org/>.
 
301
 *
 
302
 */
 
303