~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/binlog_reader.cc

  • Committer: Brian Aker
  • Date: 2009-12-29 01:38:38 UTC
  • mfrom: (1251.1.1 drizzle)
  • Revision ID: brian@gaz-20091229013838-03kb2z5xbqw03ddt
Merge of Diego fix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2009 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
 
 
22
#include <drizzled/message/binary_log.h>
 
23
 
 
24
#include <iostream>
 
25
#include <fstream>
 
26
 
 
27
#include <google/protobuf/io/coded_stream.h>
 
28
#include <google/protobuf/io/zero_copy_stream_impl.h>
 
29
 
 
30
#include <getopt.h>
 
31
#include <fcntl.h>
 
32
 
 
33
#include <sys/stat.h>
 
34
 
 
35
using namespace std;
 
36
using namespace google;
 
37
 
 
38
static void print_usage_and_exit(char *prog) {
 
39
  const char *name= strrchr(prog, '/');
 
40
 
 
41
  if (name)
 
42
    ++name;
 
43
  else
 
44
    name= "binlog_reader";
 
45
  cerr << "Usage: " << name << " <options>\n"
 
46
       << "    --input name   Read queries from file <name> (default: 'log.bin')\n"
 
47
       << flush;
 
48
  exit(1);
 
49
}
 
50
 
 
51
 
 
52
int
 
53
main(int argc, char *argv[])
 
54
{
 
55
 
 
56
  static struct option options[] = {
 
57
    { "input",     1 /* has_arg */, NULL, 0 },
 
58
    { 0, 0, 0, 0 }
 
59
  };
 
60
 
 
61
  const char *file_name= "log.bin";
 
62
 
 
63
  int ch, option_index;
 
64
  while ((ch= getopt_long(argc, argv, "", options, &option_index)) != -1) {
 
65
    if (ch == '?')
 
66
      print_usage_and_exit(argv[0]);
 
67
 
 
68
    switch (option_index) {
 
69
    case 0:                                     // --input
 
70
      file_name= optarg;
 
71
      break;
 
72
 
 
73
    }
 
74
  }
 
75
 
 
76
  if (optind > argc)
 
77
    print_usage_and_exit(argv[0]);
 
78
 
 
79
  filebuf fb;
 
80
 
 
81
  fb.open(file_name, ios::in);
 
82
  istream is(&fb);
 
83
 
 
84
  protobuf::io::ZeroCopyInputStream* raw_input=
 
85
    new protobuf::io::IstreamInputStream(&is);
 
86
  protobuf::io::CodedInputStream *coded_input=
 
87
    new protobuf::io::CodedInputStream(raw_input);
 
88
 
 
89
  BinaryLog::Event event;
 
90
  while (event.read(coded_input))
 
91
    event.print(std::cout);
 
92
 
 
93
  delete coded_input;
 
94
  delete raw_input;
 
95
  fb.close();
 
96
}