1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Only compile this file in debug build. This gives us one more level of
6 // protection that if the linker tries to link in strings/symbols appended to
7 // "DLOG() <<" in release build (which it shouldn't), we'll get "undefined
10 #include "media/cdm/ppapi/cdm_logging.h"
12 #include "base/basictypes.h"
17 #elif defined(OS_MACOSX)
18 #include <mach/mach.h>
19 #include <mach/mach_time.h>
20 #include <mach-o/dyld.h>
21 #elif defined(OS_POSIX)
22 #include <sys/syscall.h>
45 // Helper functions to wrap platform differences.
47 int32
CurrentProcessId() {
49 return GetCurrentProcessId();
50 #elif defined(OS_POSIX)
55 int32
CurrentThreadId() {
56 // Pthreads doesn't have the concept of a thread ID, so we have to reach down
59 return syscall(__NR_gettid
);
60 #elif defined(OS_ANDROID)
62 #elif defined(OS_SOLARIS)
63 return pthread_self();
64 #elif defined(OS_POSIX)
65 return reinterpret_cast<int64
>(pthread_self());
67 return static_cast<int32
>(::GetCurrentThreadId());
73 return GetTickCount();
74 #elif defined(OS_MACOSX)
75 return mach_absolute_time();
76 #elif defined(OS_POSIX)
78 clock_gettime(CLOCK_MONOTONIC
, &ts
);
80 uint64 absolute_micro
=
81 static_cast<int64
>(ts
.tv_sec
) * 1000000 +
82 static_cast<int64
>(ts
.tv_nsec
) / 1000;
84 return absolute_micro
;
90 CdmLogMessage::CdmLogMessage(const char* file
, int line
) {
91 std::string
filename(file
);
92 size_t last_slash_pos
= filename
.find_last_of("\\/");
93 if (last_slash_pos
!= std::string::npos
)
94 filename
= filename
.substr(last_slash_pos
+ 1);
98 // Process and thread ID.
99 stream_
<< CurrentProcessId() << ':';
100 stream_
<< CurrentThreadId() << ':';
102 // Time and tick count.
103 time_t t
= time(NULL
);
104 struct tm local_time
= {0};
106 localtime_s(&local_time
, &t
);
108 localtime_r(&t
, &local_time
);
110 struct tm
* tm_time
= &local_time
;
111 stream_
<< std::setfill('0')
112 << std::setw(2) << 1 + tm_time
->tm_mon
113 << std::setw(2) << tm_time
->tm_mday
115 << std::setw(2) << tm_time
->tm_hour
116 << std::setw(2) << tm_time
->tm_min
117 << std::setw(2) << tm_time
->tm_sec
119 stream_
<< TickCount() << ':';
122 stream_
<< filename
<< "(" << line
<< ")] ";
125 CdmLogMessage::~CdmLogMessage() {
126 // Use std::cout explicitly for the line break. This limits the use of this
127 // class only to the definition of DLOG() (which also uses std::cout).
129 // This appends "std::endl" after all other messages appended to DLOG(),
130 // which relies on the C++ standard ISO/IEC 14882:1998(E) $12.2.3:
131 // "Temporary objects are destroyed as the last step in evaluating the
132 // full-expression (1.9) that (lexically) contains the point where they were
134 std::cout
<< std::endl
;
137 #endif // !defined(NDEBUG)
139 std::ostream
& CdmLogStream::stream() {