xref: /aoo42x/main/sal/osl/all/printtrace.cxx (revision 87d2adbc)
1*87d2adbcSAndrew Rist /**************************************************************
2*87d2adbcSAndrew Rist  *
3*87d2adbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*87d2adbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*87d2adbcSAndrew Rist  * distributed with this work for additional information
6*87d2adbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*87d2adbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*87d2adbcSAndrew Rist  * "License"); you may not use this file except in compliance
9*87d2adbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*87d2adbcSAndrew Rist  *
11*87d2adbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*87d2adbcSAndrew Rist  *
13*87d2adbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*87d2adbcSAndrew Rist  * software distributed under the License is distributed on an
15*87d2adbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*87d2adbcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*87d2adbcSAndrew Rist  * specific language governing permissions and limitations
18*87d2adbcSAndrew Rist  * under the License.
19*87d2adbcSAndrew Rist  *
20*87d2adbcSAndrew Rist  *************************************************************/
21*87d2adbcSAndrew Rist 
22*87d2adbcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "precompiled_sal.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "sal/config.h"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <cstdarg>
29cdf0e10cSrcweir #include <cstdio>
30cdf0e10cSrcweir #include <cstring>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <stdio.h> // snprintf, vsnprintf
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include "osl/diagnose.h"
35cdf0e10cSrcweir #include "osl/thread.hxx"
36cdf0e10cSrcweir #include "rtl/string.h"
37cdf0e10cSrcweir #include "sal/types.h"
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include "printtrace.h"
40cdf0e10cSrcweir 
printTrace(unsigned long pid,char const * format,std::va_list arguments)41cdf0e10cSrcweir void printTrace(unsigned long pid, char const * format, std::va_list arguments)
42cdf0e10cSrcweir {
43cdf0e10cSrcweir     char buf[1024];
44cdf0e10cSrcweir     int n1 = snprintf(
45cdf0e10cSrcweir         buf, sizeof buf, "Trace %lu/%" SAL_PRIuUINT32 ": \"", pid,
46cdf0e10cSrcweir         osl::Thread::getCurrentIdentifier());
47cdf0e10cSrcweir     OSL_ASSERT(
48cdf0e10cSrcweir         n1 >= 0 &&
49cdf0e10cSrcweir         (static_cast< unsigned int >(n1) <
50cdf0e10cSrcweir          sizeof buf - RTL_CONSTASCII_LENGTH("\"...\n")));
51cdf0e10cSrcweir     int n2 = sizeof buf - n1 - RTL_CONSTASCII_LENGTH("\"...\n");
52cdf0e10cSrcweir     int n3 = vsnprintf(buf + n1, n2, format, arguments);
53cdf0e10cSrcweir     if (n3 < 0) {
54cdf0e10cSrcweir         std::strcpy(buf + n1, "\"???\n");
55cdf0e10cSrcweir     } else if (n3 < n2) {
56cdf0e10cSrcweir         std::strcpy(buf + n1 + n3, "\"\n");
57cdf0e10cSrcweir     } else {
58cdf0e10cSrcweir         std::strcpy(buf + n1 + n2 - 1, "\"...\n");
59cdf0e10cSrcweir     }
60cdf0e10cSrcweir     std::fputs(buf, stderr);
61cdf0e10cSrcweir }
62