23 #include "log_thread.h" 26 #include <blackboard/blackboard.h> 27 #include <logging/logger.h> 28 #include <core/exceptions/system.h> 29 #include <interfaces/SwitchInterface.h> 38 # include <sys/endian.h> 39 #elif defined(__MACH__) && defined(__APPLE__) 40 # include <sys/_endian.h> 44 #include <arpa/inet.h> 78 const char *logdir,
bool buffering,
bool flushing,
80 :
Thread(
"BBLoggerThread",
Thread::OPMODE_WAITFORWAKEUP),
84 set_name(
"BBLoggerThread(%s)", iface_uid);
86 __buffering = buffering;
87 __flushing = flushing;
88 __uid = strdup(iface_uid);
89 __logdir = strdup(logdir);
90 __scenario = strdup(scenario);
91 __start =
new Time(start_time);
93 __queue_mutex =
new Mutex();
101 Interface::parse_uid(__uid, __type, __id);
105 struct tm *tmp = localtime(&(now.
get_timeval()->tv_sec));
106 strftime(date, 21,
"%F-%H-%M-%S", tmp);
108 if (asprintf(&__filename,
"%s/%s-%s-%s-%s.log", LOGDIR, __scenario,
109 __type.c_str(), __id.c_str(), date) == -1) {
122 delete __queue_mutex;
134 __queue_mutex =
new Mutex();
138 __num_data_items = 0;
143 mode_t m = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
144 int fd = open(__filename, O_RDWR | O_CREAT | O_EXCL, m);
148 __f_data = fdopen(fd,
"w+");
176 __switch_if->
write();
190 __is_master ?
" as master" :
"");
203 for (
unsigned int q = 0; q < 2; ++q) {
204 while (!__queues[q].empty()) {
205 void *t = __queues[q].front();
232 if (enabled && !__enabled) {
234 (__num_data_items - __session_start));
235 __session_start = __num_data_items;
236 }
else if (!enabled && __enabled) {
238 (__num_data_items - __session_start));
257 __threads = thread_list;
261 BBLoggerThread::write_header()
264 memset(&header, 0,
sizeof(header));
265 header.
file_magic = htonl(BBLOGGER_FILE_MAGIC);
267 #if __BYTE_ORDER == __BIG_ENDIAN 273 strncpy(header.
scenario, (
const char *)__scenario, BBLOG_SCENARIO_SIZE);
275 strncpy(header.
interface_id, __iface->
id(), BBLOG_INTERFACE_ID_SIZE);
278 long start_time_sec, start_time_usec;
282 if (fwrite(&header,
sizeof(header), 1, __f_data) != 1) {
290 BBLoggerThread::update_header()
293 #if _POSIX_MAPPED_FILES 295 fileno(__f_data), 0);
296 if (h == MAP_FAILED) {
298 "not updating number of data items",
307 "not updating number of data items on close");
312 BBLoggerThread::write_chunk(
const void *chunk)
316 Time d = *__now - *__start;
317 long rel_time_sec, rel_time_usec;
321 if ( (fwrite(&ehead,
sizeof(ehead), 1, __f_data) == 1) &&
322 (fwrite(chunk, __data_size, 1, __f_data) == 1) ) {
323 if (__flushing) fflush(__f_data);
324 __num_data_items += 1;
334 unsigned int write_queue = __act_queue;
335 __queue_mutex->
lock();
336 __act_queue = 1 - __act_queue;
340 while (! queue.empty() ) {
341 void *c = queue.front();
356 if ((enm = dynamic_cast<SwitchInterface::EnableSwitchMessage *>(message)) != NULL) {
358 }
else if ((dism = dynamic_cast<SwitchInterface::DisableSwitchMessage *>(message)) != NULL) {
362 message->type(), interface->uid());
365 for (ThreadList::iterator i = __threads.begin(); i != __threads.end(); ++i) {
371 __switch_if->
write();
380 if (!__enabled)
return;
386 void *c = malloc(__iface->
datasize());
388 __queue_mutex->
lock();
393 __queue_mutex->
lock();
406 unsigned int instance_serial)
throw()
408 __session_start = __num_data_items;
413 unsigned int instance_serial)
throw()
416 (__num_data_items - __session_start));
File could not be opened.
virtual bool bb_interface_message_received(fawkes::Interface *interface, fawkes::Message *message)
BlackBoard message received notification.
void clear()
Clear the queue.
unsigned int datasize() const
Get data size.
Base class for all messages passed through interfaces in Fawkes BlackBoard.
const timeval * get_timeval() const
Obtain the timeval where the time is stored.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
void get_timestamp(long &sec, long &usec) const
Get time stamp.
const char * get_filename() const
Get filename.
Fawkes library namespace.
void unlock()
Unlock the mutex.
void bbil_add_writer_interface(Interface *interface)
Add an interface to the writer addition/removal watch list.
const char * id() const
Get identifier of interface.
virtual void finalize()
Finalize the thread.
A class for handling time.
virtual void init()
Initialize the thread.
virtual void unregister_listener(BlackBoardInterfaceListener *listener)
Unregister BB interface listener.
Thread class encapsulation of pthreads.
void write()
Write from local copy into BlackBoard memory.
Base class for all Fawkes BlackBoard interfaces.
virtual ~BBLoggerThread()
Destructor.
BlackBoard logger thread.
Logger * logger
This is the Logger member used to access the logger.
virtual void bb_interface_writer_added(fawkes::Interface *interface, unsigned int instance_serial)
A writing instance has been opened for a watched interface.
Clock * clock
By means of this member access to the clock is given.
const unsigned char * hash() const
Get interface hash.
virtual void bb_interface_data_changed(fawkes::Interface *interface)
BlackBoard data changed notification.
virtual void register_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Register BB event listener.
SwitchInterface Fawkes BlackBoard Interface.
void wakeup()
Wake up thread.
const char * type() const
Get type of interface.
void set_name(const char *format,...)
Set name of thread.
Base class for exceptions in Fawkes.
void read()
Read from BlackBoard into local copy.
virtual void bb_interface_writer_removed(fawkes::Interface *interface, unsigned int instance_serial)
A writing instance has been closed for a watched interface.
void set_enabled(const bool new_enabled)
Set enabled value.
DisableSwitchMessage Fawkes BlackBoard Interface Message.
const char * name() const
Get name of thread.
const char * uid() const
Get unique identifier of interface.
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
void set_coalesce_wakeups(bool coalesce=true)
Set wakeup coalescing.
void set_enabled(bool enabled)
Enable or disable logging.
virtual void loop()
Code to execute in the thread.
BBLoggerThread(const char *iface_uid, const char *logdir, bool buffering, bool flushing, const char *scenario, fawkes::Time *start_time)
Constructor.
void set_threadlist(fawkes::ThreadList &thread_list)
Set threadlist and master status.
EnableSwitchMessage Fawkes BlackBoard Interface Message.
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
void push_locked(const Type &x)
Push element to queue with lock protection.
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
void lock()
Lock this mutex.
Time & stamp()
Set this time to the current time.
const void * datachunk() const
Get data chunk.
Mutex mutual exclusion lock.
void bbil_add_message_interface(Interface *interface)
Add an interface to the message received watch list.
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for writing.
System ran out of memory and desired operation could not be fulfilled.
BlackBoard interface listener.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
void bbil_add_data_interface(Interface *interface)
Add an interface to the data modification watch list.
virtual void close(Interface *interface)=0
Close interface.