Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / cdm / ppapi / cdm_logging.cc
blob4d47cec99c0a360f3b15ce5db9f92d81863f2388
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
8 // reference" errors.
10 #include "media/cdm/ppapi/cdm_logging.h"
12 #include "base/basictypes.h"
14 #if defined(OS_WIN)
15 #include <io.h>
16 #include <windows.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>
23 #include <time.h>
24 #endif
26 #if defined(OS_POSIX)
27 #include <errno.h>
28 #include <pthread.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33 #endif
35 #include <iomanip>
36 #include <iostream>
38 namespace media {
40 #if !defined(NDEBUG)
42 namespace {
44 // Helper functions to wrap platform differences.
46 int32 CurrentProcessId() {
47 #if defined(OS_WIN)
48 return GetCurrentProcessId();
49 #elif defined(OS_POSIX)
50 return getpid();
51 #endif
54 int32 CurrentThreadId() {
55 // Pthreads doesn't have the concept of a thread ID, so we have to reach down
56 // into the kernel.
57 #if defined(OS_LINUX)
58 return syscall(__NR_gettid);
59 #elif defined(OS_ANDROID)
60 return gettid();
61 #elif defined(OS_SOLARIS)
62 return pthread_self();
63 #elif defined(OS_POSIX)
64 return reinterpret_cast<int64>(pthread_self());
65 #elif defined(OS_WIN)
66 return static_cast<int32>(::GetCurrentThreadId());
67 #endif
70 uint64 TickCount() {
71 #if defined(OS_WIN)
72 return GetTickCount();
73 #elif defined(OS_MACOSX)
74 return mach_absolute_time();
75 #elif defined(OS_POSIX)
76 struct timespec ts;
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;
84 #endif
87 } // namespace
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);
95 stream_ << '[';
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};
104 #ifdef _MSC_VER
105 localtime_s(&local_time, &t);
106 #else
107 localtime_r(&t, &local_time);
108 #endif
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
113 << '/'
114 << std::setw(2) << tm_time->tm_hour
115 << std::setw(2) << tm_time->tm_min
116 << std::setw(2) << tm_time->tm_sec
117 << ':';
118 stream_ << TickCount() << ':';
120 // File name.
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
132 // created."
133 std::cout << std::endl;
136 #endif // !defined(NDEBUG)
138 std::ostream& CdmLogStream::stream() {
139 return std::cout;
142 } // namespace media