|
| 1 | +/* |
| 2 | + * WhatPulse External PCap Service - Logger Implementation |
| 3 | + * |
| 4 | + * Copyright (c) 2025 WhatPulse. All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under CC BY-NC 4.0 with additional terms. |
| 7 | + * See LICENSE file for complete terms and conditions. |
| 8 | + * |
| 9 | + * NOTICE: This software integrates with WhatPulse services. Reverse engineering |
| 10 | + * the communication protocol or tampering with data transmission is prohibited. |
| 11 | + * |
| 12 | + * For licensing questions: [email protected] |
| 13 | + */ |
| 14 | + |
| 15 | +#include "logger.h" |
| 16 | +#include <iostream> |
| 17 | +#include <iomanip> |
| 18 | +#include <ctime> |
| 19 | + |
| 20 | +Logger &Logger::getInstance() |
| 21 | +{ |
| 22 | + static Logger instance; |
| 23 | + return instance; |
| 24 | +} |
| 25 | + |
| 26 | +void Logger::initialize(const std::string &logFile, LogLevel level, bool verbose) |
| 27 | +{ |
| 28 | + m_level = level; |
| 29 | + m_verbose = verbose; |
| 30 | + |
| 31 | + // Set performance logging intervals based on verbose mode |
| 32 | + if (verbose) |
| 33 | + { |
| 34 | + m_performanceInterval = m_verbosePerformanceInterval; |
| 35 | + } |
| 36 | + |
| 37 | + if (!logFile.empty()) |
| 38 | + { |
| 39 | + m_logFile.close(); |
| 40 | + m_logFile.open(logFile, std::ios::app); |
| 41 | + if (!m_logFile.is_open()) |
| 42 | + { |
| 43 | + std::cerr << "Failed to open log file: " << logFile << std::endl; |
| 44 | + return; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Initial log entry |
| 49 | + std::stringstream ss; |
| 50 | + ss << "Logger initialized - Level: " << levelToString(level) |
| 51 | + << ", Verbose: " << (verbose ? "Yes" : "No") |
| 52 | + << ", Performance reports every: " << m_performanceInterval.count() << " seconds"; |
| 53 | + log(LogLevel::INFO, ss.str()); |
| 54 | +} |
| 55 | + |
| 56 | +void Logger::log(LogLevel level, const std::string &message) |
| 57 | +{ |
| 58 | + if (level < m_level) |
| 59 | + { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + std::string logEntry = getCurrentTimestamp() + " [" + levelToString(level) + "] " + message; |
| 64 | + |
| 65 | + bool isOpen = m_logFile.is_open(); |
| 66 | + if (isOpen) |
| 67 | + { |
| 68 | + m_logFile << logEntry << std::endl; |
| 69 | + m_logFile.flush(); |
| 70 | + } |
| 71 | + if(!isOpen || m_verbose) |
| 72 | + { |
| 73 | + // Fallback to console if file logging fails |
| 74 | + if (level >= LogLevel::ERROR) |
| 75 | + { |
| 76 | + std::cerr << logEntry << std::endl; |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + std::cout << logEntry << std::endl; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +void Logger::logPerformance(const std::string &interfaceName, uint64_t packetCount, uint64_t totalBytes, |
| 86 | + double seconds, size_t queueSize, bool isConnected) |
| 87 | +{ |
| 88 | + if (!shouldLogPerformance()) |
| 89 | + { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + double mbps = (totalBytes * 8.0) / (seconds * 1024 * 1024); |
| 94 | + double pps = packetCount / seconds; |
| 95 | + |
| 96 | + std::stringstream ss; |
| 97 | + ss << "PERF [" << interfaceName << "] " |
| 98 | + << "Packets: " << packetCount << " (" << std::fixed << std::setprecision(1) << pps << " pps), " |
| 99 | + << "Rate: " << std::setprecision(2) << mbps << " Mbps, " |
| 100 | + << "Queue: " << queueSize << " packets, " |
| 101 | + << "TCP: " << (isConnected ? "Connected" : "Disconnected"); |
| 102 | + |
| 103 | + log(LogLevel::INFO, ss.str()); |
| 104 | +} |
| 105 | + |
| 106 | +void Logger::logConnectionAttempt(const std::string &host, uint16_t port) |
| 107 | +{ |
| 108 | + std::stringstream ss; |
| 109 | + ss << "TCP: Attempting to connect to " << host << ":" << port; |
| 110 | + log(LogLevel::INFO, ss.str()); |
| 111 | +} |
| 112 | + |
| 113 | +void Logger::logConnectionSuccess(const std::string &host, uint16_t port) |
| 114 | +{ |
| 115 | + std::stringstream ss; |
| 116 | + ss << "TCP: Successfully connected to " << host << ":" << port; |
| 117 | + log(LogLevel::INFO, ss.str()); |
| 118 | +} |
| 119 | + |
| 120 | +void Logger::logConnectionFailed(const std::string &host, uint16_t port, const std::string &error) |
| 121 | +{ |
| 122 | + std::stringstream ss; |
| 123 | + ss << "TCP: Failed to connect to " << host << ":" << port << " - " << error; |
| 124 | + log(LogLevel::WARNING, ss.str()); |
| 125 | +} |
| 126 | + |
| 127 | +void Logger::logDisconnected(const std::string &host, uint16_t port) |
| 128 | +{ |
| 129 | + std::stringstream ss; |
| 130 | + ss << "TCP: Disconnected from " << host << ":" << port; |
| 131 | + log(LogLevel::INFO, ss.str()); |
| 132 | +} |
| 133 | + |
| 134 | +void Logger::logSendError(const std::string &error) |
| 135 | +{ |
| 136 | + std::stringstream ss; |
| 137 | + ss << "TCP: Send failed - " << error; |
| 138 | + log(LogLevel::WARNING, ss.str()); |
| 139 | +} |
| 140 | + |
| 141 | +std::string Logger::getCurrentTimestamp() |
| 142 | +{ |
| 143 | + auto now = std::chrono::system_clock::now(); |
| 144 | + auto time_t = std::chrono::system_clock::to_time_t(now); |
| 145 | + auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 146 | + now.time_since_epoch()) % |
| 147 | + 1000; |
| 148 | + |
| 149 | + std::stringstream ss; |
| 150 | + ss << std::put_time(std::localtime(&time_t), "%Y-%m-%d %H:%M:%S"); |
| 151 | + ss << "." << std::setfill('0') << std::setw(3) << milliseconds.count(); |
| 152 | + |
| 153 | + return ss.str(); |
| 154 | +} |
| 155 | + |
| 156 | +std::string Logger::levelToString(LogLevel level) |
| 157 | +{ |
| 158 | + switch (level) |
| 159 | + { |
| 160 | + case LogLevel::DEBUG: |
| 161 | + return "DEBUG"; |
| 162 | + case LogLevel::INFO: |
| 163 | + return "INFO"; |
| 164 | + case LogLevel::WARNING: |
| 165 | + return "WARN"; |
| 166 | + case LogLevel::ERROR: |
| 167 | + return "ERROR"; |
| 168 | + default: |
| 169 | + return "UNKNOWN"; |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +bool Logger::shouldLogPerformance() |
| 174 | +{ |
| 175 | + auto now = std::chrono::steady_clock::now(); |
| 176 | + if (now - m_lastPerformanceLog >= m_performanceInterval) |
| 177 | + { |
| 178 | + m_lastPerformanceLog = now; |
| 179 | + return true; |
| 180 | + } |
| 181 | + return false; |
| 182 | +} |
0 commit comments