LCOV - code coverage report
Current view: top level - sal/osl/all - log.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 72 0.0 %
Date: 2012-08-25 Functions: 0 7 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 109 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*
       3                 :            :  * Version: MPL 1.1 / GPLv3+ / LGPLv3+
       4                 :            :  *
       5                 :            :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :            :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :            :  * the License or as specified alternatively below. You may obtain a copy of
       8                 :            :  * the License at http://www.mozilla.org/MPL/
       9                 :            :  *
      10                 :            :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :            :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :            :  * for the specific language governing rights and limitations under the
      13                 :            :  * License.
      14                 :            :  *
      15                 :            :  * Major Contributor(s):
      16                 :            :  * Copyright (C) 2011 Red Hat, Inc., Stephan Bergmann <sbergman@redhat.com>
      17                 :            :  *  (initial developer)
      18                 :            :  *
      19                 :            :  * All Rights Reserved.
      20                 :            :  *
      21                 :            :  * For minor contributions see the git repository.
      22                 :            :  *
      23                 :            :  * Alternatively, the contents of this file may be used under the terms of
      24                 :            :  * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
      25                 :            :  * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
      26                 :            :  * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
      27                 :            :  * instead of those above.
      28                 :            :  */
      29                 :            : 
      30                 :            : #include "sal/config.h"
      31                 :            : 
      32                 :            : #include <cassert>
      33                 :            : #include <cstdarg>
      34                 :            : #include <cstddef>
      35                 :            : #include <cstdio>
      36                 :            : #include <cstdlib>
      37                 :            : #include <cstring>
      38                 :            : #include <sstream>
      39                 :            : 
      40                 :            : #include <stdio.h> // vsnprintf
      41                 :            : 
      42                 :            : #include "osl/thread.hxx"
      43                 :            : #include "rtl/string.h"
      44                 :            : #include "sal/detail/log.h"
      45                 :            : #include "sal/log.hxx"
      46                 :            : #include "sal/types.h"
      47                 :            : 
      48                 :            : #include "logformat.hxx"
      49                 :            : 
      50                 :            : #if defined WNT
      51                 :            : #include <process.h>
      52                 :            : #define OSL_DETAIL_GETPID _getpid()
      53                 :            : #else
      54                 :            : #include <unistd.h>
      55                 :            : #define OSL_DETAIL_GETPID getpid()
      56                 :            : #endif
      57                 :            : 
      58                 :            : // Avoid the use of other sal code in this file as much as possible, so that
      59                 :            : // this code can be called from other sal code without causing endless
      60                 :            : // recursion.
      61                 :            : 
      62                 :            : namespace {
      63                 :            : 
      64                 :          0 : bool equalStrings(
      65                 :            :     char const * string1, std::size_t length1, char const * string2,
      66                 :            :     std::size_t length2)
      67                 :            : {
      68 [ #  # ][ #  # ]:          0 :     return length1 == length2 && std::memcmp(string1, string2, length1) == 0;
      69                 :            : }
      70                 :            : 
      71                 :          0 : char const * toString(sal_detail_LogLevel level) {
      72      [ #  #  # ]:          0 :     switch (level) {
      73                 :            :     default:
      74                 :            :         assert(false); // this cannot happen
      75                 :            :         // fall through
      76                 :            :     case SAL_DETAIL_LOG_LEVEL_INFO:
      77                 :          0 :         return "info";
      78                 :            :     case SAL_DETAIL_LOG_LEVEL_WARN:
      79                 :          0 :         return "warn";
      80                 :            :     case SAL_DETAIL_LOG_LEVEL_DEBUG:
      81                 :          0 :         return "debug";
      82                 :            :     }
      83                 :            : }
      84                 :            : 
      85                 :          0 : bool report(sal_detail_LogLevel level, char const * area) {
      86         [ #  # ]:          0 :     if (level == SAL_DETAIL_LOG_LEVEL_DEBUG)
      87                 :          0 :         return true;
      88                 :            :     assert(area != 0);
      89                 :          0 :     char const * env = std::getenv("SAL_LOG");
      90         [ #  # ]:          0 :     if (env == 0) {
      91                 :          0 :         env = "+WARN";
      92                 :            :     }
      93                 :          0 :     std::size_t areaLen = std::strlen(area);
      94                 :            :     enum Sense { POSITIVE = 0, NEGATIVE = 1 };
      95                 :          0 :     std::size_t senseLen[2] = { 0, 1 };
      96                 :            :         // initial senseLen[POSITIVE] < senseLen[NEGATIVE], so that if there are
      97                 :            :         // no matching switches at all, the result will be negative (and
      98                 :            :         // initializing with 1 is safe as the length of a valid switch, even
      99                 :            :         // without the "+"/"-" prefix, will always be > 1)
     100                 :          0 :     for (char const * p = env;;) {
     101                 :            :         Sense sense;
     102   [ #  #  #  # ]:          0 :         switch (*p++) {
     103                 :            :         case '\0':
     104                 :          0 :             return senseLen[POSITIVE] >= senseLen[NEGATIVE];
     105                 :            :                 // if a specific item is both postiive and negative
     106                 :            :                 // (senseLen[POSITIVE] == senseLen[NEGATIVE]), default to
     107                 :            :                 // positive
     108                 :            :         case '+':
     109                 :          0 :             sense = POSITIVE;
     110                 :          0 :             break;
     111                 :            :         case '-':
     112                 :          0 :             sense = NEGATIVE;
     113                 :          0 :             break;
     114                 :            :         default:
     115                 :          0 :             return true; // upon an illegal SAL_LOG value, enable everything
     116                 :            :         }
     117                 :          0 :         char const * p1 = p;
     118 [ #  # ][ #  # ]:          0 :         while (*p1 != '.' && *p1 != '+' && *p1 != '-' && *p1 != '\0') {
         [ #  # ][ #  # ]
                 [ #  # ]
     119                 :          0 :             ++p1;
     120                 :            :         }
     121                 :            :         bool match;
     122         [ #  # ]:          0 :         if (equalStrings(p, p1 - p, RTL_CONSTASCII_STRINGPARAM("INFO"))) {
     123                 :          0 :             match = level == SAL_DETAIL_LOG_LEVEL_INFO;
     124         [ #  # ]:          0 :         } else if (equalStrings(p, p1 - p, RTL_CONSTASCII_STRINGPARAM("WARN")))
     125                 :            :         {
     126                 :          0 :             match = level == SAL_DETAIL_LOG_LEVEL_WARN;
     127                 :            :         } else {
     128                 :          0 :             return true;
     129                 :            :                 // upon an illegal SAL_LOG value, everything is considered
     130                 :            :                 // positive
     131                 :            :         }
     132                 :          0 :         char const * p2 = p1;
     133 [ #  # ][ #  # ]:          0 :         while (*p2 != '+' && *p2 != '-' && *p2 != '\0') {
         [ #  # ][ #  # ]
     134                 :          0 :             ++p2;
     135                 :            :         }
     136         [ #  # ]:          0 :         if (match) {
     137         [ #  # ]:          0 :             if (*p1 == '.') {
     138                 :          0 :                 ++p1;
     139                 :          0 :                 std::size_t n = p2 - p1;
     140 [ #  # ][ #  # ]:          0 :                 if ((n == areaLen && equalStrings(p1, n, area, areaLen))
                 [ #  # ]
           [ #  #  #  # ]
                 [ #  # ]
     141                 :          0 :                     || (n < areaLen && area[n] == '.'
     142                 :          0 :                         && equalStrings(p1, n, area, n)))
     143                 :            :                 {
     144                 :          0 :                     senseLen[sense] = p2 - p;
     145                 :            :                 }
     146                 :            :             } else {
     147                 :          0 :                 senseLen[sense] = p1 - p;
     148                 :            :             }
     149                 :            :         }
     150                 :          0 :         p = p2;
     151                 :            :     }
     152                 :            : }
     153                 :            : 
     154                 :          0 : void log(
     155                 :            :     sal_detail_LogLevel level, char const * area, char const * where,
     156                 :            :     char const * message)
     157                 :            : {
     158         [ #  # ]:          0 :     std::ostringstream s;
     159         [ #  # ]:          0 :     if (level == SAL_DETAIL_LOG_LEVEL_DEBUG) {
     160 [ #  # ][ #  # ]:          0 :         s << toString(level) << ':' << /*no where*/' ' << message << '\n';
         [ #  # ][ #  # ]
                 [ #  # ]
     161                 :            :     } else {
     162 [ #  # ][ #  # ]:          0 :         s << toString(level) << ':' << area << ':' << OSL_DETAIL_GETPID << ':'
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     163 [ #  # ][ #  # ]:          0 :             << osl::Thread::getCurrentIdentifier() << ':' << where << message
         [ #  # ][ #  # ]
                 [ #  # ]
     164         [ #  # ]:          0 :             << '\n';
     165                 :            :     }
     166 [ #  # ][ #  # ]:          0 :     std::fputs(s.str().c_str(), stderr);
                 [ #  # ]
     167                 :          0 : }
     168                 :            : 
     169                 :            : }
     170                 :            : 
     171                 :          0 : SAL_DLLPUBLIC void sal_detail_log(
     172                 :            :     sal_detail_LogLevel level, char const * area, char const * where,
     173                 :            :     char const * message)
     174                 :            : {
     175         [ #  # ]:          0 :     if (report(level, area)) {
     176                 :          0 :         log(level, area, where, message);
     177                 :            :     }
     178                 :          0 : }
     179                 :            : 
     180                 :          0 : SAL_DLLPUBLIC void sal_detail_logFormat(
     181                 :            :     sal_detail_LogLevel level, char const * area, char const * where,
     182                 :            :     char const * format, ...)
     183                 :            : {
     184         [ #  # ]:          0 :     if (report(level, area)) {
     185                 :            :         std::va_list args;
     186                 :          0 :         va_start(args, format);
     187         [ #  # ]:          0 :         osl::detail::logFormat(level, area, where, format, args);
     188                 :          0 :         va_end(args);
     189                 :            :     }
     190                 :          0 : }
     191                 :            : 
     192                 :          0 : void osl::detail::logFormat(
     193                 :            :     sal_detail_LogLevel level, char const * area, char const * where,
     194                 :            :     char const * format, std::va_list arguments)
     195                 :            : {
     196                 :            :     char buf[1024];
     197                 :          0 :     int const len = sizeof buf - RTL_CONSTASCII_LENGTH("...");
     198                 :          0 :     int n = vsnprintf(buf, len, format, arguments);
     199         [ #  # ]:          0 :     if (n < 0) {
     200                 :          0 :         std::strcpy(buf, "???");
     201         [ #  # ]:          0 :     } else if (n >= len) {
     202                 :          0 :         std::strcpy(buf + len - 1, "...");
     203                 :            :     }
     204         [ #  # ]:          0 :     log(level, area, where, buf);
     205                 :          0 : }
     206                 :            : 
     207                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10