Skip to content
Open
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 3.15)
project(distributed-tracing VERSION 1.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(BUILD_DIRECTORY build)
Expand Down
69 changes: 69 additions & 0 deletions LamportClock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <iostream>
#include <fstream>
#include <mutex>
#include <string>
#include <stdexcept>
#include <algorithm>

class LamportClock {
private:
unsigned long timestamp;
std::mutex mtx;

public:
LamportClock() : timestamp(0) {}

unsigned long tick() {
std::lock_guard<std::mutex> lock(mtx);
timestamp++;
return timestamp;
}

unsigned long update(unsigned long received) {
std::lock_guard<std::mutex> lock(mtx);
timestamp = std::max(timestamp, received) + 1;
return timestamp;
}


unsigned long get_time() {
std::lock_guard<std::mutex> lock(mtx);
return timestamp;
}
};

// Logger: Logs messages with Lamport Logical Clock timestamps
class Logger {
private:
std::ofstream log_file;
LamportClock clock;
std::mutex log_mtx;

public:
Logger(const std::string &filename) {
log_file.open(filename, std::ios::out | std::ios::app);
if (!log_file.is_open()) {
throw std::runtime_error("Cannot open log file: " + filename);
}
}

~Logger() {
if (log_file.is_open()) {
log_file.close();
}
}

// Logs an internal event. The logical clock is incremented.
void log(const std::string &node_id, const std::string &source_filename, int line_number, const std::string &message) {
unsigned long ts = clock.tick();
std::lock_guard<std::mutex> lock(log_mtx);
log_file << ts << " " << node_id << " " << source_filename << " " << line_number << " " << message << std::endl;
}

// Logs an external event that carries a Lamport timestamp.
void log_received(const std::string &node_id, const std::string &source_filename, int line_number, const std::string &message, unsigned long received_timestamp) {
unsigned long ts = clock.update(received_timestamp);
std::lock_guard<std::mutex> lock(log_mtx);
log_file << ts << " " << node_id << " " << source_filename << " " << line_number << " " << message << std::endl;
}
};