log_parser.cc 3.35 KB
Newer Older
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
 * Copyright 2021 Max Planck Institute for Software Systems, and
 * National University of Singapore
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

25
26
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
27
28
29
#include <fstream>
#include <iostream>

Antoine Kaufmann's avatar
Antoine Kaufmann committed
30
31
32
#include "trace/events.h"
#include "trace/parser.h"
#include "trace/process.h"
33
34
35

namespace bio = boost::iostreams;

Antoine Kaufmann's avatar
Antoine Kaufmann committed
36
37
log_parser::log_parser()
    : inf(nullptr), gz_file(nullptr), gz_in(nullptr), buf_len(0), buf_pos(0) {
38
  buf = new char[block_size];
39
40
}

41
42
43
44
45
46
47
48
log_parser::~log_parser() {
  if (inf)
    delete inf;
  if (gz_file) {
    delete gz_in;
    delete gz_file;
  }
  delete[] buf;
49
50
}

51
52
53
54
55
56
57
bool log_parser::next_block() {
  if (buf_pos == buf_len) {
    buf_pos = 0;
  } else {
    memmove(buf, buf + buf_pos, buf_len - buf_pos);
    buf_pos = buf_len - buf_pos;
  }
58

59
60
  inf->read(buf + buf_pos, block_size - buf_pos);
  size_t newlen = inf->gcount();
61

62
63
  buf_len = buf_pos + newlen;
  buf_pos = 0;
64

65
  return newlen != 0;
66
67
}

68
69
void log_parser::open(const char *path) {
  inf = new std::ifstream(path, std::ios_base::in);
70
71
}

72
73
74
void log_parser::open_gz(const char *path) {
  gz_file = new std::ifstream(path, std::ios_base::in | std::ios_base::binary);
  gz_in = new bio::filtering_streambuf<bio::input>();
75

76
77
  gz_in->push(bio::gzip_decompressor());
  gz_in->push(*gz_file);
78

79
  inf = new std::istream(gz_in);
80
81
}

82
83
84
size_t log_parser::try_line() {
  size_t pos = buf_pos;
  size_t line_len = 0;
85

86
87
88
89
90
91
  for (; pos < buf_len && buf[pos] != '\n'; pos++, line_len++) {
  }
  if (pos >= buf_len) {
    // line is incomplete
    return 0;
  }
92

93
  process_line(buf + buf_pos, line_len);
94

95
  return pos + 1;
96
97
}

98
99
100
101
102
103
bool log_parser::next_event() {
  if (buf_len == 0 && !next_block()) {
    std::cerr << "escape 0" << std::endl;
    return false;
  }

104
  got_event = false;
105
106
107
108
109
110
111
  do {
    size_t newpos = try_line();
    if (!newpos) {
      if (!next_block()) {
        std::cerr << "escape 1" << std::endl;
        return false;
      }
112

113
114
115
      newpos = try_line();
      if (!newpos) {
        std::cerr << "escape 2" << std::endl;
116
        return false;
117
      }
118
    }
119
    buf_pos = newpos;
120
  } while (!got_event);
121

122
  return true;
123
}
124
125
126

void log_parser::read_coro(coro_t::push_type &sink_) {
  sink = &sink_;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
127
128
  while (next_event())
    ;
129
130
}

Antoine Kaufmann's avatar
Antoine Kaufmann committed
131
void log_parser::yield(std::shared_ptr<event> ev) {
132
133
134
135
  got_event = true;
  ev->source = this;
  (*sink)(ev);
}