Implement MoveFileLocal (with creating a snapshot).
[chromium-blink-merge.git] / media / cdm / ppapi / cdm_logging.cc
blob117db5e26d2f801ade63417707dc8f92c3696f03
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>
37 #include <string>
39 namespace media {
41 #if !defined(NDEBUG)
43 namespace {
45 // Helper functions to wrap platform differences.
47 int32 CurrentProcessId() {
48 #if defined(OS_WIN)
49 return GetCurrentProcessId();
50 #elif defined(OS_POSIX)
51 return getpid();
52 #endif
55 int32 CurrentThreadId() {
56 // Pthreads doesn't have the concept of a thread ID, so we have to reach down
57 // into the kernel.
58 #if defined(OS_LINUX)
59 return syscall(__NR_gettid);
60 #elif defined(OS_ANDROID)
61 return gettid();
62 #elif defined(OS_SOLARIS)
63 return pthread_self();
64 #elif defined(OS_POSIX)
65 return reinterpret_cast<int64>(pthread_self());
66 #elif defined(OS_WIN)
67 return static_cast<int32>(::GetCurrentThreadId());
68 #endif
71 uint64 TickCount() {
72 #if defined(OS_WIN)
73 return GetTickCount();
74 #elif defined(OS_MACOSX)
75 return mach_absolute_time();
76 #elif defined(OS_POSIX)
77 struct timespec ts;
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;
85 #endif
88 } // namespace
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);
96 stream_ << '[';
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};
105 #ifdef _MSC_VER
106 localtime_s(&local_time, &t);
107 #else
108 localtime_r(&t, &local_time);
109 #endif
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
114 << '/'
115 << std::setw(2) << tm_time->tm_hour
116 << std::setw(2) << tm_time->tm_min
117 << std::setw(2) << tm_time->tm_sec
118 << ':';
119 stream_ << TickCount() << ':';
121 // File name.
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
133 // created."
134 std::cout << std::endl;
137 #endif // !defined(NDEBUG)
139 std::ostream& CdmLogStream::stream() {
140 return std::cout;
143 } // namespace media