Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : */
9 :
10 : #include "sal/config.h"
11 :
12 : #include <cassert>
13 : #include <cstdarg>
14 : #include <cstddef>
15 : #include <cstdio>
16 : #include <cstdlib>
17 : #include <cstring>
18 : #include <sstream>
19 :
20 : #include <stdio.h>
21 : #include <string.h>
22 :
23 : #include "osl/thread.hxx"
24 : #include "rtl/string.h"
25 : #include "sal/detail/log.h"
26 : #include "sal/log.hxx"
27 : #include "sal/types.h"
28 :
29 : #include "logformat.hxx"
30 :
31 : #if defined ANDROID
32 : #include <android/log.h>
33 : #elif defined WNT
34 : #include <process.h>
35 : #define OSL_DETAIL_GETPID _getpid()
36 : #else
37 : #include <unistd.h>
38 : #define OSL_DETAIL_GETPID getpid()
39 : #endif
40 :
41 : #if HAVE_SYSLOG_H
42 : #include <syslog.h>
43 : // sal/osl/unx/salinit.cxx::sal_detail_initialize updates this:
44 : bool sal_use_syslog;
45 : #else
46 : enum { sal_use_syslog = false };
47 : #endif
48 :
49 : // Avoid the use of other sal code in this file as much as possible, so that
50 : // this code can be called from other sal code without causing endless
51 : // recursion.
52 :
53 : namespace {
54 :
55 0 : bool equalStrings(
56 : char const * string1, std::size_t length1, char const * string2,
57 : std::size_t length2)
58 : {
59 0 : return length1 == length2 && std::memcmp(string1, string2, length1) == 0;
60 : }
61 :
62 : #if !defined ANDROID
63 0 : char const * toString(sal_detail_LogLevel level) {
64 0 : switch (level) {
65 : default:
66 : assert(false); // this cannot happen
67 : // fall through
68 : case SAL_DETAIL_LOG_LEVEL_INFO:
69 0 : return "info";
70 : case SAL_DETAIL_LOG_LEVEL_WARN:
71 0 : return "warn";
72 : case SAL_DETAIL_LOG_LEVEL_DEBUG:
73 0 : return "debug";
74 : }
75 : }
76 : #endif
77 :
78 : // getenv is not thread safe, so minimize use of result; except on Android, see
79 : // 60628799633ffde502cb105b98d3f254f93115aa "Notice if SAL_LOG is changed while
80 : // the process is running":
81 : #if defined ANDROID
82 :
83 : char const * getEnvironmentVariable() {
84 : return std::getenv("SAL_LOG");
85 : }
86 :
87 : #else
88 :
89 0 : char const * getEnvironmentVariable_() {
90 0 : char const * p1 = std::getenv("SAL_LOG");
91 0 : if (p1 == 0) {
92 0 : return 0;
93 : }
94 0 : char const * p2 = strdup(p1); // leaked
95 0 : if (p2 == 0) {
96 0 : std::abort(); // cannot do much here
97 : }
98 0 : return p2;
99 : }
100 :
101 0 : char const * getEnvironmentVariable() {
102 0 : static char const * env = getEnvironmentVariable_();
103 0 : return env;
104 : }
105 :
106 : #endif
107 :
108 0 : bool report(sal_detail_LogLevel level, char const * area) {
109 0 : if (level == SAL_DETAIL_LOG_LEVEL_DEBUG)
110 0 : return true;
111 : assert(area != 0);
112 0 : char const * env = getEnvironmentVariable();
113 0 : if (env == 0) {
114 0 : env = "+WARN";
115 : }
116 0 : std::size_t areaLen = std::strlen(area);
117 : enum Sense { POSITIVE = 0, NEGATIVE = 1 };
118 0 : std::size_t senseLen[2] = { 0, 1 };
119 : // initial senseLen[POSITIVE] < senseLen[NEGATIVE], so that if there are
120 : // no matching switches at all, the result will be negative (and
121 : // initializing with 1 is safe as the length of a valid switch, even
122 : // without the "+"/"-" prefix, will always be > 1)
123 0 : for (char const * p = env;;) {
124 : Sense sense;
125 0 : switch (*p++) {
126 : case '\0':
127 0 : return senseLen[POSITIVE] >= senseLen[NEGATIVE];
128 : // if a specific item is both postiive and negative
129 : // (senseLen[POSITIVE] == senseLen[NEGATIVE]), default to
130 : // positive
131 : case '+':
132 0 : sense = POSITIVE;
133 0 : break;
134 : case '-':
135 0 : sense = NEGATIVE;
136 0 : break;
137 : default:
138 0 : return true; // upon an illegal SAL_LOG value, enable everything
139 : }
140 0 : char const * p1 = p;
141 0 : while (*p1 != '.' && *p1 != '+' && *p1 != '-' && *p1 != '\0') {
142 0 : ++p1;
143 : }
144 : bool match;
145 0 : if (equalStrings(p, p1 - p, RTL_CONSTASCII_STRINGPARAM("INFO"))) {
146 0 : match = level == SAL_DETAIL_LOG_LEVEL_INFO;
147 0 : } else if (equalStrings(p, p1 - p, RTL_CONSTASCII_STRINGPARAM("WARN")))
148 : {
149 0 : match = level == SAL_DETAIL_LOG_LEVEL_WARN;
150 : } else {
151 0 : return true;
152 : // upon an illegal SAL_LOG value, everything is considered
153 : // positive
154 : }
155 0 : char const * p2 = p1;
156 0 : while (*p2 != '+' && *p2 != '-' && *p2 != '\0') {
157 0 : ++p2;
158 : }
159 0 : if (match) {
160 0 : if (*p1 == '.') {
161 0 : ++p1;
162 0 : std::size_t n = p2 - p1;
163 0 : if ((n == areaLen && equalStrings(p1, n, area, areaLen))
164 0 : || (n < areaLen && area[n] == '.'
165 0 : && equalStrings(p1, n, area, n)))
166 : {
167 0 : senseLen[sense] = p2 - p;
168 : }
169 : } else {
170 0 : senseLen[sense] = p1 - p;
171 : }
172 : }
173 0 : p = p2;
174 0 : }
175 : }
176 :
177 0 : void log(
178 : sal_detail_LogLevel level, char const * area, char const * where,
179 : char const * message)
180 : {
181 0 : std::ostringstream s;
182 : #if !defined ANDROID
183 : // On Android, the area will be used as the "tag," and log info already
184 : // contains the PID
185 0 : if (!sal_use_syslog) {
186 0 : s << toString(level) << ':';
187 : }
188 0 : if (level != SAL_DETAIL_LOG_LEVEL_DEBUG) {
189 0 : s << area << ':';
190 : }
191 0 : s << OSL_DETAIL_GETPID << ':';
192 : #endif
193 0 : s << osl::Thread::getCurrentIdentifier() << ':';
194 0 : if (level == SAL_DETAIL_LOG_LEVEL_DEBUG) {
195 0 : s << ' ';
196 : } else {
197 0 : const size_t nStrLen(std::strlen(SRCDIR "/"));
198 : s << (where
199 0 : + (std::strncmp(where, SRCDIR "/", nStrLen) == 0
200 0 : ? nStrLen : 0));
201 : }
202 0 : s << message << '\n';
203 : #if defined ANDROID
204 : int android_log_level;
205 : switch (level) {
206 : case SAL_DETAIL_LOG_LEVEL_INFO:
207 : android_log_level = ANDROID_LOG_INFO;
208 : break;
209 : case SAL_DETAIL_LOG_LEVEL_WARN:
210 : android_log_level = ANDROID_LOG_WARN;
211 : break;
212 : case SAL_DETAIL_LOG_LEVEL_DEBUG:
213 : android_log_level = ANDROID_LOG_DEBUG;
214 : break;
215 : default:
216 : android_log_level = ANDROID_LOG_INFO;
217 : break;
218 : }
219 : __android_log_print(
220 : android_log_level, area == 0 ? "LibreOffice" : area, "%s",
221 : s.str().c_str());
222 : #else
223 0 : if (sal_use_syslog) {
224 : #if HAVE_SYSLOG_H
225 : int prio;
226 0 : switch (level) {
227 : case SAL_DETAIL_LOG_LEVEL_INFO:
228 0 : prio = LOG_INFO;
229 0 : break;
230 : default:
231 : assert(false); // this cannot happen
232 : // fall through
233 : case SAL_DETAIL_LOG_LEVEL_WARN:
234 0 : prio = LOG_WARNING;
235 0 : break;
236 : case SAL_DETAIL_LOG_LEVEL_DEBUG:
237 0 : prio = LOG_DEBUG;
238 0 : break;
239 : }
240 0 : syslog(prio, "%s", s.str().c_str());
241 : #endif
242 : } else {
243 0 : std::fputs(s.str().c_str(), stderr);
244 0 : std::fflush(stderr);
245 0 : }
246 : #endif
247 0 : }
248 :
249 : }
250 :
251 0 : void sal_detail_log(
252 : sal_detail_LogLevel level, char const * area, char const * where,
253 : char const * message)
254 : {
255 0 : if (report(level, area)) {
256 0 : log(level, area, where, message);
257 : }
258 0 : }
259 :
260 0 : void sal_detail_logFormat(
261 : sal_detail_LogLevel level, char const * area, char const * where,
262 : char const * format, ...)
263 : {
264 0 : if (report(level, area)) {
265 : std::va_list args;
266 0 : va_start(args, format);
267 0 : osl::detail::logFormat(level, area, where, format, args);
268 0 : va_end(args);
269 : }
270 0 : }
271 :
272 0 : void osl::detail::logFormat(
273 : sal_detail_LogLevel level, char const * area, char const * where,
274 : char const * format, std::va_list arguments)
275 : {
276 : char buf[1024];
277 0 : int const len = sizeof buf - RTL_CONSTASCII_LENGTH("...");
278 0 : int n = vsnprintf(buf, len, format, arguments);
279 0 : if (n < 0) {
280 0 : std::strcpy(buf, "???");
281 0 : } else if (n >= len) {
282 0 : std::strcpy(buf + len - 1, "...");
283 : }
284 0 : log(level, area, where, buf);
285 0 : }
286 :
287 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|