OpenShot Library | libopenshot  0.1.1
ZmqLogger.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Source file for ZeroMQ-based Logger class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @section LICENSE
7  *
8  * Copyright (c) 2008-2014 OpenShot Studios, LLC
9  * <http://www.openshotstudios.com/>. This file is part of
10  * OpenShot Library (libopenshot), an open-source project dedicated to
11  * delivering high quality video editing and animation solutions to the
12  * world. For more information visit <http://www.openshot.org/>.
13  *
14  * OpenShot Library (libopenshot) is free software: you can redistribute it
15  * and/or modify it under the terms of the GNU Lesser General Public License
16  * as published by the Free Software Foundation, either version 3 of the
17  * License, or (at your option) any later version.
18  *
19  * OpenShot Library (libopenshot) is distributed in the hope that it will be
20  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 #include "../include/ZmqLogger.h"
29 
30 using namespace std;
31 using namespace openshot;
32 
33 
34 // Global reference to logger
35 ZmqLogger *ZmqLogger::m_pInstance = NULL;
36 
37 // Create or Get an instance of the logger singleton
38 ZmqLogger *ZmqLogger::Instance()
39 {
40  if (!m_pInstance) {
41  // Create the actual instance of logger only once
42  m_pInstance = new ZmqLogger;
43 
44  // init ZMQ variables
45  m_pInstance->context = NULL;
46  m_pInstance->publisher = NULL;
47  m_pInstance->connection = "";
48 
49  // Default connection
50  m_pInstance->Connection("tcp://*:5556");
51  }
52 
53  return m_pInstance;
54 }
55 
56 // Set the connection for this logger
57 void ZmqLogger::Connection(string new_connection)
58 {
59  // Create a scoped lock, allowing only a single thread to run the following code at one time
60  const GenericScopedLock<CriticalSection> lock(loggerCriticalSection);
61 
62  // Does anything need to happen?
63  if (new_connection == connection)
64  return;
65  else
66  // Set new connection
67  connection = new_connection;
68 
69  if (context == NULL) {
70  // Create ZMQ Context
71  context = new zmq::context_t(1);
72  }
73 
74  if (publisher != NULL) {
75  // Close an existing bound publisher socket
76  publisher->close();
77  publisher = NULL;
78  }
79 
80  // Create new publisher instance
81  publisher = new zmq::socket_t(*context, ZMQ_PUB);
82 
83  // Bind to the socket
84  try {
85  publisher->bind(connection.c_str());
86 
87  } catch (zmq::error_t &e) {
88  cout << "ZmqLogger::Connection - Error binding to " << connection << ". Switching to an available port." << endl;
89  connection = "tcp://*:*";
90  publisher->bind(connection.c_str());
91  }
92 
93  // Sleeping to allow connection to wake up (0.25 seconds)
94  usleep(250000);
95 }
96 
97 void ZmqLogger::Log(string message)
98 {
99  // Create a scoped lock, allowing only a single thread to run the following code at one time
100  const GenericScopedLock<CriticalSection> lock(loggerCriticalSection);
101 
102  // Send example message
103  zmq::message_t reply (message.length());
104  memcpy (reply.data(), message.c_str(), message.length());
105  publisher->send(reply);
106 }
This abstract class is the base class, used by all readers in libopenshot.
Definition: ZmqLogger.h:52