1/*
2 * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26#include "awt_ole.h"
27#include <time.h>
28#include <sys/timeb.h>
29
30namespace SUN_DBG_NS{
31  //WIN32 debug channel approach
32  //inline void DbgOut(LPCTSTR lpStr) { ::OutputDebugString(lpStr); }
33
34  //Java debug channel approach
35  inline void DbgOut(LPCTSTR lpStr) { DTRACE_PRINT(_B(lpStr)); }
36
37  LPCTSTR CreateTimeStamp(LPTSTR lpBuffer, size_t iBufferSize)
38  {
39        struct _timeb tb;
40        _ftime(&tb);
41        size_t len = _tcsftime(lpBuffer, iBufferSize, _T("%b %d %H:%M:%S"), localtime(&tb.time));
42        if (len && len+4 < iBufferSize) {
43            if (_sntprintf(lpBuffer+len, iBufferSize-len-1, _T(".%03d"), tb.millitm) < 0) {
44                 lpBuffer[iBufferSize-len-1] = 0;
45            }
46        }
47        return lpBuffer;
48  }
49
50  #define DTRACE_BUF_LEN 1024
51  void snvTrace(LPCTSTR lpszFormat, va_list argList)
52  {
53        TCHAR szBuffer[DTRACE_BUF_LEN];
54        if (_vsntprintf( szBuffer, DTRACE_BUF_LEN, lpszFormat, argList ) < 0) {
55            szBuffer[DTRACE_BUF_LEN-1] = 0;
56        }
57        TCHAR szTime[32];
58        CreateTimeStamp(szTime, sizeof(szTime));
59        _tcscat(szTime, _T(" "));
60        TCHAR szBuffer1[DTRACE_BUF_LEN];
61        size_t iFormatLen = _tcslen(lpszFormat);
62        BOOL bErrorReport = iFormatLen>6 && _tcscmp(lpszFormat + iFormatLen - 6, _T("[%08x]"))==0;
63        size_t iTimeLen = _tcslen(szTime);
64        if (_sntprintf(
65            szBuffer1 + iTimeLen,
66            DTRACE_BUF_LEN - iTimeLen - 1, //reserver for \n
67            _T("P:%04d T:%04d ") TRACE_SUFFIX _T("%s%s"),
68            ::GetCurrentProcessId(),
69            ::GetCurrentThreadId(),
70            bErrorReport?_T("Error:"):_T(""),
71            szBuffer) < 0)
72        {
73            _tcscpy_s(szBuffer1 + DTRACE_BUF_LEN - 5, 5, _T("...")); //reserver for \n
74        }
75        memcpy(szBuffer1, szTime, iTimeLen*sizeof(TCHAR));
76        _tcscat(szBuffer1, _T("\n"));
77        DbgOut( szBuffer1 );
78  }
79  void snTrace(LPCTSTR lpszFormat, ... )
80  {
81        va_list argList;
82        va_start(argList, lpszFormat);
83        snvTrace(lpszFormat, argList);
84        va_end(argList);
85  }
86}//SUN_DBG_NS namespace end
87