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