Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework logs #16

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions logs/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion logs/README.md

This file was deleted.

44 changes: 8 additions & 36 deletions mirror-logging/include/mirror/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@
#include <string>
#include <thread>

// Library Includes
#include <zmq.hpp>

namespace mirror {

/**
* Context for the connection to the log server. Defined in `logger.cpp`
*/
extern zmq::context_t socketContext;

/**
* Singleton class to be used when logging in projects associated with the Clarkson Open Source Institute's Mirror
*/
Expand Down Expand Up @@ -92,41 +85,30 @@ namespace mirror {

// Configuration
/**
* Configures the connection to the log server
*
* **Must be called before any log messages can be sent**
*
* @param port Port that the log server is running on
* Must be called before any log messages can be sent.
* @param componentName Name of the component that the logger is being used in
* @param address IP address of the machine that the log server is running on
*/
void configure(uint16_t port, const std::string &componentName, const std::string &address = "localhost");
void configure(const std::string &componentName);

/**
* Shuts down the socket and destroys the Logger.
* Destroys the Logger.
*/
inline void close() { Logger::getInstance()->~Logger(); }



protected: // Functions
/**
* Default constructor.
*/
inline Logger() : m_Configured(false) {}

// Destructor
/**
* Destructor for the Logger class. Destroys the socket and context when called.
* Destructor for the Logger class.
*/
inline ~Logger() { m_LogServerSocket.close(); socketContext.shutdown(); }
inline ~Logger() {}

private: // Functions
/**
* Sends a line of text to the log server
*
* @param lineToSend Message to send to the log server
*/
void f_SendLine(const std::string &lineToSend);

/**
* Prints a log event to stdout.
* @param level_tag Level tag ( ex. [ DEBUG ] ) to prepend message with
Expand All @@ -145,16 +127,6 @@ namespace mirror {
*/
static std::mutex s_AccessMutex;

/**
* Socket used to communicate with the log server
*/
zmq::socket_t m_LogServerSocket{socketContext, zmq::socket_type::stream};

/**
* URL of the log server
*/
std::string m_URL;

/**
* False until `configure()` is run.
*
Expand All @@ -163,7 +135,7 @@ namespace mirror {
bool m_Configured;

/**
* Name of the component this class is instantiated in. Sent to the log server with each log message
* Name of the component this class is instantiated in. Included in log messages
*/
std::string m_ComponentName;

Expand Down
52 changes: 3 additions & 49 deletions mirror-logging/java/Log.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;

Expand All @@ -18,11 +15,7 @@ public class Log {
private static final String LOG_FATAL = " \u001B[31m[ FATAL ]\u001B[0m ";

private static Log instance = null;
private String log_host = "";
private int log_port = 0;
private String component_name = "";
Socket socket;
OutputStream clientOut;
boolean configured;

/**
Expand All @@ -45,19 +38,8 @@ public static synchronized Log getInstance() {
* @param log_port Log server port (ex. 4001)
*/
public synchronized void configure(String log_host, int log_port, String component_name) {
try {
this.log_host = log_host;
this.log_port = log_port;
this.component_name = component_name;
socket = new Socket(log_host, log_port);
clientOut = socket.getOutputStream();
configured = true;
printToConsole(Levels.INFO, "Logger - Connected to log server.");
} catch(IOException e) {
configured = false;
printToConsole(Levels.ERROR, "Logger - Could not connect to log server.");
printToConsole(Levels.WARN, "Logger - Running in offline mode.");
}
this.component_name = component_name;
configured = true;
}

/**
Expand All @@ -70,7 +52,7 @@ public synchronized void configure(String log_host, int log_port, String compone
* Debug log events are not sent to the log server.
* @param message Message to log
*/
public void debug(String message) { printToConsole(Levels.DEBUG, message); }
public void debug(String message) { print(Levels.DEBUG, message); }

/**
* Logs an event with severity "INFO".
Expand Down Expand Up @@ -102,34 +84,6 @@ public synchronized void configure(String log_host, int log_port, String compone
* @param message Message to log
*/
private synchronized void print(Levels level, String message) {
printToConsole(level, message);
if(configured) {
StringBuilder sb = new StringBuilder();
sb.append("@").append(component_name).append("@");
sb.append(level.ordinal()).append(message).append("\n");
try {
clientOut.write(sb.toString().getBytes(StandardCharsets.UTF_8));
clientOut.flush();
} catch (IOException e) {
printToConsole(Levels.INFO, "Logger - Reconnecting to log server...");
try {
socket = new Socket(log_host, log_port);
clientOut = socket.getOutputStream();
clientOut.write(sb.toString().getBytes(StandardCharsets.UTF_8));
clientOut.flush();
} catch(IOException f) {
printToConsole(Levels.ERROR, "Logger - Could not reconnect to log server.");
}
}
}
}

/**
* Prints a log event to the console.
* @param level Severity of the log event
* @param message Message to log
*/
private void printToConsole(Levels level, String message) {
StringBuilder sb = new StringBuilder();
sb.append(LOG_DATE_FORMAT.format(new Date()));
switch(level.ordinal()) {
Expand Down
26 changes: 1 addition & 25 deletions mirror-logging/src/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@
#include <iomanip>
#include <iostream>

// Library includes
#include <zmq.hpp>

namespace mirror {

zmq::context_t socketContext(1, 1);

// Static Member Initializations
Logger *Logger::s_Instance = nullptr;
std::mutex Logger::s_AccessMutex;
Expand Down Expand Up @@ -62,13 +57,9 @@ namespace mirror {
printEvent(" \u001B[31m[ FATAL ]\u001B[0m ", logMessage);
}

void Logger::configure(uint16_t port, const std::string &componentName, const std::string &address) {
void Logger::configure(const std::string &componentName) {
std::lock_guard<std::mutex> instanceLock(s_AccessMutex);

m_URL = "tcp://" + address + ":" + std::to_string(port);
m_LogServerSocket.connect(m_URL);


m_ComponentName = componentName;
m_Configured = true;
}
Expand All @@ -77,21 +68,6 @@ namespace mirror {
* Start Of Private Functions
*/

void Logger::f_SendLine(const std::string &lineToSend) {
std::lock_guard<std::mutex> instanceGuard(s_AccessMutex);

// Throws exception if logger is not configured. Should cause the program to end
if (!m_Configured)
throw std::logic_error("Logger not configured");

// Routing ID required for sending TCP messages, not part of the packet by default
std::string routingID = m_LogServerSocket.get(zmq::sockopt::routing_id);
m_LogServerSocket.send(zmq::message_t(routingID), zmq::send_flags::sndmore);

zmq::message_t message{"@" + m_ComponentName + lineToSend + "\n"};
m_LogServerSocket.send(message, zmq::send_flags::none);
}

void Logger::printEvent(const char* level_tag, const std::string &message) {
std::time_t now = std::time(nullptr);
auto time = *std::localtime(&now);
Expand Down
62 changes: 6 additions & 56 deletions mirror-map/src/main/java/mirrormap/log/Log.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
package mirrormap.log;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;

Expand All @@ -20,11 +15,7 @@ public class Log {
private static final String LOG_FATAL = " \u001B[31m[ FATAL ]\u001B[0m ";

private static Log instance = null;
private String log_host = "";
private int log_port = 0;
private String component_name = "";
Socket socket;
OutputStream clientOut;
boolean configured;

/**
Expand All @@ -43,23 +34,11 @@ public static synchronized Log getInstance() {

/**
* Configures and connects this instance of Log to the log server
* @param log_host Log server hostname (ex. "mirrorlog" or "localhost")
* @param log_port Log server port (ex. 4001)
* @param component_name This component's name
*/
public synchronized void configure(String log_host, int log_port, String component_name) {
try {
this.log_host = log_host;
this.log_port = log_port;
this.component_name = component_name;
socket = new Socket(log_host, log_port);
clientOut = socket.getOutputStream();
configured = true;
printToConsole(Levels.INFO, "Logger - Connected to log server.");
} catch(IOException e) {
configured = false;
printToConsole(Levels.ERROR, "Logger - Could not connect to log server.");
printToConsole(Levels.WARN, "Logger - Running in offline mode.");
}
public synchronized void configure(String component_name) {
this.component_name = component_name;
configured = true;
}

/**
Expand All @@ -69,10 +48,9 @@ public synchronized void configure(String log_host, int log_port, String compone

/**
* Logs an event with severity "DEBUG".
* Debug log events are not sent to the log server.
* @param message Message to log
*/
public void debug(String message) { printToConsole(Levels.DEBUG, message); }
public void debug(String message) { print(Levels.DEBUG, message); }

/**
* Logs an event with severity "INFO".
Expand Down Expand Up @@ -104,34 +82,6 @@ public synchronized void configure(String log_host, int log_port, String compone
* @param message Message to log
*/
private synchronized void print(Levels level, String message) {
printToConsole(level, message);
if(configured) {
StringBuilder sb = new StringBuilder();
sb.append("@").append(component_name).append("@");
sb.append(level.ordinal()).append(message).append("\n");
try {
clientOut.write(sb.toString().getBytes(StandardCharsets.UTF_8));
clientOut.flush();
} catch (IOException e) {
printToConsole(Levels.INFO, "Logger - Reconnecting to log server...");
try {
socket = new Socket(log_host, log_port);
clientOut = socket.getOutputStream();
clientOut.write(sb.toString().getBytes(StandardCharsets.UTF_8));
clientOut.flush();
} catch(IOException f) {
printToConsole(Levels.ERROR, "Logger - Could not reconnect to log server.");
}
}
}
}

/**
* Prints a log event to the console.
* @param level Severity of the log event
* @param message Message to log
*/
private void printToConsole(Levels level, String message) {
StringBuilder sb = new StringBuilder();
sb.append(LOG_DATE_FORMAT.format(new Date()));
switch(level.ordinal()) {
Expand All @@ -146,4 +96,4 @@ private void printToConsole(Levels level, String message) {
sb.append(": ").append(message);
System.out.println(sb);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class MirrorMapApplication {
public static void main(String[] args){
// Configure logging
Log log = Log.getInstance();
log.configure("mirrorlog", 4001, "Map");
log.configure("Map");

try (ZContext context = new ZContext()) {
// Start GeoIP database updater
Expand Down
2 changes: 0 additions & 2 deletions mirrorlog/.gitattributes

This file was deleted.

5 changes: 0 additions & 5 deletions mirrorlog/.gitignore

This file was deleted.

14 changes: 0 additions & 14 deletions mirrorlog/Dockerfile

This file was deleted.

21 changes: 0 additions & 21 deletions mirrorlog/LICENSE

This file was deleted.

Loading