arcsgenerallogger.cpp
1 /*
2  name: lib/arcsgenerallogger.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/arcsgenerallogger.h>
30 
31 
32 #include <arcs/arcsfactory.h>
33 #include <iostream>
34 
35 
36 void ARCSGeneralLoggerBase::setFilename(QString s)
37 {
38  if (logFile.is_open())
39  logFile.close();
40 
41  logFile.open(qPrintable(s));
42 }
43 
44 
45 bool ARCSGeneralLoggerBase::canLog()
46 {
47  if (!logFile.is_open())
48  logFile.open("arcs_log.log");
49 
50  return logFile.is_open();
51 }
52 
53 
54 /************************************************************
55  *
56  ***********************************************************/
57 
58 ARCSGeneralLogger::ARCSGeneralLogger(QObject* parent) : ARCSGeneralLoggerBase(parent)
59 {
60  idx = 0;
61 }
62 
63 
65 {
66  QStringList sl ;
67  sl << "log()" ;
68  return sl;
69 }
70 
72 {
73  return QStringList();
74 }
75 
76 
77 int ARCSGeneralLogger::cleanSlotConnect(QString sigName, QString /*sltName*/, QString objectName, QString actualSignal)
78 {
79  QString signature = objectName + "." + actualSignal + "." + sigName ;
80 
81  int id = -1;
82  if (map.contains(signature))
83  id = map[signature];
84  else
85  return -1;
86 
87  map.take(signature);
88  invertMap.take(id);
89 
90  return id;
91 }
92 
93 
94 int ARCSGeneralLogger::cleanSignalConnect(QString /*sigName*/, QString /*sltName*/, QString /*objectName*/, QString /*actualSlot*/)
95 { return -1; }
96 
97 int ARCSGeneralLogger::prepareSlotConnect(QString sigName, QString sltName, QString objectName, QString actualSignal, bool simulate)
98 {
99  if (simulate)
100  return 0;
101 
102  if (metaObject()->indexOfSlot(qPrintable(sltName)) != -1)
103  {
104  return metaObject()->indexOfSlot(qPrintable(sltName));
105  }
106 
107  // first parse sigName
108  QString signature = objectName + "." + actualSignal + "." + sigName ;
109  LogDetails ld(signature);
110 
111  QStringList paramTypes = sigName.section("(",1,1).section(")", 0,0).split(",");
112 
113  for (int i=0; i < paramTypes.count(); i++)
114  ld.addType(QMetaType::type(qPrintable(paramTypes.at(i))));
115 
116  map.insert(signature,idx);
117  invertMap.insert(idx,ld);
118  idx++;
119 
120  return idx-1 + metaObject()->methodCount();
121 }
122 
123 int ARCSGeneralLogger::prepareSignalConnect(QString /*sigName*/, QString /*sltName*/, QString /*objectName*/, QString /*actualSlot*/, bool /*simulate*/)
124 {
125  // nothing to implement here.
126  return -1 ;
127 }
128 
129 int ARCSGeneralLogger::qt_metacall(QMetaObject::Call call, int id, void ** arguments)
130 {
131  QMutexLocker lock(&mutex);
132  id = ARCSGeneralLoggerBase::qt_metacall(call,id, arguments);
133  if (id < 0 || call != QMetaObject::InvokeMetaMethod)
134  return id;
135 
136 
137  if (!canLog())
138  {
139  std::cerr << "Log file not opened" << std::endl;
140  return -1;
141  }
142 
143  // the piece of code below should be used in something else if required.
144 
145  if (!invertMap.contains(id))
146  {
147  std::cerr << "Map does not contain id " << id << std::endl;
148  return -1;
149  }
150 
151  LogDetails ld = invertMap[id];
152 
153 
154  for (int i =0; i < ld.count(); i++)
155  {
156  int it = ld.getArgument(i);
157 
158  if (it != QMetaType::Void)
159  {
160  QVariant v(it, arguments[i+1]);
161  QString s = ARCSFactory::getInstance()->dataSerialize(v);
162  logFile << qPrintable(s) << qPrintable(separator) ;
163  }
164  }
165 
166  return -1;
167 }
168 
169 
170 
171 /**********************************************************************************
172  * inner class LogDetails of ARCSSensorLogger
173  **********************************************************************************/
174 
175 
176 ARCSGeneralLogger::LogDetails::LogDetails(const LogDetails& ld)
177 {
178  signature = ld.signature;
179  typeIds = ld.typeIds;
180 }
181 
182 ARCSGeneralLogger::LogDetails::LogDetails(QString sn)
183 {
184  signature = sn ;
185 }
186 
187 
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 QStringList getSignalList()
virtual int cleanSlotConnect(QString sigName, QString sltName, QString objectName, QString actualSignal)
virtual int qt_metacall(QMetaObject::Call call, int id, void **arguments)
Method performing the actual callback task.
static ARCSFactory * getInstance()
Returns the instance of the singleton ARCSFactory.
virtual QStringList getSlotList()
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 int cleanSignalConnect(QString sigName, QString sltName, QString objectName, QString actualSlot)
Base class for ARCSGeneralLogger.