Swarm-NG  1.1
host_array_writer.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) 2011 by Eric Ford 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 "host_array_writer.hpp"
26 
27 #include "../common.hpp"
28 
29 #include "../types/config.hpp"
30 #include "../plugin.hpp"
31 
32 //#include<unordered_map>
33 #include "io.hpp"
34 #include "writer.h"
35 
36 namespace swarm { namespace log {
37 
39  host_array_writer::host_array_writer(const config &cfg) : event_codes_to_log(0), event_log(0)
40  {
41  debug = cfg.optional<int>("debug_host_array_writer",0);
42  // eventually figure out how to set multiple event types
43  int event_type = cfg.require<int>("host_array_event_type");
44  if(event_type!=0)
45  add_event_type_to_log(event_type);
46  if(cfg.valid("host_array_event_type2"))
47  add_event_type_to_log(cfg.require<int>("host_array_event_type2"));
48  if(cfg.valid("host_array_event_type3"))
49  add_event_type_to_log(cfg.require<int>("host_array_event_type3"));
50  if(cfg.valid("host_array_event_type4"))
51  add_event_type_to_log(cfg.require<int>("host_array_event_type4"));
52  }
53 
56  {
57  // int size = event_codes_to_log.size();
58  // event_code_index[et] = size;
59  event_codes_to_log.push_back(et);
60  event_log.resize(event_codes_to_log.size());
61  }
62 
64  void host_array_writer::process(const char *log_data, size_t length)
65  {
66  int events_added = 0;
67  gpulog::ilogstream ils(log_data,length);
68  gpulog::logrecord lr;
69  while(lr = ils.next())
70  {
71  bool need_to_log = false;
72  int event_code = lr.msgid();
73  for(int i=0;i<event_codes_to_log.size();++i)
74  {
75  if(event_codes_to_log[i]!=event_code) continue;
76  need_to_log = true;
77 
78  double time;
79  int sysid;
80  lr >> time >> sysid;
81  if(sysid>=event_log[i].size()) event_log[i].resize(sysid+1);
82 
83  // \todo Add code for how to process other observations-type events here
84  // if((event_code==EVT_TRANSIT)||(event_code==EVT_OCCULTATION))
85 
86  // int event_id = (depth>0.) ? log::EVT_TRANSIT : log::EVT_OCCULTATION;
87  // log::event(_log,event_id,_sys.time()+dt_min_b2,_sys.id(),j,b,vproj);
88 
89  if((event_code>=log::EVT_FIRST_OBS_CODE)&&(event_code<=log::EVT_LAST_OBS_CODE))
90  {
91  int num_ints = log::num_ints_for_event(event_code);
92  int num_doubles = log::num_doubles_for_event(event_code);
93  std::vector<int> intdata(num_ints,-1);
94  std::vector<double> doubledata(num_doubles,-1.);
95  for(int j=0;j<num_ints;++j)
96  lr >> intdata[j];
97  for(int j=0;j<num_doubles;++j)
98  lr >> doubledata[j];
99  if(num_doubles>0)
100  {
101  if(num_ints==1)
102  event_log[i][sysid].push_back(event_record_type(intdata[0],time,&doubledata[0]));
103  else if (num_ints==2)
104  event_log[i][sysid].push_back(event_record_type(intdata[0],intdata[1],time,&doubledata[0]));
105  }
106 
107  ++events_added;
108  if(debug)
109  {
110  int oldprec = std::cout.precision();
111  std::cout.precision(10);
112  std::cout << "EventCode= " << event_code << " t= " << time << " sys= " << sysid << " bod= " << intdata[0];
113  if(num_ints>=2)
114  {
115  for(int j=1;j<num_ints;++j)
116  std::cout << ", " << intdata[j];
117  }
118  std::cout.precision(6);
119  for(int j=0;j<num_doubles;++j)
120  std::cout << " " << doubledata[j];
121  std::cout.precision(oldprec);
122  std::cout << " \n";
123  }
124  }
125 
126  }
127  }
128  }
129 
130 
132  host_array_writer_plugin("host_array", "This stores selected events in simple arrays on the host");
133 
134 } } // namespcae log::swarm
135