File: event_trace_data.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (132 lines) | stat: -rw-r--r-- 4,289 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "remoting/host/win/event_trace_data.h"

#include "base/check.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/logging_win.h"
#include "base/notreached.h"
#include "base/strings/utf_string_conversions.h"

namespace remoting {

namespace {

constexpr char kInfoSeverity[] = "INFO";
constexpr char kWarningSeverity[] = "WARNING";
constexpr char kErrorSeverity[] = "ERROR";
constexpr char kFatalSeverity[] = "FATAL";
constexpr char kVerboseSeverity[] = "VERBOSE";

logging::LogSeverity EventTraceLevelToSeverity(uint8_t level) {
  switch (level) {
    case TRACE_LEVEL_NONE:
      NOTREACHED();
    case TRACE_LEVEL_FATAL:
      return logging::LOGGING_FATAL;
    case TRACE_LEVEL_ERROR:
      return logging::LOGGING_ERROR;
    case TRACE_LEVEL_WARNING:
      return logging::LOGGING_WARNING;
    case TRACE_LEVEL_INFORMATION:
      return logging::LOGGING_INFO;
    default:
      // These represent VLOG verbosity levels.
      return TRACE_LEVEL_INFORMATION - level;
  }
}

}  // namespace

EventTraceData::EventTraceData() = default;

EventTraceData::EventTraceData(EventTraceData&&) = default;

EventTraceData& EventTraceData::operator=(EventTraceData&&) = default;

EventTraceData::~EventTraceData() = default;

// static
EventTraceData EventTraceData::Create(EVENT_TRACE* event) {
  EventTraceData data;

  data.event_type = event->Header.Class.Type;

  data.severity = EventTraceLevelToSeverity(event->Header.Class.Level);
  data.process_id = event->Header.ProcessId;
  data.thread_id = event->Header.ThreadId;

  FILETIME event_time = {};
  event_time.dwLowDateTime = event->Header.TimeStamp.LowPart;
  event_time.dwHighDateTime = event->Header.TimeStamp.HighPart;
  data.time_stamp = base::Time::FromFileTime(event_time);

  // Parse the MofData.  The structure is defined in //base/logging_win.cc.
  // - For LOG_MESSAGE events, the MofData buffer just contains the message.
  // - For LOG_MESSAGE_FULL events, the MofData buffer is comprised of 5 fields
  //   which must be parsed (or skipped) in sequence.
  if (data.event_type == logging::LOG_MESSAGE) {
    data.message.assign(reinterpret_cast<const char*>(event->MofData),
                        event->MofLength);
  } else if (data.event_type == logging::LOG_MESSAGE_FULL) {
    const uint8_t* mof_data = reinterpret_cast<const uint8_t*>(event->MofData);
    uint32_t offset = 0;

    // Read the size, skip past the stack info, and move the cursor.
    DWORD stack_depth = *reinterpret_cast<const DWORD*>(mof_data);
    int bytes_to_skip = sizeof(DWORD) + stack_depth * sizeof(intptr_t);
    offset += bytes_to_skip;

    // Read the line info and move the cursor.
    data.line = *reinterpret_cast<const int32_t*>(mof_data + offset);
    offset += sizeof(int32_t);

    // Read the file info and move the cursor.
    const char* file_info = reinterpret_cast<const char*>(mof_data + offset);
    size_t str_len = strnlen_s(file_info, event->MofLength - offset);
    base::FilePath file_path(base::UTF8ToWide(file_info));
    data.file_name = base::WideToUTF8(file_path.BaseName().value());
    offset += (str_len + 1);

    // Read the message and move the cursor.
    const char* message = reinterpret_cast<const char*>(mof_data + offset);
    str_len = strnlen_s(message, event->MofLength - offset);
    data.message.assign(message);
    offset += (str_len + 1);

    DCHECK_EQ(event->MofLength, offset);
  } else {
    NOTREACHED() << "Unknown event type: " << data.event_type;
  }

  return data;
}

// static
std::string EventTraceData::SeverityToString(logging::LogSeverity severity) {
  switch (severity) {
    case logging::LOGGING_INFO:
      return kInfoSeverity;
    case logging::LOGGING_WARNING:
      return kWarningSeverity;
    case logging::LOGGING_ERROR:
      return kErrorSeverity;
    case logging::LOGGING_FATAL:
      return kFatalSeverity;
    default:
      if (severity < 0) {
        return kVerboseSeverity;
      }
      NOTREACHED();
  }
}

}  // namespace remoting