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 : #include <string.h> // strdup
42 :
43 : #include "osl/thread.hxx"
44 : #include "rtl/string.h"
45 : #include "sal/detail/log.h"
46 : #include "sal/log.hxx"
47 : #include "sal/types.h"
48 :
49 : #include "logformat.hxx"
50 :
51 : #if defined WNT
52 : #include <process.h>
53 : #define OSL_DETAIL_GETPID _getpid()
54 : #else
55 : #include <unistd.h>
56 : #define OSL_DETAIL_GETPID getpid()
57 : #endif
58 :
59 : // Avoid the use of other sal code in this file as much as possible, so that
60 : // this code can be called from other sal code without causing endless
61 : // recursion.
62 :
63 : namespace {
64 :
65 0 : bool equalStrings(
66 : char const * string1, std::size_t length1, char const * string2,
67 : std::size_t length2)
68 : {
69 0 : return length1 == length2 && std::memcmp(string1, string2, length1) == 0;
70 : }
71 :
72 0 : char const * toString(sal_detail_LogLevel level) {
73 0 : switch (level) {
74 : default:
75 : assert(false); // this cannot happen
76 : // fall through
77 : case SAL_DETAIL_LOG_LEVEL_INFO:
78 0 : return "info";
79 : case SAL_DETAIL_LOG_LEVEL_WARN:
80 0 : return "warn";
81 : case SAL_DETAIL_LOG_LEVEL_DEBUG:
82 0 : return "debug";
83 : }
84 : }
85 :
86 : // getenv is not thread safe, so minimize use of result:
87 0 : char const * getEnvironmentVariable() {
88 0 : char const * p1 = std::getenv("SAL_LOG");
89 0 : if (p1 == 0) {
90 0 : return "+WARN";
91 : }
92 0 : char const * p2 = strdup(p1); // leaked
93 0 : if (p2 == 0) {
94 0 : std::abort(); // cannot do much here
95 : }
96 0 : return p2;
97 : }
98 :
99 0 : bool report(sal_detail_LogLevel level, char const * area) {
100 0 : if (level == SAL_DETAIL_LOG_LEVEL_DEBUG)
101 0 : return true;
102 : assert(area != 0);
103 0 : static char const * env = getEnvironmentVariable();
104 0 : std::size_t areaLen = std::strlen(area);
105 : enum Sense { POSITIVE = 0, NEGATIVE = 1 };
106 0 : std::size_t senseLen[2] = { 0, 1 };
107 : // initial senseLen[POSITIVE] < senseLen[NEGATIVE], so that if there are
108 : // no matching switches at all, the result will be negative (and
109 : // initializing with 1 is safe as the length of a valid switch, even
110 : // without the "+"/"-" prefix, will always be > 1)
111 0 : for (char const * p = env;;) {
112 : Sense sense;
113 0 : switch (*p++) {
114 : case '\0':
115 0 : return senseLen[POSITIVE] >= senseLen[NEGATIVE];
116 : // if a specific item is both postiive and negative
117 : // (senseLen[POSITIVE] == senseLen[NEGATIVE]), default to
118 : // positive
119 : case '+':
120 0 : sense = POSITIVE;
121 0 : break;
122 : case '-':
123 0 : sense = NEGATIVE;
124 0 : break;
125 : default:
126 0 : return true; // upon an illegal SAL_LOG value, enable everything
127 : }
128 0 : char const * p1 = p;
129 0 : while (*p1 != '.' && *p1 != '+' && *p1 != '-' && *p1 != '\0') {
130 0 : ++p1;
131 : }
132 : bool match;
133 0 : if (equalStrings(p, p1 - p, RTL_CONSTASCII_STRINGPARAM("INFO"))) {
134 0 : match = level == SAL_DETAIL_LOG_LEVEL_INFO;
135 0 : } else if (equalStrings(p, p1 - p, RTL_CONSTASCII_STRINGPARAM("WARN")))
136 : {
137 0 : match = level == SAL_DETAIL_LOG_LEVEL_WARN;
138 : } else {
139 0 : return true;
140 : // upon an illegal SAL_LOG value, everything is considered
141 : // positive
142 : }
143 0 : char const * p2 = p1;
144 0 : while (*p2 != '+' && *p2 != '-' && *p2 != '\0') {
145 0 : ++p2;
146 : }
147 0 : if (match) {
148 0 : if (*p1 == '.') {
149 0 : ++p1;
150 0 : std::size_t n = p2 - p1;
151 0 : if ((n == areaLen && equalStrings(p1, n, area, areaLen))
152 0 : || (n < areaLen && area[n] == '.'
153 0 : && equalStrings(p1, n, area, n)))
154 : {
155 0 : senseLen[sense] = p2 - p;
156 : }
157 : } else {
158 0 : senseLen[sense] = p1 - p;
159 : }
160 : }
161 0 : p = p2;
162 : }
163 : }
164 :
165 0 : void log(
166 : sal_detail_LogLevel level, char const * area, char const * where,
167 : char const * message)
168 : {
169 0 : std::ostringstream s;
170 0 : if (level == SAL_DETAIL_LOG_LEVEL_DEBUG) {
171 0 : s << toString(level) << ':' << /*no where*/' ' << message << '\n';
172 : } else {
173 0 : s << toString(level) << ':' << area << ':' << OSL_DETAIL_GETPID << ':'
174 0 : << osl::Thread::getCurrentIdentifier() << ':' << where << message
175 0 : << '\n';
176 : }
177 0 : std::fputs(s.str().c_str(), stderr);
178 0 : }
179 :
180 : }
181 :
182 0 : void sal_detail_log(
183 : sal_detail_LogLevel level, char const * area, char const * where,
184 : char const * message)
185 : {
186 0 : if (report(level, area)) {
187 0 : log(level, area, where, message);
188 : }
189 0 : }
190 :
191 0 : void sal_detail_logFormat(
192 : sal_detail_LogLevel level, char const * area, char const * where,
193 : char const * format, ...)
194 : {
195 0 : if (report(level, area)) {
196 : std::va_list args;
197 0 : va_start(args, format);
198 0 : osl::detail::logFormat(level, area, where, format, args);
199 0 : va_end(args);
200 : }
201 0 : }
202 :
203 0 : void osl::detail::logFormat(
204 : sal_detail_LogLevel level, char const * area, char const * where,
205 : char const * format, std::va_list arguments)
206 : {
207 : char buf[1024];
208 0 : int const len = sizeof buf - RTL_CONSTASCII_LENGTH("...");
209 0 : int n = vsnprintf(buf, len, format, arguments);
210 0 : if (n < 0) {
211 0 : std::strcpy(buf, "???");
212 0 : } else if (n >= len) {
213 0 : std::strcpy(buf + len - 1, "...");
214 : }
215 0 : log(level, area, where, buf);
216 0 : }
217 :
218 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|