arcssensorlogger.cpp
1 /*
2  name: lib/arcssensorlogger.cpp
3 
4  This file is part of ARCS - Augmented Reality Component System
5  (version 2-current), written by Jean-Yves Didier
6  for IBISC Laboratory (http://www.ibisc.univ-evry.fr)
7 
8  Copyright (C) 2013 Université d'Evry-Val d'Essonne
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 General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Please send bugreports with examples or suggestions to
25  jean-yves.didier__at__ibisc.univ-evry.fr
26 */
27 
28 
29 #include <arcs/arcssensorlogger.h>
30 #include <arcs/arcsfactory.h>
31 #include <iostream>
32 #include <QTime>
33 #include <QDir>
34 
35 void ARCSSensorLoggerBase::setFilename(QString s)
36 {
37  if (logFile.is_open())
38  logFile.close();
39 
40  logFile.open(qPrintable(s));
41 }
42 
43 
44 bool ARCSSensorLoggerBase::canLog()
45 {
46  if (!logFile.is_open())
47  logFile.open("arcs_runtime.log");
48 
49  return logFile.is_open();
50 }
51 
52 
53 /**********************************************************************************
54  * ARCSSensorLogger
55  **********************************************************************************/
56 
57 ARCSSensorLogger::ARCSSensorLogger(QObject* parent) : ARCSSensorLoggerBase(parent)
58 {
59  idx = 0;
60 }
61 
62 
64 {
65  QStringList sl ;
66  sl << "log()" ;
67  return sl;
68 }
69 
71 {
72  return QStringList();
73 }
74 
75 
76 int ARCSSensorLogger::cleanSlotConnect(QString sigName, QString /*sltName*/, QString objectName, QString actualSignal)
77 {
78  QString signature = objectName + "." + actualSignal + "." + sigName ;
79 
80  int id = -1;
81  if (map.contains(signature))
82  id = map[signature];
83  else
84  return -1;
85 
86  map.take(signature);
87  invertMap.take(id);
88 
89  return id;
90 }
91 
92 
93 int ARCSSensorLogger::cleanSignalConnect(QString /*sigName*/, QString /*sltName*/, QString /*objectName*/, QString /*actualSlot*/)
94 { return -1; }
95 
96 int ARCSSensorLogger::prepareSlotConnect(QString sigName, QString sltName, QString objectName, QString actualSignal, bool simulate)
97 {
98  if (simulate)
99  return 0;
100 
101  // if slot is part of something we know locally, then we won't connect it.
102  if (objectName.isEmpty())
103  return ARCSSensorLoggerBase::metaObject()->indexOfSlot(qPrintable(sltName));
104 
105  // first parse sigName
106  QString signature = objectName + "." + actualSignal + "." + sigName ;
107  LogDetails ld(signature, objectName, actualSignal.section("(",0,0));
108 
109  QStringList paramTypes = sigName.section("(",1,1).section(")", 0,0).split(",");
110  if (!paramTypes.at(0).isEmpty())
111  for (int i=0; i < paramTypes.count(); i++)
112  ld.addType(paramTypes.at(i), QMetaType::type(qPrintable(paramTypes.at(i))));
113 
114  map.insert(signature,idx);
115  invertMap.insert(idx,ld);
116  idx++;
117 
118  return idx-1 + metaObject()->methodCount();
119 }
120 
121 int ARCSSensorLogger::prepareSignalConnect(QString /*sigName*/, QString /*sltName*/, QString /*objectName*/, QString /*actualSlot*/, bool /*simulate*/)
122 {
123  // nothing to implement here.
124  return -1 ;
125 }
126 
127 int ARCSSensorLogger::qt_metacall(QMetaObject::Call call, int id, void ** arguments)
128 {
129  QMutexLocker lock(&mutex);
130  id = ARCSSensorLoggerBase::qt_metacall(call,id, arguments);
131  if (id == -1 || call != QMetaObject::InvokeMetaMethod)
132  return id;
133 
134 
135  if (!canLog())
136  {
137  std::cerr << "Log file not opened" << std::endl;
138  return -1;
139  }
140 
141  // the piece of code below should be used in something else if required.
142 
143  if (!invertMap.contains(id))
144  {
145  std::cerr << "Map does not contain id " << id << std::endl;
146  return -1;
147  }
148 
149  LogDetails ld = invertMap[id];
150 
151  QTime time = QTime::currentTime() ;
152 
153  logFile << qPrintable(time.toString()) <<"."
154  << qPrintable(QString::number(time.msec()).rightJustified(3,'0')) << " "
155  << qPrintable(ld.getObject()) << "." << qPrintable(ld.getSignal()) << "(" << std::endl;
156 
157  if (ld.count() <= 0)
158  {
159  logFile << ")" << std::endl ;
160  return -1;
161  }
162 
163 
164  for (int i =0; i < ld.count(); i++)
165  {
166  QString nt;
167  int it;
168  ld.getArgument(i, nt, it);
169 
170  logFile << "\t\t" << qPrintable(nt) << ": " ;
171  if (it != QMetaType::Void)
172  {
173  QVariant v(it, arguments[i+1]);
174  QString s = ARCSFactory::getInstance()->dataSerialize(v);
175  logFile << "\"" << qPrintable(s) << "\"" << std::endl;
176  }
177  else
178  logFile << qPrintable(QString::number((long)arguments[i+1],16)) << std::endl;
179  }
180  logFile << "\t)" << std::endl;
181 
182 
183  return -1;
184 }
185 
186 
187 
188 /**********************************************************************************
189  * inner class LogDetails of ARCSSensorLogger
190  **********************************************************************************/
191 
192 
193 ARCSSensorLogger::LogDetails::LogDetails(const LogDetails& ld)
194 {
195  object = ld.object;
196  signature = ld.signature;
197  signal = ld.signal;
198  typeNames = ld.typeNames;
199  typeIds = ld.typeIds;
200 }
201 
202 ARCSSensorLogger::LogDetails::LogDetails(QString sn, QString obj, QString s)
203 {
204  signature = sn ;
205  object = obj ;
206  signal = s;
207 }
208 
209 
virtual QStringList getSignalList()
static ARCSFactory * getInstance()
Returns the instance of the singleton ARCSFactory.
virtual int prepareSlotConnect(QString sigName, QString sltName, QString objectName=QString::null, QString actualSignal=QString::null, bool simulate=false)
Prepares a connection with a slot which is belonging to this object.
virtual int qt_metacall(QMetaObject::Call call, int id, void **arguments)
Method performing the actual callback task.
Base class for ARCSSensorLogger.
QString dataSerialize(QVariant var)
Serializes data from their QVariant counterpart.
virtual int prepareSignalConnect(QString sigName, QString sltName, QString objectName=QString::null, QString actualSlot=QString::null, bool simulate=false)
Prepares a connection with a slot which does not belong to this object.
virtual QStringList getSlotList()
virtual int cleanSignalConnect(QString sigName, QString sltName, QString objectName, QString actualSlot)
virtual int cleanSlotConnect(QString sigName, QString sltName, QString objectName, QString actualSignal)