Home | History | Annotate | Download | only in foundation
      1 /*
      2  * Copyright 2014 The Android Open Source Project
      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 express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <errno.h>
     18 #include <stdlib.h>
     19 #include <ctype.h>
     20 
     21 #define LOG_TAG "ADebug"
     22 #include <utils/Log.h>
     23 #include <utils/misc.h>
     24 
     25 #include <cutils/properties.h>
     26 
     27 #include <ADebug.h>
     28 #include <AStringUtils.h>
     29 #include <AUtils.h>
     30 
     31 namespace android {
     32 
     33 //static
     34 ADebug::Level ADebug::GetDebugLevelFromString(
     35         const char *name, const char *value, ADebug::Level def) {
     36     // split on ,
     37     const char *next = value, *current;
     38     const unsigned long maxLevel = (unsigned long)kDebugMax;
     39     while (next != NULL) {
     40         current = next;
     41         next = strchr(current, ',');
     42         if (next != NULL) {
     43             ++next;  // pass ,
     44         }
     45 
     46         while (isspace(*current)) {
     47             ++current;
     48         }
     49         // check for :
     50         char *colon = strchr(current, ':');
     51 
     52         // get level
     53         char *end;
     54         errno = 0;  // strtoul does not clear errno, but it can be set for any return value
     55         unsigned long level = strtoul(current, &end, 10);
     56         while (isspace(*end)) {
     57             ++end;
     58         }
     59         if (errno != 0 || end == current || (end != colon && *end != '\0' && end != next)) {
     60             // invalid level - skip
     61             continue;
     62         }
     63         if (colon != NULL) {
     64             // check if pattern matches
     65             do {  // skip colon and spaces
     66                 ++colon;
     67             } while (isspace(*colon));
     68             size_t globLen = (next == NULL ? strlen(colon) : (next - 1 - colon));
     69             while (globLen > 0 && isspace(colon[globLen - 1])) {
     70                 --globLen;  // trim glob
     71             }
     72 
     73             if (!AStringUtils::MatchesGlob(
     74                     colon, globLen, name, strlen(name), true /* ignoreCase */)) {
     75                 continue;
     76             }
     77         }
     78 
     79         // update debug level
     80         def = (Level)min(level, maxLevel);
     81     }
     82     return def;
     83 }
     84 
     85 //static
     86 ADebug::Level ADebug::GetDebugLevelFromProperty(
     87         const char *name, const char *propertyName, ADebug::Level def) {
     88     char value[PROPERTY_VALUE_MAX];
     89     if (property_get(propertyName, value, NULL)) {
     90         return GetDebugLevelFromString(name, value, def);
     91     }
     92     return def;
     93 }
     94 
     95 //static
     96 char *ADebug::GetDebugName(const char *name) {
     97     char *debugName = strdup(name);
     98     const char *terms[] = { "omx", "video", "audio" };
     99     for (size_t i = 0; i < NELEM(terms) && debugName != NULL; i++) {
    100         const char *term = terms[i];
    101         const size_t len = strlen(term);
    102         char *match = strcasestr(debugName, term);
    103         if (match != NULL && (match == debugName || match[-1] == '.'
    104                 || match[len] == '.' || match[len] == '\0')) {
    105             char *src = match + len;
    106             if (match == debugName || match[-1] == '.') {
    107                 src += (*src == '.');  // remove trailing or double .
    108             }
    109             memmove(match, src, debugName + strlen(debugName) - src + 1);
    110         }
    111     }
    112 
    113     return debugName;
    114 }
    115 
    116 }  // namespace android
    117 
    118