• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 
19 // test_pv_author_engine_logger.cpp: implementation of the PVAELogger class.
20 //
21 //////////////////////////////////////////////////////////////////////
22 #ifndef PVLOGGER_FILE_APPENDER_H_INCLUDED
23 #include "pvlogger_file_appender.h"
24 #endif
25 #ifndef PVLOGGER_STDERR_APPENDER_H_INCLUDED
26 #include "pvlogger_stderr_appender.h"
27 #endif
28 #ifndef PVLOGGER_MEM_APPENDER_H_INCLUDED
29 #include "pvlogger_mem_appender.h"
30 #endif
31 #ifndef PVLOGGER_TIME_AND_ID_LAYOUT_H_INCLUDED
32 #include "pvlogger_time_and_id_layout.h"
33 #endif
34 #ifndef OSCL_MEM_H_INCLUDED
35 #include "oscl_mem.h"
36 #endif
37 #ifndef OSCL_STRING_CONTAINERS_H_INCLUDED
38 #include "oscl_string_containers.h"
39 #endif
40 #ifndef TEST_PV_AUTHOR_ENGINE_LOGGER_H_INCLUDED
41 #include "test_pv_author_engine_logger.h"
42 #endif
43 
44 ////////////////////////////////////////////////////////////////////////////////
45 // Macro redefinition for typing convenience : define an 8-bit char* or 16-bit wchar
46 #define _CSTR(x) _STRLIT_CHAR(x)
47 #define _W(x) _STRLIT_WCHAR(x)
48 
49 #define KLogFile ((OSCL_TCHAR*) _STRLIT("pvauthorlog.txt"))
50 
51 typedef OSCL_HeapString<OsclMemAllocator> HeapString;
52 typedef OSCL_wHeapString<OsclMemAllocator> wHeapString;
53 
54 // Open pwszConfigFile to read nodes (and levels) to log
55 // Returns true on success
ParseConfigFile(const oscl_wchar * pwszConfigFile)56 OSCL_EXPORT_REF bool PVAELogger::ParseConfigFile(const oscl_wchar *pwszConfigFile)
57 {
58     OSCL_ASSERT(pwszConfigFile);
59     if (!pwszConfigFile)
60         return false;
61 
62     int32 iStatus = 0;
63     PVLogger         *loggernode = NULL;
64 
65     bool bFileExists = false;
66     Oscl_FileServer fs;
67     Oscl_File       filehandle;
68 
69     // read configuration file pvaelogger.txt in working directory
70     fs.Connect();
71     if (filehandle.Open(pwszConfigFile, Oscl_File::MODE_READ, fs) == 0)
72         bFileExists = true;
73 
74     //If the file "pvaelogger.txt" does not exist in the working dir or
75     //is empty, set default: log "AuthorEngine", debug level to StdErr
76     if (!bFileExists || 0 == filehandle.Size())
77     {
78         if (bFileExists)
79             filehandle.Close();
80         fs.Close();
81         return false;
82     }
83 
84     int32 MAXLENGTH = filehandle.Size();
85     uint32 lastlineterminator = 0, firstline = 0;
86 
87     PVLoggerAppender *appender = NULL;
88     OsclRefCounter   *refCounter = NULL;
89     OsclSharedPtr<PVLoggerAppender> sharedAppenderPtr;
90 
91     bool bEof = false;
92     while (!bEof)
93     {
94         char *buf = (char *)OSCL_MALLOC(MAXLENGTH + 1);
95         filehandle.Seek(lastlineterminator, Oscl_File::SEEKSET);
96         if (filehandle.Read((void *)buf, 1, MAXLENGTH))
97         {
98             //delibrately remove const-ness char* output from oscl_strstr to a modifiable char* for further operation
99             char *ptr = OSCL_CONST_CAST(char*, oscl_strstr(buf, _CSTR("\n")));
100             if (ptr)
101             {
102                 if (*(ptr - 1) == '\r')
103                 {
104                     *(ptr - 1) = '\0';
105                 }
106                 else
107                 {
108                     *ptr = '\0';
109                 }
110 
111                 lastlineterminator += ptr - buf + 1;
112 
113             }
114             else
115             {
116                 bEof = true;
117             }
118 
119             // Check if this is a commented line
120             if (*buf == '#')
121             {
122                 firstline = lastlineterminator;
123                 if (buf)
124                 {
125                     OSCL_FREE(buf);
126                     buf = NULL;
127                 }
128                 continue;
129             }
130 
131             //If the first uncommented line in the file is NO_LOGGING all logging will be disabled
132             //If the first uncommented line in the file is LOGTOFILE, a file appender shall be created
133             //If the first uncommented line in the file is LOGTOMEM, a memory appender shall be created
134             //otherwise all logs shall be dumped on the cmd wnd
135 
136             if (appender == NULL &&
137                     ((lastlineterminator == 0) || (firstline == lastlineterminator - (ptr - buf + 1))))
138 
139             {
140                 if (oscl_strstr(buf, _CSTR("NO_LOGGING")))
141                 {
142                     filehandle.Close();
143                     fs.Close();
144                     if (buf)
145                     {
146                         OSCL_FREE(buf);
147                         buf = NULL;
148                     }
149                     return true;
150                 }
151 
152                 iStatus = PVAELogger::CreateTestAppender(buf, appender, refCounter, sharedAppenderPtr);
153 
154                 if (iStatus != OsclErrNone)
155                 {
156                     OSCL_ASSERT(!_CSTR("Author Engine Unittest: unable to create log appender"));
157                     sharedAppenderPtr.Unbind();
158                     if (refCounter)
159                         delete refCounter;
160                     if (appender)
161                         delete appender;
162                     if (buf)
163                     {
164                         OSCL_FREE(buf);
165                         buf = NULL;
166                     }
167                     return false;
168                 }
169             }
170 
171             //Extract the node
172             char *node = (char *)OSCL_MALLOC(MAXLENGTH + 1);
173             if (!node)
174             {
175                 if (NULL != buf)
176                 {
177                     OSCL_FREE(buf);
178                     buf = NULL;
179                 }
180                 return false;
181             }
182 
183             const char *marker = oscl_strstr(buf, _CSTR(";"));
184 
185             if (!marker)
186             {
187                 OSCL_FREE(node);
188                 if (buf)
189                 {
190                     OSCL_FREE(buf);
191                     buf = NULL;
192                 }
193                 continue;
194             }
195 
196             uint32 node_size = marker - buf;
197 
198             oscl_strncpy(node, buf, node_size);
199             *(node + node_size) = '\0';
200 
201             //Extract the log level
202             HeapString *loglevel = OSCL_NEW(HeapString, (oscl_strstr(buf, _CSTR(";")) + 1));
203             if (!loglevel)
204             {
205                 OSCL_FREE(node);
206                 if (buf)
207                 {
208                     OSCL_FREE(buf);
209                     buf = NULL;
210                 }
211                 return false;
212             }
213 
214             char *tmp = loglevel->get_str();
215             bool logallnodes = false;
216 
217             //If node = ALLNODES; enable logging for all
218             //ALLNODES should be written in the file before any other logger node
219             if (oscl_strstr(node, _CSTR("ALLNODES")))
220             {
221                 loggernode = PVLogger::GetLoggerObject("");
222                 logallnodes = true;
223             }
224             else
225             {
226                 loggernode = PVLogger::GetLoggerObject(node);
227             }
228 
229             loggernode->AddAppender(sharedAppenderPtr);
230 
231             //Set log level
232             if (oscl_strstr(tmp, _CSTR("PVLOGMSG_EMERG")))
233                 loggernode->SetLogLevel(PVLOGMSG_EMERG);
234             else if (oscl_strstr(tmp, _CSTR("PVLOGMSG_ALERT")))
235                 loggernode->SetLogLevel(PVLOGMSG_ALERT);
236             else if (oscl_strstr(tmp, _CSTR("PVLOGMSG_CRIT")))
237                 loggernode->SetLogLevel(PVLOGMSG_CRIT);
238             else if (oscl_strstr(tmp, _CSTR("PVLOGMSG_ERR")))
239                 loggernode->SetLogLevel(PVLOGMSG_ERR);
240             else if (oscl_strstr(tmp, _CSTR("PVLOGMSG_WARNING")))
241                 loggernode->SetLogLevel(PVLOGMSG_WARNING);
242             else if (oscl_strstr(tmp, _CSTR("PVLOGMSG_NOTICE")))
243                 loggernode->SetLogLevel(PVLOGMSG_NOTICE);
244             else if (oscl_strstr(tmp, _CSTR("PVLOGMSG_INFO")))
245                 loggernode->SetLogLevel(PVLOGMSG_INFO);
246             else if (oscl_strstr(tmp, _CSTR("PVLOGMSG_STACK_TRACE")))
247                 loggernode->SetLogLevel(PVLOGMSG_STACK_TRACE);
248             else if (oscl_strstr(tmp, _CSTR("PVLOGMSG_DEBUG")))
249                 loggernode->SetLogLevel(PVLOGMSG_DEBUG);
250 
251             if (logallnodes == true)
252                 bEof = true;
253 
254             if (node)
255                 OSCL_FREE(node);
256             if (loglevel)
257                 OSCL_DELETE(loglevel);
258             if (buf)
259             {
260                 OSCL_FREE(buf);
261                 buf = NULL;
262             }
263 
264         }
265         else //file read returns 0
266         {
267             //We have reached the end of the file
268             bEof = true;
269         }
270 
271         if (buf)
272         {
273             OSCL_FREE(buf);
274             buf = NULL;
275         }
276 
277     }//end of while loop
278 
279     filehandle.Close();
280     fs.Close();
281     return true;
282 }
283 
284 // Attach a StdErrAppender to "PVAuthorEngine" at level PVLOGMSG_DEBUG
285 // Returns true on success
286 
MakeStdErrAppender()287 OSCL_EXPORT_REF bool PVAELogger::MakeStdErrAppender()
288 {
289     int32 iStatus;
290     PVLoggerAppender *appender   = NULL;
291     OsclRefCounter   *refCounter = NULL;
292     PVLogger         *loggernode = NULL;
293 
294     typedef StdErrAppender<TimeAndIdLayout, 1024> typeStdErrAppender;
295     OSCL_TRY(iStatus,
296              appender = new typeStdErrAppender() ;
297              OsclRefCounterSA<LogAppenderDestructDealloc<typeStdErrAppender > > *appenderRefCounter =
298                  new OsclRefCounterSA<LogAppenderDestructDealloc<typeStdErrAppender > >(appender);
299              refCounter = appenderRefCounter;
300             );
301 
302     if (iStatus != OsclErrNone)
303     {
304         delete refCounter;
305         delete appender;
306         return false;
307     }
308 
309     OsclSharedPtr<PVLoggerAppender> appenderPtr(appender, refCounter);
310     loggernode = PVLogger::GetLoggerObject(_CSTR("PVAuthorEngine"));
311     loggernode->AddAppender(appenderPtr);
312     loggernode->SetLogLevel(PVLOGMSG_DEBUG);
313 
314     return true;
315 }
316 
CreateTestAppender(char * aBuf,PVLoggerAppender * & aAppender,OsclRefCounter * & aRefCounter,OsclSharedPtr<PVLoggerAppender> & aSharedAppenderPtr)317 OSCL_EXPORT_REF int32 PVAELogger::CreateTestAppender(char* aBuf, PVLoggerAppender*& aAppender, OsclRefCounter*& aRefCounter, OsclSharedPtr<PVLoggerAppender> &aSharedAppenderPtr)
318 {
319     int32 err;
320     if (oscl_strstr(aBuf, _CSTR("LOGTOFILE")))
321     {
322         typedef TextFileAppender<TimeAndIdLayout, 1024> typeAppender;
323 
324         OSCL_TRY(err,
325                  aAppender = (PVLoggerAppender*)typeAppender::CreateAppender(KLogFile);
326                  aRefCounter = new OsclRefCounterSA<LogAppenderDestructDealloc<typeAppender> >(aAppender);
327                  aSharedAppenderPtr.Bind(aAppender, aRefCounter);
328                 );
329 
330     }
331 
332     else if (oscl_strstr(aBuf, _CSTR("LOGTOMEM")))
333     {
334         typedef MemAppender<TimeAndIdLayout, 1024> typeAppender;
335         OSCL_TRY(err,
336                  aAppender = (PVLoggerAppender*)typeAppender::CreateAppender(KLogFile);
337                  aRefCounter = new OsclRefCounterSA<LogAppenderDestructDealloc<typeAppender> >(aAppender);
338                  aSharedAppenderPtr.Bind(aAppender, aRefCounter);
339                 );
340     }
341     else
342     {
343         typedef StdErrAppender<TimeAndIdLayout, 1024> typeAppender;
344         OSCL_TRY(err,
345                  aAppender = new typeAppender() ;
346                  aRefCounter = new OsclRefCounterSA<LogAppenderDestructDealloc<typeAppender> >(aAppender);
347                  aSharedAppenderPtr.Bind(aAppender, aRefCounter);
348                 );
349     }
350 
351     return err;
352 }
353