Skip to content

Commit

Permalink
ZeroMQ's std::string support is too new
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdnyc committed Mar 25, 2020
1 parent 4fad197 commit 54f5fea
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions src/ZmqLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ ZmqLogger *ZmqLogger::Instance()
}

// Set the connection for this logger
void ZmqLogger::Connection(string new_connection)
void ZmqLogger::Connection(std::string new_connection)
{
// Create a scoped lock, allowing only a single thread to run the following code at one time
const GenericScopedLock<CriticalSection> lock(loggerCriticalSection);
Expand Down Expand Up @@ -102,7 +102,7 @@ void ZmqLogger::Connection(string new_connection)
publisher->bind(connection.c_str());

} catch (zmq::error_t &e) {
cout << "ZmqLogger::Connection - Error binding to " << connection << ". Switching to an available port." << endl;
std::cout << "ZmqLogger::Connection - Error binding to " << connection << ". Switching to an available port." << std::endl;
connection = "tcp://*:*";
publisher->bind(connection.c_str());
}
Expand All @@ -111,7 +111,7 @@ void ZmqLogger::Connection(string new_connection)
usleep(250000);
}

void ZmqLogger::Log(string message)
void ZmqLogger::Log(std::string message)
{
if (!enabled)
// Don't do anything
Expand All @@ -120,15 +120,14 @@ void ZmqLogger::Log(string message)
// Create a scoped lock, allowing only a single thread to run the following code at one time
const GenericScopedLock<CriticalSection> lock(loggerCriticalSection);

#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 3, 1)
// Send message over socket (ZeroMQ)
zmq::message_t reply(message);
zmq::message_t reply (message.length());
std::memcpy (reply.data(), message.c_str(), message.length());

#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 3, 1)
// Set flags for immediate delivery (new API)
publisher->send(reply, zmq::send_flags::dontwait);
#else
zmq::message_t reply (message.length());
memcpy (reply.data(), message.c_str(), message.length());
publisher->send(reply);
#endif

Expand All @@ -137,14 +136,14 @@ void ZmqLogger::Log(string message)
}

// Log message to a file (if path set)
void ZmqLogger::LogToFile(string message)
void ZmqLogger::LogToFile(std::string message)
{
// Write to log file (if opened, and force it to write to disk in case of a crash)
if (log_file.is_open())
log_file << message << std::flush;
}

void ZmqLogger::Path(string new_path)
void ZmqLogger::Path(std::string new_path)
{
// Update path
file_path = new_path;
Expand All @@ -154,14 +153,14 @@ void ZmqLogger::Path(string new_path)
log_file.close();

// Open file (write + append)
log_file.open (file_path.c_str(), ios::out | ios::app);
log_file.open (file_path.c_str(), std::ios::out | std::ios::app);

// Get current time and log first message
time_t now = time(0);
tm* localtm = localtime(&now);
log_file << "------------------------------------------" << endl;
log_file << "libopenshot logging: " << asctime(localtm);
log_file << "------------------------------------------" << endl;
std::time_t now = std::time(0);
std::tm* localtm = std::localtime(&now);
log_file << "------------------------------------------" << std::endl;
log_file << "libopenshot logging: " << std::asctime(localtm);
log_file << "------------------------------------------" << std::endl;
}

void ZmqLogger::Close()
Expand All @@ -182,13 +181,13 @@ void ZmqLogger::Close()
}

// Append debug information
void ZmqLogger::AppendDebugMethod(string method_name,
string arg1_name, float arg1_value,
string arg2_name, float arg2_value,
string arg3_name, float arg3_value,
string arg4_name, float arg4_value,
string arg5_name, float arg5_value,
string arg6_name, float arg6_value)
void ZmqLogger::AppendDebugMethod(std::string method_name,
std::string arg1_name, float arg1_value,
std::string arg2_name, float arg2_value,
std::string arg3_name, float arg3_value,
std::string arg4_name, float arg4_value,
std::string arg5_name, float arg5_value,
std::string arg6_name, float arg6_value)
{
if (!enabled)
// Don't do anything
Expand All @@ -198,8 +197,8 @@ void ZmqLogger::AppendDebugMethod(string method_name,
// Create a scoped lock, allowing only a single thread to run the following code at one time
const GenericScopedLock<CriticalSection> lock(loggerCriticalSection);

stringstream message;
message << fixed << setprecision(4);
std::stringstream message;
message << std::fixed << std::setprecision(4);
message << method_name << " (";

// Add attributes to method JSON
Expand Down

0 comments on commit 54f5fea

Please sign in to comment.