Home | History | Annotate | Download | only in src
      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 #ifndef PVLOGGER_H_INCLUDED
     19 #include "pvlogger.h"
     20 #endif
     21 
     22 #ifndef PVLOGGER_STDERR_APPENDER_H_INCLUDED
     23 #include "pvlogger_stderr_appender.h"
     24 #endif
     25 
     26 #ifndef PVLOGGER_TIME_AND_ID_LAYOUT_H_INCLUDED
     27 #include "pvlogger_time_and_id_layout.h"
     28 #endif
     29 
     30 #ifndef PVLOGGER_FILE_APPENDER_H_INCLUDED
     31 #include "pvlogger_file_appender.h"
     32 #endif
     33 
     34 #ifndef PV_LOGGER_IMPL_H_INCLUDED
     35 #include "pv_logger_impl.h"
     36 #endif
     37 
     38 PVLoggerConfigFile::~PVLoggerConfigFile()
     39 {
     40     iFileServer.Close();
     41 }
     42 
     43 bool PVLoggerConfigFile::IsLoggerConfigFilePresent()
     44 {
     45     if (ESuccess == ReadAndParseLoggerConfigFile())
     46     {
     47         // success
     48         return true;
     49     }
     50     // failure
     51     return false;
     52 }
     53 void PVLoggerConfigFile::SetConfigFilePath(const oscl_wchar *aConfigFilepath)
     54 {
     55     oscl_strncpy(iLogFileName, aConfigFilepath, FILENAME_LEN);
     56     //set log file name
     57     oscl_strcat(iLogFileName, CONFIG_FILE_NAME);
     58 }
     59 
     60 //Read and parse the config file
     61 //retval = -1 if the config file doesnt exist
     62 int8  PVLoggerConfigFile::ReadAndParseLoggerConfigFile()
     63 {
     64     int8 retval = ESuccess;
     65 
     66     if (ESuccess != iLogFile.Open(iLogFileName, Oscl_File::MODE_READ, iFileServer))
     67     {
     68         // error occured in opening logger config file
     69         retval = EError;
     70     }
     71     else
     72     {
     73         // able to open logger config file
     74         if (!iLogFileRead)
     75         {
     76             iLogFile.Read(ibuffer, 1, sizeof(ibuffer));
     77             //Parse the buffer for \n chars
     78             Oscl_Vector<char*, OsclMemAllocator> LogConfigStrings;
     79 
     80             //Get the logger strings
     81 #if defined(__linux__) || defined(linux)
     82             const char* const lnFd = "\n";
     83 #endif
     84 
     85             const int8 lnFdLen = oscl_strlen(lnFd);
     86             int16 offset = 0;
     87             char* lastValidBffrAddr = ibuffer + oscl_strlen(ibuffer);
     88             const char* lnFdIndx = oscl_strstr(ibuffer, lnFd);
     89             while (lnFdIndx != NULL && lnFdIndx < lastValidBffrAddr)
     90             {
     91                 oscl_memset((char*)lnFdIndx, '\0', lnFdLen);
     92                 LogConfigStrings.push_back(ibuffer + offset);
     93                 offset = (lnFdIndx + lnFdLen) - ibuffer;
     94                 lnFdIndx = OSCL_CONST_CAST(char*, oscl_strstr(ibuffer + offset, lnFd));
     95             }
     96             if (NULL == lnFdIndx && ((ibuffer + offset) < lastValidBffrAddr)) //If \r\n is skipped after the last logging str in ini file
     97             {
     98                 LogConfigStrings.push_back(ibuffer + offset);
     99             }
    100 
    101 
    102             //Populate the  LoggerConfigElements vector
    103             {
    104                 if (!LogConfigStrings.empty())
    105                 {
    106                     Oscl_Vector<char*, OsclMemAllocator>::iterator it;
    107                     it = LogConfigStrings.begin();
    108                     uint32 appenderType;
    109                     PV_atoi(*it, 'd', oscl_strlen(*it), appenderType);
    110                     iAppenderType = appenderType;
    111                     if (LogConfigStrings.size() > 1)
    112                     {
    113                         for (it = LogConfigStrings.begin() + 1; it != LogConfigStrings.end(); it++)
    114                         {
    115                             char* CommaIndex = OSCL_CONST_CAST(char*, oscl_strstr(*it, ","));
    116                             if (CommaIndex != NULL)
    117                             {
    118                                 *CommaIndex = '\0';
    119                                 LoggerConfigElement obj;
    120                                 uint32 logLevel;
    121                                 PV_atoi(*it, 'd', oscl_strlen(*it), logLevel);
    122                                 obj.iLogLevel = logLevel;
    123                                 obj.iLoggerString = CommaIndex + 1;
    124                                 iLoggerConfigElements.push_back(obj);
    125                             }
    126                         }
    127                     }
    128                     else
    129                     {
    130                         //Add the config element for complete logging fo all the modules
    131                         LoggerConfigElement obj;
    132                         obj.iLoggerString = NULL;
    133                         obj.iLogLevel = 8;
    134                         iLoggerConfigElements.push_back(obj);
    135                     }
    136                 }
    137             }
    138             iLogFile.Close();
    139             iLogFileRead = true;
    140         }
    141     }
    142     return retval;
    143 }
    144 
    145 void PVLoggerConfigFile::SetAppenderType(int aAppenderType)
    146 {
    147     iAppenderType = aAppenderType;
    148 }
    149 
    150 uint32 PVLoggerConfigFile::SetLoggerSettings(CPV2WayInterface *aTerminal, const oscl_wchar *aLogPath)
    151 {
    152     Oscl_Vector<LoggerConfigElement, OsclMemAllocator>::iterator it;
    153     uint32 error = 1;
    154 
    155     PVLoggerAppender *appender = NULL;
    156     OsclRefCounter *refCounter = NULL;
    157     if (iLoggerConfigElements.empty())
    158     {
    159         return error;
    160     }
    161 
    162     if (EConsoleLog == iAppenderType)
    163     {
    164         // for console
    165         appender = new StdErrAppender<TimeAndIdLayout, 1024>();
    166         OsclRefCounterSA<AppenderDestructDealloc<StdErrAppender<TimeAndIdLayout, 1024> > > *appenderRefCounter =
    167             new OsclRefCounterSA<AppenderDestructDealloc<StdErrAppender<TimeAndIdLayout, 1024> > >(appender);
    168         refCounter = appenderRefCounter;
    169     }
    170     else if (EFileLog == iAppenderType)
    171     {
    172         //for file
    173         OSCL_wHeapString<OsclMemAllocator> logfilename(aLogPath);
    174         appender = (PVLoggerAppender*)TextFileAppender<TimeAndIdLayout, 1024>::CreateAppender(logfilename.get_str());
    175         OsclRefCounterSA<AppenderDestructDealloc<TextFileAppender<TimeAndIdLayout, 1024> > > *appenderRefCounter =
    176             new OsclRefCounterSA<AppenderDestructDealloc<TextFileAppender<TimeAndIdLayout, 1024> > >(appender);
    177         refCounter = appenderRefCounter;
    178     }
    179     else
    180     {
    181         // No Valid Logger Appender
    182         return error;
    183     }
    184 
    185     OsclSharedPtr<PVLoggerAppender> appenderPtr(appender, refCounter);
    186 
    187     for (it = iLoggerConfigElements.begin(); it != iLoggerConfigElements.end(); it++)
    188     {
    189         char* loggerString = OSCL_STATIC_CAST(char*, "");
    190         if (0 != oscl_strncmp(it->iLoggerString, ENABLE_ALL_LOGS, oscl_strlen(ENABLE_ALL_LOGS)))
    191         {
    192             loggerString = it->iLoggerString;
    193         }
    194         if (NULL == aTerminal)
    195         {
    196             PVLogger *node = NULL;
    197 
    198             node = PVLogger::GetLoggerObject(loggerString);
    199             if (NULL == node)
    200             {
    201                 error = 1;
    202                 return error;
    203             }
    204             node->AddAppender(appenderPtr);
    205             node->SetLogLevel(it->iLogLevel);
    206         }
    207         else
    208         {
    209             aTerminal->SetLogLevel("", it->iLogLevel, true);
    210             aTerminal->SetLogAppender(loggerString, appenderPtr);
    211             error = 0;
    212         }
    213 
    214         if (0 != error)
    215         {
    216             return error;
    217         }
    218     }
    219 
    220     return error;
    221 }
    222 
    223