log_parser.cc 3.14 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;

36
37
38
log_parser::log_parser() : inf(nullptr), gz_file(nullptr), gz_in(nullptr),
    buf_len(0), buf_pos(0)
{
39
  buf = new char[block_size];
40
41
}

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

52
53
54
55
56
57
58
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;
  }
59

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

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

66
  return newlen != 0;
67
68
}

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

73
74
75
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>();
76

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

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

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

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

94
  process_line(buf + buf_pos, line_len);
95

96
  return pos + 1;
97
98
}

99
bool log_parser::next_event() {
100
  cur_event.reset();
101

102
103
104
105
106
107
108
109
110
111
112
113
  if (buf_len == 0 && !next_block()) {
    std::cerr << "escape 0" << std::endl;
    return false;
  }

  do {
    size_t newpos = try_line();
    if (!newpos) {
      if (!next_block()) {
        std::cerr << "escape 1" << std::endl;
        return false;
      }
114

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

124
  return true;
125
}