Fawkes API  Fawkes Development Version
bblogfile.h
1 
2 /***************************************************************************
3  * bblogfile.h - BlackBoard log file access convenience class
4  *
5  * Created: Sun Feb 21 11:12:47 2010
6  * Copyright 2006-2010 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #ifndef _PLUGINS_BBLOGGER_BBLOGFILE_H_
24 #define _PLUGINS_BBLOGGER_BBLOGFILE_H_
25 
26 #include "file.h"
27 
28 #include <core/exceptions/software.h>
29 #include <utils/time/time.h>
30 
31 #include <cstdio>
32 #include <memory>
33 
34 namespace fawkes {
35 class Interface;
36 class BlackBoardInstanceFactory;
37 } // namespace fawkes
38 
39 class BBLogFile
40 {
41 public:
42  BBLogFile(const char *filename, bool do_sanity_check);
43  BBLogFile(const char *filename, fawkes::Interface *interface = NULL, bool do_sanity_check = true);
44  ~BBLogFile();
45 
46  bool has_next();
47  void read_next();
48  void read_index(unsigned int index);
49  const fawkes::Time &entry_offset() const;
50  void print_entry(FILE *outf = stdout);
51 
52  void rewind();
53 
54  void set_num_entries(size_t num_entries);
55  void print_info(const char *line_prefix = "", FILE *outf = stdout);
56 
57  // Header information
58  uint32_t file_version() const;
59  bool is_big_endian() const;
60  uint32_t num_data_items() const;
61  const char * scenario() const;
62  const char * interface_type() const;
63  const char * interface_id() const;
64  unsigned char *interface_hash() const;
65  uint32_t data_size();
67 
68  size_t file_size() const;
69  unsigned int remaining_entries();
70 
71  static void repair_file(const char *filename);
72 
75 
76  /** Get typed interface.
77  * @param iface will assigned to the interface on success
78  * @return interface of the given type
79  * @exception TypeMismatchException thrown if interface type or ID do not match
80  */
81  template <class IT>
82  IT *
83  interface(IT *&iface = 0) const
84  {
85  IT *rv = dynamic_cast<IT *>(interface_);
86  if (rv) {
87  iface = rv;
88  return rv;
89  } else {
90  throw fawkes::TypeMismatchException("Interface types do not match.");
91  }
92  }
93 
94 private: // methods
95  void ctor(const char *filename, bool do_sanity_check);
96  void read_file_header();
97  void sanity_check();
98  void repair();
99 
100 private: // members
101  FILE * f_;
102  bblog_file_header *header_;
103 
104  void *ifdata_;
105 
106  char *filename_;
107  char *scenario_;
108  char *interface_type_;
109  char *interface_id_;
110 
111  fawkes::Interface * interface_;
112  std::unique_ptr<fawkes::BlackBoardInstanceFactory> instance_factory_;
113  fawkes::Time start_time_;
114  fawkes::Time entry_offset_;
115 };
116 
117 #endif
BBLogFile::BBLogFile
BBLogFile(const char *filename, bool do_sanity_check)
Constructor.
Definition: bblogfile.cpp:102
BBLogFile::file_size
size_t file_size() const
Get file size.
Definition: bblogfile.cpp:624
BBLogFile::print_info
void print_info(const char *line_prefix="", FILE *outf=stdout)
Print file meta info.
Definition: bblogfile.cpp:413
BBLogFile::set_num_entries
void set_num_entries(size_t num_entries)
Set number of entries.
Definition: bblogfile.cpp:302
BBLogFile::set_interface
void set_interface(fawkes::Interface *interface)
Set the internal interface.
Definition: bblogfile.cpp:494
BBLogFile::read_next
void read_next()
Read next entry.
Definition: bblogfile.cpp:283
BBLogFile::data_size
uint32_t data_size()
Get data size.
Definition: bblogfile.cpp:587
BBLogFile::remaining_entries
unsigned int remaining_entries()
Get number of remaining entries.
Definition: bblogfile.cpp:605
BBLogFile::file_version
uint32_t file_version() const
Get file version.
Definition: bblogfile.cpp:523
BBLogFile::interface_id
const char * interface_id() const
Get interface ID.
Definition: bblogfile.cpp:568
BBLogFile::rewind
void rewind()
Rewind file to start.
Definition: bblogfile.cpp:254
BBLogFile::interface
fawkes::Interface * interface()
Get interface instance.
Definition: bblogfile.cpp:484
BBLogFile::repair_file
static void repair_file(const char *filename)
Repair file.
Definition: bblogfile.cpp:323
BBLogFile::scenario
const char * scenario() const
Get scenario identifier.
Definition: bblogfile.cpp:550
fawkes::TypeMismatchException
Type mismatch.
Definition: software.h:44
BBLogFile::has_next
bool has_next()
Check if another entry is available.
Definition: bblogfile.cpp:266
fawkes
Fawkes library namespace.
BBLogFile::print_entry
void print_entry(FILE *outf=stdout)
Print an entry.
Definition: bblogfile.cpp:459
BBLogFile::read_index
void read_index(unsigned int index)
Read entry at particular index.
Definition: bblogfile.cpp:238
fawkes::Interface
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
BBLogFile::~BBLogFile
~BBLogFile()
Destructor.
Definition: bblogfile.cpp:137
BBLogFile::is_big_endian
bool is_big_endian() const
Check if file is big endian.
Definition: bblogfile.cpp:532
fawkes::Time
A class for handling time.
Definition: time.h:93
BBLogFile::num_data_items
uint32_t num_data_items() const
Get number of data items in file.
Definition: bblogfile.cpp:541
BBLogFile::start_time
fawkes::Time & start_time()
Get start time.
Definition: bblogfile.cpp:596
BBLogFile::interface
IT * interface(IT *&iface=0) const
Get typed interface.
Definition: bblogfile.h:83
bblog_file_header
BBLogger file header definition.
Definition: file.h:53
BBLogFile::interface_hash
unsigned char * interface_hash() const
Get interface hash.
Definition: bblogfile.cpp:578
BBLogFile::interface_type
const char * interface_type() const
Get interface type.
Definition: bblogfile.cpp:559
BBLogFile::entry_offset
const fawkes::Time & entry_offset() const
Get current entry offset.
Definition: bblogfile.cpp:514
BBLogFile
Class to easily access bblogger log files.
Definition: bblogfile.h:40