Home | History | Annotate | Download | only in android
      1 /* ------------------------------------------------------------------
      2  * Copyright (C) 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 #ifndef ANDROID_LOGGER_CONFIG_H
     20 #define ANDROID_LOGGER_CONFIG_H
     21 
     22 #include "author/authordriver.h"
     23 #include "android_log_appender.h"
     24 
     25 #define MAX_STRING_LEN  255
     26 
     27 FILE *file;
     28 
     29 class PVLoggerConfigFile{
     30 /*
     31     To configure logging at runtime, a file pvlogger.txt must be located in the sdcard.
     32     The format for log level and logger tag in the file should be: "level,node".  Note that there should be no space between log level and logger tag.
     33     For example, pvlogger.txt can look like:
     34     1,PVPlayerEngine
     35     8,PVSocketNode
     36     Above example means log the message level PVLOGMSG_ALERT for PVPlayerEngine and PVLOGMSG_DEBUG for PVSocketNode.  See pvlogger.h for log level values.
     37 */
     38 public:
     39     PVLoggerConfigFile():iLogFileRead(false)
     40     {
     41         iFileServer.Connect();
     42         iLogFileName[MAX_STRING_LEN] = '\0';
     43         oscl_strncpy(iLogFileName,"/sdcard/pvlogger.txt", MAX_STRING_LEN);
     44         oscl_memset(ibuffer, 0, sizeof(ibuffer));
     45     }
     46 
     47     ~PVLoggerConfigFile()
     48     {
     49         iFileServer.Close();
     50     }
     51 
     52     bool IsLoggerConfigFilePresent()
     53     {
     54         if(-1 != ReadAndParseLoggerConfigFile())
     55             return true;
     56         return false;
     57     }
     58 
     59     //Read and parse the config file
     60     //retval = -1 if the config file doesnt exist
     61     int8 ReadAndParseLoggerConfigFile()
     62     {
     63         int8 retval = 1;
     64 
     65         if(0 != iLogFile.Open(iLogFileName,Oscl_File::MODE_READ, iFileServer))
     66         {
     67             retval = -1;
     68         }
     69         else
     70         {
     71             if(!iLogFileRead)
     72             {
     73                 int32 nCharRead = iLogFile.Read(ibuffer,1, sizeof(ibuffer));
     74                 //Parse the buffer for \n chars
     75                 Oscl_Vector<char*,OsclMemAllocator> LogConfigStrings;
     76 
     77                 //Get the logger strings
     78                 const char* const lnFd = "\n";
     79                 const int8 lnFdLen = oscl_strlen(lnFd);
     80                 int16 offset = 0;
     81                 char* lastValidBffrAddr = ibuffer + oscl_strlen(ibuffer);
     82                 const char* lnFdIndx = oscl_strstr(ibuffer, lnFd);
     83                 while(lnFdIndx!=NULL && lnFdIndx < lastValidBffrAddr)
     84                 {
     85                     oscl_memset((char*)lnFdIndx, '\0', lnFdLen);
     86                     LogConfigStrings.push_back(ibuffer + offset);
     87                     offset = (lnFdIndx + lnFdLen) - ibuffer;
     88                     lnFdIndx = oscl_strstr(ibuffer + offset, lnFd);
     89                 }
     90                 if(NULL == lnFdIndx && ((ibuffer + offset) < lastValidBffrAddr))
     91                 {
     92                     LogConfigStrings.push_back(ibuffer + offset);
     93                 }
     94 
     95                 //Populate the  LoggerConfigElements vector
     96                 {
     97                     if(!LogConfigStrings.empty())
     98                     {
     99                         Oscl_Vector<char*,OsclMemAllocator>::iterator it;
    100                         if(LogConfigStrings.size() > 0)
    101                         {
    102                             for (it = LogConfigStrings.begin(); it!= LogConfigStrings.end(); it++)
    103                             {
    104                                 char* hashIndex = OSCL_CONST_CAST(char*, oscl_strstr(*it, "#"));
    105                                 char* CommaIndex = OSCL_CONST_CAST(char*, oscl_strstr(*it, ","));
    106                                 // Skip lines with hash
    107                                 if (hashIndex == NULL)
    108                                 {
    109                                     if(CommaIndex != NULL)
    110                                     {
    111                                         *CommaIndex = '\0';
    112                                         LoggerConfigElement obj;
    113                                         uint32 logLevel;
    114                                         PV_atoi(*it,'d',oscl_strlen(*it),logLevel);
    115                                         obj.iLogLevel = logLevel;
    116                                         obj.iLoggerString= CommaIndex +1;
    117                                         iLoggerConfigElements.push_back(obj);
    118                                     }
    119                                     else
    120                                     {
    121                                         //Add the config element for logging all the modules with specified log level
    122                                         LoggerConfigElement obj;
    123                                         uint32 logLevel;
    124                                         PV_atoi(*it,'d',oscl_strlen(*it),logLevel);
    125                                         obj.iLoggerString = "";
    126                                         obj.iLogLevel = logLevel;
    127                                         iLoggerConfigElements.push_back(obj);
    128                                     }
    129                                 }
    130                             }
    131                         }
    132                     }
    133                     else
    134                     {
    135                         //Add the config element for complete logging for all the modules
    136                         LoggerConfigElement obj;
    137                         obj.iLoggerString = "";
    138                         obj.iLogLevel = 8;
    139                         iLoggerConfigElements.push_back(obj);
    140                     }
    141                 }
    142                 iLogFile.Close();
    143                 iLogFileRead = true;
    144             }
    145         }
    146         return retval;
    147     }
    148 
    149     void SetLoggerSettings()
    150     {
    151         Oscl_Vector<LoggerConfigElement, OsclMemAllocator>::iterator it;
    152 
    153         PVLoggerAppender *appender=NULL;
    154         OsclRefCounter *refCounter=NULL;
    155         if(iLoggerConfigElements.empty())
    156         {
    157             return;
    158         }
    159 
    160         appender = new AndroidLogAppender<TimeAndIdLayout,1024>();
    161         OsclRefCounterSA<LogAppenderDestructDealloc<AndroidLogAppender<TimeAndIdLayout,1024> > > *appenderRefCounter =
    162             new OsclRefCounterSA<LogAppenderDestructDealloc<AndroidLogAppender<TimeAndIdLayout,1024> > >(appender);
    163         refCounter = appenderRefCounter;
    164 
    165         OsclSharedPtr<PVLoggerAppender> appenderPtr(appender,refCounter);
    166 
    167         for (it = iLoggerConfigElements.begin(); it!= iLoggerConfigElements.end(); it++)
    168         {
    169             PVLogger *node = NULL;
    170             node = PVLogger::GetLoggerObject(it->iLoggerString);
    171             node->AddAppender(appenderPtr);
    172             node->SetLogLevel(it->iLogLevel);
    173         }
    174     }
    175 
    176 private:
    177     class LoggerConfigElement{
    178         public:
    179         LoggerConfigElement()
    180         {
    181             iLoggerString = NULL;
    182             iLogLevel = 8;
    183         }
    184         char *iLoggerString;
    185         int8 iLogLevel;
    186     };
    187     bool iLogFileRead;
    188     Oscl_File iLogFile;
    189     Oscl_FileServer iFileServer;
    190     char iLogFileName[MAX_STRING_LEN+1];
    191     char ibuffer[1024];
    192     Oscl_Vector<LoggerConfigElement, OsclMemAllocator> iLoggerConfigElements;
    193 };
    194 
    195 #endif
    196 
    197