Swarm-NG  1.1
binary_writer.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) 2011 by Saleh Dindar and the Swarm-NG Development Team *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 3 of the License. *
7  * *
8  * This program is distributed in the hope that it will be useful, *
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11  * GNU General Public License for more details. *
12  * *
13  * You should have received a copy of the GNU General Public License *
14  * along with this program; if not, write to the *
15  * Free Software Foundation, Inc., *
16  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
17  ************************************************************************/
18 
25 #include "../common.hpp"
26 
27 #include "../types/config.hpp"
28 #include "../plugin.hpp"
29 
30 #include "io.hpp"
31 #include "writer.h"
32 
33 namespace swarm { namespace log {
34 
39 class binary_writer : public writer
40 {
41 protected:
42  std::auto_ptr<std::ostream> output;
43  std::string rawfn, binfn;
44 
46 public:
47  binary_writer(const config &cfg)
48  {
49  binfn = cfg.at("log_output");
50  if(binfn=="")
51  ERROR("Expected filename for writer.")
52  rawfn = binfn + ".raw";
53 
54  output.reset(new std::ofstream(rawfn.c_str()));
55  if(!*output)
56  ERROR("Could not open '" + rawfn + "' for writing");
57 
58  // write header
59  swarm::swarm_header fh(query::UNSORTED_HEADER_FULL);
60  output->write((char*)&fh, sizeof(fh));
61  }
62 
63  /*
64  * We postpone sorting the output here. We leave the
65  * file unsorted as it is. No one knows who
66  * will benefit from sorting the file and it is
67  * very inefficient
68  */
69  ~binary_writer()
70  {
71  output.reset(NULL);
72  if(swarm::query::sort_binary_log_file(binfn, rawfn))
73  {
74  unlink(rawfn.c_str());
75 
76  // just touch it to auto-generate the indices
77  swarm::query::swarmdb db(binfn);
78  }
79  }
80 
82  virtual void process(const char *log_data, size_t length)
83  {
84  // TODO: filter out the printfs
85  output->write(log_data, length);
86  }
87 };
88 
91  binary_writer_plugin("binary", "This is the binary writer");
92 
93 } }
94