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>
44 // Helper functions to wrap platform differences.
46 int32
CurrentProcessId() {
48 return GetCurrentProcessId();
49 #elif defined(OS_POSIX)
54 int32
CurrentThreadId() {
55 // Pthreads doesn't have the concept of a thread ID, so we have to reach down
58 return syscall(__NR_gettid
);
59 #elif defined(OS_ANDROID)
61 #elif defined(OS_SOLARIS)
62 return pthread_self();
63 #elif defined(OS_POSIX)
64 return reinterpret_cast<int64
>(pthread_self());
66 return static_cast<int32
>(::GetCurrentThreadId());
72 return GetTickCount();
73 #elif defined(OS_MACOSX)
74 return mach_absolute_time();
75 #elif defined(OS_POSIX)
77 clock_gettime(CLOCK_MONOTONIC
, &ts
);
79 uint64 absolute_micro
=
80 static_cast<int64
>(ts
.tv_sec
) * 1000000 +
81 static_cast<int64
>(ts
.tv_nsec
) / 1000;
83 return absolute_micro
;
89 CdmLogMessage::CdmLogMessage(const char* file
, int line
) {
90 std::string
filename(file
);
91 size_t last_slash_pos
= filename
.find_last_of("\\/");
92 if (last_slash_pos
!= std::string::npos
)
93 filename
= filename
.substr(last_slash_pos
+ 1);
97 // Process and thread ID.
98 stream_
<< CurrentProcessId() << ':';
99 stream_
<< CurrentThreadId() << ':';
101 // Time and tick count.
102 time_t t
= time(NULL
);
103 struct tm local_time
= {0};
105 localtime_s(&local_time
, &t
);
107 localtime_r(&t
, &local_time
);
109 struct tm
* tm_time
= &local_time
;
110 stream_
<< std::setfill('0')
111 << std::setw(2) << 1 + tm_time
->tm_mon
112 << std::setw(2) << tm_time
->tm_mday
114 << std::setw(2) << tm_time
->tm_hour
115 << std::setw(2) << tm_time
->tm_min
116 << std::setw(2) << tm_time
->tm_sec
118 stream_
<< TickCount() << ':';
121 stream_
<< filename
<< "(" << line
<< ")] ";
124 CdmLogMessage::~CdmLogMessage() {
125 // Use std::cout explicitly for the line break. This limits the use of this
126 // class only to the definition of DLOG() (which also uses std::cout).
128 // This appends "std::endl" after all other messages appended to DLOG(),
129 // which relies on the C++ standard ISO/IEC 14882:1998(E) $12.2.3:
130 // "Temporary objects are destroyed as the last step in evaluating the
131 // full-expression (1.9) that (lexically) contains the point where they were
133 std::cout
<< std::endl
;
136 #endif // !defined(NDEBUG)
138 std::ostream
& CdmLogStream::stream() {